def createTestingAccounts( session, count, name_pattern='test%s', email_pattern='*****@*****.**', password='******', ): """Creating many testing accounts """ from nowin_core.models.user import UserModel user_model = UserModel(session) users = [] for i in xrange(count): name = name_pattern % i email = email_pattern % i user = user_model.get_user_by_name(name) if user is not None: print 'user %s already exist, skip' continue user_id = user_model.create_user(user_name=name, email=email, display_name=name, password=password) user_model.activate_user(user_id, name, 'TW') users.append(user_id) session.flush() print 'create user', name session.commit() return users
def add_blocked_user(self, owner_id, target_id): """Add an user to blocked list in a chat-room as a list owner_id is the id of chat-room owner, and target_id is the target user to block """ if owner_id == target_id: raise SelfBlockError("Self-blocking is not allowed") radio = self.session.query(tables.Site) \ .filter_by(user_id=owner_id) \ .first() if radio is None: raise NotExistError('Radio %s does not exist' % owner_id) user_model = UserModel(self.session) target_user = user_model.get_user_by_id(target_id) if target_user is None: raise NotExistError('Target user %s does not exist' % target_user) blocking = self.get_blocking(owner_id, target_id) if blocking is not None: raise DuplicateError('Duplicate block from %s to %s' % (owner_id, target_id)) blocking = tables.Blocking(owner_id=owner_id, target_id=target_id) self.session.add(blocking) self.session.flush()
def check_permission(self, owner_id, guest_id): """Check can an user with guest_id access chat-room of user with owner_id? if the guest is joining anonymously, then the guest_id should be None """ # TODO: check is the guest an administrator settings = self.get_settings(owner_id) acl = settings['acl'] if acl == self.ACL_CLOSED: return False if acl == self.ACL_MEMBER: if guest_id is None: return False # make sure the guest user is activated user_model = UserModel(self.session) user = user_model.get_user_by_id(guest_id) if not user.active: return False blocking = self.get_blocking(owner_id, guest_id) return blocking is None
def run(self): engine = setup_db(uri=self.uri, echo=True) from nowin_core.database.tables import DeclarativeBase DeclarativeBase.metadata.create_all(bind=engine) import getpass import transaction from ..database import tables from ..models.user import UserModel from ..models.group import GroupModel from ..models.permission import PermissionModel session = tables.DBSession from zope.sqlalchemy import ZopeTransactionExtension session.configure(extension=ZopeTransactionExtension()) user_model = UserModel(session) group_model = GroupModel(session) permission_model = PermissionModel(session) with transaction.manager: admin = user_model.get_user_by_name('admin') if admin is None: print 'Create admin account' email = raw_input('Email:') password = getpass.getpass('Password:'******'Confirm:') if password != confirm: print 'Password not match' return user_id = user_model.create_user( user_name='admin', display_name='Administrator', email=email, password=password ) user_model.activate_user(user_id, '', 'TW') admin = user_model.get_user_by_id(user_id) session.flush() print 'Created admin, user_id=%s' % admin.user_id permission = permission_model.get_permission_by_name('admin') if permission is None: print 'Create admin permission ...' permission_model.create_permission( permission_name='admin', description='Administrate', ) permission = permission_model.get_permission_by_name('admin') group = group_model.get_group_by_name('admin') if group is None: print 'Create admin group ...' group_model.create_group( group_name='admin', display_name='Administrators', ) group = group_model.get_group_by_name('admin') print 'Add admin permission to admin group' group_model.update_permissions( group.group_id, [permission.permission_id]) session.flush() print 'Add admin to admin group' user_model.update_groups(admin.user_id, [group.group_id]) session.flush() print 'Done.'