forked from blcham/record-manager
-
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.
[OIDC] Modify role mapping from OIDC access token.
This ensures only known roles are mapped, and they are mapped correctly to types used by the record manager.
- Loading branch information
Showing
7 changed files
with
162 additions
and
18 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
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,36 @@ | ||
package cz.cvut.kbss.study.security.model; | ||
|
||
import cz.cvut.kbss.study.model.Vocabulary; | ||
import cz.cvut.kbss.study.security.SecurityConstants; | ||
|
||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
public enum Role { | ||
USER(SecurityConstants.ROLE_USER, Vocabulary.s_c_doctor), | ||
ADMIN(SecurityConstants.ROLE_ADMIN, Vocabulary.s_c_administrator); | ||
|
||
private final String name; | ||
private final String type; | ||
|
||
Role(String name, String type) { | ||
this.name = name; | ||
this.type = type; | ||
} | ||
|
||
public static Optional<Role> forType(String type) { | ||
return Stream.of(Role.values()).filter(r -> r.type.equals(type)).findAny(); | ||
} | ||
|
||
public static Optional<Role> forName(String name) { | ||
return Stream.of(Role.values()).filter(r -> r.name.equals(name)).findAny(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
src/test/java/cz/cvut/kbss/study/security/model/RoleTest.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,44 @@ | ||
package cz.cvut.kbss.study.security.model; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class RoleTest { | ||
|
||
static Stream<Arguments> generator() { | ||
return Stream.of(Role.values()).map(Arguments::of); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("generator") | ||
void forTypeReturnsRoleMatchingSpecifiedType(Role r) { | ||
final Optional<Role> result = Role.forType(r.getType()); | ||
assertTrue(result.isPresent()); | ||
assertEquals(r, result.get()); | ||
} | ||
|
||
@Test | ||
void forTypeReturnsEmptyOptionalForUnknownRoleType() { | ||
assertTrue(Role.forType("unknownType").isEmpty()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("generator") | ||
void forNameReturnsRoleMatchingSpecifiedRoleName(Role r) { | ||
final Optional<Role> result = Role.forName(r.getName()); | ||
assertTrue(result.isPresent()); | ||
assertEquals(r, result.get()); | ||
} | ||
|
||
@Test | ||
void forNameReturnsEmptyOptionalForUnknownRoleName() { | ||
assertTrue(Role.forName("unknownName").isEmpty()); | ||
} | ||
} |
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