def index(): Role.insert_roles() role = Role.query.filter_by(name='User').first() user = User.query.filter_by(role=role).all() for user1 in user: print user1.username return render_template('main/index.html')
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles() self.client = self.app.test_client()
def test_anonymous_user(self): Role.insert_roles() u = AnonymousUser() self.assertFalse(u.can(Permission.COMMENT)) self.assertFalse(u.can(Permission.WRITE)) self.assertFalse(u.can(Permission.FOLLOW)) self.assertFalse(u.can(Permission.ADMIN)) self.assertFalse(u.can(Permission.MODERATE))
def test_user_role(self): Role.insert_roles() u = User(name='ose', password='******', email='*****@*****.**') self.assertTrue(u.can(Permission.COMMENT)) self.assertTrue(u.can(Permission.WRITE)) self.assertTrue(u.can(Permission.FOLLOW)) self.assertFalse(u.can(Permission.ADMIN)) self.assertFalse(u.can(Permission.MODERATE))
def alloc_user(): for i in g.data['roles']: if not g.user.restrict_permission(Role.get_by_id(i).permission): return falseReturn(msg='您无法赋予他人权限不小于自己的角色') if not g.user.restrict_functions(Role.get_by_id(i).allow_functions): return falseReturn(msg='您无法赋予他人权能不在自己范围内的角色') u = User.get_by_id(g.data['id']) u.change_role(g.data['roles']) return trueReturn()
def remove_role(): if g.user.restrict_permission(Role.get_by_id(g.data['id']).permission): if g.user.restrict_functions( Role.get_by_id(g.data['id']).allow_functions): Role.get_by_id(g.data['id']).delete() return trueReturn() else: return falseReturn(msg='您无法删除权能比自己多的角色') else: return falseReturn(msg='您无法删除权限不小于自己的角色')
def rename_role(): if g.user.restrict_permission(Role.get_by_id(g.data['id']).permission): if g.user.restrict_functions( Role.get_by_id(g.data['id']).allow_functions): Role.get_by_id(g.data['id']).rename(g.data['name']) return trueReturn() else: return falseReturn(msg='您无法为权能比自己多的角色更名') else: return falseReturn(msg='您无法为权限不小于自己的角色更名')
def test_admin(self): Role.insert_roles() u = User(name='ose', password='******', email='*****@*****.**', id=1) self.assertTrue(u.can(Permission.COMMENT)) self.assertTrue(u.can(Permission.WRITE)) self.assertTrue(u.can(Permission.FOLLOW)) self.assertTrue(u.can(Permission.ADMIN)) self.assertTrue(u.can(Permission.MODERATE))
def edit_role(): if g.user.restrict_permission(g.data['permission']): if g.user.restrict_functions(g.data['functions']): Role.get_by_id(g.data['id']).modify_permission( g.data['permission']) Role.get_by_id(g.data['id']).modify_functions(g.data['functions']) return trueReturn() else: return falseReturn(msg='您无法为角色分配自己没有的权能') else: return falseReturn(msg='您无法为角色分配不小于自身的权限')
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.drop_all() db.create_all() db.session.commit() Role.insert_roles() User.insert_root_admin() self.client = self.app.test_client()
def initialize_instance(): Admin.insert_admin(g.data['user'], g.data['password']) Domain(name='默认部员域').save() Role.new_role('成员', 0, []) Role.new_role('部长', 64, [ 'new_user', 'remove_user', 'rename_user', 'alloc_user', 'ls_user', 'import_from_sheet' ]) Role.new_role('总监', 4096, ['*']) with open('isinit.flag', 'w') as f: pass return trueReturn(msg="初始化完成")
def deploy(): """Run deployment tasks.""" upgrade() Role.insert_roles() User.insert_root_admin()
def new_role(): Role.new_role(g.data['name'], g.data['permission']) return trueReturn()
def remove_role(): Role.get_by_id(g.data['id']).delete() return trueReturn()
def new_role(): if g.user.restrict_permission(g.data['permission']): Role.new_role(g.data['name'], g.data['permission']) return trueReturn() else: return falseReturn(msg='您无法新建权限不小于自己的角色')
def ls_role(): return trueReturn({"roles": [i.get_base_info() for i in Role.objects()]})
def rename_role(): Role.get_by_id(g.data['id']).rename(g.data['name']) return trueReturn()
def change_role(self, roles): for p, i in enumerate(roles): if not isinstance(i, Role): roles[p] = Role.get_by_id(i) self.roles = roles return self.save()
def deploy(): """Run deployment tasks and prepare necessary data""" upgrade() Role.insert_roles() User.insert_root_admin()
def edit_role(): Role.get_by_id(g.data['id']).modify_permission(g.data['permission']) Role.get_by_id(g.data['id']).modify_functions(g.data['functions']) return trueReturn()