Skip to content

Commit e0e9128

Browse files
committed
#59 - Support use of @roles annotation with Jex + fix for Javalin use of @roles
1 parent 98ecba9 commit e0e9128

File tree

8 files changed

+72
-30
lines changed

8 files changed

+72
-30
lines changed

http-generator-core/src/main/java/io/avaje/http/generator/core/Util.java

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import javax.lang.model.element.AnnotationMirror;
44
import javax.lang.model.element.AnnotationValue;
55
import javax.lang.model.element.Element;
6+
import javax.lang.model.element.VariableElement;
67
import javax.lang.model.type.DeclaredType;
78
import javax.lang.model.type.TypeKind;
89
import javax.lang.model.type.TypeMirror;
10+
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
911
import java.util.ArrayList;
12+
import java.util.Collections;
1013
import java.util.List;
1114

1215
public class Util {
@@ -20,7 +23,7 @@ public static UType parse(String rawType) {
2023
return new UType.Basic(rawType);
2124
} else {
2225
String mainType = rawType.substring(0, pos);
23-
String genericParams = rawType.substring(pos+1, rawType.length()-1);
26+
String genericParams = rawType.substring(pos + 1, rawType.length() - 1);
2427
List<String> params = parseGenericParams(genericParams);
2528
return new UType.Generic(rawType, mainType, params);
2629
}
@@ -141,24 +144,14 @@ public static String initcapSnake(String input) {
141144
* @param element The bean or method
142145
*/
143146
public static List<String> findRoles(Element element) {
144-
145-
List<String> roles = new ArrayList<>();
146-
147147
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
148-
DeclaredType annotationType = annotationMirror.getAnnotationType();
149-
if (isRolesAnnotation(annotationType)) {
150-
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
151-
String raw = value.toString();
152-
if (raw.startsWith("{")) {
153-
raw = raw.substring(1, raw.length() - 1);
154-
}
155-
for (String singleRole : raw.split(",")) {
156-
roles.add(singleRole.trim());
157-
}
148+
if (isRolesAnnotation(annotationMirror.getAnnotationType())) {
149+
for (AnnotationValue annotationValue : annotationMirror.getElementValues().values()) {
150+
return annotationValue.accept(new RoleReader(), annotationValue);
158151
}
159152
}
160153
}
161-
return roles;
154+
return Collections.emptyList();
162155
}
163156

164157
private static boolean isRolesAnnotation(DeclaredType annotationType) {
@@ -181,4 +174,23 @@ public static UType parseType(TypeMirror returnType) {
181174
}
182175
return parse(returnType.toString());//typeDef(returnType));
183176
}
177+
178+
private static class RoleReader extends SimpleAnnotationValueVisitor8<List<String>, Object> {
179+
180+
private final List<String> fullRoles = new ArrayList<>();
181+
182+
@Override
183+
public List<String> visitArray(List<? extends AnnotationValue> values, Object o) {
184+
for (AnnotationValue val : values) {
185+
val.accept(this, o);
186+
}
187+
return fullRoles;
188+
}
189+
190+
@Override
191+
public List<String> visitEnumConstant(VariableElement roleEnum, Object o) {
192+
fullRoles.add(roleEnum.asType() + "." + roleEnum.getSimpleName());
193+
return fullRoles;
194+
}
195+
}
184196
}

http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ void write(boolean requestScoped) {
7676

7777
List<String> roles = method.roles();
7878
if (!roles.isEmpty()) {
79-
writer.append(", roles(");
79+
writer.append(").withRoles(");
8080
for (int i = 0; i < roles.size(); i++) {
8181
if (i > 0) {
8282
writer.append(", ");
8383
}
8484
writer.append(Util.shortName(roles.get(i)));
8585
}
86-
writer.append(")");
8786
}
8887
writer.append(");").eol().eol();
8988
}

http-generator-jex/src/main/java/io/avaje/http/generator/jex/JexAdapter.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
class JexAdapter implements PlatformAdapter {
1111

1212
static final String JEX_CONTEXT = "io.avaje.jex.Context";
13-
//static final String JAVALIN3_ROLES = "io.javalin.core.security.SecurityUtil.roles";
1413

1514
@Override
1615
public boolean isContextType(String rawType) {
@@ -39,20 +38,19 @@ public String indent() {
3938

4039
@Override
4140
public void controllerRoles(List<String> roles, ControllerReader controller) {
42-
//addRoleImports(roles, controller);
41+
addRoleImports(roles, controller);
4342
}
4443

4544
@Override
4645
public void methodRoles(List<String> roles, ControllerReader controller) {
47-
//addRoleImports(roles, controller);
46+
addRoleImports(roles, controller);
4847
}
4948

50-
// private void addRoleImports(List<String> roles, ControllerReader controller) {
51-
// controller.addStaticImportType(JAVALIN3_ROLES);
52-
// for (String role : roles) {
53-
// controller.addStaticImportType(role);
54-
// }
55-
// }
49+
private void addRoleImports(List<String> roles, ControllerReader controller) {
50+
for (String role : roles) {
51+
controller.addStaticImportType(role);
52+
}
53+
}
5654

5755
@Override
5856
public void writeReadParameter(Append writer, ParamType paramType, String paramName) {

tests/test-javalin/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ String getPlainMessage() {
5555
* @return The Hello DTO given the id and name.
5656
* @deprecated Please migrate away
5757
*/
58+
@Roles({AppRoles.ADMIN, AppRoles.BASIC_USER})
5859
@Get("/:id/:date")
5960
HelloDto hello(int id, LocalDate date, String otherParam) {
6061
return new HelloDto(id, date.toString(), otherParam);
@@ -63,10 +64,11 @@ HelloDto hello(int id, LocalDate date, String otherParam) {
6364
/**
6465
* Find Hellos by name.
6566
*
66-
* @param name The name to search for
67+
* @param name The name to search for
6768
* @param myParam My option parameter
6869
* @return The Hellos that we found.
6970
*/
71+
@Roles(AppRoles.ADMIN)
7072
@Get("/findbyname/{name}")
7173
List<HelloDto> findByName(String name, @QueryParam("my-param") @Default("one") String myParam) {
7274
return new ArrayList<>();

tests/test-jex/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<properties>
1818
<main.class>org.example.myapp.Main</main.class>
19-
<jex.version>1.3</jex.version>
19+
<jex.version>1.5</jex.version>
2020
<swagger.version>2.0.8</swagger.version>
2121
<jackson.version>2.12.3</jackson.version>
2222
<avaje-inject.version>6.0.RC3</avaje-inject.version>
@@ -52,7 +52,7 @@
5252
<dependency>
5353
<groupId>io.avaje</groupId>
5454
<artifactId>avaje-http-api</artifactId>
55-
<version>1.2</version>
55+
<version>${avaje-http.version}</version>
5656
</dependency>
5757

5858
<dependency>
@@ -123,7 +123,7 @@
123123
<plugin>
124124
<groupId>io.repaint.maven</groupId>
125125
<artifactId>tiles-maven-plugin</artifactId>
126-
<version>2.19</version>
126+
<version>2.22</version>
127127
<extensions>true</extensions>
128128
<configuration>
129129
<tiles>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.example.web;
2+
3+
4+
import io.avaje.jex.Role;
5+
6+
public enum AppRoles implements Role {
7+
ANYONE, ADMIN, BASIC_USER
8+
}

tests/test-jex/src/main/java/org/example/web/HelloController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.avaje.http.api.Produces;
77
import io.avaje.jex.Context;
88

9+
// @Roles(AppRoles.BASIC_USER)
910
@Controller
1011
@Path("/")
1112
public class HelloController {
@@ -24,6 +25,7 @@ String getText() {
2425
return "something";
2526
}
2627

28+
@Roles({AppRoles.ADMIN, AppRoles.BASIC_USER})
2729
@Produces("text/plain")
2830
@Get("other/{name}")
2931
String name(String name) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example.web;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
6+
import static java.lang.annotation.ElementType.METHOD;
7+
import static java.lang.annotation.ElementType.TYPE;
8+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
9+
10+
/**
11+
* Specify permitted roles.
12+
*/
13+
@Target(value={METHOD, TYPE})
14+
@Retention(value=RUNTIME)
15+
public @interface Roles {
16+
17+
/**
18+
* Specify the permitted roles.
19+
*/
20+
AppRoles[] value() default {};
21+
}

0 commit comments

Comments
 (0)