Search in sources :

Example 6 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class PentahoPlatformExporter method exportUsersAndRoles.

protected void exportUsersAndRoles() {
    log.debug("export users & roles");
    IUserRoleListService userRoleListService = PentahoSystem.get(IUserRoleListService.class);
    UserDetailsService userDetailsService = PentahoSystem.get(UserDetailsService.class);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class);
    ITenant tenant = TenantUtils.getCurrentTenant();
    // get the user settings for this user
    IUserSettingService service = getUserSettingService();
    // User Export
    List<String> userList = userRoleListService.getAllUsers(tenant);
    for (String user : userList) {
        UserExport userExport = new UserExport();
        userExport.setUsername(user);
        userExport.setPassword(userDetailsService.loadUserByUsername(user).getPassword());
        for (String role : userRoleListService.getRolesForUser(tenant, user)) {
            userExport.setRole(role);
        }
        if (service != null && service instanceof IAnyUserSettingService) {
            IAnyUserSettingService userSettings = (IAnyUserSettingService) service;
            List<IUserSetting> settings = userSettings.getUserSettings(user);
            if (settings != null) {
                for (IUserSetting setting : settings) {
                    userExport.addUserSetting(new ExportManifestUserSetting(setting));
                }
            }
        }
        this.getExportManifest().addUserExport(userExport);
    }
    // export the global user settings
    if (service != null) {
        List<IUserSetting> globalUserSettings = service.getGlobalUserSettings();
        if (globalUserSettings != null) {
            for (IUserSetting setting : globalUserSettings) {
                getExportManifest().addGlobalUserSetting(new ExportManifestUserSetting(setting));
            }
        }
    }
    // RoleExport
    List<String> roles = userRoleListService.getAllRoles();
    for (String role : roles) {
        RoleExport roleExport = new RoleExport();
        roleExport.setRolename(role);
        roleExport.setPermission(roleBindingDao.getRoleBindingStruct(null).bindingMap.get(role));
        exportManifest.addRoleExport(roleExport);
    }
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) ExportManifestUserSetting(org.pentaho.platform.plugin.services.importexport.ExportManifestUserSetting) IUserSetting(org.pentaho.platform.api.usersettings.pojo.IUserSetting) IAnyUserSettingService(org.pentaho.platform.api.usersettings.IAnyUserSettingService) ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) IUserSettingService(org.pentaho.platform.api.usersettings.IUserSettingService) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) IUserRoleListService(org.pentaho.platform.api.engine.IUserRoleListService) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport)

Example 7 with ITenant

use of org.pentaho.platform.api.mt.ITenant 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 8 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class AbstractJcrBackedUserRoleDaoTest method testIsMyself.

@Test(expected = RepositoryException.class)
public void testIsMyself() throws NotFoundException, RepositoryException {
    Session jcrSession = mock(Session.class);
    ITenant tenant = mock(ITenant.class);
    String[] newRoles = { "first_role" };
    setUserName("user-mock");
    roleDao.setUserRoles(jcrSession, tenant, "user-mock", newRoles);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Session(javax.jcr.Session) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) Test(org.junit.Test)

Example 9 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class MockUserRoleDao method getUsers.

public List<IPentahoUser> getUsers(ITenant tenant, boolean includeSubTenants) throws UncategorizedUserRoleDaoException {
    ArrayList<IPentahoUser> users = new ArrayList<IPentahoUser>();
    users.addAll(getUsers(tenant));
    if (includeSubTenants) {
        for (ITenant tmpTenant : tenants) {
            if (tmpTenant.getRootFolderAbsolutePath().startsWith(tenant.getRootFolderAbsolutePath() + "/", 0)) {
                users.addAll(getUsers(tmpTenant));
            }
        }
    }
    return users;
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ArrayList(java.util.ArrayList) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)

Example 10 with ITenant

use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.

the class MockUserRoleDao method getRoles.

public List<IPentahoRole> getRoles(ITenant tenant, boolean includeSubTenants) throws UncategorizedUserRoleDaoException {
    ArrayList<IPentahoRole> roles = new ArrayList<IPentahoRole>();
    roles.addAll(getRoles(tenant));
    if (includeSubTenants) {
        for (ITenant tmpTenant : tenants) {
            if (tmpTenant.getRootFolderAbsolutePath().startsWith(tenant.getRootFolderAbsolutePath() + "/", 0)) {
                roles.addAll(getRoles(tmpTenant));
            }
        }
    }
    return roles;
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) ArrayList(java.util.ArrayList) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)

Aggregations

ITenant (org.pentaho.platform.api.mt.ITenant)174 Test (org.junit.Test)120 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)86 Matchers.anyString (org.mockito.Matchers.anyString)47 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)27 ArrayList (java.util.ArrayList)21 Tenant (org.pentaho.platform.core.mt.Tenant)21 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)17 RepositoryFileSid (org.pentaho.platform.api.repository2.unified.RepositoryFileSid)15 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)14 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)12 WebResource (com.sun.jersey.api.client.WebResource)11 JerseyTest (com.sun.jersey.test.framework.JerseyTest)11 ITenantedPrincipleNameResolver (org.pentaho.platform.api.mt.ITenantedPrincipleNameResolver)10 SampleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.sample.SampleRepositoryFileData)10 Serializable (java.io.Serializable)9 Date (java.util.Date)9 HashMap (java.util.HashMap)9