parent
531ed9d8f4
commit
ab0055162c
@ -0,0 +1,28 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.security; |
||||
|
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||
|
||||
public class SecurityGroupVerifierImpl { |
||||
|
||||
private Person person; |
||||
|
||||
public void setLoggedinPerson(Person person) { |
||||
this.person = person; |
||||
} |
||||
|
||||
public boolean isPermitted(SecurityGroups... groups) { |
||||
if (person != null) { |
||||
|
||||
for (SecurityGroups g : groups) { |
||||
for (GroupDef def : person.getGroups()) { |
||||
if (g.getValue().equalsIgnoreCase(def.getName())) { |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.security; |
||||
|
||||
public enum SecurityGroups { |
||||
|
||||
ADMIN("ADMIN"), AKTIVE("Aktive"), WETTKAEMPFER("Wettkämpfer"), UEBUNGSLEITER("Übungsleiter"), |
||||
KAMPFRICHTER("Kampfrichter"); |
||||
|
||||
private final String value; |
||||
|
||||
private SecurityGroups(String value) { |
||||
this.value = value; |
||||
} |
||||
|
||||
public String getValue() { |
||||
return value; |
||||
} |
||||
|
||||
public static SecurityGroups byValue(String value) { |
||||
for (SecurityGroups g : values()) { |
||||
if (g.value.equalsIgnoreCase(value)) { |
||||
return g; |
||||
} |
||||
} |
||||
throw new IllegalArgumentException("No SecurityGroups defined for value " + value); |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@ |
||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.security; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
import static org.junit.Assert.assertFalse; |
||||
import static org.junit.Assert.assertTrue; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.GroupDef; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.ui.tests.TestPersonGenerator; |
||||
|
||||
class SecurityGroupVerifierImplTest { |
||||
|
||||
private SecurityGroupVerifierImpl securityGroupVerifier; |
||||
private Map<SecurityGroups, GroupDef> groupDefinitions; |
||||
private Person person; |
||||
private Set<GroupDef> personGroups; |
||||
|
||||
@BeforeEach |
||||
void setUp() throws Exception { |
||||
securityGroupVerifier = new SecurityGroupVerifierImpl(); |
||||
groupDefinitions = new HashMap<>(); |
||||
|
||||
for (SecurityGroups g : SecurityGroups.values()) { |
||||
GroupDef def = new GroupDef(); |
||||
def.setName(g.getValue()); |
||||
groupDefinitions.put(g, def); |
||||
} |
||||
person = TestPersonGenerator.generatePersonen(1).get(0); |
||||
personGroups = new HashSet<>(); |
||||
person.setGroups(personGroups); |
||||
securityGroupVerifier.setLoggedinPerson(person); |
||||
} |
||||
|
||||
@Test |
||||
void verifyGroupDefinitions() { |
||||
for (SecurityGroups g : SecurityGroups.values()) { |
||||
assertEquals(g.getValue(), groupDefinitions.get(g).getName()); |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
void testGroupMatch() { |
||||
personGroups.add(groupDefinitions.get(SecurityGroups.UEBUNGSLEITER)); |
||||
assertTrue(securityGroupVerifier.isPermitted(SecurityGroups.UEBUNGSLEITER)); |
||||
|
||||
personGroups.add(groupDefinitions.get(SecurityGroups.ADMIN)); |
||||
assertTrue(securityGroupVerifier.isPermitted(SecurityGroups.ADMIN)); |
||||
|
||||
personGroups.add(groupDefinitions.get(SecurityGroups.KAMPFRICHTER)); |
||||
assertTrue(securityGroupVerifier.isPermitted(SecurityGroups.KAMPFRICHTER)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void testGroupNotMatching() { |
||||
for (SecurityGroups g : SecurityGroups.values()) { |
||||
assertFalse(securityGroupVerifier.isPermitted(g)); |
||||
} |
||||
personGroups.add(groupDefinitions.get(SecurityGroups.UEBUNGSLEITER)); |
||||
personGroups.add(groupDefinitions.get(SecurityGroups.KAMPFRICHTER)); |
||||
|
||||
assertFalse(securityGroupVerifier.isPermitted(SecurityGroups.ADMIN)); |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue