Search in sources :

Example 1 with AttributePredicate

use of org.eclipse.kapua.commons.model.query.predicate.AttributePredicate 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;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 2 with AttributePredicate

use of org.eclipse.kapua.commons.model.query.predicate.AttributePredicate 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);
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) AndPredicate(org.eclipse.kapua.commons.model.query.predicate.AndPredicate) Properties(java.util.Properties) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) EntityManager(org.eclipse.kapua.commons.jpa.EntityManager) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) KapuaId(org.eclipse.kapua.model.id.KapuaId) KapuaTocd(org.eclipse.kapua.model.config.metatype.KapuaTocd)

Example 3 with AttributePredicate

use of org.eclipse.kapua.commons.model.query.predicate.AttributePredicate in project kapua by eclipse.

the class KapuaAuthorizingRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) throws AuthenticationException {
    // 
    // Extract principal
    String username = (String) principals.getPrimaryPrincipal();
    logger.debug("Getting authorization info for: {}", username);
    // 
    // Get Services
    KapuaLocator locator = KapuaLocator.getInstance();
    UserService userService = locator.getService(UserService.class);
    UserPermissionService userPermissionService = locator.getService(UserPermissionService.class);
    UserPermissionFactory userPermissionFactory = locator.getFactory(UserPermissionFactory.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    // 
    // Get the associated user by name
    final User user;
    try {
        user = KapuaSecurityUtils.doPriviledge(new Callable<User>() {

            @Override
            public User call() throws Exception {
                return userService.findByName(username);
            }
        });
    } catch (Exception e) {
        // to preserve the original exception message (if possible)
        if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        } else {
            throw new ShiroException("Error while find user!", e);
        }
    }
    // Check existence
    if (user == null) {
        throw new UnknownAccountException();
    }
    // 
    // Get user permissions set
    UserPermissionQuery query = userPermissionFactory.newQuery(user.getScopeId());
    KapuaPredicate predicate = new AttributePredicate<KapuaId>(UserPermissionPredicates.USER_ID, user.getId());
    query.setPredicate(predicate);
    final KapuaListResult<UserPermission> userPermissions;
    try {
        userPermissions = KapuaSecurityUtils.doPriviledge(new Callable<KapuaListResult<UserPermission>>() {

            @Override
            public KapuaListResult<UserPermission> call() throws Exception {
                return userPermissionService.query(query);
            }
        });
    } catch (Exception e) {
        // to preserve the original exception message (if possible)
        if (e instanceof AuthenticationException) {
            throw (AuthenticationException) e;
        } else {
            throw new ShiroException("Error while find permissions!", e);
        }
    }
    // 
    // Create SimpleAuthorizationInfo with principals permissions
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    for (UserPermission userPermission : userPermissions.getItems()) {
        Permission p = permissionFactory.newPermission(userPermission.getPermission().getDomain(), userPermission.getPermission().getAction(), userPermission.getPermission().getTargetScopeId());
        logger.trace("Username: {} has permission: {}", username, p);
        info.addStringPermission(p.toString());
    }
    return info;
}
Also used : KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) User(org.eclipse.kapua.service.user.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) UserService(org.eclipse.kapua.service.user.UserService) AuthenticationException(org.apache.shiro.authc.AuthenticationException) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) Callable(java.util.concurrent.Callable) ShiroException(org.apache.shiro.ShiroException) AuthenticationException(org.apache.shiro.authc.AuthenticationException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) KapuaException(org.eclipse.kapua.KapuaException) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate) ShiroException(org.apache.shiro.ShiroException) UserPermissionQuery(org.eclipse.kapua.service.authorization.user.permission.UserPermissionQuery) UserPermission(org.eclipse.kapua.service.authorization.user.permission.UserPermission) Permission(org.eclipse.kapua.service.authorization.permission.Permission) UserPermissionService(org.eclipse.kapua.service.authorization.user.permission.UserPermissionService) UserPermissionFactory(org.eclipse.kapua.service.authorization.user.permission.UserPermissionFactory) KapuaPredicate(org.eclipse.kapua.model.query.predicate.KapuaPredicate) UserPermission(org.eclipse.kapua.service.authorization.user.permission.UserPermission)

Example 4 with AttributePredicate

use of org.eclipse.kapua.commons.model.query.predicate.AttributePredicate in project kapua by eclipse.

the class DeviceConnectionServiceImpl method findByClientId.

@Override
public DeviceConnection findByClientId(KapuaId scopeId, String clientId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notEmptyOrNull(clientId, "clientId");
    // 
    // Build query
    DeviceConnectionQueryImpl query = new DeviceConnectionQueryImpl(scopeId);
    KapuaPredicate predicate = new AttributePredicate<String>(DeviceConnectionPredicates.CLIENT_ID, clientId);
    query.setPredicate(predicate);
    // 
    // Query and parse result
    DeviceConnection device = null;
    DeviceConnectionListResult result = query(query);
    if (result.getSize() == 1) {
        device = result.getItem(0);
    }
    return device;
}
Also used : DeviceConnectionListResult(org.eclipse.kapua.service.device.registry.connection.DeviceConnectionListResult) DeviceConnection(org.eclipse.kapua.service.device.registry.connection.DeviceConnection) KapuaPredicate(org.eclipse.kapua.model.query.predicate.KapuaPredicate) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)

Example 5 with AttributePredicate

use of org.eclipse.kapua.commons.model.query.predicate.AttributePredicate in project kapua by eclipse.

the class CredentialServiceImpl method findByUserId.

@Override
public CredentialListResult findByUserId(KapuaId scopeId, KapuaId userId) throws KapuaException {
    // 
    // Argument Validation
    ArgumentValidator.notNull(scopeId, "scopeId");
    ArgumentValidator.notNull(userId, "userId");
    // 
    // Check Access
    KapuaLocator locator = KapuaLocator.getInstance();
    AuthorizationService authorizationService = locator.getService(AuthorizationService.class);
    PermissionFactory permissionFactory = locator.getFactory(PermissionFactory.class);
    authorizationService.checkPermission(permissionFactory.newPermission(CredentialDomain.CREDENTIAL, Actions.read, scopeId));
    // 
    // Build query
    CredentialQuery query = new CredentialQueryImpl(scopeId);
    KapuaPredicate predicate = new AttributePredicate<KapuaId>(CredentialPredicates.USER_ID, userId);
    query.setPredicate(predicate);
    // Query and return result
    return query(query);
}
Also used : CredentialQuery(org.eclipse.kapua.service.authentication.credential.CredentialQuery) KapuaLocator(org.eclipse.kapua.locator.KapuaLocator) AuthorizationService(org.eclipse.kapua.service.authorization.AuthorizationService) PermissionFactory(org.eclipse.kapua.service.authorization.permission.PermissionFactory) KapuaPredicate(org.eclipse.kapua.model.query.predicate.KapuaPredicate) AttributePredicate(org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)

Aggregations

AttributePredicate (org.eclipse.kapua.commons.model.query.predicate.AttributePredicate)10 KapuaLocator (org.eclipse.kapua.locator.KapuaLocator)7 AndPredicate (org.eclipse.kapua.commons.model.query.predicate.AndPredicate)6 KapuaId (org.eclipse.kapua.model.id.KapuaId)4 KapuaPredicate (org.eclipse.kapua.model.query.predicate.KapuaPredicate)4 PermissionFactory (org.eclipse.kapua.service.authorization.permission.PermissionFactory)4 FieldSortCriteria (org.eclipse.kapua.commons.model.query.FieldSortCriteria)3 KapuaAndPredicate (org.eclipse.kapua.model.query.predicate.KapuaAndPredicate)3 AuthorizationService (org.eclipse.kapua.service.authorization.AuthorizationService)3 Device (org.eclipse.kapua.service.device.registry.Device)3 DeviceConnection (org.eclipse.kapua.service.device.registry.connection.DeviceConnection)3 Properties (java.util.Properties)2 GwtDevice (org.eclipse.kapua.app.console.shared.model.GwtDevice)2 EntityManager (org.eclipse.kapua.commons.jpa.EntityManager)2 SortOrder (org.eclipse.kapua.commons.model.query.FieldSortCriteria.SortOrder)2 KapuaTocd (org.eclipse.kapua.model.config.metatype.KapuaTocd)2 DeviceFactory (org.eclipse.kapua.service.device.registry.DeviceFactory)2 DeviceQuery (org.eclipse.kapua.service.device.registry.DeviceQuery)2 DeviceRegistryService (org.eclipse.kapua.service.device.registry.DeviceRegistryService)2 DeviceConnectionService (org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService)2