@Override public void handleGet(ActionParameters params) throws ActionException { log.info("handleGet"); final JSONObject response; long id = getId(params); try { if (id > -1) { log.info("handleGet: has id"); User user = userService.getUser(id); response = user2Json(user); } else { log.info("handleGet: no id"); List<User> users = userService.getUsers(); log.info("found: " + users.size() + "users"); response = new JSONObject(); JSONArray arr = new JSONArray(); response.put("users", arr); List<User> newUsers = userService.getUsersWithRoles(); for (User user : newUsers) { arr.put(user2Json(user)); } } } catch (ServiceException se) { throw new ActionException(se.getMessage(), se); } catch (JSONException je) { throw new ActionException(je.getMessage(), je); } log.info(response); ResponseHelper.writeResponse(params, response); }
@Override public void init() { try { userService = UserService.getInstance(); } catch (ServiceException se) { log.error(se, "Unable to initialize User service!"); } }
@Override public void handlePut(ActionParameters params) throws ActionException { log.debug("handlePut"); User user = new User(); getUserParams(user, params); String password = params.getRequiredParam(PARAM_PASSWORD); String[] roles = params.getRequest().getParameterValues("roles"); User retUser = null; try { retUser = userService.createUser(user, roles); userService.setUserPassword(retUser.getScreenname(), password); } catch (ServiceException se) { throw new ActionException(se.getMessage(), se); } JSONObject response = null; try { response = user2Json(retUser); } catch (JSONException je) { throw new ActionException(je.getMessage(), je); } ResponseHelper.writeResponse(params, response); }
@Override public void handleDelete(ActionParameters params) throws ActionException { log.debug("handleDelete"); long id = getId(params); if (id > -1) { try { userService.deleteUser(id); } catch (ServiceException se) { throw new ActionException(se.getMessage(), se); } } else { throw new ActionException("Parameter 'id' not found."); } }
@Override public void handlePost(ActionParameters params) throws ActionException { log.debug("handlePost"); User user = new User(); getUserParams(user, params); String[] roles = params.getRequest().getParameterValues("roles"); String password = params.getHttpParam(PARAM_PASSWORD); User retUser = null; try { if (user.getId() > -1) { // retUser = userService.modifyUser(user); log.debug("roles size: " + roles.length); retUser = userService.modifyUserwithRoles(user, roles); log.debug("done modifying user"); if (password != null && !"".equals(password.trim())) { userService.updateUserPassword(retUser.getScreenname(), password); } } else { log.debug("NOW IN POST and creating a new user!!!!!!!!!!!!!"); if (password == null || password.trim().isEmpty()) { throw new ActionException("Parameter 'password' not found."); } retUser = userService.createUser(user); userService.setUserPassword(retUser.getScreenname(), password); } } catch (ServiceException se) { throw new ActionException(se.getMessage(), se); } JSONObject response = null; try { response = user2Json(retUser); } catch (JSONException je) { throw new ActionException(je.getMessage(), je); } ResponseHelper.writeResponse(params, response); }
public static Role getAdminRole() { if (ADMIN_ROLE == null) { // default to Administrator final String rolename = PropertyUtil.get("oskari.user.role.admin", DEFAULT_ADMIN_ROLE_NAME); try { final Role[] roles = UserService.getInstance().getRoles(); for (Role role : roles) { if (rolename.equals(role.getName())) { ADMIN_ROLE = role; break; } } } catch (ServiceException ex) { log.error(ex, "Error getting roles from user service"); } } return ADMIN_ROLE; }
private Resource getSourcePermission(final String layerId, final User user) { if (layerId == null) { return null; } if (layerId.startsWith(AnalysisParser.ANALYSIS_LAYER_PREFIX)) { final Resource resource = new Resource(); resource.setType(AnalysisLayer.TYPE); resource.setMapping( "analysis", Long.toString(AnalysisHelper.getAnalysisIdFromLayerId(layerId))); return permissionsService.findResource(resource); } else if (layerId.startsWith(AnalysisParser.MYPLACES_LAYER_PREFIX) || layerId.equals("-1") || layerId.startsWith(AnalysisParser.USERLAYER_PREFIX)) { final Resource resource = new Resource(); // permission to publish for self final Permission permPublish = new Permission(); permPublish.setExternalType(Permissions.EXTERNAL_TYPE_USER); permPublish.setExternalId("" + user.getId()); permPublish.setType(Permissions.PERMISSION_TYPE_PUBLISH); resource.addPermission(permPublish); try { // add VIEW_PUBLISHED for all roles currently in the system for (Role role : UserService.getInstance().getRoles()) { final Permission perm = new Permission(); perm.setExternalType(Permissions.EXTERNAL_TYPE_ROLE); perm.setExternalId("" + role.getId()); perm.setType(Permissions.PERMISSION_TYPE_VIEW_PUBLISHED); resource.addPermission(perm); } } catch (Exception e) { log.error( "Something went wrong when generating source permissions for myplaces layer or temporary or user data layer"); } return resource; } // default to usual layer final OskariLayer layer = mapLayerService.find(layerId); // copy permissions from source layer to new analysis return permissionsService.getResource( Permissions.RESOURCE_TYPE_MAP_LAYER, new OskariLayerResource(layer).getMapping()); }