-
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #18 from penguineer/role
Add system roles
- Loading branch information
Showing
9 changed files
with
248 additions
and
16 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/com/penguineering/gartenplus/auth/role/RoleMapping.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,29 @@ | ||
package com.penguineering.gartenplus.auth.role; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.UUID; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(of = {"roleHandle", "userId"}) | ||
@IdClass(RoleMappingKey.class) | ||
@Table(name = "role_mapping") | ||
public class RoleMapping { | ||
@Id | ||
@Column(name = "role_handle") | ||
private String roleHandle; | ||
|
||
@Id | ||
@Column(name = "user_id") | ||
private UUID userId; | ||
|
||
@Override | ||
public String toString() { | ||
return roleHandle + "/" + userId; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/penguineering/gartenplus/auth/role/RoleMappingKey.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,34 @@ | ||
package com.penguineering.gartenplus.auth.role; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.web.server.WebSession; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@EqualsAndHashCode | ||
public class RoleMappingKey implements Serializable { | ||
private String roleHandle; | ||
private UUID userId; | ||
|
||
public static RoleMappingKey of(String roleHandle, UUID userId) { | ||
return new RoleMappingKey(roleHandle, userId); | ||
} | ||
|
||
public RoleMappingKey(String roleHandle, UUID userId) { | ||
this.roleHandle = roleHandle; | ||
this.userId = userId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return roleHandle + "/" + userId; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/penguineering/gartenplus/auth/role/RoleMappingRepository.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,10 @@ | ||
package com.penguineering.gartenplus.auth.role; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public interface RoleMappingRepository extends CrudRepository<RoleMapping, RoleMappingKey> { | ||
List<RoleMapping> findByUserId(UUID userId); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/penguineering/gartenplus/auth/role/SystemRole.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,18 @@ | ||
package com.penguineering.gartenplus.auth.role; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum SystemRole { | ||
ADMINISTRATOR("administrator", "Administrator"), | ||
MEMBER("member", "Gartenfreund"), | ||
FRIEND("friend", "Gartenfreund-Freund"); | ||
|
||
private final String handle; | ||
private final String displayName; | ||
|
||
SystemRole(String handle, String displayName) { | ||
this.handle = handle; | ||
this.displayName = displayName; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/penguineering/gartenplus/auth/role/SystemRoleService.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,73 @@ | ||
package com.penguineering.gartenplus.auth.role; | ||
|
||
import com.penguineering.gartenplus.auth.user.UserRepository; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class SystemRoleService { | ||
private static final Map<String, SystemRole> HANDLES; | ||
|
||
static { | ||
HANDLES = Arrays.stream(SystemRole.values()) | ||
.map(role -> Map.entry(role.getHandle(), role)) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
} | ||
|
||
private final RoleMappingRepository roleMappingRepository; | ||
private final UserRepository userRepository; | ||
|
||
@PersistenceContext | ||
private EntityManager entityManager; | ||
|
||
public SystemRoleService( | ||
RoleMappingRepository roleMappingRepository, | ||
UserRepository userRepository) { | ||
this.roleMappingRepository = roleMappingRepository; | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Transactional | ||
public Set<SystemRole> getRolesForUser(UUID userId) { | ||
return Optional.ofNullable(userId) | ||
.filter(userRepository::existsById) | ||
.map(roleMappingRepository::findByUserId) | ||
.map(l -> l.stream() | ||
.map(RoleMapping::getRoleHandle) | ||
// ignore unknown handles | ||
.filter(HANDLES.keySet()::contains) | ||
.map(HANDLES::get) | ||
.collect(Collectors.toSet())) | ||
.orElse(Set.of()); | ||
} | ||
|
||
@Transactional | ||
public void setRoleForUser(SystemRole role, UUID userId) { | ||
var mapping = new RoleMapping(role.getHandle(), userId); | ||
roleMappingRepository.save(mapping); | ||
} | ||
|
||
@Transactional | ||
public void clearRoleForUser(SystemRole role, UUID userId) { | ||
var mapping = new RoleMapping(role.getHandle(), userId); | ||
roleMappingRepository.delete(mapping); | ||
} | ||
|
||
@Transactional | ||
public void updateRoles(UUID userId, Set<SystemRole> roles) { | ||
var currentRoles = getRolesForUser(userId); | ||
|
||
var toAdd = new HashSet<>(roles); | ||
toAdd.removeAll(currentRoles); | ||
toAdd.forEach(role -> setRoleForUser(role, userId)); | ||
|
||
var toRemove = new HashSet<>(currentRoles); | ||
toRemove.removeAll(roles); | ||
toRemove.forEach(role -> clearRoleForUser(role, userId)); | ||
} | ||
} |
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
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
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
Oops, something went wrong.