use of org.activityinfo.legacy.shared.command.result.UserResult in project activityinfo by bedatadriven.
the class GetUsersHandler method execute.
@Override
public CommandResult execute(GetUsers cmd, User currentUser) {
Database db = em.getReference(Database.class, cmd.getDatabaseId());
UserPermission currentUserPermission = PermissionOracle.using(em).getPermissionByUser(db, currentUser);
assertAuthorized(currentUserPermission);
String whereClause = "up.database.id = :dbId and " + "up.user.id <> :currentUserId and " + "up.allowView = true";
if (!currentUserPermission.isAllowManageAllUsers()) {
whereClause += " and up.partner.id = " + currentUserPermission.getPartner().getId();
}
TypedQuery<UserPermission> query = em.createQuery("select up from UserPermission up where " + whereClause + " " + composeOrderByClause(cmd), UserPermission.class).setParameter("dbId", cmd.getDatabaseId()).setParameter("currentUserId", currentUser.getId());
List<Folder> folders = em.createQuery("select f from Folder f where f.database.id = :dbId", Folder.class).setParameter("dbId", cmd.getDatabaseId()).getResultList();
Map<ResourceId, Folder> folderMap = new HashMap<>();
for (Folder folder : folders) {
folderMap.put(CuidAdapter.folderId(folder.getId()), folder);
}
if (cmd.getOffset() > 0) {
query.setFirstResult(cmd.getOffset());
}
if (cmd.getLimit() > 0) {
query.setMaxResults(cmd.getLimit());
}
List<UserPermissionDTO> models = new ArrayList<>();
for (UserPermission perm : query.getResultList()) {
UserPermissionDTO dto = new UserPermissionDTO();
dto.setEmail(perm.getUser().getEmail());
dto.setName(perm.getUser().getName());
dto.setOrganization(perm.getUser().getOrganization());
dto.setJobtitle(perm.getUser().getJobtitle());
dto.setAllowDesign(perm.isAllowDesign());
dto.setAllowView(perm.isAllowView());
dto.setAllowViewAll(perm.isAllowViewAll());
dto.setAllowEdit(perm.isAllowEdit());
dto.setAllowEditAll(perm.isAllowEditAll());
dto.setAllowManageUsers(perm.isAllowManageUsers());
dto.setAllowManageAllUsers(perm.isAllowManageAllUsers());
dto.setPartner(new PartnerDTO(perm.getPartner().getId(), perm.getPartner().getName()));
dto.setFolderLimitation(!Strings.isNullOrEmpty(perm.getModel()));
dto.setFolders(folderList(folderMap, perm));
models.add(dto);
}
return new UserResult(models, cmd.getOffset(), queryTotalCount(cmd, currentUser, whereClause));
}
use of org.activityinfo.legacy.shared.command.result.UserResult in project activityinfo by bedatadriven.
the class UpdateUserPermissionsHandlerTest method testOwnerUpdate.
/**
* Verifies that the owner of a database can update an existing users permission
*/
@Test
@OnDataSet("/dbunit/schema1.db.xml")
public void testOwnerUpdate() throws CommandException {
setUser(1);
UserPermissionDTO user = new UserPermissionDTO();
user.setEmail("bavon@nrcdrc.org");
user.setPartner(new PartnerDTO(1, "NRC"));
user.setAllowView(true);
user.setAllowViewAll(false);
user.setAllowEdit(true);
user.setAllowEdit(false);
user.setAllowDesign(true);
execute(new UpdateUserPermissions(1, user));
UserResult result = execute(new GetUsers(1));
UserPermissionDTO reUser = result.getData().get(0);
assertThat(reUser.getEmail(), equalTo("bavon@nrcdrc.org"));
assertThat(reUser.getAllowDesign(), equalTo(true));
}
use of org.activityinfo.legacy.shared.command.result.UserResult in project activityinfo by bedatadriven.
the class UpdatePartnerHandlerTest method modifyShared.
@Test
public void modifyShared() {
SchemaDTO schema = execute(new GetSchema());
UserDatabaseDTO nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
UserDatabaseDTO healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
// The "Default" partner starts out being shared between the two database.
PartnerDTO nfiDefaultPartner = nfiDatabase.getDefaultPartner().get();
PartnerDTO healthDefaultPartner = healthDatabase.getDefaultPartner().get();
assertThat(nfiDefaultPartner.getId(), equalTo(healthDefaultPartner.getId()));
// If I update the name of "Default" partner for my database, a copy should
// be created.
PartnerDTO updatedPartner = new PartnerDTO(nfiDefaultPartner.getId(), "Solidarites");
updatedPartner.setFullName("Solidarites International");
execute(new UpdatePartner(1, updatedPartner));
// Now we should have two distinct partner objects
schema = execute(new GetSchema());
nfiDatabase = schema.getDatabaseById(NFI_DATABASE);
healthDatabase = schema.getDatabaseById(HEALTH_DATABASE);
assertThat(nfiDatabase.getPartners(), containsInAnyOrder(hasProperty("name", equalTo("Solidarites")), hasProperty("name", equalTo("NRC"))));
assertThat(healthDatabase.getPartners(), contains(hasProperty("name", equalTo("Default"))));
// Users in the NFI database should be reassigned to the new NRC partner object
UserResult nfiUsers = execute(new GetUsers(NFI_DATABASE));
Optional<UserPermissionDTO> bavon = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
Optional<UserPermissionDTO> lisa = nfiUsers.getData().stream().filter(u -> u.getEmail().equals("lisa@solidarites")).findAny();
// Bavon should stay as NRC
assertThat(bavon.get().getPartner(), hasProperty("name", equalTo("NRC")));
// Lisa should be moved to the new Solidarites partner
assertThat(lisa.get().getPartner(), hasProperty("name", equalTo("Solidarites")));
// Users in the Health database should be unaffected
UserResult healthUsers = execute(new GetUsers(HEALTH_DATABASE));
Optional<UserPermissionDTO> bavonInHealth = healthUsers.getData().stream().filter(u -> u.getEmail().equals("bavon@nrc.org")).findAny();
assertThat(bavonInHealth.get().getPartner(), hasProperty("name", equalTo("Default")));
// Sites in the NFI database should be update to point to the new partner
SiteDTO nfiSite1 = execute(GetSites.byId(1)).getData().get(0);
SiteDTO nfiSite3 = execute(GetSites.byId(3)).getData().get(0);
assertThat(nfiSite1.getPartner(), hasProperty("name", equalTo("Solidarites")));
assertThat(nfiSite3.getPartner(), hasProperty("name", equalTo("NRC")));
// Sites in the Health database should be unaffected
SiteDTO healthSite4 = execute(GetSites.byId(4)).getData().get(0);
assertThat(healthSite4.getPartner(), hasProperty("name", equalTo("Default")));
}
use of org.activityinfo.legacy.shared.command.result.UserResult in project activityinfo by bedatadriven.
the class GetUsersTest method testManageAllUsersPermission.
@Test
public void testManageAllUsersPermission() throws CommandException {
// Bavon from NRC(with manageAllUsers) permission
setUser(2);
// execute
UserResult result = execute(new GetUsers(1));
// VERIFY that we can get can see the two other users from NRC
assertThat(result.getTotalLength(), equalTo(2));
}
use of org.activityinfo.legacy.shared.command.result.UserResult in project activityinfo by bedatadriven.
the class ExportUsersServlet method doGet.
@Override
@Timed(name = "export", kind = "users")
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int dbId = Integer.valueOf(req.getParameter("dbUsers"));
UserResult userResult = dispatcher.execute(new GetUsers(dbId));
DbUserExport export = new DbUserExport(userResult.getData());
export.createSheet();
resp.setContentType("application/vnd.ms-excel");
if (req.getHeader("User-Agent").contains("MSIE")) {
resp.addHeader("Content-Disposition", "attachment; filename=ActivityInfo.xls");
} else {
resp.addHeader("Content-Disposition", "attachment; filename=" + ("ActivityInfo Export " + new Date().toString() + ".xls").replace(" ", "_"));
}
OutputStream os = resp.getOutputStream();
export.getBook().write(os);
}
Aggregations