def __init__(self, cur_user): self._cur_user_id = cur_user.id if cur_user.is_admin: self._is_admin = True self._staff_qs = Staff.query() self._staff_id_list = [s.id for s in self._staff_qs] else: all_sub_ids = [] role_id_list = [ ac.access_id for ac in AuthAccess.query().filter(staff=cur_user, access_type='role') ] #Generator will not work, why? dept_id_list = (ac.access_id for ac in AuthAccess.query().filter( staff=cur_user, access_type='department')) for role_id in role_id_list: sub_list = role_middleware.get_all_children_ids(role_id) self._is_show_sub = any([ role.is_show_data for role in Role.query().filter(id__in=sub_list) ][:-1]) if sub_list and self._is_show_sub: all_sub_ids.extend(sub_list) role_id_list = [acs.staff_id for acs in \ AuthAccess.query().filter(access_id__in = all_sub_ids, access_type='role') ] dept_id_list = [acs.staff_id for acs in \ AuthAccess.query().filter(access_id__in = dept_id_list, access_type='department') ] staff_id_list = list( set(role_id_list).intersection(set(dept_id_list))) staff_id_list.append(cur_user.id) self._staff_id_list = staff_id_list self._staff_qs = Staff.query().filter(id__in=staff_id_list)
def generate(cls, **role_info): """创建角色""" role = Role.create(**role_info) if role is None: raise BusinessError("角色创建失败") role_middleware.force_refresh() return role
def is_name_exist(cls, name, role=None): """判断角色名称是否存在""" role_qs = Role.search(name=name) if role is not None: role_qs = role_qs.filter(~Q(id=role.id)) if role_qs.count() > 0: raise BusinessError("该名称已存在") return True
def remove(cls, role_id): """删除角色""" access_type = "role" auth_access = AuthAccess.get_by_access_id(role_id, access_type) if auth_access.count() > 0: raise BusinessError("已绑定用户无法删除") role_children = role_middleware.get_children(role_id) if role_children: raise BusinessError("此角色存在下级无法删除") role = Role.get_byid(role_id) if role is None: raise BusinessError("此角色不存在") role.delete() role_middleware.force_refresh() return True
def loading(cls): return Role.query().order_by('create_time')
def is_exit(cls, role_id): """判断该角色是否存在""" role = Role.get_byid(role_id) if role is None: return False return True
def get(cls, role_id): """获取角色详情""" role = Role.get_byid(role_id) if role is None: raise BusinessError("该角色不存在") return role
def search(cls, **attrs): """查询角色列表""" role_list = Role.search(**attrs) return role_list