Пример #1
0
 def set_token_info_and_user_conf(self,
                                  user_id,
                                  role,
                                  telephone,
                                  platform=setting.PLATFORM_WEB,
                                  app_version_info=None):
     '''
     生成token并将token_info存入redis,且更新用户最新状态表
     :param user_id:
     :param role:
     :param telephone:
     :param platform:
     :return: 返回token
     '''
     kwargs = {
         'app_version_info': app_version_info,
     }
     # 设置token信息
     info = json.dumps(
         self.gen_token_info(user_id, role, platform, **kwargs))
     print info
     token = self.build_token(telephone)
     self.redis.set_token(token, info, True, platform=platform)
     # 设置用户最新状态表中的token信息
     usr_status_db = UserLastStatusDao(session=self.session)
     usr_status_db.set_token(user_id, role, token, platform)
     return token
Пример #2
0
    def edit_super(self):

        user_id = self.get_argument("user_id", "")
        user_name = self.get_argument("user_name", "")
        telephone = self.get_argument("telephone", "")
        syndicate = self.get_argument("syndicate", "")

        self.logger.info("user_id:" + user_id + " username:"******"  telephone:" + telephone + "  syndicate:" +
                         syndicate)

        if user_id == "" or user_name == "" or telephone == "" or syndicate == "":
            self.set_status(400)
            return {"result": False, 'msg': err_msgs['PARAMS_MISSING']}

        # 更新User表中的信息
        usr_db = UserDao(session=self.session)
        user_info = usr_db.user(user_id)
        old_tel = user_info['telephone']
        # 当手机号发生改变时判断该手机号是否存在
        if old_tel != telephone and usr_db.get_user_id_by_tel(telephone):
            result = {'result': False, 'msg': err_msgs['TEL_EXIST']}
            self.set_status(400)
            return result

        usr_db.update_user(user_id, user_name, telephone)

        usr_rle_db = UserConfDao(session=self.session)
        old_syn = usr_rle_db.get_companys_by_userid_and_type(
            user_id, UserConf.TYPE_ROLE_SUPMGR)[0]

        # 当超级运营员集团发生改变时更新其最后一次状态表,如果正登录,改变其登录状态
        if old_syn != syndicate:
            usr_rle_db.update_synid(user_id, syndicate)
            usr_status_db = UserLastStatusDao(session=self.session)
            usr_status_db.set_company(user_id, None, Users.ROLE_SUP_MGR)
            statuses = usr_status_db.get_status_by_userid_and_role_group(
                user_id, Users.ROLE_SUP_MGR)

            redis = RedisBase()
            for status in statuses:
                if redis.exists_token(status.token):
                    u_token_info = redis.get_token_info(status.token)
                    u_token_info['status'] = {
                        'code': CODE_COM_MODIFIED,
                        'msg': token_status[CODE_COM_MODIFIED]['msg']
                    }
                    redis.set_token(status.token, json.dumps(u_token_info),
                                    False)
        # 日志
        self.log_user_action(self.get_token().get("uuid"), "edit_super",
                             user_id)

        result = {'result': True}
        return result
Пример #3
0
    def edit_user_company(self):

        company_uuid = self.get_argument("uuid","")

        token = self.request.headers.get("user_token",None)
        redis = RedisBase()
        token_info = redis.get_token_info(token)
        user_id = token_info['uuid']
        role_group = token_info['role_group']
        role_type = self.get_user_conf_type(role_group)

        user_conf_db = UserConfDao(session=self.session)
        user_com_uuids = user_conf_db.get_companys_by_userid_and_type(user_id,role_type)
        com_db = CompanyDao(session=self.session)

        if len(user_com_uuids) > 0 and role_group == Users.ROLE_SUP_MGR:
            companies = com_db.get_companys(user_com_uuids[0]) # 一个超级管理员只能管理一个集团
            com_uuids = []
            for company in companies:
                com_uuids.append(company['uuid'])
            user_com_uuids = com_uuids

        # 日志
        self.log_user_action(token_info.get("uuid"),"edit_user_company",company_uuid)

        result = {
            'result': True
        }
        if company_uuid and company_uuid not in user_com_uuids:
            result = {
                'result': False,
                'msg':err_msgs["ACCESS_COMPANY_DENIED"],
            }
            self.set_status(403,err_msgs["ACCESS_COMPANY_DENIED"])
        else:
            user_status_db = UserLastStatusDao(session=self.session)
            user_status_db.set_company_by_token(token, company_uuid)
            platform = user_status_db.get_from_by_token(token)

            # 更新token
            kwargs = {
                "update_at":token_info.get("update_at"),
            }
            new_token = json.dumps(self.gen_token_info(user_id, role_group, platform,**kwargs))
            redis.set_token(token, new_token)

        return result
Пример #4
0
    def login_viewer(self, user_id, telephone, app_version_info):
        '''
        TODO:处理管理员登陆。
        :return:
        '''
        usr_status_db = UserLastStatusDao(session=self.session)
        usr_conf_db = UserConfDao(session=self.session)
        com_db = CompanyDao(session=self.session)

        status = usr_status_db.get_specific_status(user_id, Users.ROLE_VIEWER,
                                                   setting.PLATFORM_MOBILE)
        if status and status.token and self.redis.exists_token(status.token):
            old_token = self.redis.get_token_info(status.token)
            old_token['status'] = {
                'code': CODE_USER_KICKED,
                'msg': token_status[CODE_USER_KICKED]['msg']
            }
            self.redis.set_token(status.token, json.dumps(old_token), False)

        # 如果是手机端,则在登陆时一次性将公司列表也返回
        companys = usr_conf_db.get_companys_by_userid_and_type(
            user_id, UserConf.TYPE_ROLE_VIEWER)

        company_list = []
        for com_uuid in companys:
            company = com_db.get_company_by_uuid(com_uuid, False)

            t_item = {
                'uuid': com_uuid,
                'name': company['name'],
                'desc': company['desc'],
                'property': company['property'],
                'py': company['py'],
                'id': company['id']
            }
            company_list.append(t_item)
            company_list.sort(key=lambda com: com["py"])
        # 日志
        self.log_user_action(user_id, "login", Users.ROLE_VIEWER)

        return company_list, self.set_token_info_and_user_conf(
            user_id, Users.ROLE_VIEWER, telephone, setting.PLATFORM_MOBILE,
            app_version_info)
Пример #5
0
    def login_manager(self, user_id, telephone):
        '''
        TODO:处理运营员登陆。
        :return: token
        '''
        usr_status_db = UserLastStatusDao(session=self.session)
        status = usr_status_db.get_specific_status(user_id, Users.ROLE_MANAGER,
                                                   setting.PLATFORM_WEB)
        if status and status.token and self.redis.exists_token(status.token):
            old_token = self.redis.get_token_info(status.token)
            old_token['status'] = {
                'code': CODE_USER_KICKED,
                'msg': token_status[CODE_USER_KICKED]['msg']
            }
            self.redis.set_token(status.token, json.dumps(old_token), False)
        # 日志
        self.log_user_action(user_id, "login", Users.ROLE_MANAGER)

        return self.set_token_info_and_user_conf(user_id, Users.ROLE_MANAGER,
                                                 telephone,
                                                 setting.PLATFORM_WEB)
Пример #6
0
    def delete_syndicate(self,uuid):
        self.logger.info("uuid:"+uuid)
        com_db = CompanyDao(session=self.session)
        if com_db.delete_company(uuid):
            redis = RedisBase()
            # 改变当前登录超级运营员的token状态
            tokens = redis.get_all_tokens()
            for r_token in tokens:
                t_info = json.loads(redis.get_value(r_token))
                if t_info.get('syndicate_uuid') == uuid and t_info.get('role_group') == Users.ROLE_SUP_MGR:
                    t_info['status'] = {
                        'code': CODE_COM_MODIFIED,
                        'msg': token_status[CODE_COM_MODIFIED]['msg']
                    }
                    redis.set_value(r_token,json.dumps(t_info))
                    redis.expire(r_token,setting.KICKED_EXPIRE_TIME)

            # 删除其他表里该集团超级管理员的相关信息
            user_db = UserDao(session=self.session)
            user_status_db = UserLastStatusDao(session=self.session)
            user_conf_db = UserConfDao(session=self.session)
            supers = user_conf_db.get_super_by_synid(uuid)
            for super in supers:
                # 改变当前登录的超级运营员的状态
                user_status_db.del_status(super.user_id, Users.ROLE_SUP_MGR)
                user_conf_db.delete_user_by_usrid_and_type(super.user_id, UserConf.TYPE_ROLE_SUPMGR)
                user_db.delete_user(super.user_id, Users.ROLE_SUP_MGR)
            # 日志
            self.log_user_action(self.get_token().get("uuid"),"delete_syndicate",uuid)

            return {'result': True}

        # 集团下还有公司删除失败
        else:
            result = {
                'result': False,
                'msg': err_msgs['HAS_COMS']
            }
            self.set_status(400)
            return result
Пример #7
0
    def delete_manager(self, user_id):
        self.logger.info("user_id:" + user_id)
        token = self.request.headers.get("user_token", None)
        redis = RedisBase()

        token_info = redis.get_token_info(token)
        company_uuid = token_info['company_uuid']

        usr_rle_db = UserConfDao(session=self.session)
        usr_rle_db.delete_company_user(user_id, company_uuid,
                                       UserConf.TYPE_ROLE_MGR)

        usr_status_db = UserLastStatusDao(session=self.session)
        statuses = usr_status_db.get_status_by_userid_and_role_group(
            user_id, Users.ROLE_MANAGER)
        for status in statuses:
            if company_uuid == status.company_uuid and redis.exists_token(
                    status.token):
                u_token_info = redis.get_token_info(status.token)
                u_token_info['status'] = {
                    'code': CODE_USER_DELETED,
                    'msg': token_status[CODE_USER_DELETED]['msg']
                }
                redis.set_token(status.token, json.dumps(u_token_info), False)

        has_managers = usr_rle_db.has_role(user_id, UserConf.TYPE_ROLE_MGR)
        if not has_managers:
            usr_db = UserDao(session=self.session)
            usr_db.delete_user(user_id, Users.ROLE_MANAGER)
            usr_status_db.del_status(user_id, Users.ROLE_MANAGER)

        # 通知该用户,其已经发生变化。
        redis.user_change(user_id, CODE_USER_MODIFIED, Users.ROLE_VIEWER)
        # 日志
        self.log_user_action(token_info.get("uuid"), "delete_manager", user_id)

        result = {'result': True}

        return result
Пример #8
0
    def delete_admins(self, user_id):
        self.logger.info("user_id:" + user_id)

        usr_rle_db = UserConfDao(session=self.session)

        admins = usr_rle_db.get_admins()

        if len(admins) <= 1:
            result = {'result': False, 'msg': err_msgs['CANT_DEL_LAST_ADMIN']}
            self.set_status(400)
            return result

        usr_rle_db.delete_user_by_usrid_and_type(user_id,
                                                 UserConf.TYPE_ROLE_ADMIN)

        redis = RedisBase()
        usr_status_db = UserLastStatusDao(session=self.session)
        statuses = usr_status_db.get_status_by_userid_and_role_group(
            user_id, Users.ROLE_ADMIN)
        for status in statuses:
            if redis.exists_token(status.token):
                u_token_info = redis.get_token_info(status.token)
                u_token_info['status'] = {
                    'code': CODE_USER_DELETED,
                    'msg': token_status[CODE_USER_DELETED]['msg']
                }
                redis.set_token(status.token, json.dumps(u_token_info), False)

        usr_status_db.del_status(user_id, Users.ROLE_ADMIN)

        usr_db = UserDao(session=self.session)
        usr_db.delete_user(user_id, Users.ROLE_ADMIN)
        # 日志
        self.log_user_action(self.get_token().get("uuid"), "delete_admin",
                             user_id)

        result = {'result': True}

        return result
Пример #9
0
    def upload_feedback(self):

        token = self.request.headers.get("user_token",None)
        redis = RedisBase()
        token_info = redis.get_token_info(token)

        user_id = token_info.get("uuid",None)
#        if not redis.is_fb_overtime(user_id):
        if True:
            content = self.get_argument("content",None)
            company_uuid = self.get_argument("company_uuid",None)
            typeid = self.get_argument("type", None)

            # 根据token查询用户最后状态表中的终端信息
            usr_status_db = UserLastStatusDao(session=self.session)
            platform = usr_status_db.get_from_by_token(token)

            self.logger.info("user_id:"+user_id+"  content:"+content+"  company_uuid:"+company_uuid+"  platform:"+platform)

            uf_db = UserFeedbackDao(session=self.session)
            if uf_db.insert_user_feedback(user_id,content,company_uuid,platform,typeid):
                # 限制用户两分钟内只允许发一条反馈
                redis.set_fb_restrict_key(user_id)
                redis.expire_fb_key(user_id)
                # 日志
                self.log_user_action(token_info.get("uuid"),"upload_feedback","-")

                result = {
                    "result": True
                }
                return result
        else:
            result = {
                "msg":err_msgs["QUERY_TOO_FAST"],
                "result": False
            }
            self.set_status(400)
            return result
Пример #10
0
    def login_wx(self):
        # 获取参数
        code = self.get_argument("code", None)
        platform = self.get_argument("from", "web")
        state = self.get_argument("state", None)
        syn_uuid = self.get_argument("syn_uuid", None)
        # 获取state中存储的信息
        state_info = self.redis.get_state_info(state) if state else None

        result = {}
        # 未获取认证码
        if (platform == setting.PLATFORM_WEB and
            (not code or not state)) or (platform == setting.PLATFORM_MOBILE
                                         and (not code or not syn_uuid)):
            result.update({"result": False, "msg": err_msgs['PARAMS_MISSING']})
            self.set_status(400)
        elif platform == setting.PLATFORM_WEB and not state_info:
            result.update({
                "result": False,
                "msg": err_msgs['STATE_TIME_OUT'],
            })
            self.set_status(400)
        else:
            if platform == setting.PLATFORM_WEB:
                # 获取角色
                role = state_info.get('role')
            else:
                role = Users.ROLE_VIEWER

            # 换取access_token等信息
            auth_info = exchange_for_access_token(code, None, platform)

            # auth_info获取失败
            if auth_info.get('errcode', 0):
                result.update({
                    "result": False,
                    "msg": err_msgs[str(auth_info.get('errcode', 0))]
                })
                self.set_status(502)
            else:
                # 查找拥有此openid的用户
                user_db = UserDao(session=self.session)
                user = user_db.get_user_by_openid_and_role(
                    auth_info['openid'], role)
                if not user:
                    result.update({
                        "result": False,
                        "msg": err_msgs['WX_NOT_BOUND']
                    })
                    self.set_status(400)
                else:
                    # 用户已登录将其踢出
                    usr_status_db = UserLastStatusDao(session=self.session)
                    status = usr_status_db.get_specific_status(
                        user.user_id, role, platform)
                    if status and status.token and self.redis.exists_token(
                            status.token):
                        old_token = self.redis.get_token_info(status.token)
                        old_token['status'] = {
                            'code': CODE_USER_KICKED,
                            'msg': token_status[CODE_USER_KICKED]['msg']
                        }
                        self.redis.set_token(status.token,
                                             json.dumps(old_token), False)
                    # 设置token信息
                    info = json.dumps(
                        self.gen_token_info(user.user_id, role, platform, {}))
                    token = self.build_token(user.telephone)
                    self.redis.set_token(token, info, platform=platform)

                    # 设置用户最后一次状态表中的token信息
                    usr_status_db = UserLastStatusDao(session=self.session)
                    usr_status_db.set_token(user.user_id,
                                            role,
                                            token,
                                            platform=platform)
                    # 日志
                    self.log_user_action(user.user_id, "login_wx", "-")

                    result.update({
                        "result": True,
                        "msg": "OK",
                        "token": token,
                    })
                    if platform == setting.PLATFORM_MOBILE:
                        # 如果是手机端,则在登陆时一次性将公司列表也返回
                        usr_conf_db = UserConfDao(session=self.session)
                        com_db = CompanyDao(session=self.session)
                        companys = usr_conf_db.get_companys_by_userid_and_type(
                            user.user_id, UserConf.TYPE_ROLE_VIEWER)

                        company_list = []
                        for com_uuid in companys:
                            company = com_db.get_company_by_uuid(
                                com_uuid, False)

                            t_item = {
                                'uuid': com_uuid,
                                'name': company['name'],
                                'desc': company['desc'],
                                'property': company['property'],
                                'py': company['py'],
                                'id': company['id']
                            }
                            company_list.append(t_item)
                            company_list.sort(key=lambda com: com["py"])
                        result.update(companys=company_list,
                                      hostname=syn_uuid +
                                      setting.HOST_NAME_SUFFIX)
        return result
Пример #11
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
Пример #12
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
Пример #13
0
    def delete_company(self,uuid):
        self.logger.info("uuid:"+uuid)

        token = self.request.headers.get("user_token",None)
        redis = RedisBase()
        token_info = redis.get_token_info(token)
        usr_id = token_info['uuid']
        company_uuid = token_info.get('company_uuid')
        parent_uuid = token_info['syndicate_uuid']

        # 获取该公司下的所有运营员
        user_conf_db = UserConfDao(session=self.session)
        mgrs = user_conf_db.get_managers_by_company_uuid(uuid)
        views = user_conf_db.get_viewers_by_company_uuid(uuid)

        com_db = CompanyDao(session=self.session)
        usr_status_db = UserLastStatusDao(session=self.session)
        if com_db.delete_company(uuid):
            # 删除该公司下的运营员
            user_conf_db.delete_company_users(uuid)
            usr_db = UserDao(session=self.session)
            # 遍历该公司下的运营员,如果在其他公司不存在角色在用户表中删除该运营员
            for mgr in mgrs:
                mgr_id = mgr['user_id']
                if not user_conf_db.has_role(mgr_id,UserConf.TYPE_ROLE_MGR):
                    usr_db.delete_user(mgr_id,Users.ROLE_MANAGER)
                    usr_status_db.del_status(mgr_id,Users.ROLE_MANAGER)
            for view in views:
                view_id = view['user_id']
                if not user_conf_db.has_role(view_id,UserConf.TYPE_ROLE_VIEWER):
                    user_conf_db.delete_account(view_id)
                    usr_db.delete_user(view_id,Users.ROLE_VIEWER)

            # 删除ES中的公司索引
            es_index = self.build_es_index(parent_uuid,uuid)
            EsBase().delete_index(es_index)
            # 在grout中删除公司
            self.delete_company_grout(uuid)
            # 删除公司时发送消息到频道中
            redis.del_company(usr_id,uuid)
            # 查询token,更改状态
            tokens = redis.get_all_tokens()
            for r_token in tokens:
                t_info = json.loads(redis.get_value(r_token))
                # 将当前已登录用户com_uuid为被删除公司的用户token状态重置
                if t_info.get('company_uuid') == uuid and t_info.get('uuid') != usr_id:
                    t_info['status'] = {
                        'code': CODE_COM_MODIFIED,
                        'msg': token_status[CODE_COM_MODIFIED]['msg']
                    }
                    redis.set_value(r_token,json.dumps(t_info))
                    redis.expire(r_token,setting.KICKED_EXPIRE_TIME)

            platform = usr_status_db.get_from_by_token(token)
            # 当超级运营员把当前选择公司删除,返回301
            if uuid == company_uuid:
                kwargs = {
                    "update_at": token_info.get("update_at"),
                }
                info = json.dumps(self.gen_token_info(usr_id, Users.ROLE_SUP_MGR, platform,**kwargs))
                redis.set_token(token, info)
                self.set_status(301)
            # 日志
            self.log_user_action(token_info.get("uuid"),"delete_company",uuid)

            result = {
                'result': True
            }

        # 只有删除集团并且集团下还有公司才会删除失败
        else:
            result = {
                'result': False,
                'msg': err_msgs['HAS_COMS']
            }
            self.set_status(400)

        return result
Пример #14
0
    def get_online_user_infos(self):
        # 从query string获取参数
        beg = self.get_argument("beg", 0)
        count = self.get_argument("count", 30)
        search = self.get_argument("search", "")
        sort = self.get_argument("sort", "user_name")

        results = []
        usr_db = UserDao(session=self.session)
        tokens = self.redis.get_all_tokens()
        users = usr_db.get_distinct_users()
        for token in tokens:
            token_info = json.loads(self.redis.get_value(token))
            if token_info and token_info['status']['code'] == CODE_OK:
                # 手机端特殊处理
                if token_info[
                        'platform'] == PLATFORM_MOBILE and advanced_pre_datetime(
                            'H', TIME_GAP,
                            convert_to_str=True) > token_info['update_at']:
                    continue
                for user in users:
                    if user.user_id == token_info['uuid']:
                        telephone = user.telephone
                        break
                user_status_db = UserLastStatusDao(session=self.session)
                status = user_status_db.get_status_by_userid_and_role_group(
                    token_info['uuid'], token_info['role_group'])
                for stat in status:
                    if stat.platform == token_info['platform']:
                        online_user_info = {
                            "user_id":
                            token_info['uuid'],
                            "telephone":
                            telephone if telephone else None,
                            "user_name":
                            token_info['name'],
                            "role_group_name":
                            token_info['role_group'],
                            "role_group_title":
                            Users.MAPPING[token_info['role_group']],
                            "last_login_time":
                            str(stat.update_at) if stat else None,
                            "last_login_platform":
                            str(stat.platform) if stat else None,
                        }
                        results.append(online_user_info)
                        break
        results.sort(key=lambda online_user_info: online_user_info[sort])
        # 日志
        self.log_user_action(self.get_token().get("uuid"),
                             "get_online_user_infos", "-")

        result = {
            "result":
            True,
            "count":
            len(results),
            "online_users":
            results[int(beg):int(beg) + int(count)] if
            (int(beg) + int(count)) <= len(results) else results[int(beg):]
        }
        return result