From 62dc61019bf17b9ab0ad5137ba55dc079ddc18c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikail=20C=CC=A7OLAK?= Date: Wed, 9 Mar 2022 12:30:59 +0100 Subject: [PATCH 1/2] With the current version of the library, Annotation for permission model definition replaced with an interface so message updated accordingly. --- .../de/ebf/security/GranularPermissionsAutoConfiguration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/ebf/security/GranularPermissionsAutoConfiguration.java b/src/main/java/de/ebf/security/GranularPermissionsAutoConfiguration.java index b203591..a1d69c6 100644 --- a/src/main/java/de/ebf/security/GranularPermissionsAutoConfiguration.java +++ b/src/main/java/de/ebf/security/GranularPermissionsAutoConfiguration.java @@ -94,7 +94,8 @@ public PermissionModelRepository defaultPermissionModelRepository( permissionModelDefinition = permissionModelFinder.find(); } catch (NoPermissionModelFoundException e) { throw new FatalBeanException("Could not create Permission Model Definition bean. " + - "You need to define at least class with a @PermissionModel annotation", e); + "You need to define at least one `@Entity` that implements `" + + PermissionModel.class.getName() + "` interface.", e); } catch (MoreThanOnePermissionModelFoundException e) { throw new FatalBeanException("Could not create Permission Model Definition bean. " + "More than one Permission Model was found in classpath: " + e.getClassNames(), e); From 3988f388ab09c86ce71f360ed875843f866bf9ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikail=20C=CC=A7OLAK?= Date: Wed, 9 Mar 2022 12:47:26 +0100 Subject: [PATCH 2/2] New section `Fine tuning the initialization` created. With an example for how to provide customized PermissionInitializer. --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/README.md b/README.md index 7fede52..d58a935 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,64 @@ You can choose when this initialization process should occur in the app startup - `ON_REFRESH` > When the `ContextRefreshedEvent` is fired +### Fine tuning the initialization + +You can also alter the behavior of the initialization phase via providing a bean +with custom implementation of `de.ebf.security.init.PermissionInitializer` interface. + +For example by default `de.ebf.security.init.DefaultPermissionInitializer implements PermissionInitializer` +will do a cleanup for the permissions that previously persisted but no longer exists in the application source code (ex: `@Permission("foo") was part of the app but recently removed and no longer exists`). +In this case if you like to keep those permissions persisted rather than removed by DefaultPermissionInitializer, you may consider to +either implement the `PermissionInitializer` fully or just extend the `DefaultPermissionInitializer` +to override specific functions to alter the behavior according to needs of your application. + +```java +// Custom implementation would be +public class MyCustomPermissionsInitializer extends DefaultPermissionInitializer { + + public MyCustomPermissionsInitializer(PermissionModelRepository permissionModelRepository) { + super(permissionModelRepository); + } + + // ... + + // Overridden implementation will log any unused permissions + // otherwise the default implementation would try to remove them. + @Override + protected void removePermissions(@NotNull Set permissions) { + // Just logging the permissions are not in use anymore. + // But not removing from the db! + if (!CollectionUtils.isEmpty(permissions)) { + // Let us log those permissions during initialization as an information + log.info( + "Persisted permission(s) that no longer in use and can be cleaned up are listed below:\n\t{}", + permissions.stream().map(PermissionModel::getPermission).collect(Collectors.toSet()) + ); + } + } + + // ... +} + +// ... and Bean definition would be +@Configuration +@PermissionScan( + basePackageNames = { "my.package", "my.other.package" }, + basePackageClasses = Type.class +) +public class SGPConfiguration { + // ... + @Bean + public PermissionInitializer nonInvasivePermissionInitializer(ObjectProvider permissionModelRepository) { + PermissionModelRepository repository = permissionModelRepository.getIfAvailable(() -> { + throw new FatalBeanException("Bean for PermissionModelRepository not found! Granular Permission can not be initialized!"); + }); + return new MyCustomPermissionsInitializer(repository); + } + // ... +} +``` + ## Which errors/exceptions are thrown? Like with the [What does it do?](https://github.com/ebf/spring-granular-permissions#what-does-it-do) section, this is split in the same two parts: