def is_authenticated(request): # 调试模式, 尝试后端验证 if settings.DEBUG: sso_token = settings.DEBUG_COOKIE else: sso_token = request.COOKIES.get('SSO_COOKIE_KEY', '') account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) if account_token: url = 'http://{ip}:{port}/apiCore/sso/validationToken'.format( ip=settings.SSO_HOST, port=settings.SSO_PORT) try: r = requests.post(url, data={ 'account_token': account_token, 'ssoToken': sso_token }, timeout=settings.REQUESTS_TIMEOUT) json_data = r.json() if json_data.get('success'): # sso 验证成功 request.sso_user = json_data # 增加accountDomainMoid字段,保存账户所属域的moid,服务域账户即服务域moid,用户域账户,即用户域moid if ((json_data['data']['userDomainAdmin']) or (json_data['data']['defaultUserDomainAdmin'])): request.sso_user['data']['accountDomainMoid'] = json_data[ 'data']['userDomainMoid'] else: request.sso_user['data']['accountDomainMoid'] = json_data[ 'data']['serviceDomainMoid'] return True except Exception as e: logger.error(e) return False
def delete_device_type(mainModel): try: logger.info("[delete_device_type] mainModel :%s" % mainModel) account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) url = DEVICE_URL.format(ip=settings.SSO_HOST, port=settings.SSO_PORT) logger.info("[delete_device_type] url :%s" % url) Params = {'account_token': account_token,'name':mainModel} Headers = {'Content-Type':"application/json"} result = requests.delete(url, params=Params, headers=Headers, timeout=settings.REQUESTS_TIMEOUT) if result.status_code == 200: resultData = result.json() logger.info("[delete_device_type] result :%s" % resultData) if resultData.get('success') == 1: logger.error('[delete_device_type] success!') return True else: logger.error(resultData) return False else: logger.error("[delete_device_type] status_code :%s" % result.status_code) return False except Exception as e: logger.error('[delete_device_type] error : %s' % e) return False
def edit_device_type(mainModel,subTypeList): try: logger.info("[edit_device_type] mainModel :%s" % mainModel) logger.info("[edit_device_type] subTypeList :%s" % subTypeList) account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) url = DEVICE_URL.format(ip=settings.SSO_HOST, port=settings.SSO_PORT) logger.info("[edit_device_type] url :%s" % url) Params = {'account_token': account_token} Headers = {'Content-Type':"application/json"} Data = {"name": mainModel, "data": subTypeList } result = requests.put(url, params=Params,headers=Headers, data=json.dumps(Data, timeout=settings.REQUESTS_TIMEOUT)) if result.status_code == 200: resultData = result.json() logger.info("[edit_device_type] result :%s" % resultData) if resultData.get('success') == 1: logger.error('[edit_device_type] success!') return True else: logger.error(resultData) return False else: logger.error("[edit_device_type] status_code :%s" % result.status_code) return False except Exception as e: logger.error('[edit_device_type] error : %s' % e) return False
def set_device_type_limit_cfg(cfgValue): try: logger.info("[set_device_type_limit_cfg] cfgValue :%s" % cfgValue) account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) url = BMC_CFG_URL.format(ip=settings.SSO_HOST, port=settings.SSO_PORT) logger.info("[set_device_type_limit_cfg] url :%s" % url) Params = {'account_token':account_token} Headers = {'Content-Type':"application/json"} Data = {'cfgKey':'limit.mode', 'cfgValue':cfgValue, 'description':''} result = requests.put(url, params=Params,headers=Headers,data=json.dumps(Data), timeout=settings.REQUESTS_TIMEOUT) if result.status_code == 200: resultData = result.json() logger.info("[set_device_type_limit_cfg] result :%s" % resultData) if resultData.get('success') == 1: return True else: logger.error(resultData) return False else: logger.error("[set_device_type_limit_cfg] status_code : %s" % result.status_code) return False except Exception as e: logger.error('[set_device_type_limit_cfg] error! %s' % e) return False
def add_device_type(mainModel, terminalType, deviceTag, platformType=2): try: logger.info("[add_device_type] mainModel :%s" % mainModel) logger.info("[add_device_type] terminalType :%s" % terminalType) logger.info("[add_device_type] deviceTag :%s" % deviceTag) account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) url = DEVICE_URL.format(ip=settings.SSO_HOST, port=settings.SSO_PORT) logger.info("[add_device_type] url :%s" % url) Params = {'account_token': account_token} Headers = {'Content-Type':"application/json"} Data = {"name": mainModel, "data": [ { "terminalType": terminalType, "deviceTag": deviceTag, "platformType": platformType }] } result = requests.post(url, params=Params,headers=Headers, data=json.dumps(Data), timeout=settings.REQUESTS_TIMEOUT) if result.status_code == 200: resultData = result.json() logger.info("[add_device_type] result :%s" % resultData) if resultData.get('success') == 1: logger.error('[add_device_type] success!') return True else: logger.error(resultData) return False else: logger.error("[add_device_type] status_code :%s" % result.status_code) return False except Exception as e: logger.error('[add_device_type] error : %s' % e) return False
def add_device_type_limit(moid,e164,version,sn,ip,startIp,endIp,terminalType,mainType,subType,limitType): try: account_token = get_account_token(settings.SSO_HOST, settings.SSO_PORT) url = DEVICE_LIMIT_URL.format(ip=settings.SSO_HOST, port=settings.SSO_PORT) logger.info("[add_device_type_limit] url :%s" % url) Params = {'account_token':account_token} Headers = {'Content-Type':"application/json"} Data = {"moid": moid, "e164": e164, "version": version, "sn": sn, "ip": ip, "startIp": startIp, "endIp": endIp, "terminalType": terminalType, "mainType": mainType, "subType": subType, "limitType": limitType } result = requests.post(url, params=Params,headers=Headers,data=json.dumps(Data), timeout=settings.REQUESTS_TIMEOUT) if result.status_code == 200: resultData = result.json() logger.info("[add_device_type_limit] result :%s" % resultData) if resultData.get('success') == 1: return True else: logger.error(resultData) return False else: logger.error("[add_device_type_limit] status_code :%s" % result.status_code) return False except Exception as e: logger.error('[add_device_type_limit] error! %s' % e) return False