use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class UserRoleDaoServiceTest method testRemoveRoleFromUser.
@Test
public void testRemoveRoleFromUser() {
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);
IPentahoRole ceoRole = mock(IPentahoRole.class);
when(ceoRole.getName()).thenReturn("ceo");
IPentahoRole ctoRole = mock(IPentahoRole.class);
when(ctoRole.getName()).thenReturn("cto");
IPentahoRole powerUserRole = mock(IPentahoRole.class);
when(powerUserRole.getName()).thenReturn("Power User");
IPentahoRole businessUserRole = mock(IPentahoRole.class);
when(businessUserRole.getName()).thenReturn("Business User");
List<IPentahoRole> roleList = new ArrayList<>();
roleList.add(ceoRole);
roleList.add(ctoRole);
roleList.add(powerUserRole);
roleList.add(businessUserRole);
IUserRoleDao roleDao = mock(IUserRoleDao.class);
when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenReturn(roleList);
PentahoSystem.registerObject(roleDao);
userRoleService.removeRolesFromUser(userName, roleNames);
verify(roleDao).setUserRoles(any(ITenant.class), anyString(), argThat(new UnorderedArrayMatcher(new String[] { "ceo", "cto" })));
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class AbstractJcrBackedUserRoleDao method getUserRoles.
public List<IPentahoRole> getUserRoles(Session session, final ITenant theTenant, final String userName) throws RepositoryException {
ArrayList<IPentahoRole> roles = new ArrayList<IPentahoRole>();
User jackrabbitUser = getJackrabbitUser(theTenant, userName, session);
if ((jackrabbitUser != null) && TenantUtils.isAccessibleTenant(theTenant == null ? tenantedUserNameUtils.getTenant(jackrabbitUser.getID()) : theTenant)) {
Iterator<Group> groups = jackrabbitUser.memberOf();
while (groups.hasNext()) {
IPentahoRole role = convertToPentahoRole(groups.next());
// Exclude the extra role from the list of roles to be returned back
if (!extraRoles.contains(role.getName())) {
roles.add(role);
}
}
}
return roles;
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class DefaultUserRepositoryLifecycleManager method configureRoles.
private void configureRoles() {
if (logger.isDebugEnabled()) {
logger.debug("Configuring default role mappings.");
}
for (final String roleName : roleMappings.keySet()) {
final IPentahoRole role = userRoleDao.getRole(DEFAULT_TENANT, roleName);
if (role == null) {
if (logger.isDebugEnabled()) {
logger.debug("Creating user role: " + roleName);
}
userRoleDao.createRole(DEFAULT_TENANT, roleName, "", EMPTY_STRING_ARRAY);
final List<String> logicalRoles = roleMappings.get(roleName);
if (logicalRoles.size() > 0) {
roleBindingDao.setRoleBindings(DEFAULT_TENANT, roleName, logicalRoles);
}
if (logger.isDebugEnabled()) {
StringBuffer buffer = new StringBuffer();
for (String logicalRole : logicalRoles) {
buffer.append(logicalRole + " ");
}
logger.debug("Create Role[" + roleName + "] with logical roles [ " + buffer + " ]");
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Skipping config. Role[" + roleName + "] already registered.");
}
}
}
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole 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.engine.security.userroledao.IPentahoRole 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