Skip to content

Commit

Permalink
fix: No Session issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar committed Dec 18, 2023
1 parent 1776733 commit 166bbc0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class TranslationCommentController(
@UseDefaultPermissions
@AllowApiAccess
fun get(@PathVariable translationId: Long, @PathVariable commentId: Long): TranslationCommentModel {
val comment = translationCommentService.get(commentId)
val comment = translationCommentService.getWithAuthorFetched(commentId)
comment.checkFromProject()
return translationCommentModelAssembler.toModel(comment)
}
Expand All @@ -106,7 +106,7 @@ class TranslationCommentController(
@UseDefaultPermissions // Security: Permission check done inside; users should be able to edit their comments
@AllowApiAccess
fun update(@PathVariable commentId: Long, @RequestBody @Valid dto: TranslationCommentDto): TranslationCommentModel {
val comment = translationCommentService.get(commentId)
val comment = translationCommentService.getWithAuthorFetched(commentId)
if (comment.author.id != authenticationFacade.authenticatedUser.id) {
throw BadRequestException(io.tolgee.constants.Message.CAN_EDIT_ONLY_OWN_COMMENT)
}
Expand All @@ -123,7 +123,7 @@ class TranslationCommentController(
@PathVariable commentId: Long,
@PathVariable state: TranslationCommentState
): TranslationCommentModel {
val comment = translationCommentService.get(commentId)
val comment = translationCommentService.getWithAuthorFetched(commentId)
comment.checkFromProject()
translationCommentService.setState(comment, state)
return translationCommentModelAssembler.toModel(comment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ When null, resulting file will be a flat key-value object.
@PutMapping("")
@Operation(summary = "Sets translations for existing key")
@RequestActivity(ActivityType.SET_TRANSLATIONS)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_EDIT ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_EDIT])
@AllowApiAccess
fun setTranslations(@RequestBody @Valid dto: SetTranslationsWithKeyDto): SetTranslationsResponseModel {
val key = keyService.get(projectHolder.project.id, dto.key, dto.namespace)
Expand All @@ -182,8 +182,7 @@ When null, resulting file will be a flat key-value object.

val translations = dto.languagesToReturn
?.let { languagesToReturn ->
key.translations
.filter { languagesToReturn.contains(it.language.tag) }
translationService.findForKeyByLanguages(key, languagesToReturn)
.associateBy { it.language.tag }
}
?: modifiedTranslations
Expand All @@ -194,7 +193,7 @@ When null, resulting file will be a flat key-value object.
@PostMapping("")
@Operation(summary = "Sets translations for existing or not existing key.")
@RequestActivity(ActivityType.SET_TRANSLATIONS)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_EDIT ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_EDIT])
@AllowApiAccess
fun createOrUpdateTranslations(@RequestBody @Valid dto: SetTranslationsWithKeyDto): SetTranslationsResponseModel {
val key = keyService.find(projectHolder.projectEntity.id, dto.key, dto.namespace)?.also {
Expand All @@ -211,7 +210,7 @@ When null, resulting file will be a flat key-value object.
@PutMapping("/{translationId}/set-state/{state}")
@Operation(summary = "Sets translation state")
@RequestActivity(ActivityType.SET_TRANSLATION_STATE)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_STATE_EDIT ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_STATE_EDIT])
@AllowApiAccess
fun setTranslationState(
@PathVariable translationId: Long,
Expand Down Expand Up @@ -266,7 +265,7 @@ When null, resulting file will be a flat key-value object.

@GetMapping(value = ["select-all"])
@Operation(summary = "Get select all keys")
@RequiresProjectPermissions([ Scope.KEYS_VIEW ])
@RequiresProjectPermissions([Scope.KEYS_VIEW])
@AllowApiAccess
fun getSelectAllKeyIds(
@ParameterObject @ModelAttribute("translationFilters") params: TranslationFilters,
Expand All @@ -289,7 +288,7 @@ When null, resulting file will be a flat key-value object.
@PutMapping(value = ["/{translationId:[0-9]+}/dismiss-auto-translated-state"])
@Operation(summary = """Removes "auto translated" indication""")
@RequestActivity(ActivityType.DISMISS_AUTO_TRANSLATED_STATE)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_STATE_EDIT ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_STATE_EDIT])
@AllowApiAccess
fun dismissAutoTranslatedState(
@PathVariable translationId: Long
Expand All @@ -304,7 +303,7 @@ When null, resulting file will be a flat key-value object.
@PutMapping(value = ["/{translationId:[0-9]+}/set-outdated-flag/{state}"])
@Operation(summary = """Set's "outdated" indication""")
@RequestActivity(ActivityType.SET_OUTDATED_FLAG)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_STATE_EDIT ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_STATE_EDIT])
@AllowApiAccess
fun setOutdated(
@PathVariable translationId: Long,
Expand All @@ -322,7 +321,7 @@ When null, resulting file will be a flat key-value object.
Sorting is not supported for supported. It is automatically sorted from newest to oldest."""
)
@RequiresProjectPermissions([ Scope.TRANSLATIONS_VIEW ])
@RequiresProjectPermissions([Scope.TRANSLATIONS_VIEW])
@AllowApiAccess
fun getTranslationHistory(
@PathVariable translationId: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tolgee.hateoas.translations.comments

import io.swagger.v3.oas.annotations.media.Schema
import io.tolgee.hateoas.user_account.SimpleUserAccountModel
import io.tolgee.hateoas.user_account.UserAccountModel
import io.tolgee.model.enums.TranslationCommentState
import org.springframework.hateoas.RepresentationModel
Expand All @@ -20,7 +21,7 @@ open class TranslationCommentModel(
val state: TranslationCommentState,

@Schema(description = "User who created the comment")
val author: UserAccountModel,
val author: SimpleUserAccountModel,

@Schema(description = "Date when it was created")
val createdAt: Date,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.tolgee.hateoas.translations.comments

import io.tolgee.api.v2.controllers.translation.TranslationCommentController
import io.tolgee.hateoas.user_account.SimpleUserAccountModelAssembler
import io.tolgee.hateoas.user_account.UserAccountModelAssembler
import io.tolgee.model.translation.TranslationComment
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport
Expand All @@ -9,7 +10,7 @@ import java.util.*

@Component
class TranslationCommentModelAssembler(
private val userAccountModelAssembler: UserAccountModelAssembler
private val simpleUserAccountModelAssembler: SimpleUserAccountModelAssembler
) : RepresentationModelAssemblerSupport<TranslationComment, TranslationCommentModel>(
TranslationCommentController::class.java, TranslationCommentModel::class.java
) {
Expand All @@ -18,7 +19,7 @@ class TranslationCommentModelAssembler(
id = entity.id,
text = entity.text,
state = entity.state,
author = entity.author.let { userAccountModelAssembler.toModel(it) },
author = entity.author.let { simpleUserAccountModelAssembler.toModel(it) },
createdAt = entity.createdAt ?: Date(),
updatedAt = entity.updatedAt ?: Date()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,9 @@ interface TranslationRepository : JpaRepository<Translation, Long> {
"""
)
fun getAllByProjectId(projectId: Long): List<Translation>
@Query("""
from Translation t
where t.key = :key and t.language.tag in :languageTags
""")
fun findForKeyByLanguages(key: Key, languageTags: Collection<String>): List<Translation>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.springframework.stereotype.Repository
interface TranslationCommentRepository : JpaRepository<TranslationComment, Long> {
fun deleteAllByIdIn(ids: List<Long>)

@Query("select tc from TranslationComment tc where tc.translation = :translation")
@Query("select tc from TranslationComment tc left join fetch tc.author where tc.translation = :translation")
fun getPagedByTranslation(translation: Translation, pageable: Pageable): Page<TranslationComment>

fun deleteAllByTranslationIdIn(translationIds: Collection<Long>)
Expand All @@ -28,4 +28,14 @@ interface TranslationCommentRepository : JpaRepository<TranslationComment, Long>
"""
)
fun getAllByProjectId(projectId: Long): List<TranslationComment>


@Query(
"""
from TranslationComment tc
left join fetch tc.author
where tc.id = :id
"""
)
fun findWithFetchedAuthor(id: Long): TranslationComment?
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,26 @@ class TranslationCommentService(
}
}

@Transactional
fun find(id: Long): TranslationComment? {
return translationCommentRepository.findById(id).orElse(null)
}

@Transactional
fun get(id: Long): TranslationComment {
return find(id) ?: throw NotFoundException()
}

@Transactional
fun findWithAuthorFetched(id: Long): TranslationComment? {
return translationCommentRepository.findWithFetchedAuthor(id)
}

@Transactional
fun getWithAuthorFetched(id: Long): TranslationComment {
return findWithAuthorFetched(id) ?: throw NotFoundException()
}

@Transactional
fun update(dto: TranslationCommentDto, entity: TranslationComment): TranslationComment {
entity.text = dto.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ class TranslationService(
).mapKeys { it.key.tag }
}

fun findForKeyByLanguages(key: Key, languageTags: Collection<String>): List<Translation> {
return translationRepository.findForKeyByLanguages(key, languageTags)
}

private fun languageByTagFromLanguages(tag: String, languages: Collection<Language>) =
languages.find { it.tag == tag } ?: throw NotFoundException(Message.LANGUAGE_NOT_FOUND)

Expand Down

0 comments on commit 166bbc0

Please sign in to comment.