use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class AbstractJcrBackedUserRoleDao method canDeleteUser.
/**
* Checks to see if the removal of the received roles and users would cause the system to have no login associated
* with the Admin role. This check is to be made before any changes take place
*
* @return Error message if invalid or null if ok
* @throws RepositoryException
*/
private boolean canDeleteUser(Session session, final IPentahoUser user) throws RepositoryException {
boolean userHasAdminRole = false;
List<IPentahoRole> roles = getUserRoles(null, user.getUsername());
for (IPentahoRole role : roles) {
if (tenantAdminRoleName.equals(role.getName())) {
userHasAdminRole = true;
break;
}
}
if ((isMyself(user.getUsername()) || isDefaultAdminUser(user.getUsername())) && userHasAdminRole) {
throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0008_UNABLE_TO_DELETE_USER_IS_YOURSELF_OR_DEFAULT_ADMIN_USER"));
}
if (userHasAdminRole) {
List<IPentahoUser> usersWithAdminRole = getRoleMembers(session, null, tenantAdminRoleName);
if (usersWithAdminRole == null) {
throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0004_LAST_USER_NEEDED_IN_ROLE", tenantAdminRoleName));
}
if (usersWithAdminRole.size() > 1) {
return true;
} else if (usersWithAdminRole.size() == 1) {
return false;
} else {
throw new RepositoryException(Messages.getInstance().getString("AbstractJcrBackedUserRoleDao.ERROR_0004_LAST_USER_NEEDED_IN_ROLE", tenantAdminRoleName));
}
}
return true;
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class UserRoleDaoUserDetailsService method loadUserByUsername.
// ~ Constructors
// ====================================================================================================
// ~ Methods
// =========================================================================================================
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
final boolean ACCOUNT_NON_EXPIRED = true;
final boolean CREDS_NON_EXPIRED = true;
final boolean ACCOUNT_NON_LOCKED = true;
IPentahoUser user;
try {
if (userRoleDao == null) {
userRoleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession());
}
user = userRoleDao.getUser(null, username);
} catch (UncategorizedUserRoleDaoException e) {
throw new UserRoleDaoUserDetailsServiceException(Messages.getInstance().getString("UserRoleDaoUserDetailsService.ERROR_0003_DATA_ACCESS_EXCEPTION"), // $NON-NLS-1$
e);
}
if (user == null) {
throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
"UserRoleDaoUserDetailsService.ERROR_0001_USER_NOT_FOUND"));
}
// convert IPentahoUser to a UserDetails instance
List<IPentahoRole> userRoles = userRoleDao.getUserRoles(null, username);
int authsSize = userRoles != null ? userRoles.size() : 0;
GrantedAuthority[] auths = new GrantedAuthority[authsSize];
int i = 0;
for (IPentahoRole role : userRoles) {
auths[i++] = new SimpleGrantedAuthority(role.getName());
}
List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>(Arrays.asList(auths));
addCustomAuthorities(user.getUsername(), dbAuths);
// Store the Tenant ID in the session
IPentahoSession session = PentahoSessionHolder.getSession();
String tenantId = (String) session.getAttribute(IPentahoSession.TENANT_ID_KEY);
if (tenantId == null) {
ITenant tenant = JcrTenantUtils.getTenant(username, true);
session.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
}
if (!StringUtils.isEmpty(defaultRoleString)) {
defaultRole = new SimpleGrantedAuthority(defaultRoleString);
}
if (defaultRole != null && !dbAuths.contains(defaultRole)) {
dbAuths.add(defaultRole);
}
if (dbAuths.size() == 0) {
throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
"UserRoleDaoUserDetailsService.ERROR_0002_NO_AUTHORITIES"));
}
return new User(user.getUsername(), user.getPassword(), user.isEnabled(), ACCOUNT_NON_EXPIRED, CREDS_NON_EXPIRED, ACCOUNT_NON_LOCKED, dbAuths);
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class ProxyPentahoUserRoleHelper method syncRoles.
/**
* Synchronizes <code>role</code> with fields from <code>proxyRole</code>. The users set of given
* <code>role</code> is unmodified.
*/
public static IPentahoRole syncRoles(IPentahoRole role, ProxyPentahoRole proxyRole) {
IPentahoRole syncedRole = role;
if (syncedRole == null) {
syncedRole = new PentahoRole(proxyRole.getName());
}
syncedRole.setDescription(proxyRole.getDescription());
return syncedRole;
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class UserRoleWebServiceBase method testCreateRole.
@Test
public void testCreateRole() throws Exception {
UserRoleDaoMock userRoleDao = PentahoSystem.get(UserRoleDaoMock.class, USER_ROLE_DAO_TXN, null);
IUserRoleWebService service = getUserRoleWebService();
mockUserAsAdmin(false);
ProxyPentahoRole role = new ProxyPentahoRole("role");
role.setDescription("testing");
try {
service.createRole(role);
Assert.fail();
} catch (UserRoleException e) {
Assert.assertTrue("ERROR_0001 not found in " + e.getMessage(), e.getMessage().indexOf("ERROR_0001") >= 0);
}
mockUserAsAdmin(true);
service.createRole(role);
// the last role should have the same name and description
IPentahoRole roleVerified = userRoleDao.getRole(null, "role");
Assert.assertNotNull(roleVerified);
Assert.assertEquals("role", roleVerified.getName());
Assert.assertEquals("testing", roleVerified.getDescription());
}
use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole in project pentaho-platform by pentaho.
the class MockUserRoleDao method setRoleMembers.
public void setRoleMembers(ITenant tenant, String roleName, String[] userNames) throws NotFoundException, UncategorizedUserRoleDaoException {
if (tenant == null) {
tenant = getTenant(roleName, false);
roleName = getPrincipalName(roleName, false);
}
if (tenant == null || tenant.getId() == null) {
tenant = getCurrentTenant();
}
IPentahoRole role = getRole(tenant, roleName);
HashSet<IPentahoUser> users = roleMembers.get(role);
users.clear();
if (userNames != null) {
for (String userName : userNames) {
IPentahoUser user = getUser(tenant, userName);
if (!userRoles.containsKey(user)) {
userRoles.put(user, new HashSet<IPentahoRole>());
}
userRoles.get(user).add(role);
if (user != null) {
users.add(user);
}
}
}
}
Aggregations