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