use of org.pentaho.platform.api.engine.security.userroledao.NotFoundException in project pentaho-platform by pentaho.
the class UserRoleDaoResourceTest method testRemoveRolesFromUserNotFoundException.
@Test
public void testRemoveRolesFromUserNotFoundException() {
String user = "testUser1";
String roles = "testRole1";
doThrow(new NotFoundException("expectedTestException")).when(userRoleService).removeRolesFromUser(anyString(), anyString());
try {
userRoleResource.removeRolesFromUser(user, roles);
} catch (WebApplicationException e) {
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), e.getResponse().getStatus());
assertNotNull(e.getResponse().getEntity());
}
}
use of org.pentaho.platform.api.engine.security.userroledao.NotFoundException in project pentaho-platform by pentaho.
the class UserRoleDaoServiceTest method testRemoveRoleFromUserNotFoundException.
@Test(expected = NotFoundException.class)
public void testRemoveRoleFromUserNotFoundException() {
String userName = "testUser";
String roleNames = "Power User\tBusiness User\t";
setupMockSessionUser(SESSION_USER_NAME, true);
// Create session that will generate tenant
IPentahoSession session = mock(IPentahoSession.class);
when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn("testTenantPath");
PentahoSessionHolder.setSession(session);
IUserRoleDao roleDao = mock(IUserRoleDao.class);
when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenThrow(new NotFoundException("expectedTestException"));
PentahoSystem.registerObject(roleDao);
userRoleService.removeRolesFromUser(userName, roleNames);
}
use of org.pentaho.platform.api.engine.security.userroledao.NotFoundException in project pentaho-platform by pentaho.
the class AbstractJcrBackedRoleBindingDao method setRoleBindings.
public void setRoleBindings(Session session, ITenant tenant, String runtimeRoleName, List<String> logicalRoleNames) throws NamespaceException, RepositoryException {
if (tenant == null) {
tenant = JcrTenantUtils.getTenant(runtimeRoleName, false);
runtimeRoleName = getPrincipalName(runtimeRoleName);
}
if (!TenantUtils.isAccessibleTenant(tenant)) {
throw new NotFoundException("Tenant " + tenant.getId() + " not found");
}
PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
// $NON-NLS-1$
final String phoNsPrefix = session.getNamespacePrefix(PentahoJcrConstants.PHO_NS) + ":";
// $NON-NLS-1$
final String onlyPentahoPattern = phoNsPrefix + "*";
Node runtimeRolesFolderNode = getRuntimeRolesFolderNode(session, tenant);
NodeIterator runtimeRoleNodes = runtimeRolesFolderNode.getNodes(onlyPentahoPattern);
int i = 0;
while (runtimeRoleNodes.hasNext()) {
runtimeRoleNodes.nextNode();
i++;
}
if (i == 0) {
// consulted
for (Map.Entry<String, List<String>> entry : bootstrapRoleBindings.entrySet()) {
JcrRoleAuthorizationPolicyUtils.internalSetBindings(pentahoJcrConstants, runtimeRolesFolderNode, entry.getKey(), entry.getValue(), phoNsPrefix);
}
}
if (!isImmutable(runtimeRoleName)) {
JcrRoleAuthorizationPolicyUtils.internalSetBindings(pentahoJcrConstants, runtimeRolesFolderNode, runtimeRoleName, logicalRoleNames, phoNsPrefix);
} else {
throw new RuntimeException(Messages.getInstance().getString("JcrRoleAuthorizationPolicyRoleBindingDao.ERROR_0001_ATTEMPT_MOD_IMMUTABLE", // $NON-NLS-1$
runtimeRoleName));
}
session.save();
Assert.isTrue(NodeHelper.hasNode(runtimeRolesFolderNode, phoNsPrefix, runtimeRoleName));
// update cache
String roleId = tenantedRoleNameUtils.getPrincipleId(tenant, runtimeRoleName);
cacheManager.putInRegionCache(LOGICAL_ROLE_BINDINGS_REGION, roleId, logicalRoleNames);
}
use of org.pentaho.platform.api.engine.security.userroledao.NotFoundException in project pentaho-platform by pentaho.
the class AbstractJcrBackedUserRoleDao method setUserRoles.
public void setUserRoles(Session session, final ITenant theTenant, final String userName, final String[] roles) throws RepositoryException, NotFoundException {
if ((isMyself(userName) || isDefaultAdminUser(userName)) && !adminRoleExist(roles)) {
throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0005_YOURSELF_OR_DEFAULT_ADMIN_USER"));
}
Set<String> roleSet = new HashSet<String>();
if (roles != null) {
roleSet.addAll(Arrays.asList(roles));
}
roleSet.add(authenticatedRoleName);
User jackrabbitUser = getJackrabbitUser(theTenant, userName, session);
if ((jackrabbitUser == null) || !TenantUtils.isAccessibleTenant(theTenant == null ? tenantedUserNameUtils.getTenant(jackrabbitUser.getID()) : theTenant)) {
throw new NotFoundException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0003_USER_NOT_FOUND"));
}
HashMap<String, Group> currentlyAssignedGroups = new HashMap<String, Group>();
Iterator<Group> currentGroups = jackrabbitUser.memberOf();
while (currentGroups.hasNext()) {
Group currentGroup = currentGroups.next();
currentlyAssignedGroups.put(currentGroup.getID(), currentGroup);
}
HashMap<String, Group> finalCollectionOfAssignedGroups = new HashMap<String, Group>();
ITenant tenant = theTenant == null ? JcrTenantUtils.getTenant(userName, true) : theTenant;
for (String role : roleSet) {
Group jackrabbitGroup = getJackrabbitGroup(tenant, role, session);
if (jackrabbitGroup != null) {
finalCollectionOfAssignedGroups.put(tenantedRoleNameUtils.getPrincipleId(tenant, role), jackrabbitGroup);
}
}
ArrayList<String> groupsToRemove = new ArrayList<String>(currentlyAssignedGroups.keySet());
groupsToRemove.removeAll(finalCollectionOfAssignedGroups.keySet());
ArrayList<String> groupsToAdd = new ArrayList<String>(finalCollectionOfAssignedGroups.keySet());
groupsToAdd.removeAll(currentlyAssignedGroups.keySet());
for (String groupId : groupsToRemove) {
currentlyAssignedGroups.get(groupId).removeMember(jackrabbitUser);
}
for (String groupId : groupsToAdd) {
finalCollectionOfAssignedGroups.get(groupId).addMember(jackrabbitUser);
}
// Purge the UserDetails cache
purgeUserFromCache(userName);
}
use of org.pentaho.platform.api.engine.security.userroledao.NotFoundException in project pentaho-platform by pentaho.
the class AbstractJcrBackedUserRoleDao method createUser.
public IPentahoUser createUser(Session session, final ITenant theTenant, final String userName, final String password, final String description, final String[] roles) throws AuthorizableExistsException, RepositoryException {
ITenant tenant = theTenant;
String user = userName;
if (tenant == null) {
tenant = JcrTenantUtils.getTenant(userName, true);
user = JcrTenantUtils.getPrincipalName(userName, true);
}
if (tenant == null || tenant.getId() == null) {
tenant = JcrTenantUtils.getCurrentTenant();
}
if (!TenantUtils.isAccessibleTenant(tenant)) {
throw new NotFoundException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0006_TENANT_NOT_FOUND", theTenant.getId()));
}
String userId = tenantedUserNameUtils.getPrincipleId(tenant, user);
UserManager tenantUserMgr = getUserManager(tenant, session);
// $NON-NLS-1$
tenantUserMgr.createUser(userId, password, new PrincipalImpl(userId), "");
session.save();
/**
* This call is absolutely necessary. setUserRolesForNewUser will never inspect what roles this user is a part of.
* Since this is a new user it will not be a part of new roles
*/
setUserRolesForNewUser(session, tenant, user, roles);
setUserDescription(session, tenant, user, description);
session.save();
createUserHomeFolder(tenant, user, session);
session.save();
this.userDetailsCache.removeUserFromCache(userName);
return getUser(session, tenant, userName);
}
Aggregations