Search in sources :

Example 11 with Permission

use of com.haulmont.cuba.security.entity.Permission in project cuba by cuba-platform.

the class EntityPermissionTargetsDatasource method loadData.

@Override
protected void loadData(Map<String, Object> params) {
    if (permissionDs == null)
        return;
    if (targets == null) {
        targets = new ArrayList<>();
        PermissionConfig permissionConfig = AppBeans.get(PermissionConfig.class);
        List<OperationPermissionTarget> entities = permissionConfig.getEntities(userSessionSource.getLocale());
        for (OperationPermissionTarget target : entities) {
            try {
                OperationPermissionTarget cloneTarget = target.clone();
                loadPermissionVariants(cloneTarget);
                attachListener(cloneTarget);
                targets.add(cloneTarget);
            } catch (CloneNotSupportedException e) {
                throw new RuntimeException("Can't clone permission target", e);
            }
        }
    }
    data.clear();
    targets.stream().filter(permittedEntityFilter).filter(t -> permissionsFilter == null || permissionsFilter.test(t)).forEach(t -> data.put(t.getId(), t));
}
Also used : OperationPermissionTarget(com.haulmont.cuba.gui.app.security.entity.OperationPermissionTarget) PermissionVariant(com.haulmont.cuba.gui.app.security.entity.PermissionVariant) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Predicate(java.util.function.Predicate) CollectionDatasourceImpl(com.haulmont.cuba.gui.data.impl.CollectionDatasourceImpl) AppBeans(com.haulmont.cuba.core.global.AppBeans) UserSessionSource(com.haulmont.cuba.core.global.UserSessionSource) MetaClass(com.haulmont.chile.core.model.MetaClass) PermissionValue(com.haulmont.cuba.gui.app.security.role.edit.PermissionValue) UserSession(com.haulmont.cuba.security.global.UserSession) Permission(com.haulmont.cuba.security.entity.Permission) EntityOp(com.haulmont.cuba.security.entity.EntityOp) PermissionConfig(com.haulmont.cuba.gui.config.PermissionConfig) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) PermissionConfig(com.haulmont.cuba.gui.config.PermissionConfig) OperationPermissionTarget(com.haulmont.cuba.gui.app.security.entity.OperationPermissionTarget)

Example 12 with Permission

use of com.haulmont.cuba.security.entity.Permission in project cuba by cuba-platform.

the class UiPermissionsDatasource method loadData.

@Override
protected void loadData(Map<String, Object> params) {
    if (permissionDs == null)
        return;
    if (!data.isEmpty())
        return;
    clear();
    for (Permission p : permissionDs.getItems()) {
        String permissionTarget = p.getTarget();
        if (StringUtils.isNotEmpty(permissionTarget)) {
            int delimeterIndex = permissionTarget.lastIndexOf(Permission.TARGET_PATH_DELIMETER);
            if (delimeterIndex >= 0) {
                String component = permissionTarget.substring(delimeterIndex + 1);
                String screen = permissionTarget.substring(0, delimeterIndex);
                UiPermissionVariant permissionVariant = getPermissionVariant(p);
                String permissionValue = screen + Permission.TARGET_PATH_DELIMETER + component;
                UiPermissionTarget target = new UiPermissionTarget("ui:" + permissionValue, permissionValue, permissionValue);
                target.setPermissionVariant(permissionVariant);
                target.setComponent(component);
                target.setScreen(screen);
                includeItem(target);
            }
        }
    }
}
Also used : UiPermissionVariant(com.haulmont.cuba.gui.app.security.entity.UiPermissionVariant) Permission(com.haulmont.cuba.security.entity.Permission) UiPermissionTarget(com.haulmont.cuba.gui.app.security.entity.UiPermissionTarget)

Example 13 with Permission

use of com.haulmont.cuba.security.entity.Permission in project cuba by cuba-platform.

the class PermissionUiHelper method createPermissionItem.

/**
 * Add or edit permission item in datasource
 * @param ds Datasource
 * @param roleDs Role datasource
 * @param permissionTarget Permission identifier
 * @param type Permission type
 * @param value Permission value
 */
public static void createPermissionItem(CollectionDatasource<Permission, UUID> ds, Datasource<Role> roleDs, final String permissionTarget, PermissionType type, Integer value) {
    Permission permission = null;
    for (Permission p : ds.getItems()) {
        if (Objects.equals(p.getTarget(), permissionTarget)) {
            permission = p;
            break;
        }
    }
    if (permission == null) {
        // restore entity instead of create
        if (ds instanceof RestorablePermissionDatasource) {
            RestorablePermissionDatasource datasource = (RestorablePermissionDatasource) ds;
            permission = datasource.findRemovedEntity(p -> p != null && Objects.equals(p.getTarget(), permissionTarget));
            if (permission != null) {
                datasource.restoreEntity(permission);
            }
        }
    }
    if (permission == null) {
        Metadata metadata = AppBeans.get(Metadata.NAME);
        Permission newPermission = metadata.create(Permission.class);
        newPermission.setRole(roleDs.getItem());
        newPermission.setTarget(permissionTarget);
        newPermission.setType(type);
        newPermission.setValue(value);
        ds.addItem(newPermission);
    } else {
        permission.setValue(value);
    }
}
Also used : RestorablePermissionDatasource(com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource) PermissionVariant(com.haulmont.cuba.gui.app.security.entity.PermissionVariant) AttributePermissionVariant(com.haulmont.cuba.gui.app.security.entity.AttributePermissionVariant) Datasource(com.haulmont.cuba.gui.data.Datasource) PermissionType(com.haulmont.cuba.security.entity.PermissionType) RestorablePermissionDatasource(com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource) UUID(java.util.UUID) AppBeans(com.haulmont.cuba.core.global.AppBeans) Metadata(com.haulmont.cuba.core.global.Metadata) Objects(java.util.Objects) UiPermissionVariant(com.haulmont.cuba.gui.app.security.entity.UiPermissionVariant) Permission(com.haulmont.cuba.security.entity.Permission) Role(com.haulmont.cuba.security.entity.Role) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Permission(com.haulmont.cuba.security.entity.Permission) Metadata(com.haulmont.cuba.core.global.Metadata)

Example 14 with Permission

use of com.haulmont.cuba.security.entity.Permission in project cuba by cuba-platform.

the class EntityPermissionsFrame method markTargetPermission.

protected void markTargetPermission(OperationPermissionTarget target, String property, EntityOp operation, PermissionVariant permissionVariant) {
    if (target != null) {
        target.setValue(property, permissionVariant);
        String permissionValue = target.getPermissionValue() + Permission.TARGET_PATH_DELIMETER + operation.getId();
        if (permissionVariant != PermissionVariant.NOTSET) {
            // Create permission
            int value = PermissionUiHelper.getPermissionValue(permissionVariant);
            PermissionUiHelper.createPermissionItem(entityPermissionsDs, roleDs, permissionValue, PermissionType.ENTITY_OP, value);
        } else {
            // Remove permission
            Permission permission = null;
            for (Permission p : entityPermissionsDs.getItems()) {
                if (Objects.equals(p.getTarget(), permissionValue)) {
                    permission = p;
                    break;
                }
            }
            if (permission != null)
                entityPermissionsDs.removeItem(permission);
        }
    }
}
Also used : Permission(com.haulmont.cuba.security.entity.Permission)

Example 15 with Permission

use of com.haulmont.cuba.security.entity.Permission in project cuba by cuba-platform.

the class UpdateDetachedTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Role role = new Role();
        roleId = role.getId();
        role.setName("testRole");
        em.persist(role);
        Role role2 = new Role();
        role2Id = role2.getId();
        role2.setName("testRole2");
        em.persist(role2);
        Permission permission = new Permission();
        permissionId = permission.getId();
        permission.setRole(role);
        permission.setType(PermissionType.SCREEN);
        permission.setTarget("testTarget");
        em.persist(permission);
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) Permission(com.haulmont.cuba.security.entity.Permission) Before(org.junit.Before)

Aggregations

Permission (com.haulmont.cuba.security.entity.Permission)15 Role (com.haulmont.cuba.security.entity.Role)4 PermissionVariant (com.haulmont.cuba.gui.app.security.entity.PermissionVariant)3 Test (org.junit.Test)3 AppBeans (com.haulmont.cuba.core.global.AppBeans)2 BasicPermissionTarget (com.haulmont.cuba.gui.app.security.entity.BasicPermissionTarget)2 UiPermissionVariant (com.haulmont.cuba.gui.app.security.entity.UiPermissionVariant)2 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)2 MetaClass (com.haulmont.chile.core.model.MetaClass)1 DataService (com.haulmont.cuba.core.app.DataService)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 View (com.haulmont.cuba.core.global.View)1 RestorablePermissionDatasource (com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource)1 AttributePermissionVariant (com.haulmont.cuba.gui.app.security.entity.AttributePermissionVariant)1 OperationPermissionTarget (com.haulmont.cuba.gui.app.security.entity.OperationPermissionTarget)1 UiPermissionTarget (com.haulmont.cuba.gui.app.security.entity.UiPermissionTarget)1 PermissionValue (com.haulmont.cuba.gui.app.security.role.edit.PermissionValue)1 PermissionConfig (com.haulmont.cuba.gui.config.PermissionConfig)1