async def delete(self, _id): try: result = await self._coll.delete_one(filter={'id': _id}) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result.deleted_count is 0: raise ResourceNotFound(_id) return
async def get_all(self, request): user = request.match_info['user'] if user == '_self': user = await self.aa.get_user(request) else: await self.aa.require_admin(request) if not await self.users.resource_exists(user): raise ResourceNotFound(user) return json_response(await self.credentials.get_all(user))
async def post(self, request): await self.aa.require_admin(request) perm = request.match_info['perm'] payload = await request.json() jsonschema.validate(payload, PERMISSIONS_CREATE, format_checker=jsonschema.draft4_format_checker) payload = payload.get('data') for user in payload['users']: if not await self.users.resource_exists(user): raise ResourceNotFound("User: {0}".format(user)) result = await self.permissions.create(perm, payload) return json_response(result, status=201)
async def get(self, _id, fields=None): try: result = await self._coll.find_one( filter={ 'id': _id, 'deleting': False }, projection=self._projection(fields)) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) return self._format(result)
async def get(self, _id, fields=None): try: result = await self._coll.find_one( filter={ 'id': _id, }, projection=self._projection(fields)) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) if 'acquired_since' in result: result['acquired_since'] = result['acquired_since'].replace( tzinfo=datetime.timezone.utc).isoformat() return self._format(result)
async def get(self, _id, owner): try: result = await self._coll.find_one(filter={ 'id': self._str_uuid_2_bin(_id), 'owner': owner }, projection=self._projection()) if result is None: raise ResourceNotFound(_id) result['id'] = _id if 'created' in result: result['created'] = str(result['created']) return self._format(result) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err)
async def post(self, request): user = request.match_info['user'] if user == '_self': user = await self.aa.get_user(request) else: await self.aa.require_admin(request) payload = await request.json() jsonschema.validate(payload, CREDENTIALS_CREATE, format_checker=jsonschema.draft4_format_checker) payload = payload.get('data') if not await self.users.resource_exists(user): raise ResourceNotFound(user) return json_response(await self.credentials.create(user, payload), status=201)
async def update(self, _id, payload): if 'password' in payload: payload['password'] = await self._password(payload['password']) update = {'$set': {}} for k, v in payload.items(): update['$set'][k] = v try: result = await self._coll.find_one_and_update( filter={ 'id': _id, 'deleting': False }, update=update, projection=self._projection(), return_document=pymongo.ReturnDocument.AFTER) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) return self._format(result)
async def update(self, _id, owner, payload): update = {'$set': {}} for k, v in payload.items(): update['$set'][k] = v try: result = await self._coll.find_one_and_update( filter={ 'id': self._str_uuid_2_bin(_id), 'owner': owner }, update=update, projection=self._projection(), return_document=pymongo.ReturnDocument.AFTER) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result is None: raise ResourceNotFound(_id) if 'created' in result: result['created'] = str(result['created']) result['id'] = _id return self._format(result)
async def delete(self, _id, payload): try: if 'force' in payload: result = await self._coll.delete_one(filter={ 'id': _id, }) else: query = { 'id': _id, 'acquired_by': payload['acquired_by'], } if 'secret' in payload: query['secret'] = payload['secret'] else: query['secret'] = None result = await self._coll.delete_one(filter=query) except pymongo.errors.ConnectionFailure as err: raise MongoConnError(err) if result.deleted_count is 0: raise ResourceNotFound(_id) return