Search in sources :

Example 21 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class AuthenticationServiceTest method testLoginAndLogout.

/**
 * We should ignore this test until schema loading feature is provided.
 */
@Ignore
@Test
public void testLoginAndLogout() throws Exception {
    String username = "kapua-sys";
    char[] password = "kapua-password".toCharArray();
    KapuaLocator locator = KapuaLocator.getInstance();
    UsernamePasswordTokenFactory usernamePasswordTokenFactory = locator.getFactory(UsernamePasswordTokenFactory.class);
    UsernamePasswordToken credentialsToken = usernamePasswordTokenFactory.newInstance(username, password);
    AuthenticationService authenticationService = locator.getService(AuthenticationService.class);
    authenticationService.login(credentialsToken);
    Subject currentSubject = SecurityUtils.getSubject();
    assertTrue(currentSubject.isAuthenticated());
    assertEquals(username, currentSubject.getPrincipal());
    authenticationService.logout();
    assertFalse(currentSubject.isAuthenticated());
    assertNull(currentSubject.getPrincipal());
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) UsernamePasswordTokenFactory(org.eclipse.kapua.service.authentication.UsernamePasswordTokenFactory) AuthenticationService(org.eclipse.kapua.service.authentication.AuthenticationService) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.eclipse.kapua.service.authentication.UsernamePasswordToken) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 22 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class UserPermissionServiceImpl method query.

@Override
public UserPermissionListResult query(KapuaQuery<UserPermission> 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(UserPermissionDomain.USER_PERMISSION, Actions.read, query.getScopeId()));
    // 
    // Do query
    UserPermissionListResult result = null;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        result = UserPermissionDAO.query(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return result;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) UserPermissionListResult(org.eclipse.kapua.service.authorization.user.permission.UserPermissionListResult) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException)

Example 23 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class UserPermissionServiceImpl method find.

@Override
public UserPermission find(KapuaId scopeId, KapuaId permissionId) throws KapuaException {
    ArgumentValidator.notNull(scopeId, "accountId");
    ArgumentValidator.notNull(permissionId, "permissionId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(UserPermissionDomain.USER_PERMISSION, Actions.read, scopeId));
    // 
    // Do find
    UserPermission permission = null;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        permission = UserPermissionDAO.find(em, permissionId);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return permission;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException) UserPermission(org.eclipse.kapua.service.authorization.user.permission.UserPermission)

Example 24 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class UserPermissionServiceImpl method create.

@Override
public UserPermission create(UserPermissionCreator userPermissionCreator) throws KapuaException {
    ArgumentValidator.notNull(userPermissionCreator, "userPermissionCreator");
    ArgumentValidator.notNull(userPermissionCreator.getPermission(), "userPermissionCreator.permission");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(UserPermissionDomain.USER_PERMISSION, Actions.write, userPermissionCreator.getScopeId()));
    // 
    // Do create
    UserPermission permission = null;
    EntityManager em = AuthorizationEntityManagerFactory.getEntityManager();
    try {
        em.beginTransaction();
        permission = UserPermissionDAO.create(em, userPermissionCreator);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return permission;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaException(org.eclipse.kapua.KapuaException) UserPermission(org.eclipse.kapua.service.authorization.user.permission.UserPermission)

Example 25 with KapuaLocator

use of org.eclipse.kapua.locator.KapuaLocator in project kapua by eclipse.

the class TranslatorAppBundleKapuaKura method translate.

@Override
public KuraRequestMessage translate(BundleRequestMessage kapuaMessage) throws KapuaException {
    // 
    // Kura channel
    KapuaLocator locator = KapuaLocator.getInstance();
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.find(kapuaMessage.getScopeId());
    DeviceRegistryService deviceService = locator.getService(DeviceRegistryService.class);
    Device device = deviceService.find(kapuaMessage.getScopeId(), kapuaMessage.getDeviceId());
    KuraRequestChannel kuraRequestChannel = translate(kapuaMessage.getChannel());
    kuraRequestChannel.setScope(account.getName());
    kuraRequestChannel.setClientId(device.getClientId());
    // 
    // Kura payload
    KuraRequestPayload kuraPayload = translate(kapuaMessage.getPayload());
    // Return Kura Message
    return new KuraRequestMessage(kuraRequestChannel, kapuaMessage.getReceivedOn(), kuraPayload);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) Device(org.eclipse.kapua.service.device.registry.Device) KuraRequestMessage(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestMessage) DeviceRegistryService(org.eclipse.kapua.service.device.registry.DeviceRegistryService) KuraRequestChannel(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel) AccountService(org.eclipse.kapua.service.account.AccountService) KuraRequestPayload(org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload)

Aggregations

KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)150 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)61 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)58 KapuaId (org.eclipse.kapua.model.id.KapuaId)54 KapuaException (org.eclipse.kapua.KapuaException)52 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)42 Account (org.eclipse.kapua.service.account.Account)37 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)36 AccountService (org.eclipse.kapua.service.account.AccountService)34 Date (java.util.Date)30 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)23 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)23 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)23 Device (org.eclipse.kapua.service.device.registry.Device)22 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)21 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)17 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)11 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)10