def get_supmgrs_infos(self): beg = self.get_argument("beg", 0) count = self.get_argument("count", 30) search = self.get_argument("search", "") sort = self.get_argument("sort", "telephone") self.logger.info("sort:" + sort) results = [] usr_rules_db = UserConfDao(session=self.session) supmgr_list = usr_rules_db.get_sprmgrs() for supmgr in supmgr_list: if supmgr['user_name'].find( search) == -1 and supmgr['telephone'].find(search) == -1: continue results.append(supmgr) results.sort(key=lambda manager: manager[sort]) # 日志 self.log_user_action(self.get_token().get("uuid"), "get_supmgrs_info", "-") result = { "result": True, "count": len(results), "sup_mgrs": results[int(beg):int(beg) + int(count)] if (int(beg) + int(count)) <= len(results) else results[int(beg):] } return result
def syndicate_list(self): beg = self.get_argument("beg",0) count = self.get_argument("count",30) search = self.get_argument("search","") sort = self.get_argument('sort','id') self.logger.info("beg:"+str(beg)+"count:"+str(count)) com_db = CompanyDao(session=self.session) usr_conf_db = UserConfDao(session=self.session) syndicate_list = com_db.get_companys(Company.NO_PARENT, search) spr_mgrs = usr_conf_db.get_sprmgrs() for syndicate in syndicate_list: supers_info = [] for spr_mgr in spr_mgrs: if spr_mgr['syndicate_uuid'] == syndicate['uuid']: supers_info.append(spr_mgr['user_name']) syndicate['supers'] = supers_info syndicate_list.sort(key=lambda com: com[sort]) self.log_user_action(self.get_token().get("uuid"),"syndicate_list","-") result = { 'result': True, 'count': len(syndicate_list), 'syndicates': syndicate_list[int(beg):int(beg)+int(count)] if (int(beg)+int(count)) <= len(syndicate_list) else syndicate_list[int(beg):] } return result
def login_super_manager(self, user_id, telephone): ''' TODO:处理超级运营员登陆. :return: token ''' usr_conf_db = UserConfDao(session=self.session) syndicate = usr_conf_db.get_companys_by_userid_and_type( user_id, UserConf.TYPE_ROLE_SUPMGR)[0] tokens = self.redis.get_all_tokens() for token in tokens: token_info = json.loads(self.redis.get_value(token)) role_group = token_info.get('role_group') syndicate_uuid = token_info.get('syndicate_uuid') if role_group == Users.ROLE_SUP_MGR and syndicate_uuid == syndicate: token_info['status'] = { 'code': CODE_USER_KICKED, 'msg': token_status[CODE_USER_KICKED]['msg'] } self.redis.set_value(token, json.dumps(token_info)) self.redis.expire(token, setting.KICKED_EXPIRE_TIME) # 日志 self.log_user_action(user_id, "login", Users.ROLE_SUP_MGR) return self.set_token_info_and_user_conf(user_id, Users.ROLE_SUP_MGR, telephone, setting.PLATFORM_WEB)
def get_admins_infos(self): beg = self.get_argument("beg", 0) count = self.get_argument("count", 30) search = self.get_argument("search", "") sort = self.get_argument("sort", "telephone") self.logger.info("sort:" + sort) usr_rules_db = UserConfDao(session=self.session) admin_list = usr_rules_db.get_admins() results = [] for admin in admin_list: if admin['user_name'].find( search) == -1 and admin['telephone'].find(search) == -1: continue results.append(admin) results.sort(key=lambda admin: admin[sort]) # 日志 self.log_user_action(self.get_token().get("uuid"), "get_admins_info", "-") result = { "result": True, "count": len(results), "admins": results[int(beg):int(beg) + int(count)] if (int(beg) + int(count)) <= len(results) else results[int(beg):] } return result
def update_personal_setting(self): user_name = self.get_argument('user_name', "") email = self.get_argument('email', "") self.logger.info("user_name:" + user_name + "email:" + email) token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) user_id = token_info['uuid'] # 更新用户信息 usr_db = UserDao(session=self.session) usr_db.update_user_v2(user_id, user_name) # 更新用户配置信息 usr_conf_db = UserConfDao(session=self.session) usr_conf_db.update_account(user_id, UserConf.TYPE_EMAIL, email) # 更新token 不需要重新生产token token_info['name'] = user_name redis.set_token(token, json.dumps(token_info)) # 日志 self.log_user_action(token_info.get("uuid"), "update_personal_setting", "-") return {'result': True}
def create_syndicate(self): name = self.get_argument("name","") desc = self.get_argument("desc","") uuid = self.get_argument("uuid","") user_name = self.get_argument("user_name","") telephone = self.get_argument("telephone","") self.logger.info("name:"+name+" desc:"+desc+" user_name:"+user_name+" telephone:"+telephone) if name == "" or user_name == "" or telephone == "" or uuid == "": self.set_status(400) return {"result":False,'msg':err_msgs['PARAMS_MISSING']} usr_db = UserDao(session= self.session) # 一个超级运营员只允许管理一个集团 if usr_db.exist_role_group_by_tel(telephone, Users.ROLE_SUP_MGR): result = { 'result': False, 'msg': err_msgs['ROLE_SUP_MGR_EXIST'] } self.set_status(400) return result match, usr_id, user_name = usr_db.insert_user(user_name,telephone, Users.ROLE_SUP_MGR) if not match: result = { 'result': False, 'msg': err_msgs['TEL_NAME_NOT_MATCH'], 'name': user_name } self.set_status(400) return result token = self.request.headers.get("user_token",None) redis = RedisBase() token_info = redis.get_token_info(token) user_id = token_info['uuid'] com_db = CompanyDao(session=self.session) # 当uuid已存在时插入失败,返回推荐uuid if not com_db.insert_company(name,desc,None,user_id,Company.NO_PARENT,uuid,None): result = { 'result': False, 'msg': err_msgs['COM_UUID_EXIST'], 'recommendation': self.build_company_uuid(uuid) } self.set_status(400) return result usr_rle_db = UserConfDao(session=self.session) usr_rle_db.insert_role(usr_id,uuid,UserConf.SUB_ROLE_SUP_MGR,UserConf.TYPE_ROLE_SUPMGR) # 日志 self.log_user_action(token_info.get("uuid"),"create_syndicate",uuid) result = { 'result': True } return result
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
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
def get_manager(self, user_id): ''' 获取运营员员的详细信息。包括其管理了几个公司,在每个公司的角色。 :return: ''' result = { 'result': False, } token_info = self.get_token() # 日志 self.log_user_action(token_info.get("uuid"), "get_manager", user_id) if (token_info is not None): syndicate_uuid = token_info.get("syndicate_uuid") #获取当前集团的所有公司 com_db = CompanyDao(session=self.session) company_list = com_db.get_companys(syndicate_uuid) cmpy_dict = { com_item['uuid']: com_item['name'] for com_item in company_list } company_ids = cmpy_dict.keys() #获取此运营员所管理的公司及在每个公司的角色。 user_conf_db = UserConfDao(session=self.session) cmpy_role_dict = user_conf_db.get_manager_companies( user_id, company_ids ) #{company_id2:[role_1,role_2],company_id2:[role_2]} #获取这些运营员的信息。 user_db = UserDao(session=self.session) result = user_db.user(user_id, Users.ROLE_MANAGER) result['user_id'] = str(user_id) result["roles"] = cmpy_role_dict result["result"] = True del result['role'] else: result = {'result': False, 'msg': err_msgs['No_AUTHENTICATION']} self.set_status(400) return result
def list(self): beg = self.get_argument("beg",0) count = self.get_argument("count",30) search = self.get_argument("search","") sort = self.get_argument('sort','py') self.logger.info("beg:"+str(beg)+"count:"+str(count)) 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) usr_conf_db = UserConfDao(session=self.session) companys = usr_conf_db.get_companys_by_userid_and_type(user_id, role_type) company_list = [] com_db = CompanyDao(session=self.session) if role_group == Users.ROLE_SUP_MGR: company_list = com_db.get_companys(companys[0], search) # 一个超级管理员只能管理一个集团 else: for com_uuid in companys: company = com_db.get_company_by_uuid(com_uuid,False) if company['name'].find(search) == -1: continue result = { 'uuid': com_uuid, 'name': company['name'], 'desc': company['desc'], 'property': company['property'], 'py': company['py'], 'id': company['id'] } company_list.append(result) company_list.sort(key=lambda com: com[sort]) self.log_user_action(token_info.get("uuid"),"company_list","-") result = { 'result': True, 'count': len(company_list), 'companys': company_list[int(beg):int(beg)+int(count)] if (int(beg)+int(count)) <= len(company_list) else company_list[int(beg):] } return result
def get_managers_infos(self): ''' 获取当前公司所有运营员信息。 :return: ''' beg = self.get_argument("beg", 0) count = self.get_argument("count", 30) search = self.get_argument("search", "") sort = self.get_argument("sort", "telephone") self.logger.info("sort:" + sort) token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info['company_uuid'] results = [] usr_rules_db = UserConfDao(session=self.session) mgr_list = usr_rules_db.get_managers_by_company_uuid(company_uuid) usr_db = UserDao(session=self.session) for mgr in mgr_list: usr_info = usr_db.user(mgr['user_id']) if usr_info['user_name'].find( search) == -1 and usr_info['telephone'].find(search) == -1: continue mgr['user_name'] = usr_info['user_name'] mgr['telephone'] = usr_info['telephone'] results.append(mgr) results.sort(key=lambda manager: manager['telephone']) # 日志 self.log_user_action(token_info.get("uuid"), "get_managers_infos", "-") result = { "result": True, "count": len(results), "managers": results[int(beg):int(beg) + int(count)] if (int(beg) + int(count)) <= len(results) else results[int(beg):] } return result
def delete_manager_v2(self, user_id): token_info = self.get_token() # 日志 self.log_user_action(token_info.get("uuid"), "delete_manager_v2", user_id) result = { 'result': True, } if (token_info is not None): syndicate_uuid = token_info.get("syndicate_uuid") #获取当前集团的所有公司 com_db = CompanyDao(session=self.session) company_list = com_db.get_companys(syndicate_uuid) cmpy_dict = { com_item['uuid']: com_item['name'] for com_item in company_list } company_ids = cmpy_dict.keys() #删除用户的原来角色配置。 usr_rfg_db = UserConfDao(session=self.session) usr_rfg_db.delete_manager_by_user_and_companys( user_id, company_ids) #判断此用户是否还有其他的角色,如果没有其他角色就删除此用户。 usr_has_other = usr_rfg_db.has_role(user_id, UserConf.TYPE_ROLE_MGR) if (not usr_has_other): usr_db = UserDao(session=self.session) usr_db.delete_user(user_id, Users.ROLE_MANAGER) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(user_id, CODE_USER_DELETED, Users.ROLE_MANAGER) else: result = {'result': False, 'msg': err_msgs['No_AUTHENTICATION']} self.set_status(400) return result
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)
def get_viewers_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", "") # 组织日志 self.logger.info("sort:" + sort) token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info['company_uuid'] results = [] usr_rules_db = UserConfDao(session=self.session) view_list = usr_rules_db.get_viewers_by_company_uuid(company_uuid) usr_db = UserDao(session=self.session) for viewer in view_list: usr_info = usr_db.user(viewer['user_id']) if usr_info['user_name'].find( search) == -1 and usr_info['telephone'].find(search) == -1: continue viewer['user_name'] = usr_info['user_name'] viewer['telephone'] = usr_info['telephone'] results.append(viewer) results.sort(key=lambda viewer: viewer['telephone']) # 日志 self.log_user_action(token_info.get("uuid"), "get_viewers_infos", "-") result = { "result": True, "count": len(results), "viewers": results[int(beg):int(beg) + int(count)] if (int(beg) + int(count)) <= len(results) else results[int(beg):] } return result
def delete_viewer(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) if usr_rle_db.delete_viewer(user_id, company_uuid): usr_db = UserDao(session=self.session) usr_db.delete_user(user_id, Users.ROLE_VIEWER) # 通知该用户,其已经发生变化。 redis.user_change(user_id, CODE_USER_DELETED, Users.ROLE_VIEWER) # 日志 self.log_user_action(token_info.get("uuid"), "delete_viewer", user_id) result = {'result': True} return result
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
def add_supmanager(self): user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") syndicate = self.get_argument("syndicate", "") self.logger.info("username:"******" telephone:" + telephone + " syndicate:" + syndicate) if user_name == '' or telephone == '' or syndicate == '': self.set_status(400) return {"result": False, 'msg': err_msgs['PARAMS_MISSING']} usr_db = UserDao(session=self.session) # 一个超级运营员只允许管理一个集团 if usr_db.exist_role_group_by_tel(telephone, Users.ROLE_SUP_MGR): result = {'result': False, 'msg': err_msgs['ROLE_SUP_MGR_EXIST']} self.set_status(400) return result match, usr_id, user_name = usr_db.insert_user(user_name, telephone, Users.ROLE_SUP_MGR) if not match: result = { 'result': False, 'msg': err_msgs['TEL_NAME_NOT_MATCH'], 'name': user_name } self.set_status(400) return result usr_rle_db = UserConfDao(session=self.session) usr_rle_db.insert_role(usr_id, syndicate, UserConf.SUB_ROLE_SUP_MGR, UserConf.TYPE_ROLE_SUPMGR) # 日志 self.log_user_action(self.get_token().get("uuid"), "add_supmgr", usr_id) result = {'result': True, 'user_id': usr_id} return result
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
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
def add_admin(self): user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") roles = self.get_arguments("role[]") if user_name == '' or telephone == '': self.set_status(400) return {"result": False, 'msg': err_msgs['PARAMS_MISSING']} usr_db = UserDao(session=self.session) # 一个手机号只能存在一个管理员角色 if usr_db.exist_role_group_by_tel(telephone, Users.ROLE_ADMIN): result = {'result': False, 'msg': err_msgs['ROLE_ADMIN_EXIST']} self.set_status(400) return result match, usr_id, user_name = usr_db.insert_user(user_name, telephone, Users.ROLE_ADMIN) if not match: result = { 'result': False, 'msg': err_msgs['TEL_NAME_NOT_MATCH'], 'name': user_name } self.set_status(400) return result usr_rle_db = UserConfDao(session=self.session) for role in roles: usr_rle_db.insert_role(usr_id, UserConf.ACC_COM_ID, role, UserConf.TYPE_ROLE_ADMIN) # 日志 self.log_user_action(self.get_token().get("uuid"), "add_admin", usr_id) result = {'result': True, 'user_id': usr_id} return result
def edit_admins(self): user_id = self.get_argument("user_id", "") user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") roles = self.get_arguments("role[]") self.logger.info("user_id:" + user_id + " username:"******" telephone:" + telephone) if user_id == "" or user_name == "" or telephone == "": 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) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(user_id, CODE_USER_MODIFIED, Users.ROLE_ADMIN) # 删除admin的相关信息 usr_rle_db.delete_company_user(user_id, UserConf.ACC_COM_ID, UserConf.TYPE_ROLE_ADMIN) # 重新插入admin for role in roles: usr_rle_db.insert_role(user_id, UserConf.ACC_COM_ID, role, UserConf.TYPE_ROLE_ADMIN) # 日志 self.log_user_action(self.get_token().get("uuid"), "edit_admin", user_id) result = {'result': True} return result
def __add_manager_v2(self, user_name, telephone, roles, syndicate_uuid): ''' 增加用户的内核操作。 :param user_name: :param telephone: :param roles: [dict] 结构为{"company_uuid":[role1,role2],} :return: ''' try: user_db = UserDao(session=self.session) match, user_id, user_name_new = user_db.insert_user( user_name, telephone, Users.ROLE_MANAGER) #增加用户。 # 如果是更新则更新用户 if not match and user_id: user_db.update_user(user_id, user_name, telephone) #获取当前集团的所有公司 com_db = CompanyDao(session=self.session) company_list = com_db.get_companys(syndicate_uuid) cmpy_dict = { com_item['uuid']: com_item['name'] for com_item in company_list } company_ids = cmpy_dict.keys() #删除用户的原来角色配置。 usr_rfg_db = UserConfDao(session=self.session) usr_rfg_db.delete_manager_by_user_and_companys( user_id, company_ids) if (roles is not None): #插入新的角色配置。 usr_rfg_db.insert_manager_roles(user_id, roles) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(user_id, CODE_USER_MODIFIED, Users.ROLE_MANAGER) except Exception, e: self.logger.error(traceback.format_exc())
def test_insert_manager(self): usr_rle_db = UserConfDao(session=self.session) usr_rle_db.insert_manager(user_id="xxxx-xxx-xx", company_id="2")
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
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
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
def add_viewer(self): user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") role = self.get_argument("role", "") qq = self.get_argument("qq", "") weixin = self.get_argument("weixin", "") weibo = self.get_argument("weibo", "") self.logger.info("username:"******" telephone:" + telephone + " roles:" + role + " qq:" + qq + " weixin:" + weixin + " weibo:" + weibo) if user_name == "" or telephone == "" or role == "": self.set_status(400) return {"result": False, 'msg': err_msgs['PARAMS_MISSING']} token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info['company_uuid'] usr_db = UserDao(session=self.session) match, usr_id, user_name = usr_db.insert_user( user_name, telephone, Users.ROLE_VIEWER) #增加用户。 usr_rle_db = UserConfDao(session=self.session) if usr_rle_db.viewer_is_exist(usr_id, company_uuid): # 判断当前用户在该公司是否存在观察者角色 result = {'result': False, 'msg': err_msgs['VIEW_EXIST']} self.set_status(400) return result if not match: result = { 'result': False, 'msg': err_msgs['TEL_NAME_NOT_MATCH'], 'name': user_name } self.set_status(400) return result usr_rle_db.insert_role(user_id=usr_id, company_uuid=company_uuid, role=role, role_type=UserConf.TYPE_ROLE_VIEWER) if qq != '': usr_rle_db.insert_account(user_id=usr_id, type=UserConf.TYPE_QQ, account=qq) if weixin != '': usr_rle_db.insert_account(user_id=usr_id, type=UserConf.TYPE_WX, account=weixin) if weibo != '': usr_rle_db.insert_account(user_id=usr_id, type=UserConf.TYPE_WB, account=weibo) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(usr_id, CODE_USER_MODIFIED, Users.ROLE_VIEWER) # 日志 self.log_user_action(token_info.get("uuid"), "add_viewer", usr_id) result = {'result': True, 'user_id': usr_id} return result
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
def edit_manager(self): user_id = self.get_argument("user_id", "") user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") roles = self.get_arguments("role[]") # data = json.loads(self.request.body) # user_id = data.get("user_id","") # user_name = data.get("user_name","") # telephone = data.get("telephone","") # roles = data.get("role",[]) self.logger.info("user_id:" + user_id + " username:"******" telephone:" + telephone + " roles:" + ','.join(roles)) if user_id == "" or user_name == "" or telephone == "" or roles == []: self.set_status(400) return {"result": False, 'msg': err_msgs['PARAMS_MISSING']} token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info['company_uuid'] # 更新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) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(user_id, CODE_USER_MODIFIED, Users.ROLE_MANAGER) # 下面的是东辉的版本 貌似不能用 # 获取用户当前角色判断角色是否发生改变 # roles_old = usr_rle_db.get_spec_roles(user_id,company_uuid,UserConf.TYPE_ROLE_MGR) # if sorted(roles_old) != sorted(roles): # 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): # mgr_token_info = redis.get_token_info(status.token) # mgr_token_info['status'] = { # 'code': CODE_USER_MODIFIED, # 'msg': token_status[CODE_USER_MODIFIED]['msg'] # } # redis.set_token(status.token,json.dumps(mgr_token_info),False) # 删除manager的相关信息 usr_rle_db.delete_company_user(user_id, company_uuid, UserConf.TYPE_ROLE_MGR) # 重新插入manager for role in roles: usr_rle_db.insert_role(user_id, company_uuid, role, UserConf.TYPE_ROLE_MGR) # 日志 self.log_user_action(token_info.get("uuid"), "edit_manager", user_id) result = {'result': True} return result
def get_managers_in_syndicate(self): ''' 此方法获取到当前集团所有运营员的信息。仅包括其管理哪几个公司。并不包括其在公司里所处的角色。 :return: ''' search = self.get_argument("search", None) sort = self.get_argument("sort", "telephone") self.logger.info("sort:" + sort) result = { 'result': False, } token_info = self.get_token() if (token_info is not None): syndicate_uuid = token_info.get("syndicate_uuid") #获取当前集团的所有公司 com_db = CompanyDao(session=self.session) company_list = com_db.get_companys(syndicate_uuid) cmpy_dict = { com_item['uuid']: com_item['name'] for com_item in company_list } company_ids = cmpy_dict.keys() #获取这些公司的所有运营员。 user_conf_db = UserConfDao(session=self.session) user_cmpy_map = user_conf_db.get_managers_by_company_uuids( company_ids) #dict(user_id:set(company_uuid)) #获取这些运营员的信息。 user_db = UserDao(session=self.session) users = user_db.get_users_by_uuids(user_cmpy_map.keys(), Users.ROLE_MANAGER, sort, search) data = [] usr_idx = 0 for user in users: usr_cmys = [] for cmpy_id in user_cmpy_map[user.user_id]: cmpy_name = cmpy_dict.get(cmpy_id) t_cmpy_dict = { "company_name": cmpy_name, "company_id": cmpy_id } usr_cmys.append(t_cmpy_dict) usr_cmys.sort(key=lambda cmpy: cmpy['company_name']) usr_item = { "user_id": user.user_id, "user_name": user.user_name, "telephone": user.telephone, 'companys': usr_cmys } self.logger.debug("[%d]%s" % (usr_idx, json.dumps(usr_item))) usr_idx += 1 data.append(usr_item) # 日志 self.log_user_action(token_info.get("uuid"), "get_managers_in_syndicate", syndicate_uuid) result = {'result': True, 'managers': data} else: result = {'result': False, 'msg': err_msgs['No_AUTHENTICATION']} self.set_status(400) return result