Skip to content

Commit

Permalink
Merge pull request #944 from ZakarFin/users
Browse files Browse the repository at this point in the history
Improve user management services to enable UI changes
  • Loading branch information
ZakarFin authored Mar 31, 2023
2 parents ea0b5d3 + 7b81c14 commit 59f55b9
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fi.nls.oskari.control.admin;

import fi.nls.oskari.annotation.OskariViewModifier;
import fi.nls.oskari.control.view.modifier.bundle.BundleHandler;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.oskari.util.PropertyUtil;
import fi.nls.oskari.view.modifier.ModifierException;
import fi.nls.oskari.view.modifier.ModifierParams;
import org.json.JSONObject;
import org.oskari.user.util.UserHelper;


/**
* Injects password requirements to admin-users config
{
"requirements": {
"length": 8,
"case": true
},
"isExternal": false
}
*/
@OskariViewModifier("admin-users")
public class UsersBundleHandler extends BundleHandler {

public boolean modifyBundle(final ModifierParams params) throws ModifierException {
final JSONObject config = getBundleConfig(params.getConfig());
JSONHelper.putValue(config, "requirements", UserHelper.getPasswordRequirements());
JSONHelper.putValue(config, "isExternal", isUsersFromExternalSource());
return false;
}

/**
* If users are managed in external source any changes to them are usually overwritten when they login.
* So we can disable the fields that are and updates that can happen to users to make the admin UI more user-friendly.
* @return
*/
public static boolean isUsersFromExternalSource() {
return PropertyUtil.getOptional("oskari.user.external", false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void init() {
public void handleGet(ActionParameters params) throws ActionException {
final JSONObject response;
long id = getId(params);
long limit = params.getHttpParam(PARAM_LIMIT, 0);
long offset = params.getHttpParam(PARAM_OFFSET, 0);
int limit = params.getHttpParam(PARAM_LIMIT, 0);
int offset = params.getHttpParam(PARAM_OFFSET, 0);
String search = params.getHttpParam(PARAM_SEARCH);
try {
if (id > -1) {
Expand Down Expand Up @@ -81,67 +81,72 @@ public void handleGet(ActionParameters params) throws ActionException {

@Override
public void handlePost(ActionParameters params) throws ActionException {
User user = new User();
getUserParams(user, params);
// modify existing user
boolean extUsers = UsersBundleHandler.isUsersFromExternalSource();
long userId = params.getRequiredParamLong(PARAM_ID);
User user;
String password = null;
if (extUsers) {
user = findExistingUser(userId);
} else {
user = getUserFromParams(params);
password = params.getRequiredParam(PARAM_PASSWORD);
if (!UserHelper.isPasswordOk(password)) {
throw new ActionParamsException("Password too weak");
}
}
String[] roles = params.getRequest().getParameterValues("roles");
String password = params.getHttpParam(PARAM_PASSWORD);
User retUser = null;

AuditLog audit = AuditLog.user(params.getClientIp(), params.getUser())
.withParam("email", user.getEmail());

User retUser;
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 && !password.trim().isEmpty()) {
if (!UserHelper.isPasswordOk(password)) {
throw new ActionParamsException("Password too weak");
} else {
userService.updateUserPassword(retUser.getScreenname(), password);
}
}
audit.updated(AuditLog.ResourceType.USER);
} else {
LOG.debug("NOW IN POST and creating a new user!!!!!!!!!!!!!");
if (password == null || password.trim().isEmpty()) {
throw new ActionException("Parameter 'password' not found.");
}
if (!UserHelper.isPasswordOk(password)) {
throw new ActionParamsException("Password too weak");
}
retUser = userService.createUser(user);
userService.setUserPassword(retUser.getScreenname(), password);
audit.added(AuditLog.ResourceType.USER);
retUser = userService.modifyUserwithRoles(user, roles);
LOG.debug("done modifying user");
if (!extUsers) {
userService.updateUserPassword(retUser.getScreenname(), password);
}

} catch (ServiceException se) {
throw new ActionException(se.getMessage(), se);
}
JSONObject response = null;

AuditLog.user(params.getClientIp(), params.getUser())
.withParam("email", user.getEmail())
.updated(AuditLog.ResourceType.USER);
try {
response = user2Json(retUser);
ResponseHelper.writeResponse(params, user2Json(retUser));
} catch (JSONException je) {
throw new ActionException(je.getMessage(), je);
}
ResponseHelper.writeResponse(params, response);
}

private User findExistingUser(long id) throws ActionParamsException {
try {
return userService.getUser(id);
} catch (ServiceException e) {
throw new ActionParamsException("Error loading user with id: " + id);
}
}
private User getUserFromParams(ActionParameters params) throws ActionParamsException {
User user = new User();
getUserParams(user, params);
return user;
}

@Override
public void handlePut(ActionParameters params) throws ActionException {
LOG.debug("handlePut");
if (UsersBundleHandler.isUsersFromExternalSource()) {
throw new ActionParamsException("Users from external source, adding is disabled");
}
User user = new User();
getUserParams(user, params);
String password = params.getRequiredParam(PARAM_PASSWORD);
String[] roles = params.getRequest().getParameterValues("roles");
User retUser = null;

if (!UserHelper.isPasswordOk(password)) {
throw new ActionParamsException("Password too weak");
}

User retUser = null;
try {
retUser = userService.createUser(user, roles);
userService.setUserPassword(retUser.getScreenname(), password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,17 @@ public List<User> getUsersWithRoles() throws ServiceException {
* @return
* @throws ServiceException
*/
public Long getUserCount() throws ServiceException {
return 0L;
public int getUserCount() throws ServiceException {
return -1;
}

/**
* Return count of all users when using search
* @return
* @throws ServiceException
*/
public Long getUserSearchCount(String search) throws ServiceException {
return 0L;
public int getUserSearchCount(String search) throws ServiceException {
return -1;
}

/**
Expand All @@ -231,7 +231,7 @@ public Long getUserSearchCount(String search) throws ServiceException {
* @return List<User> users
* @throws ServiceException
*/
public List<User> getUsersWithRoles(long limit, long offset, String search) throws ServiceException {
public List<User> getUsersWithRoles(int limit, int offset, String query) throws ServiceException {
return Collections.emptyList();
}

Expand Down
Loading

0 comments on commit 59f55b9

Please sign in to comment.