-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* create a model to store user_skill and add remove unused columns in users and skills model * create a model to store user_skill and add/remove unused columns in users and skills model * change id to integer * check if a skill already exists before creating one, make `updated_at` optional in TrackedProperties * remove unit and integration test and remove skill service & skill service implementation * add todo * rename SkillType to SkillTypeEnum * set logging level debug in application-dev instead of application * add todo * remove endorsements list * add reference to user table in skills modal * Build api to create a new endorsement * create api to update an endorsement * remove unused files * remove unused code in endorsement controller * chagne import order * change the skills project structure to match the new one * rename exceptions folder to small case exceptions and create user not found and skill already exists exceptions * create enums folder and move skill type enum to the folder * rename Conifg to config, move generic response and jwtAuthenticationFilter to utils * add api to get all endorsements for a skill using skill id in skillsapi * remove skills package * fix build error * move api to create a endorsement to the new folder structure * add api to update endorsement in apis/endorsements api and remove old Endorsement folder * move health check api to the api folder & metric service inside the services folder * fix formatting * create annotation and aspect to handle authorized roles to an api * add authorizred roles annotation to skillsapi & endorsementsapi class * set authorized role for creating a skill to only superuser * fix formatting
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
skill-tree/src/main/java/com/RDS/skilltree/annotations/AuthorizedRoles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
skill-tree/src/main/java/com/RDS/skilltree/aspects/AuthorizedRolesAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
skill-tree/src/main/java/com/RDS/skilltree/exceptions/ForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters