Search in sources :

Example 1 with SystemSetting

use of org.eclipse.kapua.commons.setting.system.SystemSetting in project kapua by eclipse.

the class DefaultConfigurableJdbcConnectionUrlResolver method connectionUrl.

@Override
public String connectionUrl() {
    SystemSetting config = SystemSetting.getInstance();
    // Mandatory connection parameters
    String dbName = config.getString(SystemSettingKey.DB_NAME);
    String dbConnectionScheme = config.getString(SystemSettingKey.DB_CONNECTION_SCHEME);
    String dbConnectionHost = config.getString(SystemSettingKey.DB_CONNECTION_HOST);
    String dbConnectionPort = config.getString(SystemSettingKey.DB_CONNECTION_PORT);
    StringBuilder dbConnectionString = new StringBuilder().append(dbConnectionScheme).append("://").append(dbConnectionHost).append(":").append(dbConnectionPort).append("/").append(dbName).append(";");
    // Optional connection parameters
    String schema = config.getString(SystemSettingKey.DB_SCHEMA);
    if (schema != null) {
        dbConnectionString.append("schema=").append(schema).append(";");
        // This deletes the trailing '?' or '&'
        dbConnectionString.deleteCharAt(dbConnectionString.length() - 1);
    }
    return dbConnectionString.toString();
}
Also used : SystemSetting(org.eclipse.kapua.commons.setting.system.SystemSetting)

Example 2 with SystemSetting

use of org.eclipse.kapua.commons.setting.system.SystemSetting in project kapua by eclipse.

the class GwtAuthorizationServiceImpl method establishSession.

private GwtSession establishSession() throws KapuaException {
    KapuaLocator locator = KapuaLocator.getInstance();
    // 
    // Get info from session
    KapuaSession kapuaSession = KapuaSecurityUtils.getSession();
    // 
    // Get user info
    UserService userService = locator.getService(UserService.class);
    User user = userService.find(kapuaSession.getScopeId(), kapuaSession.getUserId());
    // 
    // Get permission info
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    boolean hasAccountCreate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
    boolean hasAccountRead = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.read, kapuaSession.getScopeId()));
    boolean hasAccountUpdate = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.write, kapuaSession.getScopeId()));
    boolean hasAccountDelete = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, Actions.delete, kapuaSession.getScopeId()));
    boolean hasAccountAll = authorizationService.isPermitted(permissionFactory.newPermission(AccountDomain.ACCOUNT, null, null));
    boolean hasDeviceCreate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDeviceRead = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.read, kapuaSession.getScopeId()));
    boolean hasDeviceUpdate = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDeviceDelete = authorizationService.isPermitted(permissionFactory.newPermission(DeviceDomain.DEVICE, Actions.delete, kapuaSession.getScopeId()));
    boolean hasDeviceManage = authorizationService.isPermitted(permissionFactory.newPermission(DeviceLifecycleDomain.DEVICE_LIFECYCLE, Actions.write, kapuaSession.getScopeId()));
    boolean hasDataRead = authorizationService.isPermitted(permissionFactory.newPermission("data", Actions.read, kapuaSession.getScopeId()));
    boolean hasUserCreate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
    boolean hasUserRead = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.read, kapuaSession.getScopeId()));
    boolean hasUserUpdate = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.write, kapuaSession.getScopeId()));
    boolean hasUserDelete = authorizationService.isPermitted(permissionFactory.newPermission("user", Actions.delete, kapuaSession.getScopeId()));
    // 
    // Get account info
    AccountService accountService = locator.getService(AccountService.class);
    Account account = accountService.find(kapuaSession.getScopeId());
    // 
    // Convert entities
    GwtUser gwtUser = KapuaGwtConverter.convert(user);
    GwtAccount gwtAccount = KapuaGwtConverter.convert(account);
    // 
    // Build the session
    GwtSession gwtSession = new GwtSession();
    // Console info
    SystemSetting commonsConfig = SystemSetting.getInstance();
    gwtSession.setVersion(commonsConfig.getString(SystemSettingKey.VERSION));
    gwtSession.setBuildVersion(commonsConfig.getString(SystemSettingKey.BUILD_VERSION));
    gwtSession.setBuildNumber(commonsConfig.getString(SystemSettingKey.BUILD_NUMBER));
    // User info
    gwtSession.setGwtUser(gwtUser);
    gwtSession.setGwtAccount(gwtAccount);
    gwtSession.setRootAccount(gwtAccount);
    gwtSession.setSelectedAccount(gwtAccount);
    // Permission info
    gwtSession.setAccountCreatePermission(hasAccountCreate);
    gwtSession.setAccountReadPermission(hasAccountRead);
    gwtSession.setAccountUpdatePermission(hasAccountUpdate);
    gwtSession.setAccountDeletePermission(hasAccountDelete);
    gwtSession.setAccountAllPermission(hasAccountAll);
    gwtSession.setDeviceCreatePermission(hasDeviceCreate);
    gwtSession.setDeviceReadPermission(hasDeviceRead);
    gwtSession.setDeviceUpdatePermission(hasDeviceUpdate);
    gwtSession.setDeviceDeletePermission(hasDeviceDelete);
    gwtSession.setDeviceManagePermission(hasDeviceManage);
    gwtSession.setDataReadPermission(hasDataRead);
    gwtSession.setUserCreatePermission(hasUserCreate);
    gwtSession.setUserReadPermission(hasUserRead);
    gwtSession.setUserUpdatePermission(hasUserUpdate);
    gwtSession.setUserDeletePermission(hasUserDelete);
    return gwtSession;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) Account(org.eclipse.kapua.service.account.Account) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) User(org.eclipse.kapua.service.user.User) UserService(org.eclipse.kapua.service.user.UserService) KapuaSession(org.eclipse.kapua.commons.security.KapuaSession) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) GwtAccount(org.eclipse.kapua.app.console.shared.model.GwtAccount) SystemSetting(org.eclipse.kapua.commons.setting.system.SystemSetting) GwtSession(org.eclipse.kapua.app.console.shared.model.GwtSession) GwtAuthorizationService(org.eclipse.kapua.app.console.shared.service.GwtAuthorizationService) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) GwtUser(org.eclipse.kapua.app.console.shared.model.GwtUser) AccountService(org.eclipse.kapua.service.account.AccountService)

Example 3 with SystemSetting

use of org.eclipse.kapua.commons.setting.system.SystemSetting 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 4 with SystemSetting

use of org.eclipse.kapua.commons.setting.system.SystemSetting in project kapua by eclipse.

the class MariaDBJdbcConnectionUrlResolver method connectionUrl.

@Override
public String connectionUrl() {
    SystemSetting config = SystemSetting.getInstance();
    // Mandatory connection parameters
    String dbName = config.getString(SystemSettingKey.DB_NAME);
    String dbConnectionScheme = config.getString(SystemSettingKey.DB_CONNECTION_SCHEME);
    String dbConnectionHost = config.getString(SystemSettingKey.DB_CONNECTION_HOST);
    String dbConnectionPort = config.getString(SystemSettingKey.DB_CONNECTION_PORT);
    StringBuilder dbConnectionString = new StringBuilder().append(dbConnectionScheme).append("://").append(dbConnectionHost).append(":").append(dbConnectionPort).append("/").append(dbName).append("?");
    // Optional connection parameters
    String useTimezone = config.getString(SystemSettingKey.DB_USE_TIMEZIONE);
    if (useTimezone != null) {
        dbConnectionString.append("useTimezone=").append(useTimezone).append("&");
    }
    String useLegacyDatetimeCode = config.getString(SystemSettingKey.DB_USE_LEGACY_DATETIME_CODE);
    if (useLegacyDatetimeCode != null) {
        dbConnectionString.append("useLegacyDatetimeCode=").append(useLegacyDatetimeCode).append("&");
    }
    String serverTimezone = config.getString(SystemSettingKey.DB_SERVER_TIMEZONE);
    if (serverTimezone != null) {
        dbConnectionString.append("serverTimezone=").append(serverTimezone).append("&");
    }
    String characterEncoding = config.getString(SystemSettingKey.DB_CHAR_ENCODING);
    if (characterEncoding != null) {
        dbConnectionString.append("characterEncoding=").append(characterEncoding).append("&");
    }
    // This deletes the trailing '?' or '&'
    dbConnectionString.deleteCharAt(dbConnectionString.length() - 1);
    return dbConnectionString.toString();
}
Also used : SystemSetting(org.eclipse.kapua.commons.setting.system.SystemSetting)

Aggregations

SystemSetting (org.eclipse.kapua.commons.setting.system.SystemSetting)4 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)2 Account (org.eclipse.kapua.service.account.Account)2 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)2 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)2 KapuaEntityNotFoundException (org.eclipse.kapua.KapuaEntityNotFoundException)1 KapuaException (org.eclipse.kapua.KapuaException)1 KapuaIllegalAccessException (org.eclipse.kapua.KapuaIllegalAccessException)1 KapuaIllegalArgumentException (org.eclipse.kapua.KapuaIllegalArgumentException)1 GwtAccount (org.eclipse.kapua.app.console.shared.model.GwtAccount)1 GwtSession (org.eclipse.kapua.app.console.shared.model.GwtSession)1 GwtUser (org.eclipse.kapua.app.console.shared.model.GwtUser)1 GwtAuthorizationService (org.eclipse.kapua.app.console.shared.service.GwtAuthorizationService)1 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)1 KapuaSession (org.eclipse.kapua.commons.security.KapuaSession)1 AccountService (org.eclipse.kapua.service.account.AccountService)1 Actions (org.eclipse.kapua.service.authorization.permission.Actions)1 User (org.eclipse.kapua.service.user.User)1 UserService (org.eclipse.kapua.service.user.UserService)1