class SecureTagAPI(object): """The public API to secure L{Tag}-related functionality. @param user: The L{User} to perform operations on behalf of. """ def __init__(self, user): self._api = CachingTagAPI(user) self._user = user def create(self, values): """See L{TagAPI.create}. @raises PermissionDeniedError: Raised if the user is not authorized to create L{Tag}s. """ pathsAndOperations = [(getParentPath(path), Operation.CREATE_NAMESPACE) for path, description in values] deniedOperations = checkPermissions(self._user, pathsAndOperations) if deniedOperations: raise PermissionDeniedError(self._user.username, deniedOperations) return self._api.create(values) def delete(self, paths): """See L{TagAPI.delete}. @raises PermissionDeniedError: Raised if the user is not authorized to delete L{Tag}s. """ if isgenerator(paths): paths = list(paths) pathsAndOperations = [(path, Operation.DELETE_TAG) for path in paths] deniedOperations = checkPermissions(self._user, pathsAndOperations) if deniedOperations: raise PermissionDeniedError(self._user.username, deniedOperations) return self._api.delete(paths) def get(self, paths, withDescriptions=None): """See L{TagAPI.get}.""" return self._api.get(paths, withDescriptions=withDescriptions) def set(self, values): """See L{TagAPI.set}. @raises PermissionDeniedError: Raised if the user is not authorized to update L{Tag}s. """ pathsAndOperations = [(path, Operation.UPDATE_TAG) for path in values.iterkeys()] deniedOperations = checkPermissions(self._user, pathsAndOperations) if deniedOperations: raise PermissionDeniedError(self._user.username, deniedOperations) return self._api.set(values)
def setUp(self): super(CachingTagAPIWithBrokenCacheTest, self).setUp() self.system = createSystemData() UserAPI().create([(u'username', u'password', u'User', u'*****@*****.**')]) self.user = getUser(u'username') self.tags = CachingTagAPI(self.user) self.permissions = CachingPermissionAPI(self.user)
def testDeleteInvalidatesRecentObjectActivity(self): """ L{CachingTagValueAPI.delete} invalidates cache recent activity for the object IDs that have been modified. """ objectID = uuid4() CachingTagAPI(self.user).create([(u'username/tag', u'A tag')]) cache = RecentObjectActivityCache() cache.save(objectID, [(u'username/tag', objectID, u'about-value', u'tag-value', u'username', datetime.utcnow())]) self.tagValues.delete([(objectID, u'username/tag')]) result = cache.get(objectID) self.assertEqual({}, result.results) self.assertEqual([objectID], result.uncachedValues)
def testDeleteInvalidatesRecentUserActivity(self): """ L{CachingTagValueAPI.delete} invalidates cached recent activity data for the L{User} that's removed data. """ objectID = uuid4() CachingTagAPI(self.user).create([(u'username/tag', u'A tag')]) cache = RecentUserActivityCache() cache.save( u'username', [(u'username/tag', objectID, u'about-value', u'tag-value', u'username', datetime.utcnow())]) self.tagValues.delete([(objectID, u'username/tag')]) result = cache.get(u'username') self.assertEqual({}, result.results) self.assertEqual([u'username'], result.uncachedValues)
def tags(self, user): """Get a new L{TagAPI} instance.""" from fluiddb.cache.tag import CachingTagAPI return CachingTagAPI(user)
def __init__(self, user): self._api = CachingTagAPI(user) self._user = user