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 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 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 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 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 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 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 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 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 edit_viewer(self): user_id = self.get_argument("user_id", "") 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("user_id:" + user_id + " username:"******" telephone:" + telephone + " roles:" + role + " qq:" + qq + " weixin:" + weixin + " weibo" + weibo) if user_id == "" or 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'] # 更新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) # 删除user_conf表中对应的角色 usr_rle_db = UserConfDao(session=self.session) usr_rle_db.delete_company_user(user_id, company_uuid, UserConf.TYPE_ROLE_VIEWER) # 插入新的角色 usr_rle_db.insert_role(user_id, company_uuid, role, UserConf.TYPE_ROLE_VIEWER) # 删除账户信息 usr_rle_db.delete_account(user_id) # 插入账户信息 if qq != '': usr_rle_db.insert_account(user_id=user_id, type=UserConf.TYPE_QQ, account=qq) if weixin != '': usr_rle_db.insert_account(user_id=user_id, type=UserConf.TYPE_WX, account=weixin) if weibo != '': usr_rle_db.insert_account(user_id=user_id, type=UserConf.TYPE_WB, account=weibo) # 通知该用户,其已经发生变化。 redis = RedisBase() redis.user_change(user_id, CODE_USER_MODIFIED, Users.ROLE_VIEWER) # 日志 self.log_user_action(token_info.get("uuid"), "edit_viewer", user_id) result = {'result': True} return result