use of org.pentaho.platform.core.mt.Tenant 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.core.mt.Tenant in project pentaho-platform by pentaho.
the class DefaultDeleteHelperTest method testPermanentlyDeleteFile.
@Test
public void testPermanentlyDeleteFile() throws Exception {
String fileID = "testFileID";
final Node nodeDeletedFile = mock(Node.class);
final Node nodeTrash = mock(Node.class);
when(nodeTrash.hasNode(anyString())).thenReturn(true);
when(nodeTrash.getNode(anyString())).thenReturn(nodeDeletedFile);
final Node nodeUserFolder = mock(Node.class);
when(nodeUserFolder.hasNode(anyString())).thenReturn(true);
when(nodeUserFolder.getNode(anyString())).thenReturn(nodeTrash);
final Node nodeDeletedParent = mock(Node.class);
when(nodeDeletedParent.getPath()).thenReturn("parentPath");
final PropertyIterator referencesPropertyIterator = mock(PropertyIterator.class);
when(referencesPropertyIterator.hasNext()).thenReturn(false);
final Node nodeToRemove = mock(Node.class);
when(nodeToRemove.getPath()).thenReturn("nodePath");
when(nodeToRemove.getParent()).thenReturn(nodeDeletedParent);
when(nodeToRemove.getReferences()).thenReturn(referencesPropertyIterator);
when(nodeToRemove.isLocked()).thenReturn(false);
when(session.getNodeByIdentifier(fileID)).thenReturn(nodeToRemove);
when(session.getItem(anyString())).thenReturn(nodeUserFolder);
Workspace workspace = mock(Workspace.class);
VersionManager versionManager = mock(VersionManager.class);
when(workspace.getVersionManager()).thenReturn(versionManager);
when(session.getWorkspace()).thenReturn(workspace);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(nodeToRemove).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// test locked file
when(nodeToRemove.isLocked()).thenReturn(true);
final Lock lock = mock(Lock.class);
final LockManager lockManager = mock(LockManager.class);
when(lockManager.getLock(eq(nodeToRemove.getPath()))).thenReturn(lock);
when(workspace.getLockManager()).thenReturn(lockManager);
when(session.getWorkspace()).thenReturn(workspace);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(lockHelper).removeLockToken(eq(session), eq(pentahoJcrConstants), eq(lock));
verify(nodeToRemove, times(2)).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// if remove from trash folder, folder containing the file need to be removed
final String trashPath = ServerRepositoryPaths.getUserHomeFolderPath(new Tenant(null, true), PentahoSessionHolder.getSession().getName()) + RepositoryFile.SEPARATOR + ".trash";
when(nodeToRemove.getPath()).thenReturn(trashPath);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
verify(nodeToRemove, times(3)).remove();
verify(nodeDeletedParent).remove();
} catch (Exception e) {
e.printStackTrace();
fail();
}
// test if there are references to this file
final Node referenceNode = mock(Node.class);
when(referenceNode.isNodeType(eq(pentahoJcrConstants.getPHO_NT_PENTAHOHIERARCHYNODE()))).thenReturn(true);
when(referenceNode.isNodeType(eq(pentahoJcrConstants.getPHO_NT_PENTAHOFOLDER()))).thenReturn(false);
final Property property = mock(Property.class);
when(property.getParent()).thenReturn(referenceNode);
when(referencesPropertyIterator.hasNext()).thenReturn(true, true, false);
when(referencesPropertyIterator.nextProperty()).thenReturn(property);
try {
defaultDeleteHelper.permanentlyDeleteFile(session, pentahoJcrConstants, fileID);
} catch (RepositoryFileDaoReferentialIntegrityException e1) {
// it's ok
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.pentaho.platform.core.mt.Tenant in project pentaho-platform by pentaho.
the class SpringSecurityPrincipalProvider_Caching_Test method setUp.
@Before
public void setUp() throws Exception {
userResolver = mock(ITenantedPrincipleNameResolver.class);
roleResolver = mock(ITenantedPrincipleNameResolver.class);
cacheManager = spy(new SimpleMapCacheManager());
doReturn(false).when(cacheManager).cacheEnabled(USER_CACHE_REGION);
doReturn(false).when(cacheManager).cacheEnabled(ROLE_CACHE_REGION);
userTenant = new Tenant(USER_PRINCIPLE, true);
roleTenant = new Tenant(ROLE_PRINCIPLE, true);
mp = new MicroPlatform(getSolutionPath());
mp.defineInstance("tenantedUserNameUtils", userResolver);
mp.defineInstance("tenantedRoleNameUtils", roleResolver);
mp.defineInstance(ICacheManager.class, cacheManager);
mp.start();
provider = new SpringSecurityPrincipalProvider();
provider.init(new Properties());
provider = spy(provider);
User user = new User(USER_PRINCIPLE, "", true, true, true, true, new ArrayList<GrantedAuthority>());
doReturn(user).when(provider).internalGetUserDetails(USER_PRINCIPLE);
}
use of org.pentaho.platform.core.mt.Tenant in project pentaho-platform by pentaho.
the class TenantUtils method getCurrentTenant.
/**
* Returns the tenant ID of the current user.
*/
public static ITenant getCurrentTenant() {
IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
if (pentahoSession == null) {
throw new IllegalStateException();
}
String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
if (tenantId == null) {
ITenantedPrincipleNameResolver tenantedUserNameUtils = PentahoSystem.get(ITenantedPrincipleNameResolver.class, "tenantedUserNameUtils", pentahoSession);
if (tenantedUserNameUtils != null) {
ITenant tenant = tenantedUserNameUtils.getTenant(pentahoSession.getId());
pentahoSession.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
return new Tenant(tenant.getId(), true);
}
}
return new Tenant(tenantId, true);
}
use of org.pentaho.platform.core.mt.Tenant in project pentaho-platform by pentaho.
the class DefaultDeleteHelper method getOrCreateTrashInternalFolderNode.
/**
* Creates and/or returns an internal folder called {@code .trash} located just below the user's home folder.
*/
private Node getOrCreateTrashInternalFolderNode(final Session session, final PentahoJcrConstants pentahoJcrConstants) throws RepositoryException {
IPentahoSession pentahoSession = PentahoSessionHolder.getSession();
String tenantId = (String) pentahoSession.getAttribute(IPentahoSession.TENANT_ID_KEY);
Tenant tenant = new Tenant(tenantId, true);
String userName = pentahoSession.getName();
Node userHomeFolderNode = (Node) session.getItem(ServerRepositoryPaths.getUserHomeFolderPath(tenant, JcrStringHelper.fileNameEncode(userName)));
if (userHomeFolderNode.hasNode(FOLDER_NAME_TRASH)) {
return userHomeFolderNode.getNode(FOLDER_NAME_TRASH);
} else {
return userHomeFolderNode.addNode(FOLDER_NAME_TRASH, pentahoJcrConstants.getPHO_NT_INTERNALFOLDER());
}
}
Aggregations