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());
}
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;
}
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;
}
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();
}
}
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;
}
Aggregations