def delete_group(gid): """ 删除一个分组 """ exist = manager.group_model.get(id=gid) if not exist: raise NotFound("分组不存在,删除失败") guest_group = manager.group_model.get(level=GroupLevelEnum.GUEST.value) root_group = manager.group_model.get(level=GroupLevelEnum.ROOT.value) if gid in (guest_group.id, root_group.id): raise Forbidden("不可删除此分组") if manager.user_model.select_page_by_group_id(gid, root_group.id): raise Forbidden("分组下存在用户,不可删除") with db.auto_commit(): # 删除group id 对应的关联记录 manager.group_permission_model.query.filter_by(group_id=gid).delete( synchronize_session=False) # 删除group exist.delete() return Success("删除分组成功")
def delete_user(uid): """ 删除用户 """ user = manager.user_model.get(id=uid) if user is None: raise NotFound("用户不存在") groups = manager.group_model.select_by_user_id(uid) # 超级管理员分组的用户仅有一个分组 if groups[0].level == GroupLevelEnum.ROOT.value: raise Forbidden("无法删除此用户") with db.auto_commit(): manager.user_group_model.query.filter_by(user_id=uid).delete( synchronize_session=False) user.hard_delete() return Success("操作成功")
def create_group(json: CreateGroupSchema): """ 新建分组 """ exists = manager.group_model.get(name=g.name) if exists: raise Forbidden("分组已存在,不可创建同名分组") with db.auto_commit(): group = manager.group_model.create( name=g.name, info=g.info, ) db.session.flush() group_permission_list = list() for permission_id in g.permission_ids: gp = manager.group_permission_model() gp.group_id = group.id gp.permission_id = permission_id group_permission_list.append(gp) db.session.add_all(group_permission_list) return Success("新建分组成功")
raise NotFound("用户不存在") with db.auto_commit(): user.reset_password(g.new_password) return Success("密码修改成功") @admin_api.route("/user/<int:uid>", methods=["DELETE"]) @permission_meta(name="删除用户", module="管理员", mount=False) @Logger(template="管理员删除了一个用户") @admin_required @api.validate( tags=["管理员"], security=[AuthorizationBearerSecurity], resp=DocResponse(NotFound("用户不存在"), Success("删除成功"), Forbidden("用户不能删除")), ) def delete_user(uid): """ 删除用户 """ user = manager.user_model.get(id=uid) if user is None: raise NotFound("用户不存在") groups = manager.group_model.select_by_user_id(uid) # 超级管理员分组的用户仅有一个分组 if groups[0].level == GroupLevelEnum.ROOT.value: raise Forbidden("无法删除此用户") with db.auto_commit(): manager.user_group_model.query.filter_by(user_id=uid).delete( synchronize_session=False)