Search in sources :

Example 1 with RestorablePermissionDatasource

use of com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource 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)

Aggregations

AppBeans (com.haulmont.cuba.core.global.AppBeans)1 Metadata (com.haulmont.cuba.core.global.Metadata)1 RestorablePermissionDatasource (com.haulmont.cuba.gui.app.security.ds.RestorablePermissionDatasource)1 AttributePermissionVariant (com.haulmont.cuba.gui.app.security.entity.AttributePermissionVariant)1 PermissionVariant (com.haulmont.cuba.gui.app.security.entity.PermissionVariant)1 UiPermissionVariant (com.haulmont.cuba.gui.app.security.entity.UiPermissionVariant)1 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)1 Datasource (com.haulmont.cuba.gui.data.Datasource)1 Permission (com.haulmont.cuba.security.entity.Permission)1 PermissionType (com.haulmont.cuba.security.entity.PermissionType)1 Role (com.haulmont.cuba.security.entity.Role)1 Objects (java.util.Objects)1 UUID (java.util.UUID)1