Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.

Commit be30550

Browse files
authored
Merge pull request #6 from E-Edu/issue/1
Implemented basic scopes
2 parents 6c5f326 + a5245aa commit be30550

7 files changed

Lines changed: 270 additions & 1 deletion

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</modules>
1515

1616
<properties>
17-
<java.version>11</java.version>
17+
<java.version>8</java.version>
1818
<maven.compiler.source>${java.version}</maven.compiler.source>
1919
<maven.compiler.target>${java.version}</maven.compiler.target>
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

scope/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>gewia-common</artifactId>
7+
<groupId>com.gewia.common</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>scope</artifactId>
13+
14+
15+
</project>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.gewia.common.scope;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A scope is a mechanism to limit permissions of a person or application.
8+
*
9+
* <p>
10+
* This implementation is the reference implementation of scopes for the 'Gewia' project.
11+
*
12+
* Examples:
13+
* user.email.read.self - let's the user read his own email address
14+
* user.email.write.self - let's the user change his own email address
15+
* user.email.write.all - let's the user change every email address of every user
16+
* user.email.write.steve - let's the user change the email address of "steve"; this is a custom behaviour
17+
* and not part of the official specification
18+
* </p>
19+
* @since 1.0
20+
*/
21+
public abstract class Scope {
22+
23+
protected final List<ScopePart> scopeParts = new ArrayList<>();
24+
25+
/**
26+
* Gets the currently available scope parts.
27+
*
28+
* <p>
29+
* Whether the returned list is readonly or even a copy will be specified by
30+
* the implementation.
31+
* </p>
32+
*
33+
* @return all scope parts
34+
*
35+
* @since 1.0
36+
*/
37+
public abstract List<ScopePart> getScopeParts();
38+
39+
/**
40+
* Adds the given <i>scopeParts</i> at the end of the scope.
41+
*
42+
* @param scopeParts the scope parts to add
43+
*
44+
* @return this
45+
*
46+
* @since 1.0
47+
*/
48+
public Scope addScopeParts(String... scopeParts) {
49+
for (String scopePart : scopeParts) this.addScopePart(new ScopePart(scopePart));
50+
return this;
51+
}
52+
53+
/**
54+
* Adds the given <i>scopePart</i> at the end of the scope.
55+
*
56+
* @param scopePart the scope part to add
57+
*
58+
* @return this
59+
*
60+
* @since 1.0
61+
*/
62+
public Scope addScopePart(String scopePart) {
63+
return this.addScopePart(new ScopePart(scopePart));
64+
}
65+
66+
/**
67+
* Adds the given <i>scopePart</i> at the end of the scope.
68+
*
69+
* @param scopePart the scope part to add
70+
*
71+
* @return this
72+
*
73+
* @since 1.0
74+
*/
75+
public abstract Scope addScopePart(ScopePart scopePart);
76+
77+
/**
78+
* Removes the scope part at the given <i>index</i>.
79+
*
80+
* @param index the index used to remove the scope part
81+
*
82+
* @return this
83+
* @throws IndexOutOfBoundsException when the index is greater than the amount scope parts
84+
*
85+
* @since 1.0
86+
*/
87+
public abstract Scope removeScopePart(int index);
88+
89+
/**
90+
* Gets the scope part at the given <i>index</i>.
91+
*
92+
* @param index the index used to get the scope part
93+
*
94+
* @return this
95+
* @throws IndexOutOfBoundsException when the index is greater than the amount scope parts
96+
*
97+
* @since 1.0
98+
*/
99+
public abstract ScopePart getScopePart(int index);
100+
101+
/**
102+
* Gets the amount of scope parts of this scope.
103+
*
104+
* @return the amount of scope parts
105+
*
106+
* @since 1.0
107+
*/
108+
public abstract int getScopePartsSize();
109+
110+
@Override
111+
public String toString() {
112+
StringBuilder sb = new StringBuilder();
113+
114+
this.getScopeParts().forEach(scopePart -> {
115+
if (sb.length() != 0) sb.append(".");
116+
117+
sb.append(scopePart);
118+
});
119+
120+
return sb.toString();
121+
}
122+
123+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.gewia.common.scope;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Scopes are made out of {@link ScopePart}s which are concatenated using dots.
7+
*
8+
* @since 1.0
9+
*/
10+
@Data
11+
public class ScopePart {
12+
13+
private final String content;
14+
15+
@Override
16+
public String toString() {
17+
return this.content;
18+
}
19+
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.gewia.common.scope.impl;
2+
3+
import com.gewia.common.scope.Scope;
4+
import com.gewia.common.scope.ScopePart;
5+
import lombok.AccessLevel;
6+
import lombok.RequiredArgsConstructor;
7+
import java.util.List;
8+
9+
/**
10+
* Represents a generic scope without further restrictions.
11+
*
12+
* @since 1.0
13+
*/
14+
@RequiredArgsConstructor(access = AccessLevel.MODULE)
15+
public class BasicScope extends Scope {
16+
17+
/**
18+
* Gets the currently available scope parts.
19+
*
20+
* <p>
21+
* The returned list is the real modifiable list used for internal
22+
* purposes.
23+
* Modifications to this list will directly affect this scope.
24+
* </p>
25+
*
26+
* @return all scope parts
27+
*/
28+
@Override
29+
public List<ScopePart> getScopeParts() {
30+
return this.scopeParts;
31+
}
32+
33+
@Override
34+
public Scope addScopePart(ScopePart scopePart) {
35+
this.scopeParts.add(scopePart);
36+
return this;
37+
}
38+
39+
@Override
40+
public Scope removeScopePart(int index) {
41+
this.scopeParts.remove(index);
42+
return this;
43+
}
44+
45+
@Override
46+
public ScopePart getScopePart(int index) {
47+
return this.scopeParts.get(index);
48+
}
49+
50+
@Override
51+
public int getScopePartsSize() {
52+
return this.scopeParts.size();
53+
}
54+
55+
56+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gewia.common.scope.impl;
2+
3+
import com.gewia.common.scope.Scope;
4+
5+
public class ScopeFactory {
6+
7+
/**
8+
* Creates a {@link BasicScope} with not further restrictions.
9+
*
10+
* @return a {@link BasicScope}
11+
*
12+
* @since 1.0
13+
*/
14+
public static Scope createBasicScope() {
15+
return new BasicScope();
16+
}
17+
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.gewia.common.scope.test;
2+
3+
import com.gewia.common.scope.Scope;
4+
import com.gewia.common.scope.impl.ScopeFactory;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class BasicScopeTest {
9+
10+
@Test
11+
public void basicCreateAndReadAndDeleteTest() {
12+
Scope scope = ScopeFactory.createBasicScope();
13+
14+
Assert.assertTrue("Scope parts not empty", scope.getScopeParts().isEmpty());
15+
Assert.assertEquals("Scope parts size not zero", 0, scope.getScopePartsSize());
16+
Assert.assertEquals("Output not empty", 0, scope.toString().length());
17+
18+
Assert.assertEquals(
19+
"Returned scope not the same",
20+
scope,
21+
scope.addScopeParts("user", "email", "read")
22+
);
23+
24+
Assert.assertEquals("Scope output doesn't match", "user.email.read", scope.toString());
25+
Assert.assertEquals("Scope parts do not match size (list access)", 3, scope.getScopeParts().size());
26+
Assert.assertEquals("Scope parts do not match size", 3, scope.getScopePartsSize());
27+
28+
Assert.assertThrows("Scope didn't catch out of bound remove", IndexOutOfBoundsException.class,() -> scope.removeScopePart(4));
29+
30+
scope.removeScopePart(1);
31+
32+
Assert.assertEquals("Scope output doesn't match", "user.read", scope.toString());
33+
Assert.assertEquals("Scope parts do not match size (list access)", 2, scope.getScopeParts().size());
34+
Assert.assertEquals("Scope parts do not match size", 2, scope.getScopePartsSize());
35+
}
36+
37+
}

0 commit comments

Comments
 (0)