def updateUser(self, id, name='', password='', uid=1000, institution='', email='', permissions=755): """ Modifies user information. """ if id not in self.passwords: raise SeisHubError("User does not exists!") session = self.Session() user = session.query(User).filter_by(id=id).one() user.name = name if password: self._validatePassword(password) user.password = hash(password) user.institution = institution user.email = email user.uid = uid user.permissions = permissions session.add(user) try: session.commit() except: session.rollback() self.refresh()
def checkPassword(self, id, password): """ Check current password. """ if id not in self.passwords: raise SeisHubError("User does not exists!") return self.passwords[id] == hash(password)
def addUser(self, id, name, password, uid=1000, institution='', email='', permissions=755, checkPassword=True): """ Adds an user. """ if id in self.passwords: raise DuplicateObjectError("User already exists!") if checkPassword: self._validatePassword(password) user = User(id=id, uid=uid, name=name, password=hash(password), institution=institution, email=email, permissions=permissions) session = self.Session() session.add(user) try: session.commit() except Exception, e: session.rollback() raise SeisHubError(str(e))
def setData(self, data): """set data, convert to unicode and remove XML declaration""" if not data or data == "": self._data = None return if not isinstance(data, unicode): raise TypeError("Data has to be unicode!") # encode "utf-8" to determine hash and size raw_data = data.encode("utf-8") self._data = data self.meta._size = len(raw_data) + XML_DECLARATION_LENGTH self.meta._hash = hash(raw_data)
def requestAvatarId(self, credentials): """ @param credentials: something which implements one of the interfaces in self.credentialInterfaces. @return: a Deferred which will fire a string which identifies an avatar, an empty tuple to specify an authenticated anonymous user (provided as checkers.ANONYMOUS) or fire a Failure(UnauthorizedLogin). Alternatively, return the result itself. """ username = credentials.username if username in self.env.auth.passwords: if hash(credentials.password) == self.env.auth.passwords[username]: return defer.succeed(username) err = error.UnauthorizedLogin("No such user or bad password") return defer.fail(err)
def testUnversionedResource(self): #====================================================================== # addResource() #====================================================================== # add empty resource empty = Resource(document=XmlDocument()) self.assertRaises(InvalidParameterError, self.xmldbm.addResource, empty) res1 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data, uid='testuser')) otherpackage = self.env.registry.db_registerPackage("otherpackage") othertype = self.env.registry.db_registerResourceType("otherpackage", "testml") res2 = Resource(othertype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(res1) self.xmldbm.addResource(res2) # try to add a resource with same id res3 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data), id=res2.id) self.assertRaises(DuplicateObjectError, self.xmldbm.addResource, res3) #====================================================================== # getResource() #====================================================================== result = self.xmldbm.getResource(id=res1.id) # check lazyness of Resource.data: assert isinstance(result.document._data, DbAttributeProxy) self.assertEquals(result.name, str(res1.id)) self.assertEquals(result.document.data, self.test_data) self.assertTrue(result.document.meta.datetime) self.assertEquals(result.document.meta.size, len(self.test_data) + XML_DECLARATION_LENGTH) self.assertEquals(result.document.meta.hash, hash(self.test_data)) self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) result = self.xmldbm.getResource(id=res2.id) self.assertEquals(result.document.data, self.test_data) self.assertEquals(result.document.meta.uid, None) self.assertEquals(result.package.package_id, otherpackage.package_id) self.assertEquals(result.resourcetype.resourcetype_id, othertype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # modifyResource() #====================================================================== modres = Resource(res1.resourcetype, res1.id, document=newXMLDocument(self.test_data_mod)) self.xmldbm.modifyResource(res1, modres) result = self.xmldbm.getResource(res1.package.package_id, res1.resourcetype.resourcetype_id, id=res1.id) self.assertEquals(result.document.data, self.test_data_mod) # user id is still the same self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # deleteResource() #====================================================================== # by object self.xmldbm.deleteResource(res1) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # add again self.xmldbm.addResource(res1) # there again? self.assertTrue(self.xmldbm.getResource(id=res1.id)) # now by resource_id self.xmldbm.deleteResource(resource_id=res1._id) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # now for res2 self.xmldbm.deleteResource(res2) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res2.id) # cleanup self.env.registry.db_deleteResourceType(otherpackage.package_id, othertype.resourcetype_id) self.env.registry.db_deletePackage(otherpackage.package_id)
def testUnversionedResource(self): #====================================================================== # addResource() #====================================================================== # add empty resource empty = Resource(document=XmlDocument()) self.assertRaises(InvalidParameterError, self.xmldbm.addResource, empty) res1 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data, uid='testuser')) otherpackage = self.env.registry.db_registerPackage("otherpackage") othertype = self.env.registry.db_registerResourceType( "otherpackage", "testml") res2 = Resource(othertype, document=newXMLDocument(self.test_data)) self.xmldbm.addResource(res1) self.xmldbm.addResource(res2) # try to add a resource with same id res3 = Resource(self.test_resourcetype, document=newXMLDocument(self.test_data), id=res2.id) self.assertRaises(DuplicateObjectError, self.xmldbm.addResource, res3) #====================================================================== # getResource() #====================================================================== result = self.xmldbm.getResource(id=res1.id) # check lazyness of Resource.data: assert isinstance(result.document._data, DbAttributeProxy) self.assertEquals(result.name, str(res1.id)) self.assertEquals(result.document.data, self.test_data) self.assertTrue(result.document.meta.datetime) self.assertEquals(result.document.meta.size, len(self.test_data) + XML_DECLARATION_LENGTH) self.assertEquals(result.document.meta.hash, hash(self.test_data)) self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) result = self.xmldbm.getResource(id=res2.id) self.assertEquals(result.document.data, self.test_data) self.assertEquals(result.document.meta.uid, None) self.assertEquals(result.package.package_id, otherpackage.package_id) self.assertEquals(result.resourcetype.resourcetype_id, othertype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # modifyResource() #====================================================================== modres = Resource(res1.resourcetype, res1.id, document=newXMLDocument(self.test_data_mod)) self.xmldbm.modifyResource(res1, modres) result = self.xmldbm.getResource(res1.package.package_id, res1.resourcetype.resourcetype_id, id=res1.id) self.assertEquals(result.document.data, self.test_data_mod) # user id is still the same self.assertEquals(result.document.meta.uid, 'testuser') self.assertEquals(result.package.package_id, self.test_package.package_id) self.assertEquals(result.resourcetype.resourcetype_id, self.test_resourcetype.resourcetype_id) self.assertEquals(result.resourcetype.version_control, False) #====================================================================== # deleteResource() #====================================================================== # by object self.xmldbm.deleteResource(res1) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # add again self.xmldbm.addResource(res1) # there again? self.assertTrue(self.xmldbm.getResource(id=res1.id)) # now by resource_id self.xmldbm.deleteResource(resource_id=res1._id) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res1.id) # now for res2 self.xmldbm.deleteResource(res2) self.assertRaises(NotFoundError, self.xmldbm.getResource, id=res2.id) # cleanup self.env.registry.db_deleteResourceType(otherpackage.package_id, othertype.resourcetype_id) self.env.registry.db_deletePackage(otherpackage.package_id)