Skip to content

Commit

Permalink
post warning permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Oct 29, 2024
1 parent 9975f9c commit 96264d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
24 changes: 6 additions & 18 deletions src/main/scala/ru/org/linux/warning/WarningController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,10 @@ class PostWarningRequest(@BeanProperty var topic: Topic, @BeanProperty var comme
class WarningController(warningService: WarningService, topicDao: TopicDao, commentReadService: CommentReadService) {
@RequestMapping(value = Array("/post-warning"), method = Array(RequestMethod.GET))
def showForm(@ModelAttribute(value = "request") request: PostWarningRequest): ModelAndView = AuthUtil.AuthorizedOnly { currentUser =>
if (!warningService.canPostWarning(currentUser)) {
if (!warningService.canPostWarning(currentUser, request.topic, Option(request.comment))) {
throw new AccessViolationException("Вы не можете отправить уведомление")
}

if (request.topic.isDeleted) {
throw new AccessViolationException("Топик удален")
}

if (request.topic.isExpired) {
throw new AccessViolationException("Топик перемещен в архив")
}

if (request.comment!=null && request.comment.isDeleted) {
throw new AccessViolationException("Комментарий удален")
}

// TODO rate limit warning
// TODO show topic / comment

Expand All @@ -62,19 +50,19 @@ class WarningController(warningService: WarningService, topicDao: TopicDao, comm
@RequestMapping(value = Array("/post-warning"), method = Array(RequestMethod.POST))
def post(@ModelAttribute(value = "request") request: PostWarningRequest,
errors: Errors): ModelAndView = AuthUtil.AuthorizedOnly { currentUser =>
if (!warningService.canPostWarning(currentUser)) {
throw new AccessViolationException("Вы не можете отправить уведомление")
if (!warningService.canPostWarning(currentUser, request.topic, Option(request.comment))) {
errors.reject(null, "Вы не можете отправить уведомление")
}

if (request.topic.isDeleted) {
if (request.topic.deleted) {
errors.reject(null, "Топик удален")
}

if (request.topic.isExpired) {
if (request.topic.expired) {
errors.reject(null, "Топик перемещен в архив")
}

if (request.comment!=null && request.comment.isDeleted) {
if (request.comment!=null && request.comment.deleted) {
errors.reject(null, "Комментарий удален")
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/ru/org/linux/warning/WarningService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class WarningService(warningDao: WarningDao, eventService: UserEventService, use
warningDao.postWarning(topicId = topic.id, commentId = comment.map(_.id), authorId = author.getId, message = message)
}

def canPostWarning(user: CurrentUser): Boolean = {
user.moderator
def canPostWarning(user: CurrentUser, topic: Topic, comment: Option[Comment]): Boolean = {
!topic.deleted && !topic.expired && comment.forall(!_.deleted) && user.moderator
}
}

0 comments on commit 96264d6

Please sign in to comment.