Search in sources :

Example 21 with IUserRoleDao

use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testChangeOwnPasswordSuccess.

@Test
public void testChangeOwnPasswordSuccess() throws Exception {
    setupMockSessionUser(SESSION_USER_NAME, false);
    IUserRoleDao roleDao = registerMockUserRoleDao();
    addMockUserToUserRoleDao(roleDao, SESSION_USER_NAME, SESSION_USER_PASSWORD);
    userRoleService.changeUserPassword(SESSION_USER_NAME, A_NEW_PASSWORD, SESSION_USER_PASSWORD);
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 22 with IUserRoleDao

use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.

the class SolutionImportHandler method importRoles.

protected void importRoles(List<RoleExport> roles, Map<String, List<String>> roleToUserMap) {
    IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class);
    ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class);
    Set<String> existingRoles = new HashSet<>();
    if (roles != null) {
        for (RoleExport role : roles) {
            log.debug("Importing role: " + role.getRolename());
            try {
                List<String> users = roleToUserMap.get(role.getRolename());
                String[] userarray = users == null ? new String[] {} : users.toArray(new String[] {});
                IPentahoRole role1 = roleDao.createRole(tenant, role.getRolename(), null, userarray);
            } catch (AlreadyExistsException e) {
                existingRoles.add(role.getRolename());
                // it's ok if the role already exists, it is probably a default role
                log.info(Messages.getInstance().getString("ROLE.Already.Exists", role.getRolename()));
            }
            try {
                if (existingRoles.contains(role.getRolename())) {
                    // Only update an existing role if the overwrite flag is set
                    if (isOverwriteFile()) {
                        roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                    }
                } else {
                    // Always write a roles permissions that were not previously existing
                    roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                }
            } catch (Exception e) {
                log.info(Messages.getInstance().getString("ERROR.SettingRolePermissions", role.getRolename()), e);
            }
        }
    }
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) 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) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) HashSet(java.util.HashSet)

Example 23 with IUserRoleDao

use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.

the class UserRoleDaoService method changeUserPassword.

public void changeUserPassword(final String userName, final String newPass, String oldPass) throws Exception {
    if (inputValid(userName, newPass, oldPass)) {
        final IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
        // You must be either an admin or trying to change your own password
        if (canAdminister() || (null != pentahoSession && userName.equals(pentahoSession.getName()))) {
            final IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", pentahoSession);
            IPentahoUser pentahoUser = roleDao.getUser(null, userName);
            if (credentialValid(pentahoUser, oldPass)) {
                SecurityHelper.getInstance().runAsSystem(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        roleDao.setPassword(null, userName, newPass);
                        return null;
                    }
                });
            } else {
                throw new SecurityException();
            }
        } else {
            throw new SecurityException();
        }
    } else {
        throw new ValidationFailedException();
    }
}
Also used : IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) AuthenticationException(org.springframework.security.core.AuthenticationException) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 24 with IUserRoleDao

use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.

the class UserRoleDaoResource method assignAllUsersToRole.

/**
 * Associates all user to a particular role
 *
 * @param tenantPath (tenant path where the user exist, null of empty string assumes default tenant)
 * @param roleName   (role name)
 * @return
 */
@PUT
@Path("/assignAllUsersToRole")
@Consumes({ MediaType.WILDCARD })
@Facet(name = "Unsupported")
public Response assignAllUsersToRole(@QueryParam("tenant") String tenantPath, @QueryParam("roleName") String roleName) {
    IUserRoleDao roleDao = getUserRoleDao();
    Set<String> assignedUserNames = new HashSet<String>();
    for (IPentahoUser pentahoUser : roleDao.getUsers(getTenant(tenantPath))) {
        assignedUserNames.add(pentahoUser.getUsername());
    }
    roleDao.setRoleMembers(getTenant(tenantPath), roleName, assignedUserNames.toArray(new String[0]));
    if (assignedUserNames.contains(getSession().getName())) {
        updateRolesForCurrentSession();
    }
    return Response.ok().build();
}
Also used : IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Facet(org.codehaus.enunciate.Facet)

Example 25 with IUserRoleDao

use of org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao in project pentaho-platform by pentaho.

the class UserRoleDaoServiceTest method testDeleteRole.

@Test
public void testDeleteRole() {
    String roles = "role1\trole2\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    IPentahoRole role = mock(IPentahoRole.class);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getRole(any(ITenant.class), anyString())).thenReturn(role);
    PentahoSystem.registerObject(roleDao);
    userRoleService.deleteRoles(roles);
    verify(roleDao, times(2)).deleteRole(any(IPentahoRole.class));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Matchers.anyString(org.mockito.Matchers.anyString) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

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