use of org.pentaho.platform.api.mt.ITenant in project pentaho-engineering-samples by pentaho.
the class PentahoSamlAuthenticationSuccessHandler method createUserHomeFolder.
private void createUserHomeFolder(final String username) {
final ITenant tenantName = JcrTenantUtils.getTenant(username, true);
final ISecurityHelper securityHelper = PentahoSystem.get(ISecurityHelper.class) != null ? PentahoSystem.get(ISecurityHelper.class) : SecurityHelper.getInstance();
final IBackingRepositoryLifecycleManager lifecycleManager = PentahoSystem.get(IBackingRepositoryLifecycleManager.class);
if (tenantName == null || securityHelper == null || lifecycleManager == null) {
logger.error("null " + (tenantName == null ? "ITenant" : securityHelper == null ? "ISecurityHelper" : "IBackingRepositoryLifecycleManager"));
return;
}
try {
securityHelper.runAsSystem(new Callable<Void>() {
@Override
public Void call() throws Exception {
// Execute new tenant with the tenant id from the logged in user
lifecycleManager.newTenant(tenantName);
return null;
}
});
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
try {
securityHelper.runAsSystem(new Callable<Void>() {
@Override
public Void call() throws Exception {
// Execute new tenant with the tenant id from the logged in user
lifecycleManager.newUser(tenantName, username);
return null;
}
});
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
try {
// The newTenant() call should be executed as the system (or more correctly the tenantAdmin)
securityHelper.runAsSystem(new Callable<Void>() {
@Override
public Void call() throws Exception {
lifecycleManager.newTenant();
return null;
}
});
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
try {
// run as user to populate SecurityContextHolder and PentahoSessionHolder since Spring Security events are
// fired before SecurityContextHolder is set
securityHelper.runAsUser(username, new Callable<Void>() {
@Override
public Void call() throws Exception {
lifecycleManager.newUser();
return null;
}
});
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-kettle by pentaho.
the class PurRepositoryIT method setUp.
@Before
public void setUp() throws Exception {
this.testContextManager = new TestContextManager(getClass());
this.testContextManager.prepareTestInstance(this);
IRepositoryVersionManager mockRepositoryVersionManager = mock(IRepositoryVersionManager.class);
when(mockRepositoryVersionManager.isVersioningEnabled(anyString())).thenReturn(true);
when(mockRepositoryVersionManager.isVersionCommentEnabled(anyString())).thenReturn(false);
JcrRepositoryFileUtils.setRepositoryVersionManager(mockRepositoryVersionManager);
loginAsRepositoryAdmin();
SimpleJcrTestUtils.deleteItem(testJcrTemplate, ServerRepositoryPaths.getPentahoRootFolderPath());
mp = new MicroPlatform();
// used by DefaultPentahoJackrabbitAccessControlHelper
mp.defineInstance("tenantedUserNameUtils", userNameUtils);
mp.defineInstance("tenantedRoleNameUtils", roleNameUtils);
mp.defineInstance(IAuthorizationPolicy.class, authorizationPolicy);
mp.defineInstance(ITenantManager.class, tenantManager);
mp.defineInstance("roleAuthorizationPolicyRoleBindingDaoTarget", roleBindingDaoTarget);
mp.defineInstance("repositoryAdminUsername", repositoryAdminUsername);
mp.defineInstance("RepositoryFileProxyFactory", new RepositoryFileProxyFactory(testJcrTemplate, repositoryFileDao));
mp.defineInstance("useMultiByteEncoding", new Boolean(false));
// Start the micro-platform
mp.start();
loginAsRepositoryAdmin();
setAclManagement();
systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), singleTenantAdminRoleName, tenantAuthenticatedRoleName, "Anonymous");
userRoleDao.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { singleTenantAdminRoleName });
logout();
super.setUp();
KettleEnvironment.init();
// programmatically register plugins, annotation based plugins do not get loaded unless
// they are in kettle's plugins folder.
JobEntryPluginType.getInstance().registerCustom(JobEntryAttributeTesterJobEntry.class, "test", "JobEntryAttributeTester", "JobEntryAttributeTester", "JobEntryAttributeTester", "");
StepPluginType.getInstance().registerCustom(TransStepAttributeTesterTransStep.class, "test", "StepAttributeTester", "StepAttributeTester", "StepAttributeTester", "");
repositoryMeta = new PurRepositoryMeta();
repositoryMeta.setName("JackRabbit");
repositoryMeta.setDescription("JackRabbit test repository");
userInfo = new UserInfo(EXP_LOGIN, "password", EXP_USERNAME, "Apache Tomcat user", true);
repository = new PurRepository();
repository.init(repositoryMeta);
login(sysAdminUserName, systemTenant, new String[] { singleTenantAdminRoleName, tenantAuthenticatedRoleName });
ITenant tenantAcme = tenantManager.createTenant(systemTenant, EXP_TENANT, singleTenantAdminRoleName, tenantAuthenticatedRoleName, "Anonymous");
userRoleDao.createUser(tenantAcme, EXP_LOGIN, "password", "", new String[] { singleTenantAdminRoleName });
logout();
setUpUser();
PurRepository purRep = (PurRepository) repository;
purRep.setPurRepositoryConnector(new PurRepositoryConnector(purRep, (PurRepositoryMeta) repositoryMeta, purRep.getRootRef()));
((PurRepository) repository).setTest(repo);
repository.connect(EXP_LOGIN, "password");
login(EXP_LOGIN, tenantAcme, new String[] { singleTenantAdminRoleName, tenantAuthenticatedRoleName });
System.out.println("PUR NAME!!!: " + repo.getClass().getCanonicalName());
RepositoryFile repositoryFile = repo.getFile(ClientRepositoryPaths.getPublicFolderPath());
Serializable repositoryFileId = repositoryFile.getId();
List<RepositoryFile> files = repo.getChildren(repositoryFileId);
StringBuilder buf = new StringBuilder();
for (RepositoryFile file : files) {
buf.append("\n").append(file);
}
assertTrue("files not deleted: " + buf, files.isEmpty());
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-kettle by pentaho.
the class PurRepositoryIT method getTenant.
protected ITenant getTenant(String principalId, boolean isUser) {
ITenant tenant = null;
ITenantedPrincipleNameResolver nameUtils = isUser ? userNameUtils : roleNameUtils;
if (nameUtils != null) {
tenant = nameUtils.getTenant(principalId);
}
if (tenant == null || tenant.getId() == null) {
tenant = getCurrentTenant();
}
return tenant;
}
use of org.pentaho.platform.api.mt.ITenant 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);
}
}
}
}
use of org.pentaho.platform.api.mt.ITenant in project pentaho-platform by pentaho.
the class UserRoleDaoService method assignRolesToUser.
public void assignRolesToUser(String userName, String roleNames) throws NotFoundException, UncategorizedUserRoleDaoException, SecurityException {
if (canAdminister()) {
StringTokenizer tokenizer = new StringTokenizer(roleNames, "\t");
Set<String> assignedRoles = new HashSet<>();
ITenant tenant = TenantUtils.getCurrentTenant();
// Build the set of roles the user already contians
for (IPentahoRole pentahoRole : getRoleDao().getUserRoles(tenant, userName)) {
assignedRoles.add(pentahoRole.getName());
}
// Append the parameter of roles
while (tokenizer.hasMoreTokens()) {
assignedRoles.add(tokenizer.nextToken());
}
getRoleDao().setUserRoles(tenant, userName, assignedRoles.toArray(new String[assignedRoles.size()]));
} else {
throw new SecurityException();
}
}
Aggregations