Esempio n. 1
0
 def update_account_by_pk(cls, pk_id, params):
     """
     :param pk_id:
     :param params:
     :return:
     """
     _params = {p: params[p] for p in params if p in Accounts.field_names()}
     with db:
         Accounts.update(**params).where(Accounts.id == pk_id).execute()
Esempio n. 2
0
 def default_guest_account(cls):
     guest: Accounts = Accounts.select().where(
         Accounts.name == "guest").first()
     if guest:
         if not guest.fuzzy_id:
             fuzzy_id = obfuscate_id(guest.id)
             guest.fuzzy_id = fuzzy_id
             with db:
                 Accounts.update(fuzzy_id=fuzzy_id).where(
                     Accounts.id == guest.id).execute()
     return guest
Esempio n. 3
0
 def update_account_by_pk(cls, pk_id, params):
     """
     :param pk_id:
     :param params:
     :return:
     """
     fields = [
         'password', 'client_id', 'client_secret', 'access_token',
         'login_token', 'token_updated_at', 'login_updated_at', 'mobile_no',
         'fuzzy_id', 'pin', 'last_login_at', 'nickname'
     ]
     _params = {p: params[p] for p in params if p in fields}
     with db:
         Accounts.update(**params).where(Accounts.id == pk_id).execute()
Esempio n. 4
0
    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