Skip to content

Add roles support for Helidon #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public class WidgetController$Route implements HttpFeature {
private void _getById(ServerRequest req, ServerResponse res) throws Exception {
res.status(OK_200);
var pathParams = req.path().pathParameters();
var id = asInt(pathParams.first("id").get());
var id = asInt(pathParams.contains("id") ? pathParams.get("id") : null);
var result = controller.getById(id);
res.send(result);
}
Expand Down Expand Up @@ -263,7 +263,7 @@ public class WidgetController$Route implements HttpFeature {
private void _getById(ServerRequest req, ServerResponse res) throws Exception {
res.status(OK_200);
var pathParams = req.path().pathParameters();
var id = asInt(pathParams.first("id").get());
var id = asInt(pathParams.contains("id") ? pathParams.get("id") : null);
var result = controller.getById(id);
res.headers().contentType(MediaTypes.APPLICATION_JSON);
//jsonb has a special accommodation for helidon to improve performance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ void writeRule() {
writer.append(" routing.addFilter(this::_%s);", method.simpleName()).eol();
} else {
writer.append(" routing.%s(\"%s\", ", webMethod.name().toLowerCase(), method.fullPath().replace("\\", "\\\\"));
var roles = method.roles();
if (!roles.isEmpty()) {
writer.append("SecurityFeature.rolesAllowed(");
writer.append("\"%s\"", Util.shortName(roles.getFirst(), true));
for (var i = 1; i < roles.size(); i++) {
writer.append(", \"%s\"", Util.shortName(roles.get(i), true));
}
writer.append("), ");
}
var hxRequest = method.hxRequest();
if (hxRequest != null) {
writer.append("HxHandler.builder(this::_%s)", method.simpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class ControllerWriter extends BaseControllerWriter {
reader.addImportType("io.helidon.webserver.http.ServerResponse");
reader.addImportType("io.helidon.webserver.http.HttpFeature");
reader.addImportType("io.helidon.http.HeaderNames");
if (!reader.roles().isEmpty() || reader.methods().stream().anyMatch(m -> !m.roles().isEmpty())) {
reader.addImportType("io.helidon.webserver.security.SecurityFeature");
}
if (reader.isIncludeValidator()) {
reader.addImportType("io.helidon.http.HeaderName");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public void methodRoles(List<String> roles, ControllerReader controller) {
}

private void addRoleImports(List<String> roles, ControllerReader controller) {
// nothing here yet
for (final String role : roles) {
controller.addStaticImportType(role);
}
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions tests/test-nima/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
<artifactId>helidon-webserver</artifactId>
<version>${nima.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-security</artifactId>
<version>${nima.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.http.media</groupId>
<artifactId>helidon-http-media-jsonb</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions tests/test-nima/src/main/java/org/example/AppRoles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.example;

public enum AppRoles {
ANYONE, ADMIN, BASIC_USER
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.avaje.http.api.Controller;
import io.avaje.http.api.Get;
import io.avaje.http.api.Produces;

@Controller
public class HelloController {
Expand All @@ -18,4 +19,11 @@ Person person(String name, String sortBy) {
p.setName(name + " hello" + " sortBy:" + sortBy);
return p;
}

@Roles({AppRoles.ADMIN, AppRoles.BASIC_USER})
@Produces("text/plain")
@Get("other/{name}")
String name(String name) {
return "hi " + name;
}
}
21 changes: 21 additions & 0 deletions tests/test-nima/src/main/java/org/example/Roles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.example;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Specify permitted roles.
*/
@Target(value={METHOD, TYPE})
@Retention(value=RUNTIME)
public @interface Roles {

/**
* Specify the permitted roles.
*/
AppRoles[] value() default {};
}