Skip to content

Commit

Permalink
PO-449: Add redis cache for permissions/userState (#415)
Browse files Browse the repository at this point in the history
* PO-449: Add redis cache for permissions/userState

* PO-449: Add redis cache for permissions/userState
  • Loading branch information
sabahirfan authored Jul 2, 2024
1 parent 0d89efa commit 6eefa9b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.hmcts.opal.authorisation.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.NonNull;
import lombok.Value;
Expand All @@ -14,6 +16,13 @@ public class Permission {
@NonNull
String permissionName;

@JsonCreator
public Permission(@JsonProperty("permissionId") Long permissionId,
@JsonProperty("permissionName") String permissionName) {
this.permissionId = permissionId;
this.permissionName = permissionName;
}

boolean matches(Permissions candidate) {
return candidate.id == permissionId;
}
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/uk/gov/hmcts/opal/authorisation/model/Role.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package uk.gov.hmcts.opal.authorisation.model;

import lombok.AccessLevel;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import lombok.experimental.FieldDefaults;

import java.util.Collections;
import java.util.Set;

@Builder
@EqualsAndHashCode
@Getter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@ToString
@Data
public class Role {

@NonNull
Expand All @@ -28,6 +24,16 @@ public class Role {
@NonNull
Set<Permission> permissions;

@JsonCreator
public Role(@JsonProperty("businessUserId") String businessUserId,
@JsonProperty("businessUnitId") Short businessUnitId,
@JsonProperty("permissions") Set<Permission> permissions) {

this.businessUserId = businessUserId;
this.businessUnitId = businessUnitId;
this.permissions = permissions;
}

public boolean hasPermission(Permissions permission) {
return permissions.stream().anyMatch(p -> p.matches(permission));
}
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/uk/gov/hmcts/opal/authorisation/model/UserState.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package uk.gov.hmcts.opal.authorisation.model;

import lombok.AccessLevel;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import lombok.experimental.FieldDefaults;
import uk.gov.hmcts.opal.authorisation.model.Role.DeveloperRole;

import java.util.Collections;
Expand All @@ -15,10 +14,7 @@
import java.util.stream.Collectors;

@Builder
@EqualsAndHashCode
@Getter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@ToString
@Data
public class UserState {

@NonNull
Expand All @@ -30,6 +26,17 @@ public class UserState {
@EqualsAndHashCode.Exclude
Set<Role> roles;

@JsonCreator
public UserState(
@JsonProperty("userId") Long userId,
@JsonProperty("userName") String userName,
@JsonProperty("roles") Set<Role> roles
) {
this.userId = userId;
this.userName = userName;
this.roles = roles;
}

public boolean anyRoleHasPermission(Permissions permission) {
return roles.stream().anyMatch(r -> r.hasPermission(permission));
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/service/opal/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -49,6 +50,7 @@ public List<UserEntity> searchUsers(UserSearchDto criteria) {
* in the UserEntitlementService, but will still return a UserState even if no Entitlements exist for that user,
* but the User <i>does</i> exist in the table.
*/
@Cacheable(cacheNames = "users", key = "#username")
public UserState getUserStateByUsername(String username) {
UserEntity user = userRepository.findByUsername(username);
return UserState.builder()
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ server:
management:
health:
redis:
enabled: false
enabled: ${opal.redis.enabled:false}
endpoint:
health:
show-details: "always"
Expand Down

0 comments on commit 6eefa9b

Please sign in to comment.