-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
152 changed files
with
4,099 additions
and
318 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
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
File renamed without changes.
100 changes: 100 additions & 0 deletions
100
...nd/api/src/main/kotlin/io/tolgee/api/v2/controllers/batch/BatchJobManagementController.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,100 @@ | ||
package io.tolgee.api.v2.controllers.batch | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import io.tolgee.batch.BatchJobCancellationManager | ||
import io.tolgee.batch.BatchJobDto | ||
import io.tolgee.batch.BatchJobService | ||
import io.tolgee.hateoas.batch.BatchJobModel | ||
import io.tolgee.hateoas.batch.BatchJobModelAssembler | ||
import io.tolgee.model.batch.BatchJob | ||
import io.tolgee.model.enums.Scope | ||
import io.tolgee.model.views.BatchJobView | ||
import io.tolgee.security.AuthenticationFacade | ||
import io.tolgee.security.apiKeyAuth.AccessWithApiKey | ||
import io.tolgee.security.project_auth.AccessWithAnyProjectPermission | ||
import io.tolgee.security.project_auth.AccessWithProjectPermission | ||
import io.tolgee.security.project_auth.ProjectHolder | ||
import io.tolgee.service.security.SecurityService | ||
import org.springdoc.api.annotations.ParameterObject | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.data.web.PagedResourcesAssembler | ||
import org.springframework.data.web.SortDefault | ||
import org.springframework.hateoas.PagedModel | ||
import org.springframework.web.bind.annotation.CrossOrigin | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PutMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import javax.validation.Valid | ||
|
||
@RestController | ||
@CrossOrigin(origins = ["*"]) | ||
@RequestMapping(value = ["/v2/projects/{projectId:\\d+}/", "/v2/projects/"]) | ||
@Tag(name = "Batch job management") | ||
@Suppress("SpringJavaInjectionPointsAutowiringInspection", "MVCPathVariableInspection") | ||
class BatchJobManagementController( | ||
private val batchJobCancellationManager: BatchJobCancellationManager, | ||
private val batchJobService: BatchJobService, | ||
private val projectHolder: ProjectHolder, | ||
private val batchJobModelAssembler: BatchJobModelAssembler, | ||
private val pagedResourcesAssembler: PagedResourcesAssembler<BatchJobView>, | ||
private val authenticationFacade: AuthenticationFacade, | ||
private val securityService: SecurityService | ||
) { | ||
@GetMapping(value = ["batch-jobs"]) | ||
@AccessWithApiKey() | ||
@AccessWithProjectPermission(Scope.BATCH_JOBS_VIEW) | ||
@Operation(summary = "Lists all batch jobs in project") | ||
fun list(@Valid @ParameterObject @SortDefault("id") pageable: Pageable): PagedModel<BatchJobModel> { | ||
val views = batchJobService.getViews(projectHolder.project.id, null, pageable) | ||
return pagedResourcesAssembler.toModel(views, batchJobModelAssembler) | ||
} | ||
|
||
@GetMapping(value = ["my-batch-jobs"]) | ||
@AccessWithApiKey() | ||
@AccessWithAnyProjectPermission() | ||
@Operation(summary = "Lists all batch jobs in project started by current user") | ||
fun myList(@Valid @ParameterObject @SortDefault("id") pageable: Pageable): PagedModel<BatchJobModel> { | ||
val views = batchJobService.getViews( | ||
projectId = projectHolder.project.id, | ||
userAccount = authenticationFacade.userAccount, | ||
pageable = pageable | ||
) | ||
return pagedResourcesAssembler.toModel(views, batchJobModelAssembler) | ||
} | ||
|
||
@GetMapping(value = ["batch-jobs/{id}"]) | ||
@AccessWithApiKey() | ||
@AccessWithAnyProjectPermission() | ||
@Operation(summary = "Returns the batch job") | ||
fun get(@PathVariable id: Long): BatchJobModel { | ||
val view = batchJobService.getView(id) | ||
checkViewPermission(view.batchJob) | ||
return batchJobModelAssembler.toModel(view) | ||
} | ||
|
||
@PutMapping(value = ["batch-jobs/{id}/cancel"]) | ||
@AccessWithApiKey() | ||
@AccessWithAnyProjectPermission() | ||
@Operation(summary = "Stops batch job (if possible)") | ||
fun cancel(@PathVariable id: Long) { | ||
checkCancelPermission(batchJobService.getJobDto(id)) | ||
batchJobCancellationManager.cancel(id) | ||
} | ||
|
||
private fun checkViewPermission(job: BatchJob) { | ||
if (job.author?.id == authenticationFacade.userAccount.id) { | ||
return | ||
} | ||
securityService.checkProjectPermission(projectHolder.project.id, Scope.BATCH_JOBS_VIEW) | ||
} | ||
|
||
private fun checkCancelPermission(job: BatchJobDto) { | ||
if (job.authorId == authenticationFacade.userAccount.id) { | ||
return | ||
} | ||
securityService.checkProjectPermission(projectHolder.project.id, Scope.BATCH_JOBS_CANCEL) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
backend/api/src/main/kotlin/io/tolgee/api/v2/controllers/batch/StartBatchJobController.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,68 @@ | ||
package io.tolgee.api.v2.controllers.batch | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import io.tolgee.batch.BatchJobService | ||
import io.tolgee.batch.BatchJobType | ||
import io.tolgee.batch.request.BatchTranslateRequest | ||
import io.tolgee.batch.request.DeleteKeysRequest | ||
import io.tolgee.hateoas.batch.BatchJobModel | ||
import io.tolgee.hateoas.batch.BatchJobModelAssembler | ||
import io.tolgee.model.batch.BatchJob | ||
import io.tolgee.model.enums.Scope | ||
import io.tolgee.security.AuthenticationFacade | ||
import io.tolgee.security.apiKeyAuth.AccessWithApiKey | ||
import io.tolgee.security.project_auth.AccessWithProjectPermission | ||
import io.tolgee.security.project_auth.ProjectHolder | ||
import io.tolgee.service.security.SecurityService | ||
import org.springframework.web.bind.annotation.CrossOrigin | ||
import org.springframework.web.bind.annotation.PutMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import javax.validation.Valid | ||
|
||
@RestController | ||
@CrossOrigin(origins = ["*"]) | ||
@RequestMapping(value = ["/v2/projects/{projectId:\\d+}/start-batch-job", "/v2/projects/start-batch-job"]) | ||
@Tag(name = "Start batch jobs") | ||
@Suppress("SpringJavaInjectionPointsAutowiringInspection", "MVCPathVariableInspection") | ||
class StartBatchJobController( | ||
private val securityService: SecurityService, | ||
private val projectHolder: ProjectHolder, | ||
private val batchJobService: BatchJobService, | ||
private val authenticationFacade: AuthenticationFacade, | ||
private val batchJobModelAssembler: BatchJobModelAssembler | ||
) { | ||
@PutMapping(value = ["/translate"]) | ||
@AccessWithApiKey() | ||
@AccessWithProjectPermission(Scope.BATCH_AUTO_TRANSLATE) | ||
@Operation(summary = "Translates provided keys to provided languages") | ||
fun translate(@Valid @RequestBody data: BatchTranslateRequest): BatchJobModel { | ||
securityService.checkLanguageTranslatePermission(projectHolder.project.id, data.targetLanguageIds) | ||
securityService.checkKeyIdsExistAndIsFromProject(data.keyIds, projectHolder.project.id) | ||
return batchJobService.startJob( | ||
data, | ||
projectHolder.projectEntity, | ||
authenticationFacade.userAccountEntity, | ||
BatchJobType.TRANSLATION | ||
).model | ||
} | ||
|
||
@PutMapping(value = ["/delete-keys"]) | ||
@AccessWithApiKey() | ||
@AccessWithProjectPermission(Scope.KEYS_DELETE) | ||
@Operation(summary = "Translates provided keys to provided languages") | ||
fun deleteKeys(@Valid @RequestBody data: DeleteKeysRequest): BatchJobModel { | ||
securityService.checkKeyIdsExistAndIsFromProject(data.keyIds, projectHolder.project.id) | ||
return batchJobService.startJob( | ||
data, | ||
projectHolder.projectEntity, | ||
authenticationFacade.userAccountEntity, | ||
BatchJobType.DELETE_KEYS | ||
).model | ||
} | ||
|
||
val BatchJob.model | ||
get() = batchJobModelAssembler.toModel(batchJobService.getView(this)) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
16 changes: 14 additions & 2 deletions
16
backend/api/src/main/kotlin/io/tolgee/component/lockingProvider/RedissonLockingProvider.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 |
---|---|---|
@@ -1,11 +1,23 @@ | ||
package io.tolgee.component.lockingProvider | ||
|
||
import io.tolgee.component.LockingProvider | ||
import org.redisson.api.RLock | ||
import org.redisson.api.RedissonClient | ||
import java.util.concurrent.locks.Lock | ||
|
||
class RedissonLockingProvider(private val redissonClient: RedissonClient) : LockingProvider { | ||
override fun getLock(name: String): Lock { | ||
override fun getLock(name: String): RLock { | ||
return redissonClient.getLock(name) | ||
} | ||
|
||
override fun <T> withLocking(name: String, fn: () -> T): T { | ||
val lock = this.getLock(name) | ||
lock.lock() | ||
try { | ||
return fn() | ||
} finally { | ||
if (lock.isHeldByCurrentThread) { | ||
lock.unlock() | ||
} | ||
} | ||
} | ||
} |
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
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
40 changes: 40 additions & 0 deletions
40
backend/api/src/main/kotlin/io/tolgee/hateoas/batch/BatchJobModel.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,40 @@ | ||
package io.tolgee.hateoas.batch | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema | ||
import io.tolgee.batch.BatchJobType | ||
import io.tolgee.hateoas.user_account.SimpleUserAccountModel | ||
import io.tolgee.model.batch.BatchJobStatus | ||
import org.springframework.hateoas.RepresentationModel | ||
import org.springframework.hateoas.server.core.Relation | ||
import java.io.Serializable | ||
|
||
@Suppress("unused") | ||
@Relation(collectionRelation = "batchJobs", itemRelation = "batchJob") | ||
open class BatchJobModel( | ||
@Schema(description = "Batch job id") | ||
val id: Long, | ||
|
||
@Schema(description = "Status of the batch job") | ||
val status: BatchJobStatus, | ||
|
||
@Schema(description = "Type of the batch job") | ||
val type: BatchJobType, | ||
|
||
@Schema(description = "Total items, that have been processed so far") | ||
val progress: Int, | ||
|
||
@Schema(description = "Total items") | ||
val totalItems: Int, | ||
|
||
@Schema(description = "The user who started the job") | ||
val author: SimpleUserAccountModel?, | ||
|
||
@Schema(description = "The time when the job created") | ||
val createdAt: String, | ||
|
||
@Schema(description = "The activity revision id, that stores the activity details of the job") | ||
val activityRevisionId: Long?, | ||
|
||
@Schema(description = "If the job failed, this is the error message") | ||
val errorMessage: String?, | ||
) : RepresentationModel<BatchJobModel>(), Serializable |
28 changes: 28 additions & 0 deletions
28
backend/api/src/main/kotlin/io/tolgee/hateoas/batch/BatchJobModelAssembler.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,28 @@ | ||
package io.tolgee.hateoas.batch | ||
|
||
import io.tolgee.api.v2.controllers.batch.BatchJobManagementController | ||
import io.tolgee.hateoas.user_account.SimpleUserAccountModelAssembler | ||
import io.tolgee.model.views.BatchJobView | ||
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class BatchJobModelAssembler( | ||
val simpleUserAccountModelAssembler: SimpleUserAccountModelAssembler | ||
) : RepresentationModelAssemblerSupport<BatchJobView, BatchJobModel>( | ||
BatchJobManagementController::class.java, BatchJobModel::class.java | ||
) { | ||
override fun toModel(view: BatchJobView): BatchJobModel { | ||
return BatchJobModel( | ||
id = view.batchJob.id, | ||
type = view.batchJob.type, | ||
status = view.batchJob.status, | ||
progress = view.progress, | ||
totalItems = view.batchJob.totalItems, | ||
author = view.batchJob.author?.let { simpleUserAccountModelAssembler.toModel(it) }, | ||
createdAt = view.batchJob.createdAt.toString(), | ||
activityRevisionId = view.batchJob.activityRevision?.id, | ||
errorMessage = view.errorMessage?.code | ||
) | ||
} | ||
} |
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
Oops, something went wrong.