Search in sources :

Example 1 with IUserRoleDao

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;
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) HashMap(java.util.HashMap) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with IUserRoleDao

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();
    }
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)

Example 3 with IUserRoleDao

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();
    }
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)

Example 4 with IUserRoleDao

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");
    }
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)

Example 5 with IUserRoleDao

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();
    }
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Facet(org.codehaus.enunciate.Facet)

Aggregations

IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)43 Test (org.junit.Test)28 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)21 ITenant (org.pentaho.platform.api.mt.ITenant)20 Matchers.anyString (org.mockito.Matchers.anyString)13 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)11 ArrayList (java.util.ArrayList)9 User (org.pentaho.platform.web.http.api.resources.User)9 IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)8 HashSet (java.util.HashSet)6 Consumes (javax.ws.rs.Consumes)6 PUT (javax.ws.rs.PUT)6 Path (javax.ws.rs.Path)6 Facet (org.codehaus.enunciate.Facet)6 UncategorizedUserRoleDaoException (org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException)5 AuthenticationProvider (org.springframework.security.authentication.AuthenticationProvider)5 NotFoundException (org.pentaho.platform.api.engine.security.userroledao.NotFoundException)3 Tenant (org.pentaho.platform.core.mt.Tenant)3 UserListWrapper (org.pentaho.platform.web.http.api.resources.UserListWrapper)3 IOException (java.io.IOException)2