Skip to content
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

create AuthorizedRoles annotation and AuthorizedRolesAspect #136

Merged
merged 32 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
872e5e6
create a model to store user_skill and add remove unused columns in u…
yesyash Jun 23, 2024
2495c34
create a model to store user_skill and add/remove unused columns in u…
yesyash Jun 24, 2024
2d73260
change id to integer
yesyash Jun 24, 2024
b1b20a0
check if a skill already exists before creating one, make `updated_at…
yesyash Jun 24, 2024
8063f40
remove unit and integration test and remove skill service & skill ser…
yesyash Jun 24, 2024
7f09890
add todo
yesyash Jun 24, 2024
8f17bbc
rename SkillType to SkillTypeEnum
yesyash Jun 24, 2024
76b71bf
set logging level debug in application-dev instead of application
yesyash Jun 24, 2024
711757d
add todo
yesyash Jun 24, 2024
ff0f665
remove endorsements list
yesyash Jun 24, 2024
dc03b6d
add reference to user table in skills modal
yesyash Jun 24, 2024
4f52d11
Build api to create a new endorsement
yesyash Jun 24, 2024
14424c4
create api to update an endorsement
yesyash Jun 25, 2024
7a48e9c
remove unused files
yesyash Jun 25, 2024
22eece3
remove unused code in endorsement controller
yesyash Jun 25, 2024
514ffde
chagne import order
yesyash Jun 25, 2024
15e5740
change the skills project structure to match the new one
yesyash Jun 25, 2024
c77efb4
rename exceptions folder to small case exceptions and create user not…
yesyash Jun 25, 2024
43ae5b5
create enums folder and move skill type enum to the folder
yesyash Jun 25, 2024
a2aabe7
rename Conifg to config, move generic response and jwtAuthenticationF…
yesyash Jun 25, 2024
f6d364c
add api to get all endorsements for a skill using skill id in skillsapi
yesyash Jun 26, 2024
9592f6f
remove skills package
yesyash Jun 26, 2024
169c212
fix build error
yesyash Jun 26, 2024
2cfa444
move api to create a endorsement to the new folder structure
yesyash Jun 26, 2024
0508fc1
add api to update endorsement in apis/endorsements api and remove old…
yesyash Jun 26, 2024
1418fc4
move health check api to the api folder & metric service inside the s…
yesyash Jun 26, 2024
be2e5bb
fix formatting
yesyash Jun 26, 2024
5c3b983
create annotation and aspect to handle authorized roles to an api
yesyash Jul 7, 2024
abd38a4
add authorizred roles annotation to skillsapi & endorsementsapi class
yesyash Jul 7, 2024
884e8cc
set authorized role for creating a skill to only superuser
yesyash Jul 7, 2024
8bc7c9f
fix formatting
yesyash Jul 7, 2024
7eb0bac
Merge branch 'develop' of github.com:Real-Dev-Squad/skill-tree-backen…
yesyash Jul 7, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.RDS.skilltree.annotations;

import com.RDS.skilltree.User.UserRoleEnum;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthorizedRoles {
UserRoleEnum[] value() default {};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.RDS.skilltree.apis;

import com.RDS.skilltree.User.UserRoleEnum;
import com.RDS.skilltree.annotations.AuthorizedRoles;
import com.RDS.skilltree.services.EndorsementService;
import com.RDS.skilltree.viewmodels.CreateEndorsementViewModel;
import com.RDS.skilltree.viewmodels.EndorsementViewModel;
Expand All @@ -15,6 +17,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("v1/endorsements")
@AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER})
public class EndorsementsApi {
private final EndorsementService endorsementService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.RDS.skilltree.apis;

import com.RDS.skilltree.User.UserRoleEnum;
import com.RDS.skilltree.annotations.AuthorizedRoles;
import com.RDS.skilltree.services.EndorsementService;
import com.RDS.skilltree.services.SkillService;
import com.RDS.skilltree.viewmodels.CreateSkillViewModel;
Expand All @@ -19,6 +21,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("v1/skills")
@AuthorizedRoles({UserRoleEnum.USER, UserRoleEnum.SUPERUSER})
public class SkillsApi {
private final SkillService skillService;
private final EndorsementService endorsementService;
Expand All @@ -29,6 +32,7 @@ public ResponseEntity<List<SkillViewModel>> getAll() {
}

@PostMapping
@AuthorizedRoles({UserRoleEnum.SUPERUSER})
public ResponseEntity<SkillViewModel> create(@Valid @RequestBody CreateSkillViewModel skill) {
return ResponseEntity.ok(skillService.create(skill));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.RDS.skilltree.aspects;

import com.RDS.skilltree.User.JwtUserModel;
import com.RDS.skilltree.User.UserRoleEnum;
import com.RDS.skilltree.annotations.AuthorizedRoles;
import com.RDS.skilltree.exceptions.ForbiddenException;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AuthorizedRolesAspect {

@Around("@within(authorizedRoles) || @annotation(authorizedRoles)")
public Object authorize(ProceedingJoinPoint joinPoint, AuthorizedRoles authorizedRoles)
throws Throwable {
JwtUserModel jwtDetails =
(JwtUserModel) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserRoleEnum role = jwtDetails.getRole();

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Class<?> targetClass = method.getDeclaringClass();

AuthorizedRoles methodAuthorized = method.getAnnotation(AuthorizedRoles.class);
AuthorizedRoles classAuthorized = targetClass.getAnnotation(AuthorizedRoles.class);

UserRoleEnum[] allowedRoles = {};

if (methodAuthorized != null) {
allowedRoles = methodAuthorized.value();
} else if (classAuthorized != null) {
allowedRoles = classAuthorized.value();
} else {
// If no roles are specified, proceed with the method execution
joinPoint.proceed();
}

if (!isAuthorized(role, allowedRoles)) {
throw new ForbiddenException("You're not authorized to make this request");
}

return joinPoint.proceed();
}

private boolean isAuthorized(UserRoleEnum userRole, UserRoleEnum[] allowedRoles) {
for (UserRoleEnum role : allowedRoles) {
if (role.equals(userRole)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.RDS.skilltree.exceptions;

public class ForbiddenException extends RuntimeException {
public ForbiddenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,10 @@ public ResponseEntity<?> handleEndorsementNotException(
log.error("Exception - Error : {}", ex.getMessage(), ex);
return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<?> handleForbiddenException(ForbiddenException ex, WebRequest request) {
log.error("Exception - Error : {}", ex.getMessage(), ex);
return new ResponseEntity<>(new GenericResponse<>(null, ex.getMessage()), HttpStatus.FORBIDDEN);
}
}
Loading