Search in sources :

Example 1 with AuthorizationService

use of org.eclipse.kapua.service.authorization.AuthorizationService in project kapua by eclipse.

the class AbstractKapuaConfigurableService method setConfigValues.

@Override
public void setConfigValues(KapuaId scopeId, Map<String, Object> values) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.write, scopeId));
    KapuaTocd ocd = this.getConfigMetadata();
    validateConfigurations(this.pid, ocd, values);
    Properties props = toProperties(values);
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    ServiceConfig serviceConfig = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResultImpl result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    // In not exists create then return
    if (result == null || result.getSize() == 0) {
        ServiceConfigImpl serviceConfigNew = new ServiceConfigImpl(scopeId);
        serviceConfigNew.setPid(this.pid);
        serviceConfigNew.setConfigurations(props);
        serviceConfig = this.create(em, serviceConfigNew);
        return;
    }
    // If exists update it
    serviceConfig = result.getItem(0);
    serviceConfig.setConfigurations(props);
    this.update(em, serviceConfig);
    return;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 2 with AuthorizationService

use of org.eclipse.kapua.service.authorization.AuthorizationService in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigValues.

@Override
public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    AndPredicate predicate = new AndPredicate().and(new AttributePredicate<String>("pid", this.pid, Operator.EQUAL)).and(new AttributePredicate<KapuaId>("scopeId", scopeId, Operator.EQUAL));
    ServiceConfigQueryImpl query = new ServiceConfigQueryImpl(scopeId);
    query.setPredicate(predicate);
    Properties properties = null;
    EntityManager em = this.entityManagerFactory.createEntityManager();
    ServiceConfigListResult result = ServiceConfigDAO.query(em, ServiceConfig.class, ServiceConfigImpl.class, new ServiceConfigListResultImpl(), query);
    if (result != null && result.getSize() > 0)
        properties = result.getItem(0).getConfigurations();
    KapuaTocd ocd = this.getConfigMetadata();
    return toValues(ocd, properties);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 3 with AuthorizationService

use of org.eclipse.kapua.service.authorization.AuthorizationService in project kapua by eclipse.

the class AbstractKapuaConfigurableService method getConfigMetadata.

@Override
public KapuaTocd getConfigMetadata() throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    KapuaId scopeId = KapuaSecurityUtils.getSession().getScopeId();
    authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
    try {
        TmetadataImpl metadata = readMetadata(this.pid);
        if (metadata.getOCD() != null && metadata.getOCD().size() > 0) {
            for (KapuaTocd ocd : metadata.getOCD()) {
                if (ocd.getId() != null && ocd.getId().equals(pid)) {
                    return ocd;
                }
            }
        }
        return null;
    } catch (Exception e) {
        throw KapuaConfigurationException.internalError(e);
    }
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaId(org.eclipse.kapua.model.id.KapuaId) TmetadataImpl(org.eclipse.kapua.commons.configuration.metatype.TmetadataImpl) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd) XMLStreamException(javax.xml.stream.XMLStreamException) KapuaEntityNotFoundException(org.eclipse.kapua.KapuaEntityNotFoundException) IOException(java.io.IOException) KapuaException(org.eclipse.kapua.KapuaException)

Example 4 with AuthorizationService

use of org.eclipse.kapua.service.authorization.AuthorizationService in project kapua by eclipse.

the class AccountServiceImpl method find.

@Override
public Account find(KapuaId id) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(id, "id");
    ArgumentValidator.notNull(id.getId(), "id.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, id));
    // Make sure account exists
    return findById(id);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory)

Example 5 with AuthorizationService

use of org.eclipse.kapua.service.authorization.AuthorizationService in project kapua by eclipse.

the class AccountServiceImpl method update.

@Override
public Account update(Account account) throws KapuaException {
    // 
    // Validation of the fields
    ArgumentValidator.notNull(account.getId(), "id");
    ArgumentValidator.notNull(account.getId().getId(), "id.id");
    ArgumentValidator.notEmptyOrNull(account.getName(), "accountName");
    ArgumentValidator.notNull(account.getOrganization(), "organization");
    ArgumentValidator.match(account.getOrganization().getEmail(), 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, account.getScopeId()));
    // 
    // Update the Account
    EntityManager em = AccountEntityManagerFactory.getInstance().createEntityManager();
    try {
        Account oldAccount = AccountDAO.find(em, account.getId());
        if (oldAccount == null) {
            throw new KapuaEntityNotFoundException(Account.TYPE, account.getId());
        }
        // Verify unchanged parent account ID and parent account path
        if (!oldAccount.getScopeId().equals(account.getScopeId())) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "scopeId");
        }
        if (oldAccount.getParentAccountPath().compareTo(account.getParentAccountPath()) != 0) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "parentAccountPath");
        }
        if (oldAccount.getName().compareTo(account.getName()) != 0) {
            throw new KapuaAccountException(KapuaAccountErrorCodes.ILLEGAL_ARGUMENT, null, "accountName");
        }
        // Update
        em.beginTransaction();
        AccountDAO.update(em, account);
        em.commit();
    } catch (Exception e) {
        em.rollback();
        throw KapuaExceptionUtils.convertPersistenceException(e);
    } finally {
        em.close();
    }
    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) 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)

Aggregations

AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)65 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)65 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)58 KapuaException (org.eclipse.kapua.KapuaException)50 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)43 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)42 Date (java.util.Date)17 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)17 DeviceCallExecutor (org.eclipse.kapua.service.device.management.commons.call.DeviceCallExecutor)17 DeviceEventCreator (org.eclipse.kapua.service.device.registry.event.DeviceEventCreator)17 DeviceEventFactory (org.eclipse.kapua.service.device.registry.event.DeviceEventFactory)17 DeviceEventService (org.eclipse.kapua.service.device.registry.event.DeviceEventService)17 DeviceManagementException (org.eclipse.kapua.service.device.management.commons.exception.DeviceManagementException)8 DeviceManagementSetting (org.eclipse.kapua.service.device.management.commons.setting.DeviceManagementSetting)8 PackageRequestChannel (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestChannel)8 PackageRequestMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestMessage)8 PackageRequestPayload (org.eclipse.kapua.service.device.management.packages.message.internal.PackageRequestPayload)8 PackageResponseMessage (org.eclipse.kapua.service.device.management.packages.message.internal.PackageResponseMessage)8 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)7 Account (org.eclipse.kapua.service.account.Account)7