-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from UnityFoundation-io/user-crud
Add endpoints to accomodate crud actions for users
- Loading branch information
Showing
13 changed files
with
690 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
UnityAuth/src/main/java/io/unityfoundation/auth/NullOrNotBlank.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.unityfoundation.auth; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
@Target({ElementType.FIELD}) | ||
@Retention(RUNTIME) | ||
@Documented | ||
@Constraint(validatedBy = NullOrNotBlankValidator.class) | ||
public @interface NullOrNotBlank { | ||
String message() default "{javax.validation.constraints.NullOrNotBlank.message}"; | ||
Class<?>[] groups() default { }; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
12 changes: 12 additions & 0 deletions
12
UnityAuth/src/main/java/io/unityfoundation/auth/NullOrNotBlankValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.unityfoundation.auth; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
|
||
public class NullOrNotBlankValidator implements ConstraintValidator<NullOrNotBlank, String> { | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
return value == null || !value.trim().isEmpty(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
UnityAuth/src/main/java/io/unityfoundation/auth/PermissionsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.unityfoundation.auth; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
import io.unityfoundation.auth.entities.Permission; | ||
import io.unityfoundation.auth.entities.Tenant; | ||
import io.unityfoundation.auth.entities.User; | ||
import io.unityfoundation.auth.entities.UserRepo; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.util.List; | ||
import java.util.function.BiPredicate; | ||
import java.util.function.Predicate; | ||
|
||
@Singleton | ||
public class PermissionsService { | ||
|
||
private final UserRepo userRepo; | ||
|
||
private final BiPredicate<TenantPermission, Tenant> isTenantOrSystemOrSubtenantScopeAndBelongsToTenant = (tp, t) -> | ||
Permission.PermissionScope.SYSTEM.equals(tp.permissionScope()) || ( | ||
(Permission.PermissionScope.TENANT.equals(tp.permissionScope()) | ||
|| Permission.PermissionScope.SUBTENANT.equals(tp.permissionScope())) | ||
&& tp.tenantId == t.getId()); | ||
|
||
private final Predicate<TenantPermission> isTenantOrSystemOrSubtenantScope = (tp) -> | ||
Permission.PermissionScope.SYSTEM.equals(tp.permissionScope()) || ( | ||
(Permission.PermissionScope.TENANT.equals(tp.permissionScope()) | ||
|| Permission.PermissionScope.SUBTENANT.equals(tp.permissionScope()))); | ||
|
||
public PermissionsService(UserRepo userRepo) { | ||
this.userRepo = userRepo; | ||
} | ||
|
||
public List<String> checkUserPermission(User user, Tenant tenant, List<String> permissions) { | ||
return getPermissionsFor(user, tenant).stream() | ||
.filter(permissions::contains).toList(); | ||
} | ||
|
||
public List<String> getPermissionsFor(User user, Tenant tenant) { | ||
return userRepo.getTenantPermissionsFor(user.getId()).stream() | ||
.filter(tenantPermission -> | ||
isTenantOrSystemOrSubtenantScopeAndBelongsToTenant.test(tenantPermission, tenant)) | ||
.map(TenantPermission::permissionName) | ||
.toList(); | ||
} | ||
|
||
public List<String> checkUserPermissionsAcrossAllTenants(User user, List<String> permissions) { | ||
return userRepo.getTenantPermissionsFor(user.getId()).stream() | ||
.filter(isTenantOrSystemOrSubtenantScope) | ||
.map(TenantPermission::permissionName) | ||
.filter(permissions::contains) | ||
.toList(); | ||
} | ||
|
||
@Introspected | ||
public record TenantPermission( | ||
long tenantId, | ||
String permissionName, | ||
Permission.PermissionScope permissionScope | ||
) {} | ||
} |
Oops, something went wrong.