def makeFolderWithPerms(self, agent, actionsName): root = Folder.getRootFolder() actions = getattr(Actions, actionsName.upper()) prefix = re.sub(r'^\w+:', '', agent) folder = root.makeSubFolder('%s_%s' % (prefix, actionsName)) folder.clearAcl() folder.setPermissions(agent, actions) # insert an object to the folder so we can test read access m = Member(name='foo') m.save() m.folders = [folder] m.save() return folder
def setUp(self): self.admin = User.objects.create_superuser('admin', '*****@*****.**', password='******') self.alice = User.objects.create_user('alice', '*****@*****.**') self.bob = User.objects.create_user('bob', '*****@*****.**') self.clara = User.objects.create_user('clara', '*****@*****.**') self.dave = User.objects.create_user('dave', '*****@*****.**') root = Folder.getRootFolder() self.f1 = root.makeSubFolder('f1') self.f1.setPermissions(self.alice, Actions.ALL) self.f1.setPermissions(self.bob, Actions.WRITE) self.f1.setPermissions(self.clara, Actions.READ) self.f1.setPermissions('group:anyuser', Actions.NONE) levels = ('all', 'write', 'read', 'none') self.anyuserDir = {} for level in levels: self.anyuserDir[level] = self.makeFolderWithPerms('group:anyuser', level) self.authuserDir = {} for level in levels: self.authuserDir[level] = self.makeFolderWithPerms('group:authuser', level)
def save(self, commit=True): group = super(GroupForm, self).save(commit=True) if commit: group.save() # Create and save the group profile group_profile = GroupProfile() group_profile.group = group # Need to add validation for the password fields if len(self.cleaned_data['group_password1']) > 0: group_profile.set_password( self.cleaned_data['group_password1']) group_profile.save() # Now we need to actually create folders private_folder = Folder() private_folder.name = '%s Private' % group.name private_folder.notes = 'This folder is private to the %s group' % group.name private_folder.save() # Give users in this group read and write access to it private_folder.setPermissions(group, Actions.WRITE) private_folder.save() # Now we need to create the public folder for this group public_folder = Folder() public_folder.name = '%s Public' % group.name public_folder.notes = 'This folder is publically viewable and writable memebers of the %s group' % group.name public_folder.save() # Give any user the ability to read this folder anyuser = Group.objects.get(name="anyuser") public_folder.setPermissions(anyuser, Actions.READ) # Give this group read and write access public_folder.setPermissions(group, Actions.WRITE) public_folder.save() return (group, group_profile, public_folder, private_folder) return group
def byClara(): Folder.mkdirAssertAllowed(self.clara, '/f1/byClara')
def test_mkdir(self): # in these cases the getFolder() call should raise an exception if # the mkdir did not create the dir successfully # admin, alice, and bob have write privileges Folder.mkdirAssertAllowed(self.admin, '/f1/byAdmin') Folder.getFolderAssertAllowed(self.admin, '/f1/byAdmin') Folder.mkdirAssertAllowed(self.alice, '/f1/byAlice') Folder.getFolderAssertAllowed(self.alice, '/f1/byAlice') Folder.mkdirAssertAllowed(self.bob, '/f1/byBob') Folder.getFolderAssertAllowed(self.bob, '/f1/byBob') # clara has only read privileges, denied def byClara(): Folder.mkdirAssertAllowed(self.clara, '/f1/byClara') self.assertRaises(PermissionDenied, byClara)
def migrate(opts): # back up the database before migrating if not opts.nodump: db = settings.DATABASES['default'] dbName = db['NAME'] timeText = datetime.datetime.now().strftime('%Y_%m_%d_%H%M%S') dumpFile = '%s_%s_migrate.sql' % (timeText, dbName) cmd = ('mysqldump --user="******" --password="******" %s > %s' % (db['USER'], db['PASSWORD'], dbName, dumpFile)) dosys(cmd, stopOnError=True) cursor = connection.cursor() # drop tables with no data noDataTables = ('geocamCore_assignment', 'geocamCore_permission', 'geocamCore_unit', 'geocamCore_change', 'geocamCore_operation') for table in noDataTables: cursor.execute('DROP TABLE `%s`' % table) transaction.commit() # create tables dosys('%s/manage.py syncdb --noinput' % settings.CHECKOUT_DIR) # change UserProfile column definitions cursor.execute( 'ALTER TABLE `geocamCore_userprofile` CHANGE COLUMN `homeTitle` `homeJobTitle` varchar(64) NOT NULL' ) # syncdb will do the following for us since ManyToManyFields are # stored in separate tables: # drop: userPermissions, assignments # add: operations transaction.commit() # change Track column definitions cursor.execute( 'ALTER TABLE `geocamTrack_track` CHANGE COLUMN `name` `name` varchar(80) NOT NULL' ) newColumns = ('`author_id` integer', '`sensor_id` integer', '`isAerial` bool NOT NULL', '`notes` longtext NOT NULL', '`tags` varchar(255) NOT NULL', '`icon` varchar(16) NOT NULL', '`status` varchar(1) NOT NULL', '`processed` bool NOT NULL', '`version` integer UNSIGNED NOT NULL', '`purgeTime` datetime', '`workflowStatus` integer UNSIGNED NOT NULL', '`mtime` datetime', '`minTime` datetime NOT NULL', '`maxTime` datetime NOT NULL', '`minLat` double precision', '`minLon` double precision', '`maxLat` double precision', '`maxLon` double precision') for col in newColumns: cursor.execute('ALTER TABLE `geocamTrack_track` ADD COLUMN %s' % col) transaction.commit() # for each old folder, make a Folder, Group, and Context cursor.execute('SELECT `id`, `name`, `timeZone` FROM `geocamCore_folder`') newFolderLookup = {} for oldFolderId, name, timeZone in cursor.fetchall(): g = Group(name=name) g.save() transaction.commit() rootFolder = Folder.objects.get(id=1) f = Folder(name=name, parent=rootFolder) f.save() transaction.commit() f.setPermissions(g, Actions.ALL) transaction.commit() c = Context(name=name, uploadFolder=f, timeZone=timeZone) c.save() transaction.commit() gp = g.groupprofile gp.context = c gp.save() transaction.commit() newFolderLookup[oldFolderId] = f # syncdb should create these tables (according to sqlall) but # doesn't for some reason cursor.execute(""" CREATE TABLE `geocamLens_photo_folders` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `photo_id` integer NOT NULL, `folder_id` integer NOT NULL, UNIQUE (`photo_id`, `folder_id`) ) """) transaction.commit() cursor.execute(""" CREATE TABLE `geocamTrack_track_folders` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `track_id` integer NOT NULL, `folder_id` integer NOT NULL, UNIQUE (`track_id`, `folder_id`) ) """) transaction.commit() # fill in new 'folders' field folderTables = ('geocamLens_photo', ) for table in folderTables: modelName = table.split('_')[1] cursor.execute('SELECT `id`, `folder_id` FROM `%s`' % table) for objId, oldFolderId in cursor.fetchall(): newFolderId = newFolderLookup[oldFolderId].id cursor.execute( 'INSERT INTO `%s_folders` (`%s_id`, `folder_id`) VALUES (%d, %d)' % (table, modelName, objId, newFolderId)) transaction.commit() # drop old 'folder' field for table in folderTables: cursor.execute('ALTER TABLE `%s` DROP COLUMN `folder_id`' % table) transaction.commit() # drop remaining obsolete tables cursor.execute('DROP TABLE `geocamCore_folder`') transaction.commit()
def save(self, commit=True): group = super(GroupForm, self).save(commit=True) if commit: group.save() # Create and save the group profile group_profile = GroupProfile() group_profile.group = group # Need to add validation for the password fields if len(self.cleaned_data['group_password1']) > 0: group_profile.set_password(self.cleaned_data['group_password1']) group_profile.save() # Now we need to actually create folders private_folder = Folder() private_folder.name = '%s Private' % group.name private_folder.notes = 'This folder is private to the %s group' % group.name private_folder.save() # Give users in this group read and write access to it private_folder.setPermissions(group, Actions.WRITE) private_folder.save() # Now we need to create the public folder for this group public_folder = Folder() public_folder.name = '%s Public' % group.name public_folder.notes = 'This folder is publically viewable and writable memebers of the %s group' % group.name public_folder.save() # Give any user the ability to read this folder anyuser = Group.objects.get(name="anyuser") public_folder.setPermissions(anyuser, Actions.READ) # Give this group read and write access public_folder.setPermissions(group, Actions.WRITE) public_folder.save() return (group, group_profile, public_folder, private_folder) return group
def migrate(opts): # back up the database before migrating if not opts.nodump: db = settings.DATABASES['default'] dbName = db['NAME'] timeText = datetime.datetime.now().strftime('%Y_%m_%d_%H%M%S') dumpFile = '%s_%s_migrate.sql' % (timeText, dbName) cmd = ('mysqldump --user="******" --password="******" %s > %s' % (db['USER'], db['PASSWORD'], dbName, dumpFile)) dosys(cmd, stopOnError=True) cursor = connection.cursor() # drop tables with no data noDataTables = ('geocamCore_assignment', 'geocamCore_permission', 'geocamCore_unit', 'geocamCore_change', 'geocamCore_operation') for table in noDataTables: cursor.execute('DROP TABLE `%s`' % table) transaction.commit() # create tables dosys('%s/manage.py syncdb --noinput' % settings.CHECKOUT_DIR) # change UserProfile column definitions cursor.execute('ALTER TABLE `geocamCore_userprofile` CHANGE COLUMN `homeTitle` `homeJobTitle` varchar(64) NOT NULL') # syncdb will do the following for us since ManyToManyFields are # stored in separate tables: # drop: userPermissions, assignments # add: operations transaction.commit() # change Track column definitions cursor.execute('ALTER TABLE `geocamTrack_track` CHANGE COLUMN `name` `name` varchar(80) NOT NULL') newColumns = ('`author_id` integer', '`sensor_id` integer', '`isAerial` bool NOT NULL', '`notes` longtext NOT NULL', '`tags` varchar(255) NOT NULL', '`icon` varchar(16) NOT NULL', '`status` varchar(1) NOT NULL', '`processed` bool NOT NULL', '`version` integer UNSIGNED NOT NULL', '`purgeTime` datetime', '`workflowStatus` integer UNSIGNED NOT NULL', '`mtime` datetime', '`minTime` datetime NOT NULL', '`maxTime` datetime NOT NULL', '`minLat` double precision', '`minLon` double precision', '`maxLat` double precision', '`maxLon` double precision') for col in newColumns: cursor.execute('ALTER TABLE `geocamTrack_track` ADD COLUMN %s' % col) transaction.commit() # for each old folder, make a Folder, Group, and Context cursor.execute('SELECT `id`, `name`, `timeZone` FROM `geocamCore_folder`') newFolderLookup = {} for oldFolderId, name, timeZone in cursor.fetchall(): g = Group(name=name) g.save() transaction.commit() rootFolder = Folder.objects.get(id=1) f = Folder(name=name, parent=rootFolder) f.save() transaction.commit() f.setPermissions(g, Actions.ALL) transaction.commit() c = Context(name=name, uploadFolder=f, timeZone=timeZone) c.save() transaction.commit() gp = g.groupprofile gp.context = c gp.save() transaction.commit() newFolderLookup[oldFolderId] = f # syncdb should create these tables (according to sqlall) but # doesn't for some reason cursor.execute(""" CREATE TABLE `geocamLens_photo_folders` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `photo_id` integer NOT NULL, `folder_id` integer NOT NULL, UNIQUE (`photo_id`, `folder_id`) ) """) transaction.commit() cursor.execute(""" CREATE TABLE `geocamTrack_track_folders` ( `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `track_id` integer NOT NULL, `folder_id` integer NOT NULL, UNIQUE (`track_id`, `folder_id`) ) """) transaction.commit() # fill in new 'folders' field folderTables = ('geocamLens_photo',) for table in folderTables: modelName = table.split('_')[1] cursor.execute('SELECT `id`, `folder_id` FROM `%s`' % table) for objId, oldFolderId in cursor.fetchall(): newFolderId = newFolderLookup[oldFolderId].id cursor.execute('INSERT INTO `%s_folders` (`%s_id`, `folder_id`) VALUES (%d, %d)' % (table, modelName, objId, newFolderId)) transaction.commit() # drop old 'folder' field for table in folderTables: cursor.execute('ALTER TABLE `%s` DROP COLUMN `folder_id`' % table) transaction.commit() # drop remaining obsolete tables cursor.execute('DROP TABLE `geocamCore_folder`') transaction.commit()