use of org.jbei.ice.lib.dto.web.RemoteUser in project ice by JBEI.
the class Groups method addGroup.
/**
* Adds group to the list of groups for current user
*
* @param userGroup information about group to be added, including members (local and remote)
* @return added group
*/
public UserGroup addGroup(UserGroup userGroup) {
if (userGroup.getType() == null)
userGroup.setType(GroupType.PRIVATE);
if (userGroup.getType() == GroupType.PUBLIC && !accountController.isAdministrator(userId)) {
String errMsg = "Non admin '" + userId + "' attempting to create public group";
Logger.error(errMsg);
throw new PermissionException(errMsg);
}
Account account = accountDAO.getByEmail(userId);
Group group = new Group();
group.setUuid(Utils.generateUUID());
group.setLabel(userGroup.getLabel());
group.setDescription(userGroup.getDescription() == null ? "" : userGroup.getDescription());
group.setType(userGroup.getType());
group.setOwner(account);
group.setAutoJoin(userGroup.isAutoJoin());
group.setCreationTime(new Date());
group = dao.create(group);
// add local members
if (userGroup.getMembers() != null && !userGroup.getMembers().isEmpty()) {
for (AccountTransfer accountTransfer : userGroup.getMembers()) {
Account memberAccount = accountDAO.getByEmail(accountTransfer.getEmail());
if (memberAccount == null)
continue;
group.getMembers().add(memberAccount);
memberAccount.getGroups().add(group);
accountDAO.update(memberAccount);
}
}
// add remote members
for (RemoteUser remoteUser : userGroup.getRemoteMembers()) {
RegistryPartner partner = remoteUser.getPartner();
if (partner == null)
continue;
RemotePartner remotePartner = remotePartnerDAO.get(partner.getId());
if (remotePartner == null)
continue;
AccountTransfer accountTransfer = remoteUser.getUser();
if (accountTransfer == null || StringUtils.isEmpty(accountTransfer.getEmail()))
continue;
String email = accountTransfer.getEmail();
RemoteClientModel remoteClientModel = remoteClientModelDAO.getModel(email, remotePartner);
if (remoteClientModel == null) {
remoteClientModel = new RemoteClientModel();
remoteClientModel.setEmail(email);
remoteClientModel.setRemotePartner(remotePartner);
remoteClientModel = remoteClientModelDAO.create(remoteClientModel);
}
remoteClientModel.getGroups().add(group);
remoteClientModelDAO.update(remoteClientModel);
}
return group.toDataTransferObject();
}
use of org.jbei.ice.lib.dto.web.RemoteUser in project ice by JBEI.
the class GroupsTest method testUpdate.
// @Test
// public void testGetGroupMembers() throws Exception {
//
// }
//
@Test
public void testUpdate() throws Exception {
Account account = AccountCreator.createTestAccount("GroupsTest.testUpdate", false);
// create local test accounts
Account account1 = AccountCreator.createTestAccount("GroupsTest.testUpdate1", false);
Account account2 = AccountCreator.createTestAccount("GroupsTest.testUpdate2", false);
Account account3 = AccountCreator.createTestAccount("GroupsTest.testUpdate3", false);
// create remote test accounts
RemotePartner partner = new RemotePartner();
partner.setUrl("registry-test2.jbei.org");
partner = DAOFactory.getRemotePartnerDAO().create(partner);
RemoteUser remoteUser1 = new RemoteUser();
remoteUser1.setUser(new AccountTransfer("Remote.GroupsTest.testUpdate1", ""));
remoteUser1.setPartner(partner.toDataTransferObject());
RemoteUser remoteUser2 = new RemoteUser();
remoteUser2.setUser(new AccountTransfer("Remote.GroupsTest.testUpdate2", ""));
remoteUser2.setPartner(partner.toDataTransferObject());
RemoteUser remoteUser3 = new RemoteUser();
remoteUser3.setUser(new AccountTransfer("Remote.GroupsTest.testUpdate3", ""));
remoteUser3.setPartner(partner.toDataTransferObject());
// create group with account1, account2 and remote1, remote2 as a members
UserGroup group = new UserGroup();
group.setLabel("label");
group.setDescription("description");
group.getMembers().add(account1.toDataTransferObject());
group.getMembers().add(account2.toDataTransferObject());
group.getRemoteMembers().add(remoteUser1);
group.getRemoteMembers().add(remoteUser2);
Groups groups = new Groups(account.getEmail());
UserGroup result = groups.addGroup(group);
Assert.assertNotNull(result);
// get members and test
result = groups.getGroupMembers(result.getId());
Assert.assertNotNull(result);
Assert.assertEquals(2, result.getMembers().size());
Assert.assertEquals(2, result.getRemoteMembers().size());
// update group to account2 as the only local and 1 remote account
result.getMembers().clear();
result.getRemoteMembers().clear();
result.getMembers().add(account3.toDataTransferObject());
result.getRemoteMembers().add(remoteUser3);
// update
result.setLabel("updated label");
result.setDescription("updated description");
Assert.assertTrue(groups.update(result.getId(), result));
// get members and test
group = groups.getGroupMembers(result.getId());
Assert.assertEquals("updated label", group.getLabel());
Assert.assertEquals("updated description", group.getDescription());
Assert.assertEquals(1, group.getMembers().size());
Assert.assertEquals(account3.getEmail(), group.getMembers().get(0).getEmail());
Assert.assertEquals(1, group.getRemoteMembers().size());
Assert.assertEquals(remoteUser3.getUser().getEmail(), group.getRemoteMembers().get(0).getUser().getEmail());
}
use of org.jbei.ice.lib.dto.web.RemoteUser in project ice by JBEI.
the class GroupsTest method testAddGroup.
// @Test
// public void testGet() throws Exception {
//
// }
//
// @Test
// public void testGet1() throws Exception {
//
// }
//
// @Test
// public void testGetMatchingGroups() throws Exception {
//
// }
@Test
public void testAddGroup() throws Exception {
Account account = AccountCreator.createTestAccount("GroupsTest.testAddGroup", false);
String email = account.getEmail();
UserGroup group = new UserGroup();
group.setLabel("label");
group.setDescription("description");
Account account1 = AccountCreator.createTestAccount("GroupsTest.testAddGroup2", false);
group.getMembers().add(account1.toDataTransferObject());
// add remote account
RemoteUser remoteUser = new RemoteUser();
AccountTransfer accountTransfer = new AccountTransfer();
accountTransfer.setEmail("Remote.GroupsTest.testAddGroup3");
remoteUser.setUser(accountTransfer);
// create remote partner
RemotePartner partner = new RemotePartner();
partner.setUrl("registry-test3.jbei.org");
partner = DAOFactory.getRemotePartnerDAO().create(partner);
remoteUser.setPartner(partner.toDataTransferObject());
group.getRemoteMembers().add(remoteUser);
Groups groups = new Groups(email);
UserGroup result = groups.addGroup(group);
Assert.assertNotNull(result);
result = groups.getGroupMembers(result.getId());
Assert.assertNotNull(result);
Assert.assertEquals(1, result.getMembers().size());
Assert.assertEquals(1, result.getRemoteMembers().size());
}
use of org.jbei.ice.lib.dto.web.RemoteUser in project ice by JBEI.
the class RemoteClientModel method toDataTransferObject.
@Override
public RemoteUser toDataTransferObject() {
RemoteUser remoteUser = new RemoteUser();
RegistryPartner partner = new RegistryPartner();
partner.setId(remotePartner.getId());
partner.setUrl(remotePartner.getUrl());
partner.setName(remotePartner.getName());
remoteUser.setPartner(partner);
AccountTransfer accountTransfer = new AccountTransfer();
accountTransfer.setEmail(email);
remoteUser.setUser(accountTransfer);
return remoteUser;
}
use of org.jbei.ice.lib.dto.web.RemoteUser in project ice by JBEI.
the class Groups method setGroupMembers.
protected void setGroupMembers(Group group, List<AccountTransfer> members, List<RemoteUser> remoteUsers) {
//
// deal with local members
//
Set<String> memberHash = new HashSet<>();
for (Account member : group.getMembers()) {
memberHash.add(member.getEmail());
}
for (AccountTransfer accountTransfer : members) {
String email = accountTransfer.getEmail();
// do not add if found in set
if (memberHash.remove(email))
continue;
// not found so add
Account memberToAdd = accountDAO.getByEmail(email);
if (memberToAdd == null) {
Logger.error("Could not find account " + email + " to add to group");
continue;
}
group.getMembers().add(memberToAdd);
memberToAdd.getGroups().add(group);
accountDAO.update(memberToAdd);
}
// all emails remaining should be removed
for (String memberEmail : memberHash) {
Account memberAccount = accountDAO.getByEmail(memberEmail);
if (memberAccount == null)
continue;
memberAccount.getGroups().remove(group);
group.getMembers().remove(memberAccount);
accountDAO.update(memberAccount);
}
//
// deal with remote users
//
Map<String, RemoteClientModel> remoteMembers = new HashMap<>();
// get remote members
List<RemoteClientModel> clients = remoteClientModelDAO.getClientsForGroup(group);
for (RemoteClientModel clientModel : clients) {
// technically the same email can be at different instances so adding partner id
String identifier = (clientModel.getEmail() + clientModel.getRemotePartner().getUrl()).toLowerCase();
remoteMembers.put(identifier, clientModel);
}
for (RemoteUser remoteUser : remoteUsers) {
String identifier = (remoteUser.getUser().getEmail() + remoteUser.getPartner().getUrl()).toLowerCase();
// do not add if found in set (already exists)
if (remoteMembers.remove(identifier) != null)
continue;
// add // todo : duplicates line 147. move to separate method
RemotePartner remotePartner = remotePartnerDAO.getByUrl(remoteUser.getPartner().getUrl());
if (remotePartner == null)
continue;
AccountTransfer accountTransfer = remoteUser.getUser();
if (accountTransfer == null || StringUtils.isEmpty(accountTransfer.getEmail()))
continue;
String email = accountTransfer.getEmail();
RemoteClientModel remoteClientModel = remoteClientModelDAO.getModel(email, remotePartner);
if (remoteClientModel == null) {
remoteClientModel = new RemoteClientModel();
remoteClientModel.setEmail(email);
remoteClientModel.setRemotePartner(remotePartner);
remoteClientModel = remoteClientModelDAO.create(remoteClientModel);
}
remoteClientModel.getGroups().add(group);
remoteClientModelDAO.update(remoteClientModel);
}
// remove all remaining
if (remoteMembers.isEmpty())
return;
for (String identifier : remoteMembers.keySet()) {
RemoteClientModel remoteClientModel = remoteMembers.get(identifier);
remoteClientModel.getGroups().remove(group);
remoteClientModelDAO.update(remoteClientModel);
}
}
Aggregations