-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added vulnerability permission (#2303)
* Added vulnerability permission
- Loading branch information
1 parent
4bb142a
commit d453f65
Showing
3 changed files
with
106 additions
and
20 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
...src/main/kotlin/com/saveourtool/save/backend/security/VulnerabilityPermissionEvaluator.kt
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,53 @@ | ||
package com.saveourtool.save.backend.security | ||
|
||
import com.saveourtool.save.authservice.utils.AuthenticationDetails | ||
import com.saveourtool.save.backend.service.vulnerability.VulnerabilityService | ||
import com.saveourtool.save.backend.utils.hasRole | ||
import com.saveourtool.save.domain.Role | ||
|
||
import com.saveourtool.save.permission.Permission | ||
import com.saveourtool.save.utils.orNotFound | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* Class that is capable of assessing user permissions. | ||
*/ | ||
@Component | ||
class VulnerabilityPermissionEvaluator( | ||
private val vulnerabilityService: VulnerabilityService, | ||
) { | ||
/** | ||
* Check permission for user to read, write and delete vulnerabilities by its [vulnerabilityName] | ||
* | ||
* @param authentication | ||
* @param vulnerabilityName | ||
* @param permission | ||
* @return true if user with [authentication] has [permission] for [vulnerabilityName] | ||
*/ | ||
fun hasPermission( | ||
authentication: Authentication?, | ||
vulnerabilityName: String, | ||
permission: Permission, | ||
): Boolean { | ||
authentication ?: return false | ||
|
||
return when { | ||
authentication.hasRole(Role.SUPER_ADMIN) -> true | ||
permission == Permission.READ -> true | ||
else -> hasFullPermission(vulnerabilityName, authentication) | ||
} | ||
} | ||
|
||
/** | ||
* @param vulnerabilityName | ||
* @param authentication | ||
* @return check permission | ||
*/ | ||
fun hasFullPermission(vulnerabilityName: String, authentication: Authentication): Boolean { | ||
val vulnerability = vulnerabilityService.findByName(vulnerabilityName).orNotFound { "Not found vulnerability $vulnerabilityName" } | ||
val linkUsers = vulnerabilityService.getUsers(vulnerability.requiredId()).map { it.user.name } | ||
|
||
return vulnerability.userId == (authentication.details as AuthenticationDetails).id || authentication.name in linkUsers | ||
} | ||
} |
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