logined_user.py 2.91 KB
Newer Older
qunfeng qiu's avatar
qunfeng qiu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Version: 1.0
@Python Version:3.6.6
@Author: ludq1
@Email: ludq1@chinaunicom.cn
@date: 2023/04/07 11:40:00
@Description:
"""

from .app_exception import AppRuntimeException
from ..globalutility import Utility


class LoginedUserInfo:
    r"""
    登陆用户的信息POJO类,不依赖任何其他应用基础类
    """

    def __init__(self):
        self.account_id = None
        self.account_name = None
        self.user_id = None
        self.user_name = None
        self.mobile = None
        self.email = None
        self.is_root = None
        self.session_uuid = None
        r"""
        目前获取不到session_uuid
        """
        self.access_token = None
        r"""
        继续调用其他API时使用的access_token
        """

    def is_login(self):
        return not (self.user_id is None or self.user_id == '')

    def __str__(self):
        return Utility.dict2jsonstr(self.__dict__)

    @classmethod
    def from_dict(cls, user_info_dict, access_token: str = None):
        r"""
        根据sso_api返回的用户信息dict转换成LoginedUserInfo

        :param user_info_dict
        :param access_token:
        """
        return cls().fill_attr_by_dict(user_info_dict, access_token)

    def fill_attr_by_dict(self, user_info_dict: dict, access_token: str = None):
        r"""
        根据sso_api返回的用户信息dict填充自身属性,返回自身

        :param user_info_dict:
        :param access_token:
        :return:
        """

        tmp_value = user_info_dict.get('accountID')
        if not tmp_value:
            raise AppRuntimeException(
                message=f"通过字典转换成LoginedUserInfo对象失败",
                detail=f"字典内容不包括accountID,user_info_dict={Utility.dict2jsonstr(user_info_dict)}"
            )
        self.account_id = tmp_value

        tmp_value = user_info_dict.get('userID')
        if not tmp_value:
            raise AppRuntimeException(
                message=f"通过字典转换成LoginedUserInfo对象失败",
                detail=f"字典内容不包括userID,user_info_dict={Utility.dict2jsonstr(user_info_dict)}"
            )
        self.user_id = tmp_value

        tmp_value = user_info_dict.get('userName')
        if not tmp_value:
            raise AppRuntimeException(
                message=f"通过字典转换成LoginedUserInfo对象失败",
                detail=f"字典内容不包括userName,user_info_dict={Utility.dict2jsonstr(user_info_dict)}"
            )
        self.user_name = tmp_value

        self.account_name = user_info_dict.get('accountName')
        self.mobile = user_info_dict.get('mobile')
        self.email = user_info_dict.get('email')
        self.is_root = user_info_dict.get('isRoot')

        self.access_token = access_token

        return self