use of io.jmix.core.security.AccessDeniedException in project jmix by jmix-framework.
the class EntityLogBrowser method onInstancePickerLookup.
@Subscribe("instancePicker.lookup")
public void onInstancePickerLookup(Action.ActionPerformedEvent event) {
final MetaClass metaClass = instancePicker.getMetaClass();
if (instancePicker.isEditable()) {
if (metaClass == null) {
throw new IllegalStateException("Please specify metaclass or property for PickerField");
}
if (!secureOperations.isEntityReadPermitted(metaClass, policyStore)) {
notifications.create(Notifications.NotificationType.ERROR).withCaption(messages.getMessage(EntityLogBrowser.class, "entityAccessDeniedMessage")).show();
return;
}
try {
Screen lookup = screenBuilders.lookup(instancePicker).withSelectHandler(items -> {
if (!items.isEmpty()) {
Object item = items.iterator().next();
instancePicker.setValue(item);
}
}).build();
lookup.addAfterCloseListener(afterCloseEvent -> instancePicker.focus());
lookup.show();
} catch (AccessDeniedException ex) {
notifications.create(Notifications.NotificationType.ERROR).withCaption(messages.getMessage(EntityLogBrowser.class, "entityScreenAccessDeniedMessage")).show();
return;
}
}
}
use of io.jmix.core.security.AccessDeniedException in project jmix by jmix-framework.
the class FoldersServiceBean method importFolder.
@Override
public Folder importFolder(Folder parentFolder, byte[] bytes) throws IOException {
if (!security.isEntityOpPermitted(Folder.class, EntityOp.CREATE)) {
throw new AccessDeniedException("entity", Folder.class.getSimpleName(), "create");
}
Folder folder = null;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
ZipArchiveInputStream archiveReader;
archiveReader = new ZipArchiveInputStream(byteArrayInputStream);
ZipArchiveEntry archiveEntry;
while (((archiveEntry = archiveReader.getNextZipEntry()) != null) && (folder == null)) {
if (archiveEntry.getName().equals("folder.xml")) {
String xml = new String(IOUtils.toByteArray(archiveReader), StandardCharsets.UTF_8);
folder = (Folder) createXStream().fromXML(xml);
}
}
byteArrayInputStream.close();
if (folder != null) {
if (folder.equals(parentFolder)) {
throw new RuntimeException("Cannot import the folder to itself. Select another parent folder.");
}
try (Transaction tx = persistence.createTransaction()) {
List<Folder> allParentFolders = findAllParentFolders(parentFolder, new ArrayList<>());
if (allParentFolders.contains(folder)) {
throw new RuntimeException("Cannot import the folder. The imported folder is found among ancestors of the target parent folder. " + "Select another parent folder.");
}
tx.commit();
}
// all parent folders starting with the target parent folder
checkImportPermissions(folder);
folder.setParent(parentFolder);
Transaction tx = persistence.createTransaction();
try {
EntityManager em = persistence.getEntityManager();
em.setSoftDeletion(false);
Folder existingFolder = em.find(Folder.class, folder.getId());
if (existingFolder != null) {
checkImportPermissions(existingFolder);
folder.setVersion(existingFolder.getVersion());
folder.setCreateTs(existingFolder.getCreateTs());
folder.setCreatedBy(existingFolder.getCreatedBy());
} else {
UserDetails user = userSessionSource.getUserSession().getUser();
folder.setCreatedBy(user.getUsername().toLowerCase());
folder.setCreateTs(timeSource.currentTimestamp());
folder.setUpdatedBy(null);
folder.setUpdateTs(null);
folder.setVersion(0);
}
em.merge(folder);
tx.commit();
} finally {
tx.end();
}
}
return folder;
}
use of io.jmix.core.security.AccessDeniedException in project jmix by jmix-framework.
the class DataStoreCrudListener method beforeEntitySave.
@Override
public void beforeEntitySave(DataStoreBeforeEntitySaveEvent event) {
SaveContext context = event.getSaveContext();
Collection<AccessConstraint<?>> accessConstraints = context.getAccessConstraints();
if (accessConstraints.isEmpty()) {
return;
}
Map<MetaClass, CrudEntityContext> accessCache = new HashMap<>();
for (Object entity : context.getEntitiesToSave()) {
if (entity == null) {
continue;
}
MetaClass metaClass = metadata.getClass(entity);
CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
if (entityStates.isNew(entity)) {
if (!entityContext.isCreatePermitted()) {
throw new AccessDeniedException("entity", metaClass.getName(), "create");
}
} else if (!entityContext.isUpdatePermitted()) {
throw new AccessDeniedException("entity", metaClass.getName(), "update");
}
}
for (Object entity : context.getEntitiesToRemove()) {
if (entity == null) {
continue;
}
MetaClass metaClass = metadata.getClass(entity);
CrudEntityContext entityContext = accessCache.computeIfAbsent(metaClass, key -> evaluateCrudAccess(key, accessConstraints));
if (!entityContext.isDeletePermitted()) {
throw new AccessDeniedException("entity", metaClass.getName(), "update");
}
}
}
use of io.jmix.core.security.AccessDeniedException in project jmix by jmix-framework.
the class DataStoreInMemoryCrudListener method entitySaving.
@Override
public void entitySaving(DataStoreEntitySavingEvent event) {
SaveContext context = event.getSaveContext();
for (Object entity : event.getEntities()) {
MetaClass metaClass = metadata.getClass(entity);
InMemoryCrudEntityContext entityContext = new InMemoryCrudEntityContext(metaClass, applicationContext);
accessManager.applyConstraints(entityContext, context.getAccessConstraints());
if (isNew(context, entity)) {
if (!entityContext.isCreatePermitted(entity)) {
throw new AccessDeniedException("entity", entity.toString(), "create");
}
} else {
if (!entityContext.isUpdatePermitted(entity)) {
throw new AccessDeniedException("entity", entity.toString(), "update");
}
}
}
}
use of io.jmix.core.security.AccessDeniedException in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method checkPermission.
protected void checkPermission() {
Security security = AppBeans.get(Security.NAME);
MetaClass parentMetaClass = masterDs.getMetaClass();
if (!security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.MODIFY)) {
throw new AccessDeniedException("attribute", parentMetaClass + "." + metaProperty.getName());
}
}
Aggregations