def account_list(cls, pin=None, _type=None, offset=0, page_size=30): if pin and _type: account_list = Accounts.select(Accounts, AuthUser).join( AuthUser, on=(Accounts.id == AuthUser.acc_id), attr='auth').where(Accounts.pin == pin, AuthUser.type == _type).order_by( Accounts.created_at.desc()).offset( offset).limit(page_size) elif _type: account_list = Accounts.select(Accounts, AuthUser).join( AuthUser, on=(Accounts.id == AuthUser.acc_id), attr='auth').where(AuthUser.type == _type).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) elif pin: account_list = Accounts.select().where( Accounts.pin == pin).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) else: account_list = Accounts.select(Accounts).order_by( Accounts.created_at.desc()).offset(offset).limit(page_size) # print("account_list:", account_list) rs = [] n = 0 acc_ids = [] for acc in account_list: acc_dict = Accounts.to_dict(acc) if hasattr(acc, 'auth') and acc.auth: auth_user_dict = AuthUser.to_dict(acc.auth, BASE_FIELDS) acc_dict['auth'] = auth_user_dict else: acc_ids.append(acc.id) if not acc.fuzzy_id: acc_dict['fuzzy_id'] = obfuscate_id(acc.id) rs.append(acc_dict) n = n + 1 au_list = AuthUser.select().where(AuthUser.acc_id.in_(acc_ids)) au_dict = {} if au_list: for au in au_list: au_dict[au.acc_id] = AuthUser.to_dict(au) for acc_dict in rs: if acc_dict['id'] in au_dict: acc_dict['auth'] = au_dict[acc_dict['id']] uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_dict['id']) if uoe_list: acc_dict['extorgs'] = [uoe.org_id for uoe in uoe_list] ure_list = UserRoleExtend.select().where( UserRoleExtend.acc_id == acc_dict['id']) if ure_list: acc_dict['extroles'] = [ure.role_id for ure in ure_list] acc_dict.pop('id') return rs, n == page_size
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 new_account(cls, name, nickname, mobile_no, password, org_id, role_id, _type, extorgs, extroles, now, envelop_user_lambda: Callable[..., Tuple[str, dict]] = None, ctx=None): with db: user_token = None user_ext_dict = {} acc: Accounts = Accounts(name=name, mobile_no=mobile_no, nickname=nickname, password=password) acc.save(force_insert=True) acc_id = acc.id ur: UReference = UReference() ur.save(force_insert=True) au: AuthUser = AuthUser(acc_id=acc_id, org_id=org_id, role_id=role_id, ref_id=ur.id, type=_type) au.save(force_insert=True) if extorgs: for oid in extorgs: UserOrgExtend(acc_id=acc_id, org_id=oid).save(force_insert=True) if extroles: for rid in extroles: UserRoleExtend(acc_id=acc_id, role_id=rid).save(force_insert=True) if envelop_user_lambda: if ctx: user_token, user_ext_dict = envelop_user_lambda(acc, ctx) else: user_token, user_ext_dict = envelop_user_lambda(acc) cls.update_account_by_pk(pk_id=acc_id, params={ "fuzzy_id": user_ext_dict['id'], "login_updated_at": now, "login_token": user_token }) return user_token, user_ext_dict
def auth_user_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] org: Org = Org.select().where(Org.id == au.org_id).first() auth_user_dict['org'] = { 'base': Org.to_dict(org, BASE_FIELDS), 'parent': { 'id': 0, 'name': '无' } } if org.parent: porg: Org = Org.select().where(Org.id == org.parent).first() auth_user_dict['org']['parent'] = porg ext_org_list = Org.select(Org).join( UserOrgExtend, on=(UserOrgExtend.org_id == Org.id)).where( UserOrgExtend.acc_id == au.acc_id) if ext_org_list: ext_org = auth_user_dict['org']['ext'] = [] for org in ext_org_list: ext_org.append(Org.to_dict(org, BASE_FIELDS)) 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) return auth_user_dict
def auth_user(cls, account_id) -> dict: rs = AuthUser.select().where(AuthUser.acc_id == account_id) auth_user_dict = {} if rs: au: AuthUser = rs[0] # au_dict = AuthUser.to_dict(au, ['created_at', 'updated_at', 'acc_id', 'type']) auth_user_dict['au'] = { 'oid': au.org_id, 'rfid': au.ref_id, 'rid': au.role_id } auth_user_ext = {} base_fun, ext_fun_list, default_path_list = cls._get_fun_codes(au) auth_user_ext['p'] = default_path_list auth_user_ext['bf'] = base_fun auth_user_ext['ef'] = ext_fun_list auth_user_ext['t'] = au.type if au.type == USER_TYPE['GROUP']: origin_org_ids, org_ids = cls._get_org_ids(au) auth_user_ext['oog'] = origin_org_ids auth_user_ext['og'] = org_ids auth_user_dict['ext'] = auth_user_ext return auth_user_dict
def update_account(cls, acc_id, name, nickname, password, org_id, role_id, _type, extorgs, extroles, now, envelop_user_lambda: Callable[..., Tuple[str, dict]]): with db: update_params = dict(name=name, nickname=nickname, login_updated_at=now) if password: update_params['password'] = password user_token = None user_ext_dict = {} au_params = {} au: AuthUser = AuthUser.select().where( AuthUser.acc_id == acc_id).first() if not au: ur: UReference = UReference() ur.save(force_insert=True) au = AuthUser(acc_id=acc_id, org_id=org_id, role_id=role_id, ref_id=ur.id, type=_type) au.save(force_insert=True) if au.role_id != role_id: au_params['role_id'] = role_id if au.org_id != org_id: au_params['org_id'] = org_id if au.type != _type: au_params['type'] = _type if au_params: AuthUser.update(**au_params).where( AuthUser.acc_id == acc_id).execute() if extorgs: uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.not_in(extorgs)) del_uoe_ids = [uoe.org_id for uoe in uoe_list] if del_uoe_ids: UserOrgExtend.delete().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(del_uoe_ids)) exc_uoe_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(extorgs)) exc_ext_org_ids = [uoe.org_id for uoe in exc_uoe_list] insert_ext_org_ids = extorgs if exc_ext_org_ids: insert_ext_org_ids = [ oid for oid in extorgs if oid not in exc_ext_org_ids ] for oid in insert_ext_org_ids: UserOrgExtend(acc_id=acc_id, org_id=oid).save(force_insert=True) if extroles: ure_list = UserRoleExtend.select().where( UserRoleExtend.acc_id == acc_id, UserRoleExtend.role_id.not_in(extroles)) del_ure_ids = [ure.role_id for ure in ure_list] if del_ure_ids: UserRoleExtend.delete().where( UserRoleExtend.acc_id == acc_id, UserRoleExtend.role_id.in_(del_ure_ids)) exc_ure_list = UserOrgExtend.select().where( UserOrgExtend.acc_id == acc_id, UserOrgExtend.org_id.in_(extorgs)) exc_ext_role_ids = [ure.role_id for ure in exc_ure_list] insert_ext_role_ids = extroles if exc_ext_role_ids: insert_ext_role_ids = [ rid for rid in extroles if rid not in exc_ext_role_ids ] for rid in insert_ext_role_ids: UserRoleExtend(acc_id=acc_id, role_id=rid).save(force_insert=True) if envelop_user_lambda: acc: Accounts = Accounts.select().where( Accounts.id == acc_id).first() user_token, user_ext_dict = envelop_user_lambda(acc) update_params['fuzzy_id'] = user_ext_dict['id'] update_params['login_token'] = user_token Accounts.update(**update_params).where( Accounts.id == acc_id).execute() return user_token, user_ext_dict
def query_account_auth(cls, user_id) -> AuthUser: return AuthUser.select().where(AuthUser.acc_id == user_id).first()