use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class KapuaAuthorizingRealm method doGetAuthorizationInfo.
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) throws AuthenticationException {
//
// Extract principal
String username = (String) principals.getPrimaryPrincipal();
logger.debug("Getting authorization info for: {}", username);
//
// Get Services
KapuaLocator locator = KapuaLocator.getInstance();
UserService userService = locator.getService(UserService.class);
UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
//
// Get the associated user by name
final User user;
try {
user = KapuaSecurityUtils.doPriviledge(new Callable<User>() {
@Override
public User call() throws Exception {
return userService.findByName(username);
}
});
} catch (Exception e) {
// to preserve the original exception message (if possible)
if (e instanceof AuthenticationException) {
throw (AuthenticationException) e;
} else {
throw new ShiroException("Error while find user!", e);
}
}
// Check existence
if (user == null) {
throw new UnknownAccountException();
}
//
// Get user permissions set
UserPermissionQuery query = userPermissionFactory.newQuery(user.getScopeId());
KapuaPredicate predicate = new AttributePredicate<KapuaId>(UserPermissionPredicates.USER_ID, user.getId());
query.setPredicate(predicate);
final KapuaListResult<UserPermission> userPermissions;
try {
userPermissions = KapuaSecurityUtils.doPriviledge(new Callable<KapuaListResult<UserPermission>>() {
@Override
public KapuaListResult<UserPermission> call() throws Exception {
return userPermissionService.query(query);
}
});
} catch (Exception e) {
// to preserve the original exception message (if possible)
if (e instanceof AuthenticationException) {
throw (AuthenticationException) e;
} else {
throw new ShiroException("Error while find permissions!", e);
}
}
//
// Create SimpleAuthorizationInfo with principals permissions
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (UserPermission userPermission : userPermissions.getItems()) {
Permission p = permissionFactory.newPermission(userPermission.getPermission().getDomain(), userPermission.getPermission().getAction(), userPermission.getPermission().getTargetScopeId());
logger.trace("Username: {} has permission: {}", username, p);
info.addStringPermission(p.toString());
}
return info;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class UserServiceTest method testCreate.
/**
* We should ignore this test until we have build fixed.
*/
@Test
public void testCreate() throws Exception {
// prepare the UserCreator
long now = (new Date()).getTime();
String username = MessageFormat.format("aaa_test_username_{0,number,#}", now);
String userEmail = MessageFormat.format("testuser_{0,number,#}@organization.com", now);
String displayName = MessageFormat.format("User Display Name {0}", now);
// KapuaPeid accountPeid = KapuaEidGenerator.generate();//
KapuaLocator locator = KapuaLocator.getInstance();
IdGeneratorService idGeneratorService = locator.getService(IdGeneratorService.class);
KapuaId scopeId = idGeneratorService.generate();
KapuaLocator serviceLocator = KapuaLocator.getInstance();
UserFactory kapuaEntityCreatorFactory = serviceLocator.getFactory(UserFactory.class);
UserCreator userCreator = kapuaEntityCreatorFactory.newCreator(scopeId, username);
userCreator.setDisplayName(displayName);
userCreator.setEmail(userEmail);
userCreator.setPhoneNumber("+1 555 123 4567");
// create the User
UserService userService = serviceLocator.getService(UserService.class);
User user = userService.create(userCreator);
user = userService.find(user.getScopeId(), user.getId());
//
// User asserts
assertNotNull(user.getId());
assertNotNull(user.getId().getId());
assertTrue(user.getOptlock() >= 0);
assertEquals(scopeId, user.getScopeId());
assertEquals(userCreator.getName(), user.getName());
assertNotNull(user.getCreatedOn());
assertNotNull(user.getCreatedBy());
assertNotNull(user.getModifiedOn());
assertNotNull(user.getModifiedBy());
assertEquals(userCreator.getDisplayName(), user.getDisplayName());
assertEquals(userCreator.getEmail(), user.getEmail());
assertEquals(userCreator.getPhoneNumber(), user.getPhoneNumber());
assertEquals(UserStatus.ENABLED, user.getStatus());
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class AuthenticationServiceMock method login.
@Override
public AccessToken login(AuthenticationCredentials authenticationToken) throws KapuaException {
if (!(authenticationToken instanceof UsernamePasswordTokenMock))
throw KapuaException.internalError("Unmanaged credentials type");
UsernamePasswordTokenMock usrPwdTokenMock = (UsernamePasswordTokenMock) authenticationToken;
KapuaLocator serviceLocator = KapuaLocator.getInstance();
UserService userService = serviceLocator.getService(UserService.class);
User user = userService.findByName(usrPwdTokenMock.getUsername());
KapuaSession kapuaSession = new KapuaSession(null, null, user.getScopeId(), user.getId(), user.getName());
KapuaSecurityUtils.setSession(kapuaSession);
// TODO Auto-generated method stub
return null;
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class KapuaTest method tearDown.
@AfterClass
public static void tearDown() {
LOG.debug("Stopping Kapua test context.");
isInitialized = false;
try {
KapuaLocator locator = KapuaLocator.getInstance();
AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
authenticationService.logout();
} catch (KapuaException exc) {
exc.printStackTrace();
}
}
use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.
the class UserRolesServiceImpl method count.
@Override
public long count(KapuaQuery<UserRoles> query) throws KapuaException {
ArgumentValidator.notNull(query, "query");
ArgumentValidator.notNull(query.getScopeId(), "query.scopeId");
//
// Check Access
KapuaLocator locator = KapuaLocator.getInstance();
AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
authorizationService.checkPermission(permissionFactory.newPermission(RoleDomain.ROLE, Actions.read, query.getScopeId()));
//
// Do count
long count = 0;
EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
try {
count = UserRolesDAO.count(em, query);
} catch (Exception e) {
throw KapuaExceptionUtils.convertPersistenceException(e);
} finally {
em.close();
}
return count;
}
Aggregations