Example #1
0
    def pkg_token(self, user_id, role_group, platform):
        '''
        :param user_id:
        :param role_group:
        :param platform:
        :return:
        '''

        #TODO: 此方法要修改成逻辑更清晰的,且需要的数据都是通过参数传入,而不依赖数据库。依赖之前的token,如果之前的为NONE,则使用当前传入数据重新构建一个。

        # 获取用户相关信息
        user_db = UserDao(session=self.session)
        user_info = user_db.user(user_id)

        if (platform == 'mobile'):
            expire_time = setting.TOKEN_EXPIRE_TIME_FOR_MOBILE
        else:
            expire_time = setting.TOKEN_EXPIRE_TIME

        if role_group == Users.ROLE_ADMIN:
            token = {
                "uuid": user_id,
                "openid":user_info['openid'],
                "name": user_info['user_name'],
                "expire_time": expire_time,
                "role_group": role_group,
                "rule": ['rule_sys_manager_syndicate','rule_sys_manager_super'],
                "status": {
                    'code': CODE_OK,
                    'msg': token_status[CODE_OK]['msg']
                }
            }
            return token

        # 获取用户user_conf表中对应的role_type
        role_type = self.get_user_conf_type(role_group)

        # 根据最后状态表得到用户的company_id
        usr_status_db = UserLastStatusDao(session=self.session)
        status = usr_status_db.get_specific_status(user_id, role_group, platform)
        com_db = CompanyDao(session=self.session)
        if status and status.company_uuid and com_db.com_exists(status.company_uuid):  # 最后状态表没有数据或者上次操作的公司不存在需要重新选择公司
            company_uuid = status.company_uuid

            # 查询公司的相关信息
            com_db = CompanyDao(session=self.session)
            com_info = com_db.get_company_by_uuid(company_uuid)

            # 查询该角色在该公司下的角色和权限
            user_conf = UserConfDao(session=self.session)
            if role_group == Users.ROLE_SUP_MGR:
                roles = user_conf.get_spec_roles(user_id,com_info['parent']['uuid'],role_type)
            else:
                roles = user_conf.get_spec_roles(user_id,company_uuid,role_type)

            role_rule_db = RoleRulesDao(session=self.session)
            rules = []
            for role in roles:
                rules.extend(role_rule_db.get_role_rules(role))
            token = {
                "name": user_info['user_name'],
                "uuid": user_id,
                "openid":user_info['openid'],
                "role": roles,
                "role_group": role_group,
                "expire_time": expire_time,
                "rule": rules,
                "company_uuid": company_uuid,
                "company": {
                    'name': com_info['name'],
                    'es_host': ES_HOST,
                    'es_index': com_info['index']
                },
                "syndicate_uuid": com_info['parent']['uuid'],
                "syndicate": com_info['parent']['name']
            }
        else:
            user_conf_db = UserConfDao(session=self.session)
            syndicate_uuid = user_conf_db.get_companys_by_userid_and_type(user_id,role_type)[0]
            syndicate_info = com_db.get_company_by_uuid(syndicate_uuid,False)
            roles = user_conf_db.get_spec_roles(user_id,syndicate_uuid,role_type)

            role_rule_db = RoleRulesDao(session=self.session)
            rules = []
            for role in roles:
                rules.extend(role_rule_db.get_role_rules(role))

            token = {
                "name": user_info['user_name'],
                "uuid": user_id,
                "openid":user_info['openid'],
                "role": roles,
                "role_group": role_group,
                "expire_time": expire_time,
                "rule": rules,#['rule_setting_personal','rule_ac_manager_company'],
                "syndicate_uuid": syndicate_uuid,
                "syndicate": syndicate_info['name']
            }

        token['status'] = {
            'code': CODE_OK,
            'msg': token_status[CODE_OK]['msg']
        }
        return token
Example #2
0
    def gen_token_info(self, user_id=None, role_group=None, platform='web', old_token_info=None, **kwargs):
        '''
        查询与生成token_info相关的信息,并调用组装函数组装token
        :param user_id: 用户ID
        :param role_group:  用户角色组
        :param platform:    登录平台
        :param old_token_info: 旧的token
        :param kwargs: 需要更新的内容,不管是重新生成还是更新旧的token,格式需要注意:指定的key可以是openid,name,update_at,role,role_group,rule,company,syndicate_uuid,syndicate.
        :return:
        '''
        #TODO: 此方法要修改成逻辑更清晰的,且需要的数据都是通过参数传入,而不依赖数据库。依赖之前的token,如果之前的为NONE,则使用当前传入数据重新构建一个。

        token_info = {}
        # 移动端存入app的版本信息和登录平台信息
        if platform == setting.PLATFORM_MOBILE:
            token_info.update(expire_time=setting.TOKEN_EXPIRE_TIME_FOR_MOBILE,app_version_info=kwargs.get("app_version_info"),platform=platform)
        else:
            token_info.update(expire_time=setting.TOKEN_EXPIRE_TIME,platform=platform)

        # 根据之前的token更新键值对,不需要重新生成新token
        if old_token_info:
            for k,v in kwargs.items():
                if old_token_info.has_key(k):
                    old_token_info.update({k:v})
            token_info.update(old_token_info)
        else:
            user_db = UserDao(session=self.session)
            usr_status_db = UserLastStatusDao(session=self.session)
            com_db = CompanyDao(session=self.session)
            user_conf_db = UserConfDao(session=self.session)
            role_rule_db = RoleRulesDao(session=self.session)
            # 获取用户相关信息
            user_info = user_db.user(user_id)

            # 获取用户user_conf表中对应的role_type
            role_type = self.get_user_conf_type(role_group)

            if role_group == Users.ROLE_ADMIN:
                # TODO 获取admin角色rules的函数可能需要修改成通过role_group参数获取,当前是role参数,也可以通过新建一个函数获取
                roles = user_conf_db.get_spec_roles(user_id,UserConf.ACC_COM_ID,role_type)
                com_and_synd_info = None
            else:
                # 根据最后状态表得到用户的company_id
                status = usr_status_db.get_specific_status(user_id, role_group, platform)
                com_and_synd_info = {}

                # 查询公司的相关信息(包括公司和集团)
                if status and status.company_uuid and com_db.com_exists(status.company_uuid):  # 最后状态表没有数据或者上次操作的公司不存在需要重新选择公司
                    com_and_synd_info.update(com_db.get_company_by_uuid(status.company_uuid))
                else:
                    syndicate_uuid = user_conf_db.get_companys_by_userid_and_type(user_id,role_type)[0]
                    com_and_synd_info.update(com_db.get_company_by_uuid(syndicate_uuid,False))

                # 获取roles
                if com_and_synd_info.get('parent') and role_group == Users.ROLE_SUP_MGR:
                    roles = user_conf_db.get_spec_roles(user_id,com_and_synd_info['parent']['uuid'],role_type)
                else:
                    roles = user_conf_db.get_spec_roles(user_id,com_and_synd_info['uuid'],role_type)

            # 获取rules
            rules = []
            for role in roles:
                rules.extend(role_rule_db.get_role_rules(role))
            token_info.update(self.pkg_token_v2(user_info, role_group, roles=roles, rules=rules,com_and_synd_info=com_and_synd_info,**kwargs))

        return token_info