use of org.eclipse.kapua.service.authorization.permission.PermissionFactory 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;
}
use of org.eclipse.kapua.service.authorization.permission.PermissionFactory 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);
}
use of org.eclipse.kapua.service.authorization.permission.PermissionFactory 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);
}
}
use of org.eclipse.kapua.service.authorization.permission.PermissionFactory 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);
}
use of org.eclipse.kapua.service.authorization.permission.PermissionFactory 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());
}
Aggregations