Search in sources :

Example 96 with KapuaLocator

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

the class AccountServiceImpl method create.

@Override
public Account create(AccountCreator accountCreator) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(accountCreator, "accountCreator");
    ArgumentValidator.notEmptyOrNull(accountCreator.getName(), "name");
    ArgumentValidator.notEmptyOrNull(accountCreator.getAccountPassword(), "accountPassword");
    ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationName(), "organizationName");
    ArgumentValidator.notEmptyOrNull(accountCreator.getOrganizationEmail(), "organizationEmail");
    ArgumentValidator.notNull(accountCreator.getScopeId(), "scopeId");
    ArgumentValidator.notNull(accountCreator.getScopeId().getId(), "scopeId.id");
    ArgumentValidator.match(accountCreator.getAccountPassword(), ArgumentValidator.PASSWORD_REGEXP, "accountPassword");
    ArgumentValidator.match(accountCreator.getOrganizationEmail(), ArgumentValidator.EMAIL_REGEXP, "organizationEmail");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, accountCreator.getScopeId()));
    // Check if the parent account exists
    if (findById(accountCreator.getScopeId()) == null) {
        throw new KapuaIllegalArgumentException("scopeId", "parent account does not exist");
    }
    // 
    // Create the account
    Account account = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        em.beginTransaction();
        account = AccountDAO.create(em, accountCreator);
        em.persist(account);
        // Set the parent account path
        String parentAccountPath = AccountDAO.find(em, accountCreator.getScopeId()).getParentAccountPath() + "/" + account.getId();
        account.setParentAccountPath(parentAccountPath);
        AccountDAO.update(em, account);
        em.commit();
    } catch (Exception pe) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(pe);
    } finally {
        em.close();
    }
    // to make sure we get all the latest info/version
    return find(account.getScopeId(), account.getId());
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 97 with KapuaLocator

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

the class AccountServiceImpl method findChildsRecursively.

@Override
public AccountListResult findChildsRecursively(KapuaId id) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(id, "scopeId");
    ArgumentValidator.notNull(id.getId(), "scopeId.id");
    // 
    // Make sure account exists
    Account account = findById(id);
    if (account == null) {
        throw new KapuaEntityNotFoundException(Account.TYPE, id);
    }
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.read, account.getId()));
    AccountListResult result = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        TypedQuery<Account> q;
        q = em.createNamedQuery("Account.findChildAccountsRecursive", Account.class);
        q.setParameter("parentAccountPath", account.getParentAccountPath() + "/%");
        result = new AccountListResultImpl();
        result.addItems(q.getResultList());
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return result;
}
Also used : Account(org.eclipse.kapua.service.account.Account) 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) AccountListResult(org.eclipse.kapua.service.account.AccountListResult) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 98 with KapuaLocator

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

the class AccountServiceImpl method count.

@Override
public long count(KapuaQuery<Account> 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(AccountDomain.ACCOUNT, Actions.read, query.getScopeId()));
    // 
    // Do count
    long count = 0;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        count = AccountDAO.count(em, query);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    return count;
}
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) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 99 with KapuaLocator

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

the class AccountServiceImpl method delete.

@Override
public void delete(KapuaId scopeId, KapuaId accountId) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(accountId, "id");
    ArgumentValidator.notNull(scopeId, "id.id");
    // 
    // Check Access
    Actions action = Actions.write;
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, action, scopeId));
    // Check if it has children
    if (this.findChildAccountsTrusted(accountId).size() > 0) {
        throw new KapuaAccountException(KapuaAccountErrorCodes.OPERATION_NOT_ALLOWED, null, "This account cannot be deleted. Delete its child first.");
    }
    // 
    // Delete the Account
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        // Entity needs to be loaded in the context of the same EntityManger to be able to delete it afterwards
        Account accountx = AccountDAO.find(em, accountId);
        if (accountx == null) {
            throw new KapuaEntityNotFoundException(Account.TYPE, accountId);
        }
        // do not allow deletion of the kapua admin account
        SystemSetting settings = SystemSetting.getInstance();
        if (settings.getString(SystemSettingKey.SYS_PROVISION_ACCOUNT_NAME).equals(accountx.getName())) {
            throw new KapuaIllegalAccessException(action.name());
        }
        if (settings.getString(SystemSettingKey.SYS_ADMIN_ACCOUNT).equals(accountx.getName())) {
            throw new KapuaIllegalAccessException(action.name());
        }
        em.beginTransaction();
        AccountDAO.delete(em, accountId);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) Actions(org.eclipse.kapua.service.authorization.permission.Actions) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) SystemSetting(org.eclipse.kapua.commons.setting.system.SystemSetting) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

Example 100 with KapuaLocator

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

the class AccountServiceImpl method findByName.

@Override
public Account findByName(String name) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notEmptyOrNull(name, "name");
    // 
    // Do the find
    Account account = null;
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        account = AccountDAO.findByName(em, name);
    } catch (Exception e) {
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    // Check Access
    if (account != null) {
        KapuaLocator locator = KapuaLocator.getInstance();
        AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
        PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
        authorizationService.checkPermission(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.read, account.getId()));
    }
    return account;
}
Also used : Account(org.eclipse.kapua.service.account.Account) 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) KapuaIllegalArgumentException(org.eclipse.kapua.KapuaIllegalArgumentException) KapuaIllegalAccessException(org.eclipse.kapua.KapuaIllegalAccessException) KapuaException(org.eclipse.kapua.KapuaException)

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