def put(self, role_id): role = Role.get_object(id=role_id) args = role_parser.parse_args() role.update(**args) g.db.commit() return http_responses.HTTP_200_OK(msg="Modify success")
def get(self, role_id): role = Role.get_object(id=role_id) user_roles = UsersRoles.query.filter_by(role_id=role_id).all() users = [user_role.user.to_dict() for user_role in user_roles] return http_responses.HTTP_200_OK(msg={ "role": role.name, "users": users })
def post(self): args = role_parser.parse_args() _role = Role.query.filter_by(name=args.name).first() if _role: return http_responses.HTTP_400_BAD_REQUEST( msg={"error": u"角色已存在 - %s" % args.name}) role = Role(**args) g.db.add(role) g.db.commit() return http_responses.HTTP_200_OK()
def get(self, role_id): role = Role.get_object(id=role_id) role_permissions = RolesPermissions.query.filter_by( role_id=role_id).all() role_permissions = [ _role_per.permission.to_dict() for _role_per in role_permissions ] return http_responses.HTTP_200_OK(msg={ "role": role.name, "permissions": role_permissions })
def post(self, user_id): args = user_roles_parser.parse_args() user = User.get_object(id=user_id) for role_id in args.role_ids: try: role_id = int(role_id) except ValueError: return http_responses.HTTP_400_BAD_REQUEST( msg={"error": u"角色Id必须为整数"}) if UsersRoles.query.filter_by(user_id=user_id, role_id=role_id).first(): continue role = Role.get_object(id=role_id) user_role = UsersRoles(user, role) g.db.add(user_role) if len(args.role_ids): g.db.commit() return http_responses.HTTP_200_OK()
def post(self, role_id): args = role_permissions_parser.parse_args() role = Role.get_object(id=role_id) for permission_id in args.permission_ids: try: p_id = int(permission_id) except ValueError: return http_responses.HTTP_400_BAD_REQUEST( msg={"error": u"权限id'%s'必须为整数" % permission_id}) if RolesPermissions.query.filter_by(role_id=role_id, permission_id=p_id).first(): continue permission_obj = Permission.get_object(id=p_id) role_permission = RolesPermissions(role, permission_obj) g.db.add(role_permission) if len(args.permission_ids): g.db.commit() return http_responses.HTTP_200_OK()
def put(self, role_id): args = role_permissions_parser.parse_args() role = Role.get_object(id=role_id) all_role_permissions = RolesPermissions.query.filter_by( role_id=role_id).all() if args.permission_ids: all_permission_ids = set( [role_per.permission.id for role_per in all_role_permissions]) new_permission_ids = set(args.permission_ids) add_permission_ids = new_permission_ids - all_permission_ids delete_permission_ids = all_permission_ids - new_permission_ids # 删除权限 for per_id in delete_permission_ids: g.db.delete( filter(lambda x: x.permission_id == per_id, all_role_permissions)[0]) # 新增权限 for per_id in add_permission_ids: permission = Permission.query.filter_by(id=per_id).first() if permission is None: continue role_permission = RolesPermissions(role=role, permission=permission) g.db.add(role_permission) else: for role_per in all_role_permissions: g.db.delete(role_per) g.db.commit() return http_responses.HTTP_200_OK(msg="Update role permission success")
def get(self, role_id): role = Role.get_object(id=role_id) return http_responses.HTTP_200_OK(msg={"role": role.to_dict()})