Search in sources :

Example 1 with UserResult

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));
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Folder(org.activityinfo.server.database.hibernate.entity.Folder) UserPermissionDTO(org.activityinfo.legacy.shared.model.UserPermissionDTO) ResourceId(org.activityinfo.model.resource.ResourceId) Database(org.activityinfo.server.database.hibernate.entity.Database) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 2 with UserResult

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));
}
Also used : PartnerDTO(org.activityinfo.legacy.shared.model.PartnerDTO) UpdateUserPermissions(org.activityinfo.legacy.shared.command.UpdateUserPermissions) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) UserPermissionDTO(org.activityinfo.legacy.shared.model.UserPermissionDTO) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 3 with UserResult

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")));
}
Also used : CommandTestCase2(org.activityinfo.server.command.CommandTestCase2) RunWith(org.junit.runner.RunWith) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) org.activityinfo.legacy.shared.model(org.activityinfo.legacy.shared.model) Matchers(org.hamcrest.Matchers) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) Assert.assertThat(org.junit.Assert.assertThat) GetSites(org.activityinfo.legacy.shared.command.GetSites) InjectionSupport(org.activityinfo.fixtures.InjectionSupport) Optional(java.util.Optional) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) UpdatePartner(org.activityinfo.legacy.shared.command.UpdatePartner) Test(org.junit.Test)

Example 4 with UserResult

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));
}
Also used : UserResult(org.activityinfo.legacy.shared.command.result.UserResult) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) Test(org.junit.Test)

Example 5 with UserResult

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);
}
Also used : OutputStream(java.io.OutputStream) UserResult(org.activityinfo.legacy.shared.command.result.UserResult) GetUsers(org.activityinfo.legacy.shared.command.GetUsers) Date(java.util.Date) Timed(org.activityinfo.server.util.monitoring.Timed)

Aggregations

UserResult (org.activityinfo.legacy.shared.command.result.UserResult)9 GetUsers (org.activityinfo.legacy.shared.command.GetUsers)8 Test (org.junit.Test)7 UserPermissionDTO (org.activityinfo.legacy.shared.model.UserPermissionDTO)5 PartnerDTO (org.activityinfo.legacy.shared.model.PartnerDTO)4 OnDataSet (org.activityinfo.server.database.OnDataSet)4 UpdateUserPermissions (org.activityinfo.legacy.shared.command.UpdateUserPermissions)3 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 InjectionSupport (org.activityinfo.fixtures.InjectionSupport)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 GetSites (org.activityinfo.legacy.shared.command.GetSites)1 UpdatePartner (org.activityinfo.legacy.shared.command.UpdatePartner)1 org.activityinfo.legacy.shared.model (org.activityinfo.legacy.shared.model)1 FolderDTO (org.activityinfo.legacy.shared.model.FolderDTO)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 CommandTestCase2 (org.activityinfo.server.command.CommandTestCase2)1