Search in sources :

Example 26 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class GwtAccountServiceImpl method create.

public GwtAccount create(GwtXSRFToken xsrfToken, GwtAccountCreator gwtAccountCreator) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    GwtAccount gwtAccount = null;
    KapuaId parentAccountId = KapuaEid.parseShortId(gwtAccountCreator.getParentAccountId());
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
        AccountCreator accountCreator = accountFactory.newAccountCreator(parentAccountId, gwtAccountCreator.getAccountName());
        accountCreator.setAccountPassword(gwtAccountCreator.getAccountPassword());
        accountCreator.setOrganizationName(gwtAccountCreator.getOrganizationName());
        accountCreator.setOrganizationPersonName(gwtAccountCreator.getOrganizationPersonName());
        accountCreator.setOrganizationEmail(gwtAccountCreator.getOrganizationEmail());
        accountCreator.setOrganizationPhoneNumber(gwtAccountCreator.getOrganizationPhoneNumber());
        accountCreator.setOrganizationAddressLine1(gwtAccountCreator.getOrganizationAddressLine1());
        accountCreator.setOrganizationAddressLine2(gwtAccountCreator.getOrganizationAddressLine2());
        accountCreator.setOrganizationCity(gwtAccountCreator.getOrganizationCity());
        accountCreator.setOrganizationZipPostCode(gwtAccountCreator.getOrganizationZipPostCode());
        accountCreator.setOrganizationStateProvinceCounty(gwtAccountCreator.getOrganizationStateProvinceCounty());
        accountCreator.setOrganizationCountry(gwtAccountCreator.getOrganizationCountry());
        // create the Account
        AccountService accountService = locator.getService(AccountService.class);
        Account account = accountService.create(accountCreator);
        // convert to GwtAccount and return
        gwtAccount = KapuaGwtConverter.convert(account);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtAccount;
}
Also used : GwtAccountCreator(org.eclipse.kapua.app.console.shared.model.GwtAccountCreator) AccountCreator(org.eclipse.kapua.service.account.AccountCreator) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AccountFactory(org.eclipse.kapua.service.account.AccountFactory) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) KapuaId(org.eclipse.kapua.model.id.KapuaId) AccountService(org.eclipse.kapua.service.account.AccountService) GwtAccountService(org.eclipse.kapua.app.console.shared.service.GwtAccountService)

Example 27 with Account

use of org.eclipse.kapua.service.account.Account in project kapua by eclipse.

the class GwtAccountServiceImpl method updateAccountProperties.

public GwtAccount updateAccountProperties(GwtXSRFToken xsrfToken, GwtAccount gwtAccount) throws GwtKapuaException {
    // 
    // Checking validity of the given XSRF Token
    checkXSRFToken(xsrfToken);
    GwtAccount gwtAccountUpdated = null;
    KapuaId scopeId = KapuaEid.parseShortId(gwtAccount.getId());
    try {
        KapuaLocator locator = KapuaLocator.getInstance();
        AccountService accountService = locator.getService(AccountService.class);
        Account account = accountService.find(scopeId);
        // update properties
        Properties property = account.getEntityProperties();
        if (property == null) {
            property = new Properties();
        }
        if (gwtAccount.getUnescapedDashboardPreferredTopic() != null) {
            property.put("topic", gwtAccount.getUnescapedDashboardPreferredTopic());
        }
        if (gwtAccount.getUnescapedDashboardPreferredMetric() != null) {
            property.put("metric", gwtAccount.getUnescapedDashboardPreferredMetric());
        }
        account.setEntityProperties(property);
        account = accountService.update(account);
        // convert to GwtAccount and return
        gwtAccountUpdated = KapuaGwtConverter.convert(account);
    } catch (Throwable t) {
        KapuaExceptionHandler.handle(t);
    }
    return gwtAccountUpdated;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) KapuaId(org.eclipse.kapua.model.id.KapuaId) Properties(java.util.Properties) AccountService(org.eclipse.kapua.service.account.AccountService) GwtAccountService(org.eclipse.kapua.app.console.shared.service.GwtAccountService)

Example 28 with Account

use of org.eclipse.kapua.service.account.Account 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 29 with Account

use of org.eclipse.kapua.service.account.Account 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 30 with Account

use of org.eclipse.kapua.service.account.Account 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)

Aggregations

Account (org.eclipse.kapua.service.account.Account)42 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)37 AccountService (org.eclipse.kapua.service.account.AccountService)31 KapuaId (org.eclipse.kapua.model.id.KapuaId)14 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)12 Device (org.eclipse.kapua.service.device.registry.Device)11 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)11 KapuaException (org.eclipse.kapua.KapuaException)10 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)10 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)9 GwtAccountService (org.eclipse.kapua.app.console.shared.service.GwtAccountService)9 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)8 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)8 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)7 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)7 Date (java.util.Date)5 AccountCreator (org.eclipse.kapua.service.account.AccountCreator)5 KuraRequestChannel (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestChannel)5 KuraRequestMessage (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestMessage)5 KuraRequestPayload (org.eclipse.kapua.service.device.call.message.app.request.kura.KuraRequestPayload)5