def add_manager(self): user_name = self.get_argument("user_name", "") telephone = self.get_argument("telephone", "") roles = self.get_arguments("role[]") # data = json.loads(self.request.body) # user_name = data.get("user_name","") # telephone = data.get("telephone","") # roles = data.get("role",[]) self.logger.info("username:"******" telephone:" + telephone + " role:" + ','.join(roles)) if user_name == '' or telephone == '' or roles == []: self.set_status(400) return {"result": False, 'msg': err_msgs['PARAMS_MISSING']} match = False try: token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info.get('company_uuid', None) usr_db = UserDao(session=self.session) match, usr_id, user_name = usr_db.insert_user( user_name, telephone, Users.ROLE_MANAGER) #增加用户。 except Exception, e: self.logger.error(traceback.format_exc())
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 get_personal_setting(self): token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) user_id = token_info['uuid'] user_db = UserDao(session=self.session) user_info = user_db.user(user_id) self.set_header('Content-Type', 'image/jpg') avatar = self.get_avatar(user_id) # 日志 self.log_user_action(token_info.get("uuid"), "get_personal_setting", "") result = { 'user_type': Users.MAPPING[token_info['role_group']], 'company': token_info['company']['name'] if token_info.get('company') else '', 'user_name': user_info['user_name'], 'telephone': user_info['telephone'], 'email': user_info['email'], 'avatar': avatar, 'openid': token_info['openid'] } return result
def get_user(self): token = self.request.headers.get("user_token", None) rule_db = RuleDao(session=self.session) setting_db = SettingsDao(session=self.session) redis = RedisBase() token_info = redis.get_token_info(token) company_uuid = token_info.get('company_uuid', None) app_version_info = token_info.get("app_version_info") update_at = str_to_datetime( token_info['update_at']).strftime('%Y-%m-%d %H:%M:%S') avatar = self.get_avatar(token_info['uuid']) # 日志 self.log_user_action(token_info.get("uuid"), "get_user", token_info.get("uuid")) if app_version_info: # app_version_info = json.loads(app_version_info) is_newest = setting_db.is_newest_version( app_version_info.get('app_version'), app_version_info.get('m_platform')) if not is_newest: self.set_status( CODE_APP_NEW_VERSION_AVAILABLE, token_status[CODE_APP_NEW_VERSION_AVAILABLE]['msg']) if company_uuid: com_db = CompanyDao(session=self.session) syndicate = com_db.get_company_by_uuid( token_info['syndicate_uuid']) #根据公司再对显示权限进行过滤。这里一定不能修改token的内容。 company_rules = rule_db.get_rules_by_level(Rules.LV_COMPANY, token_info['rule']) result = { 'uuid': token_info['uuid'], 'name': token_info['name'], 'py_full': token_info['py_full'], 'role_group': token_info['role_group'], 'role': token_info['role'], 'rule': company_rules, 'avatar': avatar, 'company': token_info['company']['name'], 'company_uuid': company_uuid, 'syndicate': { 'name': syndicate['name'], 'media_solution': syndicate['media_solution'] }, "openid": token_info['openid'], 'update_at': update_at, } return result else: #如果没有选择公司,则返回给另外一些显示权限。 syndicate_rules = rule_db.get_rules_by_level( Rules.LV_SYNDICATE, token_info['rule']) token_info['rule'] = list(syndicate_rules) #token_info['rule'] = syndicate_rules, token_info['avatar'] = avatar token_info['update_at'] = update_at return token_info
def create_company(self): name = self.get_argument("name","") desc = self.get_argument("desc","") uuid = self.get_argument("uuid","").lower() prop = self.get_argument("property","") self.logger.info("name:"+name+" desc:"+desc) if name == "" or uuid == "": 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) user_id = token_info['uuid'] parent_uuid = token_info['syndicate_uuid'] com_db = CompanyDao(session=self.session) es_index = self.build_es_index(parent_uuid,uuid) # 当uuid已存在时添加失败,返回推荐uuid if not com_db.insert_company(name,desc,prop,user_id,parent_uuid,uuid,es_index): result = { 'result': False, 'msg': err_msgs['COM_UUID_EXIST'], 'recommendation': self.build_company_uuid(uuid) } self.set_status(400) return result # 在es中创建公司索引,当公司索引创建失败时删除公司返回错误信息 if not EsBase().create_index(es_index): com_db.delete_company(uuid) result = { 'result': False, 'msg': err_msgs['INDEX_CREATE_ERR'] } self.set_status(400) return result # 在grout中设置公司的配置信息,设置失败删除公司和es中的索引,返回错误信息 if not self.post_company_grout(uuid,es_index): com_db.delete_company(uuid) EsBase().delete_index(es_index) result = { 'result': False, 'msg': err_msgs['COM_CONF_ERR'] } self.set_status(400) return result # 设置公司的关注媒体类型 self.set_com_media(uuid) self.log_user_action(self.get_token().get("uuid"),"create_company","-") result = { 'result': True } return result
def get_signature(self): noncestr = self.get_argument("noncestr", None) timestamp = self.get_argument("timestamp", None) url = self.get_argument("url", None) result = {} if not noncestr or not timestamp or not url: result.update({"result": False, "msg": err_msgs['PARAMS_MISSING']}) self.set_status(400) else: redis = RedisBase() ticket = redis.get_value(setting.TICKET_STR) if not ticket: access_token = exchange_for_access_token_for_js_sdk() # auth_info获取失败 if access_token.get('errcode', 0): result.update({ "result": False, "msg": err_msgs[str(access_token.get('errcode'))] }) self.set_status(502) else: ticket = exchange_for_ticket( access_token.get("access_token")) if ticket.get('errcode', 0): result.update({ "result": False, "msg": err_msgs[str(ticket.get('errcode'))] }) self.set_status(502) else: # 将该ticket存入redis redis.set_ticket(setting.TICKET_STR, json.dumps(ticket), setting.TICKET_EXPIRE_TIME) params = { "noncestr": noncestr, "jsapi_ticket": ticket.get("ticket"), "timestamp": timestamp, "url": url } signature = sha1_signature( con_str_from_dic(False, **params)) result.update({"result": True, "signature": signature}) else: params = { "noncestr": noncestr, "jsapi_ticket": json.loads(ticket).get("ticket"), "timestamp": timestamp, "url": url } signature = sha1_signature(con_str_from_dic(False, **params)) result.update({"result": True, "signature": signature}) return result
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 get_token(self): ''' 获取当前用户的token对象. token对象的结构见pkg_token函数。 :return: ''' token = self.request.headers.get("user_token",None) token_info = None if(token is not None): redis = RedisBase() token_info = redis.get_token_info(token) return token_info
def update_cur_time_in_token(self): token = self.request.headers.get('user_token', None) redis = RedisBase() token_info = redis.get_token_info(token) if token_info and token_info.get('update_at'): token_info.update(update_at=pre_cur_datetime()) redis.set_token(token, json.dumps(token_info)) # 日志 self.log_user_action(token_info.get("uuid"), "update_cur_time_in_token", "-") 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 set_avatar(self): dir_path = os.path.abspath( os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, os.path.pardir, 'dist/avatar')) avatar = self.request.files.get('avatar') token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) user_id = token_info['uuid'] filename = user_id + '.jpg' filepath = os.path.join(dir_path, filename) with open(filepath, 'wb') as up: up.write(avatar[0]['body']) # 日志 self.log_user_action(token_info.get("uuid"), "set_avatar", "-") return {'result': True}
def captcha(self): telephone = self.get_argument("telephone", '') token = self.request.headers.get("user_token", None) redis = RedisBase() token_info = redis.get_token_info(token) user_id = token_info['uuid'] user = UserDao(session=self.session) user_info = user.user(user_id) old_tel = user_info['telephone'] if old_tel != telephone and user.get_user_id_by_tel(telephone): result = {'result': False, 'msg': err_msgs['TEL_EXIST']} self.set_status(400) return result if setting.SMS_SEND: send_res = AuthCode().send_msg(telephone) if send_res['result']: code = send_res['code'] else: result = send_res self.set_status(400) return result else: code = '123456' r_captcha = self.redis_captcha(telephone) redis.set_value(r_captcha, code) redis.expire(r_captcha, setting.SET_CAPT_EXPIRE_TIME) result = {'result': True, 'msg': "OK"} 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 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 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 __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 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 wrapper(self, *args, **kw): token = self.request.headers.get("user_token", None) # platform = self.request.headers.get("platform", None) redis = RedisBase() if token and redis.exists_token(token): redis.expire_token(token) token_info = redis.get_token_info(token) # 检查当前token状态 status_code = token_info['status']['code'] if status_code != CODE_OK: self.set_status(status_code, token_status[status_code]['reason']) return { 'result': False, 'msg': token_status[status_code]['msg'] } rules = token_info['rule'] if rule in rules: return func(self, *args, **kw) else: self.set_status(403) return { 'result': False, 'msg': err_msgs['PERMISSION_DENIED'] } else: self.set_status(401) return {'result': False, 'msg': err_msgs['No_AUTHENTICATION']}
def wrapper(self, *args, **kw): token = self.request.headers.get("user_token", None) # platform = self.request.headers.get("platform", None) redis = RedisBase() if token and redis.exists_token(token): redis.expire_token(token) token_info = redis.get_token_info(token) rules = token_info['rule'] src_rules = set(rules) t_rules = or_rules if not isinstance(or_rules, set): in_rules = set(t_rules) else: in_rules = or_rules #判断权限是否有交集。 and_rules = in_rules & src_rules if not and_rules.isEmpty(): #有则代表有此权限 return func(self, *args, **kw) else: self.set_status(403) return { 'result': False, 'msg': err_msgs['PERMISSION_DENIED'] } else: self.set_status(401) return {'result': False, 'msg': err_msgs['No_AUTHENTICATION']}
def bind_telephone(self): telephone_old = self.get_argument('telephone_old', "") telephone_new = self.get_argument('telephone_new', "") captcha = self.get_argument("captcha", "") self.logger.info(" telephone:" + telephone_new) 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) user_info = usr_db.user(user_id) if telephone_old != user_info["telephone"]: result = {'result': False, 'msg': err_msgs['TEL_NOT_SAME']} self.set_status(400) return result if telephone_old != telephone_new and usr_db.get_user_id_by_tel( telephone_new): result = {'result': False, 'msg': err_msgs['TEL_ALREADY_BOUND']} self.set_status(400) return result r_captcha = self.redis_captcha(telephone_old) authcode = redis.get_value(r_captcha) if not authcode: result = {'result': False, 'msg': err_msgs['SMS_TIMEOUT']} self.set_status(400) return result if authcode != captcha: result = {'result': False, 'msg': err_msgs['SMS_ERR']} self.set_status(400) return result # 删除redis中的验证码信息 redis.del_key(r_captcha) usr_db.bind_user_with_tel(user_id, telephone_new) token_info['telephone'] = telephone_new redis.set_token(token, json.dumps(token_info)) # 日志 self.log_user_action(token_info.get("uuid"), "bind_telephone", telephone_new) return {'result': True}
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 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_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 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
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 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 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 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