def auth_user_role_detail(cls, account_id) -> dict: acc: Accounts = Accounts.select(Accounts).where( Accounts.id == account_id).first() rs = AuthUser.select().where(AuthUser.acc_id == account_id) auth_user_dict = { 'acc': Accounts.to_dict( acc, BASE_FIELDS + [ "id", "password", "fuzzy_id", "login_token", "login_updated_at", "pin" ]) } if rs: au: AuthUser = rs[0] auth_user_dict['au'] = { 'oid': au.org_id, 'rfid': au.ref_id, 'rid': au.role_id, 't': au.type } auth_user_dict['role'] = cls._get_roles(au) role_list = [] role_obj_list = Role.select() for r in role_obj_list: role_list.append(Role.to_dict(r, BASE_FIELDS)) auth_user_dict['roles'] = role_list return auth_user_dict
def query_role_list(cls, ids): data_list = Role.select().where(Role.id.in_(ids)) rs = [] for obj in data_list: obj_dict = Role.to_dict(obj, BASE_FIELDS) rs.append(obj_dict) return rs
def role_list(cls, offset=0, page_size=30, transfer_to_dict=True): data_list = Role.select().offset(offset).limit(page_size) rs = [] n = 0 for obj in data_list: if transfer_to_dict: obj_dict = Role.to_dict(obj, BASE_FIELDS) else: obj_dict = obj rs.append(obj_dict) n = n + 1 return rs, n == page_size
def _get_fun_codes(cls, au: AuthUser): role_ids = [] origin_role_ids = [au.role_id] ext_role_list = UserRoleExtend.select().where( UserRoleExtend.acc_id == au.acc_id) if ext_role_list: ext_role: UserRoleExtend = None for ext_role in ext_role_list: origin_role_ids.append(ext_role.role_id) role_extends_list = RoleExtend.select().where( RoleExtend.role_id.in_(origin_role_ids)) if role_extends_list: role_extend: RoleExtend = None for role_extend in role_extends_list: role_ids.append(role_extend.parent) role_list = Role.select().where(Role.id.in_(origin_role_ids + role_ids)) role: Role = None base_rs = 0 ext_fun_list = [] default_path_list = [] for role in role_list: if role.id in origin_role_ids: default_path_list.append(role.default_path) if role.ext_fun and role.ext_fun not in ext_fun_list: ext_fun_list.append(role.ext_fun) base_rs = base_rs | role.base_fun return base_rs, ext_fun_list, default_path_list
def role_list(self, page): offset = int(page) * self.max_page_size role_list, has_next = AuthDao.role_list(offset, self.max_page_size, transfer_to_dict=False) role_dict_list = [Role.to_dict(r, BASE_FIELDS) for r in role_list] parent_ids = {r['id']: r for r in role_dict_list} _ids = [] for r in role_dict_list: base_fun_list = AuthDao.get_base_funs(r['base_fun']) r['base_fun_list'] = base_fun_list if r['ext_fun']: ext_codes = r['ext_fun'].split(',') r['ext_fun_list'] = AuthDao.get_ext_funs(ext_codes) if r['parent'] in parent_ids: r['p'] = parent_ids[r['parent']] else: _ids.append(r['parent']) if _ids: p_list = AuthDao.query_role_list(_ids) p = {r['id']: r for r in p_list} for r in role_dict_list: if r['parent'] in p: r['p'] = p[r['parent']] for r in role_list: bind_dict = AuthDao.bind_parent_role_detail(r) parent_ids[r.id]['parent_role_detail'] = bind_dict return role_dict_list, has_next
def new_role(cls, name, parent, base_fun, ext_fun, default_path): data_item = Role(desc=name, parent=parent, base_fun=base_fun, ext_fun=ext_fun, default_path=default_path) with db: data_item.save(force_insert=True) if parent and data_item.id: re: RoleExtend = RoleExtend(role_id=data_item.id, parent=parent) re.save(force_insert=True) re_list = RoleExtend.select().where( RoleExtend.role_id == parent) for re in re_list: RoleExtend(role_id=data_item.id, parent=re.parent).save(force_insert=True) return data_item.id
def update_role(cls, role_id, params): _params = {p: params[p] for p in params if p in Role.field_names()} with db: if 'parent' in params and params['parent']: r: Role = Role.select().where(Role.id == role_id).first() if r.parent != params['parent']: RoleExtend.delete().where(RoleExtend.role_id == role_id) re: RoleExtend = RoleExtend(role_id=role_id, parent=params['parent']) re.save(force_insert=True) re_list = RoleExtend.select().where( RoleExtend.role_id == params['parent']) for re in re_list: RoleExtend(role_id=role_id, parent=re.parent).save(force_insert=True) Role.update(**_params).where(Role.id == role_id).execute()
def update_org(cls, org_id, params): _params = {p: params[p] for p in params if p in Role.field_names()} with db: if 'parent' in params and params['parent']: o: Org = Org.select().where(Org.id == org_id).first() if o.parent != params['parent']: OrgOrg.delete().where(OrgOrg.org_id == org_id) oo: OrgOrg = OrgOrg(org_id=org_id, parent=params['parent']) oo.save(force_insert=True) oo_list = OrgOrg.select().where( OrgOrg.org_id == params['parent']) for oo in oo_list: OrgOrg(org_id=org_id, parent=oo.parent).save(force_insert=True) Org.update(**_params).where(Org.id == org_id).execute()
def bind_parent_role_detail(cls, r: Role): ext_parent_role = {} if r.parent: role_extends_list = RoleExtend.select(RoleExtend).where( RoleExtend.role_id == r.id) ext_role_ids = [] if role_extends_list: role_extend: RoleExtend = None for role_extend in role_extends_list: ext_role_ids.append(role_extend.parent) role_list = Role.select().where(Role.id.in_(ext_role_ids)) ext_base_rs = 0 ext_ext_fun_list = [] for role in role_list: if role.ext_fun and role.ext_fun not in ext_ext_fun_list: ext_codes = role.ext_fun.split(',') ext_ext_fun_list = ext_ext_fun_list + ext_codes ext_base_rs = ext_base_rs | role.base_fun # 解析为可读 功能点 ext_parent_role['base'] = cls._get_base_funs(ext_base_rs) ext_parent_role['ext'] = cls._get_ext_funs(ext_ext_fun_list) return ext_parent_role
def _get_roles(cls, au: AuthUser): rs = {} role_ids = [] origin_role_ids = [au.role_id] role_extends_list = RoleExtend.select().where( RoleExtend.role_id.in_(origin_role_ids)) if role_extends_list: rs['base'] = role_extends_list[0] role_extend: RoleExtend = None for role_extend in role_extends_list: role_ids.append(role_extend.parent) role_list = Role.select().where(Role.id.in_(origin_role_ids + role_ids)) base_rs = 0 ext_fun_list = [] default_path_list = [] role: Role = None base_role = {'roles': []} rs['base'] = base_role for role in role_list: if role.id in origin_role_ids: base_role['roles'].append( Role.to_dict(role, ['created_at', 'updated_at', 'parent'])) default_path_list.append(role.default_path) if role.ext_fun and role.ext_fun not in ext_fun_list: ext_codes = role.ext_fun.split(',') ext_fun_list = ext_fun_list + ext_codes base_rs = base_rs | role.base_fun # 解析为可读 功能点 base_role['fun'] = fun_dict = {} fun_dict['base'] = cls._get_base_funs(base_rs) fun_dict['ext'] = cls._get_ext_funs(ext_fun_list) fun_dict['paths'] = default_path_list role_extends_list = RoleExtend.select(RoleExtend).join( UserRoleExtend, on=(UserRoleExtend.role_id == RoleExtend.role_id)).where( UserRoleExtend.acc_id == au.acc_id) ext_origin_role_ids = [] ext_role_ids = [] if role_extends_list: role_extend: RoleExtend = None for role_extend in role_extends_list: ext_role_ids.append(role_extend.parent) if role_extend.role_id not in ext_origin_role_ids: ext_origin_role_ids.append(role_extend.role_id) role_list = Role.select().where( Role.id.in_(ext_origin_role_ids + ext_role_ids)) ext_base_rs = 0 ext_ext_fun_list = [] ext_default_path_list = [] ext_role = {'roles': []} rs['ext'] = ext_role for role in role_list: if role.id in ext_origin_role_ids: ext_role['roles'].append( Role.to_dict(role, ['created_at', 'updated_at', 'parent'])) ext_default_path_list.append(role.default_path) if role.ext_fun and role.ext_fun not in ext_ext_fun_list: ext_codes = role.ext_fun.split(',') ext_ext_fun_list = ext_ext_fun_list + ext_codes ext_base_rs = ext_base_rs | role.base_fun # 解析为可读 功能点 ext_role['fun'] = fun_dict = {} fun_dict['base'] = cls._get_base_funs(ext_base_rs) fun_dict['ext'] = cls._get_ext_funs(ext_ext_fun_list) fun_dict['paths'] = ext_default_path_list return rs