diff --git a/src/main/java/cz/cvut/kbss/study/model/Role.java b/src/main/java/cz/cvut/kbss/study/model/Role.java index 83f8d19b..cb7807f8 100644 --- a/src/main/java/cz/cvut/kbss/study/model/Role.java +++ b/src/main/java/cz/cvut/kbss/study/model/Role.java @@ -55,12 +55,7 @@ public enum Role { this.iri = iri; } - public static Role getFromSecurityRole(cz.cvut.kbss.study.security.model.Role r) { - return switch (r) { - case USER -> user; - case ADMIN -> administrator; - }; - } + public String getIri() { return iri; diff --git a/src/main/java/cz/cvut/kbss/study/security/model/Role.java b/src/main/java/cz/cvut/kbss/study/security/model/Role.java deleted file mode 100644 index 64665215..00000000 --- a/src/main/java/cz/cvut/kbss/study/security/model/Role.java +++ /dev/null @@ -1,36 +0,0 @@ -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_i_user), - ADMIN(SecurityConstants.ROLE_ADMIN, Vocabulary.s_i_administrator); - - private final String name; - private final String type; - - Role(String name, String type) { - this.name = name; - this.type = type; - } - - public static Optional forType(String type) { - return Stream.of(Role.values()).filter(r -> r.type.equals(type)).findAny(); - } - - public static Optional 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; - } -} diff --git a/src/main/java/cz/cvut/kbss/study/service/security/SecurityUtils.java b/src/main/java/cz/cvut/kbss/study/service/security/SecurityUtils.java index 6233af86..587e144b 100644 --- a/src/main/java/cz/cvut/kbss/study/service/security/SecurityUtils.java +++ b/src/main/java/cz/cvut/kbss/study/service/security/SecurityUtils.java @@ -1,13 +1,9 @@ package cz.cvut.kbss.study.service.security; import cz.cvut.kbss.study.exception.NotFoundException; -import cz.cvut.kbss.study.model.PatientRecord; -import cz.cvut.kbss.study.model.RoleGroup; -import cz.cvut.kbss.study.model.User; -import cz.cvut.kbss.study.model.Vocabulary; +import cz.cvut.kbss.study.model.*; import cz.cvut.kbss.study.persistence.dao.PatientRecordDao; import cz.cvut.kbss.study.persistence.dao.UserDao; -import cz.cvut.kbss.study.security.model.Role; import cz.cvut.kbss.study.security.model.UserDetails; import cz.cvut.kbss.study.service.ConfigReader; import cz.cvut.kbss.study.util.ConfigParam; @@ -106,8 +102,7 @@ private User resolveAccountFromOAuthPrincipal(Jwt principal) { RoleGroup roleGroup = new RoleGroup(); user.setRoleGroup(roleGroup); - roles.stream().map(Role::forName).filter(Optional::isPresent).map(Optional::get) - .forEach(r -> roleGroup.addRole(cz.cvut.kbss.study.model.Role.getFromSecurityRole(r))); + roles.forEach(r -> roleGroup.addRole(Role.fromIriOrName(r))); return user; } diff --git a/src/test/java/cz/cvut/kbss/study/security/model/RoleTest.java b/src/test/java/cz/cvut/kbss/study/security/model/RoleTest.java deleted file mode 100644 index b006c35c..00000000 --- a/src/test/java/cz/cvut/kbss/study/security/model/RoleTest.java +++ /dev/null @@ -1,44 +0,0 @@ -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 generator() { - return Stream.of(Role.values()).map(Arguments::of); - } - - @ParameterizedTest - @MethodSource("generator") - void forTypeReturnsRoleMatchingSpecifiedType(Role r) { - final Optional 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 result = Role.forName(r.getName()); - assertTrue(result.isPresent()); - assertEquals(r, result.get()); - } - - @Test - void forNameReturnsEmptyOptionalForUnknownRoleName() { - assertTrue(Role.forName("unknownName").isEmpty()); - } -} \ No newline at end of file