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);
}
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());
}
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());
}
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);
}
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);
}
}
Aggregations