Skip to content

Commit

Permalink
[Gepardec/mega#735] rename mega RolesAllowed to MegaRolesAllowed to a…
Browse files Browse the repository at this point in the history
…void name clash with java RolesAllowed annotation
  • Loading branch information
rainer-gepardec authored and Ollitod committed Feb 23, 2024
1 parent a912d37 commit 335f0dd
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface RolesAllowed {
public @interface MegaRolesAllowed {

@Nonbinding
Role[] value() default {Role.EMPLOYEE};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
import java.util.Set;

@Interceptor
@RolesAllowed
@MegaRolesAllowed
@Priority(Interceptor.Priority.APPLICATION)
public class RolesAllowedInterceptor {
public class MegaRolesAllowedInterceptor {

@Inject
UserContext userContext;

@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
RolesAllowed rolesAllowedAnnotation = invocationContext.getMethod().getAnnotation(RolesAllowed.class);
if (rolesAllowedAnnotation == null) {
rolesAllowedAnnotation = invocationContext.getTarget().getClass().getAnnotation(RolesAllowed.class);
MegaRolesAllowed megaRolesAllowedAnnotation = invocationContext.getMethod().getAnnotation(MegaRolesAllowed.class);
if (megaRolesAllowedAnnotation == null) {
megaRolesAllowedAnnotation = invocationContext.getTarget().getClass().getAnnotation(MegaRolesAllowed.class);
}

Objects.requireNonNull(rolesAllowedAnnotation,
Objects.requireNonNull(megaRolesAllowedAnnotation,
"Could not resolve Authorizaion annotation. Do you use Stereotype annotations, which are currently not supported?");

Role[] allowedRoles = rolesAllowedAnnotation.value();
Role[] allowedRoles = megaRolesAllowedAnnotation.value();
if (isInRole(userContext.getUser().getRoles(), allowedRoles)) {
return invocationContext.proceed();
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.application.interceptor.RolesAllowed;
import com.gepardec.mega.application.interceptor.MegaRolesAllowed;
import com.gepardec.mega.domain.model.Employee;
import com.gepardec.mega.domain.model.Role;
import com.gepardec.mega.rest.api.EmployeeResource;
Expand All @@ -16,7 +16,7 @@

@RequestScoped
@Authenticated
@RolesAllowed({Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
@MegaRolesAllowed({Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
public class EmployeeResourceImpl implements EmployeeResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.application.interceptor.RolesAllowed;
import com.gepardec.mega.application.interceptor.MegaRolesAllowed;
import com.gepardec.mega.domain.model.Role;
import com.gepardec.mega.rest.api.EnterpriseResource;
import com.gepardec.mega.rest.model.EnterpriseEntryDto;
Expand All @@ -15,7 +15,7 @@

@RequestScoped
@Authenticated
@RolesAllowed(value = {Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
@MegaRolesAllowed(value = {Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
public class EnterpriseResourceImpl implements EnterpriseResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.application.interceptor.RolesAllowed;
import com.gepardec.mega.application.interceptor.MegaRolesAllowed;
import com.gepardec.mega.db.entity.employee.EmployeeState;
import com.gepardec.mega.db.entity.employee.StepEntry;
import com.gepardec.mega.db.entity.employee.User;
Expand Down Expand Up @@ -35,7 +35,7 @@

@RequestScoped
@Authenticated
@RolesAllowed(value = {Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
@MegaRolesAllowed(value = {Role.PROJECT_LEAD, Role.OFFICE_MANAGEMENT})
public class ManagementResourceImpl implements ManagementResource {

private static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.application.interceptor.RolesAllowed;
import com.gepardec.mega.application.interceptor.MegaRolesAllowed;
import com.gepardec.mega.domain.model.Role;
import com.gepardec.mega.domain.model.UserContext;
import com.gepardec.mega.rest.api.UserResource;
Expand All @@ -13,7 +13,7 @@

@RequestScoped
@Authenticated
@RolesAllowed(Role.EMPLOYEE)
@MegaRolesAllowed(Role.EMPLOYEE)
public class UserResourceImpl implements UserResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.application.interceptor.RolesAllowed;
import com.gepardec.mega.application.interceptor.MegaRolesAllowed;
import com.gepardec.mega.domain.model.Role;
import com.gepardec.mega.domain.model.monthlyreport.MonthlyReport;
import com.gepardec.mega.rest.api.WorkerResource;
Expand All @@ -13,7 +13,7 @@

@RequestScoped
@Authenticated
@RolesAllowed(Role.EMPLOYEE)
@MegaRolesAllowed(Role.EMPLOYEE)
public class WorkerResourceImpl implements WorkerResource {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class RolesAllowedInterceptorTest {
class MegaMegaRolesAllowedInterceptorTest {

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private UserContext userContext;
Expand All @@ -31,15 +31,15 @@ class RolesAllowedInterceptorTest {
private InvocationContext invocationContext;

@InjectMocks
private RolesAllowedInterceptor rolesAllowedInterceptor;
private MegaRolesAllowedInterceptor megaRolesAllowedInterceptor;

@Test
void invoke_whenAnnotationOnClassLevel_thenUsesClassLevelAnnotation() throws Exception {
when(invocationContext.getMethod().getAnnotation(any())).thenReturn(null);
when(invocationContext.getTarget()).thenReturn(new TargetWithAnnotation());
when(userContext.getUser().getRoles()).thenReturn(Set.of(Role.EMPLOYEE));

rolesAllowedInterceptor.intercept(invocationContext);
megaRolesAllowedInterceptor.intercept(invocationContext);

verify(invocationContext, times(1)).proceed();
}
Expand All @@ -49,7 +49,7 @@ void invoke_whenNoAnnotationOnMethodAndClassLevel_thenThrowsNullpointerException
when(invocationContext.getMethod().getAnnotation(any())).thenReturn(null);
when(invocationContext.getTarget()).thenReturn(new TargetNoAnnotation());

assertThatThrownBy(() -> rolesAllowedInterceptor.intercept(invocationContext))
assertThatThrownBy(() -> megaRolesAllowedInterceptor.intercept(invocationContext))
.isInstanceOf(NullPointerException.class);
}

Expand All @@ -58,42 +58,42 @@ void invoke_whenNoAnnotationOnMethodAndClassLevel_thenThrowsNullpointerException
@Disabled
void intercept_whenNotLogged_thenThrowsForbiddenException() {
when(invocationContext.getMethod().getAnnotation(any())).thenReturn(createAnnotation(new Role[]{Role.EMPLOYEE}));
assertThatThrownBy(() -> rolesAllowedInterceptor.intercept(invocationContext)).isInstanceOf(ForbiddenException.class);
assertThatThrownBy(() -> megaRolesAllowedInterceptor.intercept(invocationContext)).isInstanceOf(ForbiddenException.class);
}

@Test
void invoke_whenLoggedAndNotInRole_thenThrowsForbiddenException() {
when(invocationContext.getMethod().getAnnotation(any())).thenReturn(createAnnotation(new Role[]{Role.OFFICE_MANAGEMENT}));
when(userContext.getUser().getRoles()).thenReturn(Set.of(Role.EMPLOYEE));

assertThatThrownBy(() -> rolesAllowedInterceptor.intercept(invocationContext)).isInstanceOf(ForbiddenException.class);
assertThatThrownBy(() -> megaRolesAllowedInterceptor.intercept(invocationContext)).isInstanceOf(ForbiddenException.class);
}

@Test
void invoke_whenLoggedAndInRoleMethodAnnotated_thenThrowsForbiddenException() throws Exception {
when(invocationContext.getMethod().getAnnotation(any())).thenReturn(createAnnotation(Role.values()));
when(userContext.getUser().getRoles()).thenReturn(Set.of(Role.EMPLOYEE));

rolesAllowedInterceptor.intercept(invocationContext);
megaRolesAllowedInterceptor.intercept(invocationContext);

verify(invocationContext, times(1)).proceed();
}

private RolesAllowed createAnnotation(final Role[] roles) {
return new RolesAllowed() {
private MegaRolesAllowed createAnnotation(final Role[] roles) {
return new MegaRolesAllowed() {
@Override
public Role[] value() {
return roles;
}

@Override
public Class<? extends Annotation> annotationType() {
return RolesAllowed.class;
return MegaRolesAllowed.class;
}
};
}

@RolesAllowed(Role.EMPLOYEE)
@MegaRolesAllowed(Role.EMPLOYEE)
private static class TargetWithAnnotation {

}
Expand Down

0 comments on commit 335f0dd

Please sign in to comment.