-
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.
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...kend/src/main/kotlin/com/saveourtool/save/backend/controllers/UserPermissionController.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,47 @@ | ||
package com.saveourtool.save.backend.controllers | ||
|
||
import com.saveourtool.save.backend.security.UserPermissionEvaluator | ||
import com.saveourtool.save.configs.ApiSwaggerSupport | ||
import com.saveourtool.save.info.UserPermissions | ||
import com.saveourtool.save.utils.blockingToMono | ||
import com.saveourtool.save.v1 | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
import reactor.core.publisher.Mono | ||
|
||
/** | ||
* Controller for user permissions. | ||
*/ | ||
@ApiSwaggerSupport | ||
@RestController | ||
@RequestMapping(path = ["/api/$v1"]) | ||
class UserPermissionController( | ||
private val userPermissionEvaluator: UserPermissionEvaluator, | ||
) { | ||
/** | ||
* @param authentication | ||
* @return UserPermissions | ||
*/ | ||
@GetMapping("/users/permissions") | ||
fun getUserPermissions( | ||
authentication: Authentication, | ||
): Mono<UserPermissions> = blockingToMono { | ||
userPermissionEvaluator.getUserPermissions(authentication) | ||
} | ||
|
||
/** | ||
* @param authentication | ||
* @param organizationName | ||
* @return UserPermissions | ||
*/ | ||
@GetMapping("/users/permissions-by-organization") | ||
fun getUserPermissions( | ||
authentication: Authentication, | ||
@RequestParam organizationName: String, | ||
): Mono<UserPermissions> = blockingToMono { | ||
userPermissionEvaluator.getUserPermissionsByOrganizationName(authentication, organizationName) | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...-backend/src/main/kotlin/com/saveourtool/save/backend/security/UserPermissionEvaluator.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,49 @@ | ||
package com.saveourtool.save.backend.security | ||
|
||
import com.saveourtool.save.authservice.utils.username | ||
import com.saveourtool.save.backend.service.LnkUserOrganizationService | ||
import com.saveourtool.save.info.UserPermissions | ||
import com.saveourtool.save.info.UserPermissionsInOrganization | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* Class that is capable of assessing user's permissions regarding. | ||
*/ | ||
@Component | ||
class UserPermissionEvaluator( | ||
private var lnkUserOrganizationService: LnkUserOrganizationService, | ||
) { | ||
/** | ||
* @param authentication | ||
* @return UserPermissions | ||
*/ | ||
fun getUserPermissions( | ||
authentication: Authentication, | ||
): UserPermissions { | ||
val lnkOrganizations = lnkUserOrganizationService.getOrganizationsByUserNameAndCreatedStatus(authentication.username()) | ||
|
||
return UserPermissions( | ||
lnkOrganizations.associate { it.organization.name to UserPermissionsInOrganization(it.organization.canCreateContests, it.organization.canBulkUpload) }, | ||
) | ||
} | ||
|
||
/** | ||
* @param authentication | ||
* @param organizationName | ||
* @return UserPermissions | ||
*/ | ||
fun getUserPermissionsByOrganizationName( | ||
authentication: Authentication, | ||
organizationName: String, | ||
): UserPermissions { | ||
val lnkOrganization = lnkUserOrganizationService.getOrganizationsByUserNameAndCreatedStatusAndOrganizationName(authentication.username(), organizationName) | ||
|
||
val isPermittedCreateContest = lnkOrganization?.organization?.canCreateContests ?: false | ||
val isPermittedToBulkUpload = lnkOrganization?.organization?.canBulkUpload ?: false | ||
|
||
return UserPermissions( | ||
mapOf(organizationName to UserPermissionsInOrganization(isPermittedCreateContest, isPermittedToBulkUpload)), | ||
) | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/info/UserPermissions.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,11 @@ | ||
package com.saveourtool.save.info | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* @property inOrganizations user permissions in organizations | ||
*/ | ||
@Serializable | ||
data class UserPermissions( | ||
val inOrganizations: Map<String, UserPermissionsInOrganization> = emptyMap(), | ||
) |
13 changes: 13 additions & 0 deletions
13
...d-common/src/commonMain/kotlin/com/saveourtool/save/info/UserPermissionsInOrganization.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,13 @@ | ||
package com.saveourtool.save.info | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* @property canCreateContest permission for create contests in organizations | ||
* @property canDoBulkUpload permission for upload COSV files in organizations | ||
*/ | ||
@Serializable | ||
data class UserPermissionsInOrganization( | ||
val canCreateContest: Boolean, | ||
val canDoBulkUpload: Boolean, | ||
) |