use of org.jbei.ice.lib.dto.access.AccessPermission in project ice by JBEI.
the class EntryCreator method createEntry.
/**
* Create an entry in the database.
* <p/>
* Generates a new Part Number, the record id (UUID), version id, and timestamps.
* Optionally set the record globally visible or schedule an index rebuild.
*
* @param account account of user creating entry
* @param entry entry record being created
* @param accessPermissions list of permissions to associate with created entry
* @return entry that was saved in the database.
*/
public Entry createEntry(Account account, Entry entry, ArrayList<AccessPermission> accessPermissions) {
if (entry.getRecordId() == null) {
entry.setRecordId(Utils.generateUUID());
entry.setVersionId(entry.getRecordId());
}
entry.setCreationTime(Calendar.getInstance().getTime());
entry.setModificationTime(entry.getCreationTime());
if (StringUtils.isEmpty(entry.getOwner()))
entry.setOwner(account.getFullName());
if (StringUtils.isEmpty(entry.getOwnerEmail()))
entry.setOwnerEmail(account.getEmail());
if (entry.getSelectionMarkers() != null) {
for (SelectionMarker selectionMarker : entry.getSelectionMarkers()) {
selectionMarker.setEntry(entry);
}
}
if (entry.getLinks() != null) {
for (Link link : entry.getLinks()) {
link.setEntry(entry);
}
}
if (entry.getStatus() == null)
entry.setStatus("");
if (entry.getBioSafetyLevel() == null)
entry.setBioSafetyLevel(0);
entry = dao.create(entry);
// check for pi
String piEmail = entry.getPrincipalInvestigatorEmail();
if (StringUtils.isNotEmpty(piEmail)) {
Account pi = DAOFactory.getAccountDAO().getByEmail(piEmail);
if (pi != null) {
// add write permission for the PI
addWritePermission(pi, entry);
}
}
// add write permissions for owner
addWritePermission(account, entry);
// add read permission for all public groups
ArrayList<Group> groups = new GroupController().getAllPublicGroupsForAccount(account);
for (Group group : groups) {
addReadPermission(null, group, entry);
}
if (accessPermissions != null) {
for (AccessPermission accessPermission : accessPermissions) {
if (accessPermission.getArticle() == AccessPermission.Article.ACCOUNT) {
Account accessAccount = DAOFactory.getAccountDAO().get(accessPermission.getArticleId());
// add account read permission
addReadPermission(accessAccount, null, entry);
} else {
// add group read permission
Group group = DAOFactory.getGroupDAO().get(accessPermission.getArticleId());
addReadPermission(null, group, entry);
}
}
}
// rebuild blast database
if (sequenceDAO.hasSequence(entry.getId())) {
BlastPlus.scheduleBlastIndexRebuildTask(true);
}
return entry;
}
use of org.jbei.ice.lib.dto.access.AccessPermission in project ice by JBEI.
the class EntryPermissionTask method addPermissions.
protected void addPermissions(Entry entry) {
for (AccessPermission access : permissions) {
// account or group
Account account = null;
Group group = null;
switch(access.getArticle()) {
case ACCOUNT:
default:
account = accountDAO.get(access.getArticleId());
break;
case GROUP:
group = groupDAO.get(access.getArticleId());
break;
}
// does the permissions already exists
if (permissionDAO.hasPermission(entry, null, null, account, group, access.isCanRead(), access.isCanWrite()))
return;
// add the permission if not
Permission permission = new Permission();
permission.setEntry(entry);
entry.getPermissions().add(permission);
permission.setGroup(group);
permission.setFolder(null);
permission.setUpload(null);
permission.setAccount(account);
permission.setCanRead(access.isCanRead());
permission.setCanWrite(access.isCanWrite());
permissionDAO.create(permission);
}
}
use of org.jbei.ice.lib.dto.access.AccessPermission in project ice by JBEI.
the class EntryPermissions method add.
/**
* Adds a new permission to the specified entry. If the entry does not exist, a new one is created
*
* @param access permissions to be added to the entry
* @return created permission if successful, null otherwise
* @throws PermissionException if the requesting user does not have write permissions on the entry
*/
public AccessPermission add(AccessPermission access) {
if (access == null)
return null;
authorization.expectWrite(userId, entry);
Permission permission = addPermission(access, entry, null, null);
if (permission == null)
return null;
return permission.toDataTransferObject();
}
use of org.jbei.ice.lib.dto.access.AccessPermission in project ice by JBEI.
the class FolderPermissionsTest method testCreateFolderPermission.
@Test
public void testCreateFolderPermission() throws Exception {
Account account = AccountCreator.createTestAccount("FolderPermissionsTest.testCreateFolderPermission", false);
String userId = account.getEmail();
Folder folder = new Folder();
folder.setOwnerEmail(userId);
folder.setType(FolderType.PRIVATE);
folder.setDescription("test folder");
folder.setName("test");
folder = DAOFactory.getFolderDAO().create(folder);
Assert.assertNotNull(folder);
FolderPermissions folderPermissions = new FolderPermissions(userId, folder.getId());
AccessPermission accessPermission = new AccessPermission();
// create a new account
Account account2 = AccountCreator.createTestAccount("FolderPermissionsTest.testCreateFolderPermission2", false);
// give read permission to folder for account
accessPermission.setArticle(AccessPermission.Article.ACCOUNT);
accessPermission.setType(AccessPermission.Type.READ_FOLDER);
accessPermission.setArticleId(account2.getId());
accessPermission.setTypeId(folder.getId());
Assert.assertNotNull(folderPermissions.createPermission(accessPermission));
}
use of org.jbei.ice.lib.dto.access.AccessPermission in project ice by JBEI.
the class PermissionsController method addPermission.
protected Permission addPermission(AccessPermission access, Entry entry, Folder folder, BulkUpload upload) {
// account or group
Account account = null;
Group group = null;
switch(access.getArticle()) {
case ACCOUNT:
default:
account = accountDAO.get(access.getArticleId());
break;
case GROUP:
group = groupDAO.get(access.getArticleId());
break;
}
// does the permissions already exists
if (dao.hasPermission(entry, folder, upload, account, group, access.isCanRead(), access.isCanWrite())) {
return dao.retrievePermission(entry, folder, upload, account, group, access.isCanRead(), access.isCanWrite());
}
// add the permission if not
Permission permission = new Permission();
permission.setEntry(entry);
if (entry != null)
entry.getPermissions().add(permission);
permission.setGroup(group);
permission.setFolder(folder);
permission.setUpload(upload);
permission.setAccount(account);
permission.setCanRead(access.isCanRead());
permission.setCanWrite(access.isCanWrite());
return dao.create(permission);
}
Aggregations