Search in sources :

Example 1 with UserResult

use of org.activityinfo.shared.command.result.UserResult in project activityinfo by bedatadriven.

the class GetUsersHandler method execute.

@Override
public CommandResult execute(GetUsers cmd, User currentUser) throws CommandException {
    String orderByClause = "";
    if (cmd.getSortInfo().getSortDir() != Style.SortDir.NONE) {
        String dir = cmd.getSortInfo().getSortDir() == Style.SortDir.ASC ? "asc" : "desc";
        String property = null;
        String field = cmd.getSortInfo().getSortField();
        if ("name".equals(field)) {
            property = "up.user.name";
        } else if ("email".equals(field)) {
            property = "up.user.email";
        } else if ("partner".equals(field)) {
            property = "up.partner.name";
        } else if (field != null && field.startsWith("allow")) {
            property = "up." + field;
        }
        if (property != null) {
            orderByClause = " order by " + property + " " + dir;
        }
    }
    Query query = em.createQuery("select up from UserPermission up where " + "up.database.id = :dbId and " + "up.user.id <> :currentUserId " + orderByClause).setParameter("dbId", cmd.getDatabaseId()).setParameter("currentUserId", currentUser.getId());
    if (cmd.getOffset() > 0) {
        query.setFirstResult(cmd.getOffset());
    }
    if (cmd.getLimit() > 0) {
        query.setMaxResults(cmd.getLimit());
    }
    List<UserPermission> perms = query.getResultList();
    List<UserPermissionDTO> models = new ArrayList<UserPermissionDTO>();
    for (UserPermission perm : perms) {
        models.add(mapper.map(perm, UserPermissionDTO.class));
    }
    int totalCount = ((Number) em.createQuery("select count(up) from UserPermission up where " + "up.database.id = :dbId and " + "up.user.id <> :currentUserId ").setParameter("dbId", cmd.getDatabaseId()).setParameter("currentUserId", currentUser.getId()).getSingleResult()).intValue();
    return new UserResult(models, cmd.getOffset(), totalCount);
}
Also used : Query(javax.persistence.Query) ArrayList(java.util.ArrayList) UserResult(org.activityinfo.shared.command.result.UserResult) UserPermissionDTO(org.activityinfo.shared.dto.UserPermissionDTO) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 2 with UserResult

use of org.activityinfo.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
    Assert.assertEquals("number of results", 2, result.getTotalLength());
}
Also used : UserResult(org.activityinfo.shared.command.result.UserResult) GetUsers(org.activityinfo.shared.command.GetUsers) Test(org.junit.Test)

Example 3 with UserResult

use of org.activityinfo.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
 *
 * @throws CommandException
 */
@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);
    Assert.assertEquals("bavon@nrcdrc.org", reUser.getEmail());
    Assert.assertTrue("design rights", user.getAllowDesign());
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) UpdateUserPermissions(org.activityinfo.shared.command.UpdateUserPermissions) UserResult(org.activityinfo.shared.command.result.UserResult) GetUsers(org.activityinfo.shared.command.GetUsers) UserPermissionDTO(org.activityinfo.shared.dto.UserPermissionDTO) OnDataSet(org.activityinfo.server.database.OnDataSet) Test(org.junit.Test)

Example 4 with UserResult

use of org.activityinfo.shared.command.result.UserResult in project activityinfo by bedatadriven.

the class DbUserEditor method createGrid.

private void createGrid() {
    loader = new BasePagingLoader<UserResult>(new UserProxy());
    store = new ListStore<UserPermissionDTO>(loader);
    store.setKeyProvider(new ModelKeyProvider<UserPermissionDTO>() {

        @Override
        public String getKey(UserPermissionDTO model) {
            return model.getEmail();
        }
    });
    store.addListener(Store.Update, new Listener<StoreEvent<UserPermissionDTO>>() {

        @Override
        public void handleEvent(StoreEvent<UserPermissionDTO> event) {
            toolBar.setDirty(!store.getModifiedRecords().isEmpty());
        }
    });
    final List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
    columns.add(new ColumnConfig("name", I18N.CONSTANTS.name(), 100));
    columns.add(new ColumnConfig("email", I18N.CONSTANTS.email(), 150));
    columns.add(new ColumnConfig("partner", I18N.CONSTANTS.partner(), 150));
    PermissionCheckConfig allowView = new PermissionCheckConfig("allowViewSimple", I18N.CONSTANTS.allowView(), 75);
    allowView.setDataIndex("allowView");
    allowView.setToolTip(I18N.CONSTANTS.allowViewLong());
    columns.add(allowView);
    PermissionCheckConfig allowEdit = new PermissionCheckConfig("allowEditSimple", I18N.CONSTANTS.allowEdit(), 75);
    allowEdit.setDataIndex("allowEdit");
    allowEdit.setToolTip(I18N.CONSTANTS.allowEditLong());
    columns.add(allowEdit);
    PermissionCheckConfig allowViewAll = new PermissionCheckConfig("allowViewAll", I18N.CONSTANTS.allowViewAll(), 75);
    allowViewAll.setToolTip(I18N.CONSTANTS.allowViewAllLong());
    columns.add(allowViewAll);
    PermissionCheckConfig allowEditAll = new PermissionCheckConfig("allowEditAll", I18N.CONSTANTS.allowEditAll(), 75);
    allowEditAll.setToolTip(I18N.CONSTANTS.allowEditAllLong());
    columns.add(allowEditAll);
    PermissionCheckConfig allowManageUsers = null;
    allowManageUsers = new PermissionCheckConfig("allowManageUsers", I18N.CONSTANTS.allowManageUsers(), 150);
    columns.add(allowManageUsers);
    PermissionCheckConfig allowManageAllUsers = new PermissionCheckConfig("allowManageAllUsers", I18N.CONSTANTS.manageAllUsers(), 150);
    columns.add(allowManageAllUsers);
    // only users with the right to design them selves can change the design
    // attribute
    PermissionCheckConfig allowDesign = new PermissionCheckConfig("allowDesign", I18N.CONSTANTS.allowDesign(), 75);
    allowDesign.setToolTip(I18N.CONSTANTS.allowDesignLong());
    columns.add(allowDesign);
    grid = new Grid<UserPermissionDTO>(store, new ColumnModel(columns));
    grid.setLoadMask(true);
    grid.setSelectionModel(new GridSelectionModel<UserPermissionDTO>());
    grid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<UserPermissionDTO>() {

        @Override
        public void selectionChanged(SelectionChangedEvent<UserPermissionDTO> se) {
            onSelectionChanged(se.getSelectedItem());
        }
    });
    grid.addPlugin(allowEdit);
    grid.addPlugin(allowViewAll);
    grid.addPlugin(allowEditAll);
    grid.addPlugin(allowManageUsers);
    grid.addPlugin(allowManageAllUsers);
    grid.addPlugin(allowDesign);
    add(grid);
}
Also used : ColumnConfig(com.extjs.gxt.ui.client.widget.grid.ColumnConfig) CheckColumnConfig(com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig) ArrayList(java.util.ArrayList) UserPermissionDTO(org.activityinfo.shared.dto.UserPermissionDTO) StoreEvent(com.extjs.gxt.ui.client.store.StoreEvent) UserResult(org.activityinfo.shared.command.result.UserResult) ColumnModel(com.extjs.gxt.ui.client.widget.grid.ColumnModel)

Example 5 with UserResult

use of org.activityinfo.shared.command.result.UserResult in project activityinfo by bedatadriven.

the class ExportUsersServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int dbId = Integer.valueOf(req.getParameter("dbUsers"));
    try {
        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").indexOf("MSIE") != -1) {
            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);
    } catch (Exception e) {
        e.printStackTrace();
        resp.setStatus(500);
    }
}
Also used : OutputStream(java.io.OutputStream) UserResult(org.activityinfo.shared.command.result.UserResult) GetUsers(org.activityinfo.shared.command.GetUsers) Date(java.util.Date) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

UserResult (org.activityinfo.shared.command.result.UserResult)8 GetUsers (org.activityinfo.shared.command.GetUsers)6 Test (org.junit.Test)5 UserPermissionDTO (org.activityinfo.shared.dto.UserPermissionDTO)4 ArrayList (java.util.ArrayList)2 OnDataSet (org.activityinfo.server.database.OnDataSet)2 UpdateUserPermissions (org.activityinfo.shared.command.UpdateUserPermissions)2 PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)2 StoreEvent (com.extjs.gxt.ui.client.store.StoreEvent)1 CheckColumnConfig (com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig)1 ColumnConfig (com.extjs.gxt.ui.client.widget.grid.ColumnConfig)1 ColumnModel (com.extjs.gxt.ui.client.widget.grid.ColumnModel)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Date (java.util.Date)1 Query (javax.persistence.Query)1 ServletException (javax.servlet.ServletException)1 UserPermission (org.activityinfo.server.database.hibernate.entity.UserPermission)1