use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.
the class SolutionImportHandler method importUsers.
/**
* Imports UserExport objects into the platform as users.
*
* @param users
* @return A map of role names to list of users in that role
*/
protected Map<String, List<String>> importUsers(List<UserExport> users) {
Map<String, List<String>> roleToUserMap = new HashMap<>();
IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class);
ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true);
if (users != null && roleDao != null) {
for (UserExport user : users) {
String password = user.getPassword();
log.debug("Importing user: " + user.getUsername());
// map the user to the roles he/she is in
for (String role : user.getRoles()) {
List<String> userList;
if (!roleToUserMap.containsKey(role)) {
userList = new ArrayList<>();
roleToUserMap.put(role, userList);
} else {
userList = roleToUserMap.get(role);
}
userList.add(user.getUsername());
}
String[] userRoles = user.getRoles().toArray(new String[] {});
try {
roleDao.createUser(tenant, user.getUsername(), password, null, userRoles);
} catch (AlreadyExistsException e) {
// it's ok if the user already exists, it is probably a default user
log.info(Messages.getInstance().getString("USER.Already.Exists", user.getUsername()));
try {
if (isOverwriteFile()) {
// set the roles, maybe they changed
roleDao.setUserRoles(tenant, user.getUsername(), userRoles);
// set the password just in case it changed
roleDao.setPassword(tenant, user.getUsername(), password);
}
} catch (Exception ex) {
// couldn't set the roles or password either
log.debug("Failed to set roles or password for existing user on import", ex);
}
} catch (Exception e) {
log.error(Messages.getInstance().getString("ERROR.CreatingUser", user.getUsername()));
}
importUserSettings(user);
}
}
return roleToUserMap;
}
use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.
the class UserRoleDaoService method createRole.
public void createRole(String roleName) throws Exception {
if (canAdminister()) {
if (strNotEmpty(roleName)) {
IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession());
roleDao.createRole(null, roleName, "", new String[0]);
} else {
throw new ValidationFailedException();
}
} else {
throw new SecurityException();
}
}
use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.
the class UserRoleDaoService method createUser.
public void createUser(User user) throws Exception {
if (canAdminister()) {
if (userValid(user)) {
IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession());
roleDao.createUser(null, decode(user.getUserName()), decode(user.getPassword()), "", new String[0]);
} else {
throw new ValidationFailedException();
}
} else {
throw new SecurityException();
}
}
use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.
the class UserRoleDaoService method updatePassword.
public void updatePassword(User user) throws SecurityException {
if (canAdminister()) {
String userName = decode(user.getUserName());
String password = decode(user.getPassword());
IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession());
IPentahoUser puser = roleDao.getUser(null, userName);
if (puser != null) {
roleDao.setPassword(null, userName, password);
} else {
throw new SecurityException("User not found");
}
} else {
throw new SecurityException("Logged-in user is not authorized to change password");
}
}
use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.
the class UserRoleDaoResource method removeAllRolesFromUser.
/**
* Remove all roles from the selected user
*
* @param tenantPath (tenant path where the user exist, null of empty string assumes default tenant)
* @param userName (username)
* @return
*/
@PUT
@Path("/removeAllRolesFromUser")
@Consumes({ MediaType.WILDCARD })
@Facet(name = "Unsupported")
public Response removeAllRolesFromUser(@QueryParam("tenant") String tenantPath, @QueryParam("userName") String userName) {
if (canAdminister()) {
try {
IUserRoleDao roleDao = getUserRoleDao();
roleDao.setUserRoles(getTenant(tenantPath), userName, new String[0]);
if (userName.equals(getSession().getName())) {
updateRolesForCurrentSession();
}
return Response.ok().build();
} catch (Throwable th) {
return processErrorResponse(th.getLocalizedMessage());
}
} else {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
Aggregations