def decorated(*args, **kwargs): try: decode_jwt = decode_token(get_token()) if decode_jwt: if is_same_user: if get_http_status_code( ObjectId(decode_jwt['user_id']), ObjectId(get_view_args())) == 403: return return_error_response(403, 'NO_AUTORIZED') if model is not None and \ get_http_status_code(get_model(model), get_model(decode_jwt['role'])) == 403: return return_error_response(403, 'NO_AUTORIZED') if get_model(decode_jwt['role']) == Provider: if is_by_id: filters = { "_id": ObjectId(get_view_args()), attribute: ObjectId(decode_jwt['user_id']) } else: filters = { attribute: ObjectId(decode_jwt['user_id']) } data = find_one_in_collection(collection, filters) if data is None: return return_error_response(401, 'NO_AUTORIZED') except Exception: return return_error_response(403, '') return fn(*args, **kwargs)
def get(self, id): self.project = find_one_in_collection('projects', {"_id": ObjectId(id)}) sproject = find_one_in_collection('sprojects', {"project": self.project['_id']}) instance = find_one_in_collection('instances', {"sproject": sproject["_id"]}) if instance is not None: item = self.create_data(str(instance["instance_id"])) if item is None: return respond_json(create_message_error(400, "BAD_URL"), 400) data = self.orch.get_billing(item) status_code = data.status_code response = data.text else: response = create_message_error(404, "NO_INSTANCES") status_code = 404 return respond_json(response, status=status_code)
def get_project(self, name_project, data): """ Consultamos el proyecto/entidad en Mongo, ya que guardamos cierta información de la entitiy y la API de keystone no ofrece un findByName. :return: """ entity = find_one_in_collection('entities', {"name": name_project}) entity_id = entity['keystone_project_id'] json_data = read_json_file(get_cfg('keystone__data_create_user')) json_data['user']['default_project_id'] = entity_id json_data['user']['name'] = data['email'] json_data['user']['password'] = data['password'] return json_data, entity_id, entity
def get(self, id): item = [] limit = get_int(get_arg('limit')) skip = get_int(get_arg('skip')) _filter = self.get_status(id) result = super(SProjectStatusRes, self)._get_items(skip * limit, limit, _filter) for sproject in result: sitems = [] sitem = {} project = find_one_in_collection( 'projects', {"services": { "$in": [ObjectId(sproject['_id'])] }}) data = item_to_json(project, [ '_id', 'name', 'summary', 'description', 'client', 'created_at' ]) data['sproject'] = str(sproject['_id']) data['status'] = sproject['status'] service = find_one_in_collection('services', {'_id': sproject['service']}) provider = find_one_in_collection('users', {'_id': service['provider']}) client = find_one_in_collection( 'users', {"_id": ObjectId(project['client'])}) sitem['provider'] = item_to_json(provider, ['_id', 'user_name']) sitem['service'] = item_to_json(service, ['_id', 'name']) data['client'] = item_to_json(client, ['_id', 'user_name']) data['provider'] = str(sproject['provider']) sitems.append(sitem) data['services'] = sitems item.append(data) response = dict(count=count_collection(self.collection, _filter), skip=skip, limit=limit, result=item) return dict(response=response, status=200)
def user_login(self, email, password): self.keystone.keystone_admin = email self.keystone.keystone_admin_pass = password response = self.keystone.get_login() if response.status_code == 201: # Encontrado el usuario en Keystone. Lo buscamos en mongo data = find_one_in_collection( 'users', { 'keystone_user_id': json.loads(response.text)['token']['user']['id'] }) if data is not None: self.create_role_user(data) response.status_code = 200 else: response.status_code = 404 self.item = create_message_error(response.status_code, "USER_NOT_FOUND") return create_response(response.status_code, self.item)
def decorated(*args, **kwargs): try: decode_jwt = decode_token(get_token()) if decode_jwt: data = find_one_in_collection( 'users', { "_id": ObjectId(decode_jwt['user_id']), "activated": True }) if data is None: return return_error_response(401, 'NO_AUTORIZED') if model is not None: if get_http_status_code( get_model(model), get_model(decode_jwt['role'])) == 403: return return_error_response(403, 'NO_AUTORIZED') else: return return_error_response(403, 'TOKEN_EXPIRED') return fn(*args, **kwargs) except Exception: return return_error_response(403, '')
def put(self, id): data = get_json() user = find_one_in_collection(self.collection, {"_id": ObjectId(id)}) if 'activated' in data: response = self.keystone.patch_user(user['keystone_user_id'], data['activated']) if response.status_code == 200: if user['activated'] and not data['activated']: self.smm.ban(user['email']) if data['activated'] and not user['activated']: self.smm.welcome(user['email']) if '_id' in data: del data['_id'] try: self.u.update(id, data) status_code = 204 except Exception as e: print e status_code = 400 self.item = create_message_error(status_code, "USER_NOT_FOUND") if 'activated' in data: self.keystone.patch_user(user['keystone_user_id'], not (data['activated'])) return create_response(status_code, self.item)
def get_project(collection, filter): return find_one_in_collection(collection, filter)