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