Hello everyone,
Context: We have a requirement to create an users admin
role in openmrs, to sole purpose of this role is to create/update users without having access to any clinical data.
The Challenge with this is that fact that in openmrs-core
there are some validations within UserService#save
method which checks
- That the logged in user has all the privileges that it is trying to assign to the user being created/updated.
private void checkPrivileges(User user) {
Collection<Role> roles = user.getAllRoles();
List<String> requiredPrivs = new ArrayList<>();
for (Role r : roles) {
if (r.getRole().equals(RoleConstants.SUPERUSER)
&& !Context.hasPrivilege(PrivilegeConstants.ASSIGN_SYSTEM_DEVELOPER_ROLE)) {
throw new APIException("User.you.must.have.role", new Object[] { RoleConstants.SUPERUSER });
}
if (r.getPrivileges() != null) {
for (Privilege p : r.getPrivileges()) {
if (!Context.hasPrivilege(p.getPrivilege())) {
requiredPrivs.add(p.getPrivilege());
}
}
}
}
if (requiredPrivs.size() == 1) {
throw new APIException("User.you.must.have.privilege", new Object[] { requiredPrivs.get(0) });
} else if (requiredPrivs.size() > 1) {
StringBuilder txt = new StringBuilder("You must have the following privileges in order to assign them: ");
for (String s : requiredPrivs) {
txt.append(s).append(", ");
}
throw new APIException(txt.substring(0, txt.length() - 2));
}
}
Ref.: UserServiceImpl#checkPrivileges(User)
The Approach that we are thinking of in order to solve this
- create a special privilege (something like
assign roles
) - bypass the check based on
assign roles
privilege , which will need a code change in the method mentioned above.
Please let us know your thoughts on the approach and also if there is any other suggested way of achieving this.
Thanks