Skip to content

Commit

Permalink
fix: Key complex edit should return 400 on limit exceeded (#1765)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Jun 26, 2023
1 parent c4ee209 commit 8f794b2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class KeyController(
@AccessWithAnyProjectPermission
// key permissions are checked separately in method body
@AccessWithApiKey
@Transactional
fun complexEdit(@PathVariable id: Long, @RequestBody @Valid dto: ComplexEditKeyDto): KeyWithDataModel {
return KeyComplexEditHelper(applicationContext, id, dto).doComplexUpdate()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import io.tolgee.service.key.ScreenshotService
import io.tolgee.service.key.TagService
import io.tolgee.service.security.SecurityService
import io.tolgee.service.translation.TranslationService
import io.tolgee.util.executeInNewTransaction
import io.tolgee.util.getSafeNamespace
import org.springframework.context.ApplicationContext
import org.springframework.transaction.PlatformTransactionManager
import kotlin.properties.Delegates

class KeyComplexEditHelper(
Expand All @@ -34,6 +36,8 @@ class KeyComplexEditHelper(
private val tagService: TagService = applicationContext.getBean(TagService::class.java)
private val screenshotService: ScreenshotService = applicationContext.getBean(ScreenshotService::class.java)
private val activityHolder: ActivityHolder = applicationContext.getBean(ActivityHolder::class.java)
private val transactionManager: PlatformTransactionManager =
applicationContext.getBean(PlatformTransactionManager::class.java)

private lateinit var key: Key
private var modifiedTranslations: Map<String, String?>? = null
Expand All @@ -46,33 +50,35 @@ class KeyComplexEditHelper(
private var isScreenshotAdded by Delegates.notNull<Boolean>()

fun doComplexUpdate(): KeyWithDataModel {
prepareData()
prepareConditions()
setActivityHolder()

if (modifiedTranslations != null && areTranslationsModified) {
projectHolder.projectEntity.checkTranslationsEditPermission()
securityService.checkLanguageTagPermissions(modifiedTranslations!!.keys, projectHolder.project.id)
translationService.setForKey(key, translations = modifiedTranslations!!)
}
return executeInNewTransaction(transactionManager = transactionManager) {
prepareData()
prepareConditions()
setActivityHolder()

if (dtoTags !== null && areTagsModified) {
key.project.checkKeysEditPermission()
tagService.updateTags(key, dtoTags)
}
if (modifiedTranslations != null && areTranslationsModified) {
projectHolder.projectEntity.checkTranslationsEditPermission()
securityService.checkLanguageTagPermissions(modifiedTranslations!!.keys, projectHolder.project.id)
translationService.setForKey(key, translations = modifiedTranslations!!)
}

if (isScreenshotAdded || isScreenshotDeleted) {
updateScreenshotsWithPermissionCheck(dto, key)
}
if (dtoTags !== null && areTagsModified) {
key.project.checkKeysEditPermission()
tagService.updateTags(key, dtoTags)
}

var edited = key
if (isScreenshotAdded || isScreenshotDeleted) {
updateScreenshotsWithPermissionCheck(dto, key)
}

if (isKeyModified) {
key.project.checkKeysEditPermission()
edited = keyService.edit(key, dto.name, dto.namespace)
}
var edited = key

if (isKeyModified) {
key.project.checkKeysEditPermission()
edited = keyService.edit(key, dto.name, dto.namespace)
}

return keyWithDataModelAssembler.toModel(edited)
keyWithDataModelAssembler.toModel(edited)
}
}

private fun setActivityHolder() {
Expand Down

0 comments on commit 8f794b2

Please sign in to comment.