def get_user_id_by_name(user_name): try: # check user_name is it valid if not check_user_name(user_name): return False, 400 admin_token = get_token() if not admin_token: em = "get admin token error" LOG.exception(em) return False, 500 header = { 'Content-type': 'application/json', 'X-Auth-Token': admin_token.strip() } ret = get_http(url=options.user_ep + "?name=%s" % user_name, headers=header) if ret.status_code != 200: em = "get user's id error" LOG.exception(em) return False, 500 data = ret.json() return True, data.get('users')[0].get("id") except Exception as e: LOG.exception(e) return False, 500
def get_role_id_by_name(member_name="user"): try: admin_token = get_token() if not admin_token: return False, 500 header = { 'Content-type': 'application/json', 'X-Auth-Token': admin_token.strip() } ret = get_http(url=options.roles_ep + "?name=%s" % member_name, headers=header) if ret.status_code != 200: em = "get role id error....." LOG.exception(em) return False, ret.status_code roles = ret.json().get("roles") if not roles: em = "can not get member name from OpenStack. name: <{0}>".format( member_name) LOG.exception(em) return False, 500 return True, ''.join( [s.get("id") for s in roles if s.get("name") == member_name]) except Exception as e: LOG.exception(e) return False, 500
def get_usermsg_from_keystone(self, token): """ Return object from keystone by token """ try: headers = { 'X-Auth-Token': token, 'X-Subject-Token': token, 'Content-type': 'application/json' } token_info = utils.get_http(url=options.keystone_endpoint + '/auth/tokens', headers=headers).json()['token'] # 获取腾迅token信息 # tencent_token_info = utils.get_http(url=options.api_gateway_url + "/cloud/tencent/token", headers=headers).\ # json().get("data", {}) return { 'user': token_info['user'], 'roles': [role for role in token_info['roles'] if role], 'project': token_info['project'], 'admin': 'admin' in [role['name'] for role in token_info['roles'] if role], # "TxToken": tencent_token_info.get("TxToken", ""), # "TxKey": tencent_token_info.get("TxKey", ""), # "TxId": tencent_token_info.get("TxId", ""), } except Exception, e: print "Get usermsg error....\n" * 3 print e return (False, 401)
def get_user_info(token, user_id): url = options.keystone_endpoint + "/users/" + user_id headers = {'X-Auth-Token': token} ret = get_http(url=url, headers=headers) if ret.status_code != 200: em = u"获取用户信息失败" LOG.exception(em) return False, 500 return True, ret.json()
def get_auth_info(user_id, id=None, auth_type=None): result = [] auth_infos = AuthInfo.query.filter( and_( AuthInfo.user_id == user_id, AuthInfo.deleted == False, )).all() if id: auth_infos = AuthInfo.query.filter( and_(AuthInfo.user_id == user_id, AuthInfo.deleted == False, AuthInfo.id == id)).all() elif auth_type: auth_infos = AuthInfo.query.filter( and_(AuthInfo.user_id == user_id, AuthInfo.deleted == False, AuthInfo.auth_type == auth_type)).all() # if not auth_infos: # return {} for auth_info in auth_infos: # 得到图片 images = Images.query.filter( and_(Images.deleted == False, Images.auth_info_id == auth_info.id)).all() auth_info.image_list = [] for image in images: auth_info.image_list.append({ "id": image.id, "url": get_pictuer_url(image.name) }) result.append(auth_info) # 老的实名认证获取信息 admin_token = get_token() if admin_token: url = options.worder_ep + u"/work_orders/?title=实名认证&user_id={0}&status=1&is_admin=True".format( user_id) headers = {"X-Auth-Token": admin_token.strip()} ret = get_http(url=url, headers=headers).json() if ret.get("code") == 0 and ret.get("data"): auth_type = ret.get("data")[0].get("app_service_id") # 企业申请 if auth_type == "98ff8097-f1f0-11e7-815d-00b367deaa81": auth_type = 2 # 个人申请 if auth_type == "98af11e3-f1f0-11e7-a7e8-004b6ca3331e": auth_type = 1 data_obj = AuthInfo() data_obj.image_list = ret.get("data")[0].get("image_list") data_obj.auth_type = auth_type data_obj.status = ret.get("data")[0].get("status") data_obj.created_at = ret.get("data")[0].get("created_at") data_obj.user_id = ret.get("data")[0].get("user_id") data_obj.user_name = ret.get("data")[0].get("user_name") data_obj.auth_phone = ret.get("data")[0].get("auth_phone") data_obj.auth_id = ret.get("data")[0].get("auth_id") result.append(data_obj) return result
def get_old_platform_user_id(user_name): try: url = options.old_platform_userid_ep + user_name ret = get_http(url=url).json() if ret.get("code") != 0: return False, 500 return True, ret.get("objects")[0].get("id") except Exception as e: em = "get user id from old platform error. user name: <{0}>. msg: <{1}>".format( user_name, e) LOG.exception(em) return False, 500
def get_cmdb_info_by_uuid(uuid): """根据uuid从cmdb获取数据""" try: admin_token = get_token() url = "{0}/assets/{1}".format(options.cmdb_ep, uuid) headers = {'X-Auth-Token': admin_token.strip()} ret = get_http(url=url, headers=headers) if ret.status_code != 200: em = "get data from cmdb error...." LOG.exception(em) return {} return ret.json() except Exception as e: em = "get data from cmdb error. msg: <{0}>".format(e) LOG.exception(em) return {}
def get_project_id_by_name(project_name): if not check_user_name(project_name): return False, 400 try: admin_token = get_token() if not admin_token: return False, 500 header = { 'Content-type': 'application/json', 'X-Auth-Token': admin_token.strip() } ret = get_http(url=options.project_ep + '?name=%s' % project_name, headers=header) if ret.status_code != 200: em = "get project id by name error. name: <{0}>".format( project_name) LOG.exception(em) return False, ret.status_code project_id = ret.json().get("projects")[0].get("id") return True, project_id except Exception as e: LOG.exception(e) return False, 500
def real_name_auth(user_id): """从cmdb处获取实名认证信息""" admin_token = get_token() if not admin_token: em = "get admin token error" LOG.exception(em) return False headers = {'X-Auth-Token': admin_token} # 生成请求url url = parse_url(options.cmdb_host, path="/work_orders/", port=options.cmdb_port, user_id=user_id, title="实名认证", is_admin=True) ret = get_http(url=url, headers=headers) if ret.json().get("code") != 0: em = "get real name auth from cmdb error" LOG.exception(em) return False data = ret.json() if data.get("data"): return ret.json().get("data")[0] return False