Skip to content

Commit

Permalink
Permissions cleanup (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Nov 19, 2024
1 parent 612e91e commit 4967d1b
Show file tree
Hide file tree
Showing 26 changed files with 198 additions and 190 deletions.
42 changes: 22 additions & 20 deletions src/main/java/ru/org/linux/group/GroupPermissionService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
}
}

def isUndeletable(topic: Topic, user: AuthorizedSession): Boolean = {
def isUndeletable(topic: Topic)(implicit user: AnySession): Boolean = {
if (!topic.deleted || !user.moderator) {
false
} else {
Expand All @@ -81,16 +81,16 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
Math.max(group.topicRestriction, section.getTopicsRestriction)
}

def enableAllowAnonymousCheckbox(group: Group, currentUser: AnySession): Boolean = {
def enableAllowAnonymousCheckbox(group: Group)(implicit currentUser: AnySession): Boolean = {
currentUser.authorized && !group.premoderated &&
Math.max(group.commentsRestriction,
Section.getCommentPostscore(group.sectionId))<TopicPermissionService.POSTSCORE_REGISTERED_ONLY
}

def isTopicPostingAllowed(section: Section, currentUser: AnySession): Boolean =
def isTopicPostingAllowed(section: Section)(implicit currentUser: AnySession): Boolean =
isTopicPostingAllowed(section.getTopicsRestriction, currentUser.userOpt.orNull)

def isTopicPostingAllowed(group: Group, currentUser: AnySession): Boolean =
def isTopicPostingAllowed(group: Group)(implicit currentUser: AnySession): Boolean =
isTopicPostingAllowed(effectivePostscore(group), currentUser.userOpt.orNull)

private def isTopicPostingAllowed(restriction: Int, @Nullable currentUser: User): Boolean = {
Expand All @@ -110,7 +110,7 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
}
}

def isImagePostingAllowed(section: Section, currentUser: AnySession): Boolean = {
def isImagePostingAllowed(section: Section)(implicit currentUser: AnySession): Boolean = {
if (section.isImagepost) {
true
} else if (currentUser.authorized &&
Expand All @@ -121,8 +121,8 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
}
}

def additionalImageLimit(section: Section, currentUser: AnySession): Int = {
if (isImagePostingAllowed(section, currentUser) &&
def additionalImageLimit(section: Section)(implicit currentUser: AnySession): Int = {
if (isImagePostingAllowed(section) &&
section.getId == Section.SECTION_GALLERY &&
currentUser.administrator) {
3
Expand All @@ -148,16 +148,16 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
}
}

def isDeletable(topic: Topic, user: User): Boolean = {
if (user.isAdministrator) {
def isDeletable(topic: Topic)(implicit user: AuthorizedSession): Boolean = {
if (user.administrator) {
true
} else {
val section = sectionService.getSection(topic.sectionId)

val deletableByUser = isDeletableByUser(topic, user, section)
val deletableByUser = isDeletableByUser(topic, user.user, section)

if (!deletableByUser && user.isModerator) {
isDeletableByModerator(topic, user, section)
if (!deletableByUser && user.moderator) {
isDeletableByModerator(topic, user.user, section)
} else {
deletableByUser
}
Expand Down Expand Up @@ -188,10 +188,11 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
* Можно ли редактировать сообщения полностью
*
* @param topic тема
* @param by редактор
* @return true если можно, false если нет
*/
def isEditable(topic: PreparedTopic, @Nullable by: User): Boolean = {
def isEditable(topic: PreparedTopic)(implicit session: AnySession): Boolean = {
val by = session.userOpt.orNull

val message = topic.message
val section = topic.section
val author = topic.author
Expand Down Expand Up @@ -235,11 +236,10 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
* Можно ли редактировать теги сообщения
*
* @param topic тема
* @param byOpt редактор
* @return true если можно, false если нет
*/
def isTagsEditable(topic: PreparedTopic, byOpt: Option[User]): Boolean = {
val by = byOpt.orNull
def isTagsEditable(topic: PreparedTopic)(implicit session: AnySession): Boolean = {
val by = session.userOpt.orNull

val message = topic.message
val section = topic.section
Expand Down Expand Up @@ -276,14 +276,16 @@ class GroupPermissionService(sectionService: SectionService, deleteInfoDao: Dele
}
}

def canCreateTag(section: Section, @Nullable user: User): Boolean = {
def canCreateTag(section: Section)(implicit session: AnySession): Boolean = {
val user = session.userOpt.orNull

if (section.isPremoderated && user!=null && !user.isAnonymous) {
true
} else {
user != null && user.getScore >= CreateTagScore
}
}

def canCommit(userOpt: Option[User], topic: Topic): Boolean =
userOpt.exists(user => user.isModerator || (user.canCorrect && topic.authorUserId != user.getId))
def canCommit(topic: Topic)(implicit session: AnySession): Boolean =
session.userOpt.exists(user => user.isModerator || (user.canCorrect && topic.authorUserId != user.getId))
}
26 changes: 13 additions & 13 deletions src/main/java/ru/org/linux/tag/TagPageController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
imageService: ImageService, groupPermissionService: GroupPermissionService) extends StrictLogging {

@RequestMapping(method = Array(RequestMethod.GET, RequestMethod.HEAD))
def tagPage(@PathVariable tag: String): CompletionStage[ModelAndView] = MaybeAuthorized { currentUser =>
def tagPage(@PathVariable tag: String): CompletionStage[ModelAndView] = MaybeAuthorized { implicit currentUser =>
val deadline = TagPageController.Timeout.fromNow

if (!TagName.isGoodTag(tag)) {
Expand Down Expand Up @@ -93,11 +93,11 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
Future.successful(new ModelAndView(new RedirectView(mainName.url.get, false, false))).toJava
}.getOrElse(throw new TagNotFoundException())
case Some(tagInfo) =>
val (news, newsDate) = getNewsSection(tag, currentUser)
val (forum, forumDate) = getTopicList(tag, tagInfo.id, Section.SECTION_FORUM, CommitMode.POSTMODERATED_ONLY, currentUser)
val gallery = getGallerySection(tag, tagInfo.id, currentUser)
val (polls, _) = getTopicList(tag, tagInfo.id, Section.SECTION_POLLS, CommitMode.COMMITED_ONLY, currentUser)
val (articles, _) = getTopicList(tag, tagInfo.id, Section.SECTION_ARTICLES, CommitMode.COMMITED_ONLY, currentUser)
val (news, newsDate) = getNewsSection(tag)
val (forum, forumDate) = getTopicList(tag, tagInfo.id, Section.SECTION_FORUM, CommitMode.POSTMODERATED_ONLY)
val gallery = getGallerySection(tag, tagInfo.id)
val (polls, _) = getTopicList(tag, tagInfo.id, Section.SECTION_POLLS, CommitMode.COMMITED_ONLY)
val (articles, _) = getTopicList(tag, tagInfo.id, Section.SECTION_ARTICLES, CommitMode.COMMITED_ONLY)

val newsFirst = newsDate.isDefined && (newsDate.exists(isRecent) || newsDate.zip(forumDate).exists(p => p._1.isAfter(p._2)))

Expand Down Expand Up @@ -138,7 +138,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
}
}

private def getNewsSection(tag: String, currentUser: AnySession) = {
private def getNewsSection(tag: String)(implicit currentUser: AnySession) = {
val newsSection = sectionService.getSection(Section.SECTION_NEWS)
val newsTopics = topicListService.getTopicsFeed(newsSection, None, Some(tag), 0, None,
TagPageController.TotalNewsCount, currentUser.userOpt, noTalks = false, tech = false)
Expand All @@ -150,7 +150,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
}

val tmpl = Template.getTemplate
val fullNews = prepareService.prepareTopicsForUser(fullNewsTopics, currentUser.opt, tmpl.getProf, loadUserpics = false)
val fullNews = prepareService.prepareTopicsForUser(fullNewsTopics, tmpl.getProf, loadUserpics = false)

val briefNewsByDate = TopicListTools.datePartition(briefNewsTopics)

Expand All @@ -162,7 +162,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ

val newestDate = newsTopics.headOption.map(_.commitDate.toInstant)

val addNews = if (groupPermissionService.isTopicPostingAllowed(newsSection, currentUser)) {
val addNews = if (groupPermissionService.isTopicPostingAllowed(newsSection)) {
Some("addNews" -> AddTopicController.getAddUrl(newsSection, tag))
} else {
None
Expand All @@ -174,11 +174,11 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
) ++ more ++ addNews, newestDate)
}

private def getGallerySection(tag: String, tagId: Int, currentUser: AnySession) = {
private def getGallerySection(tag: String, tagId: Int)(implicit currentUser: AnySession) = {
val list = imageService.prepareGalleryItem(imageService.getGalleryItems(TagPageController.GalleryCount, tagId).asJava)
val section = sectionService.getSection(Section.SECTION_GALLERY)

val add = if (groupPermissionService.isTopicPostingAllowed(section, currentUser)) {
val add = if (groupPermissionService.isTopicPostingAllowed(section)) {
Some("addGallery" -> AddTopicController.getAddUrl(section, tag))
} else {
None
Expand All @@ -195,7 +195,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
) ++ add ++ more
}

private def getTopicList(tag: String, tagId: Int, section: Int, mode: CommitMode, currentUser: AnySession) = {
private def getTopicList(tag: String, tagId: Int, section: Int, mode: CommitMode)(implicit currentUser: AnySession) = {
val forumSection = sectionService.getSection(section)

val topicListDto = new TopicListDto
Expand All @@ -213,7 +213,7 @@ class TagPageController(tagService: TagService, prepareService: TopicPrepareServ
None
}

val add = if (groupPermissionService.isTopicPostingAllowed(forumSection, currentUser)) {
val add = if (groupPermissionService.isTopicPostingAllowed(forumSection)) {
Some(forumSection.getUrlName+"Add" -> AddTopicController.getAddUrl(forumSection, tag))
} else {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AddCommentController(ipBlockDao: IPBlockDao, commentPrepareService: Commen

val postscore = topicPermissionService.getPostscore(add.getTopic)

new ModelAndView("add_comment", (commentService.prepareReplyto(add, currentUser.opt, tmpl.getProf, add.getTopic) + (
new ModelAndView("add_comment", (commentService.prepareReplyto(add, currentUser, tmpl.getProf, add.getTopic) + (
"postscoreInfo" -> TopicPermissionService.getPostScoreInfo(postscore)
)).asJava)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ class AddCommentController(ipBlockDao: IPBlockDao, commentPrepareService: Commen

add.setMsg(StringUtil.escapeForceHtml(add.getMsg))

new ModelAndView("add_comment", (commentService.prepareReplyto(add, sessionUserOpt.opt, tmpl.getProf, add.getTopic) ++ info).asJava)
new ModelAndView("add_comment", (commentService.prepareReplyto(add, sessionUserOpt, tmpl.getProf, add.getTopic) ++ info).asJava)
} else {
val (msgid, mentions) = commentService.create(user, comment, msg, remoteAddress = request.getRemoteAddr,
xForwardedFor = Option(request.getHeader("X-Forwarded-For")), userAgent = Option(request.getHeader("user-agent")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ class CommentCreateService(commentDao: CommentDao, topicDao: TopicDao, userServi
}

@throws[UserNotFoundException]
def prepareReplyto(add: CommentRequest, currentUser: Option[AuthorizedSession], profile: Profile, topic: Topic): Map[String, AnyRef] = {
def prepareReplyto(add: CommentRequest, currentUser: AnySession, profile: Profile, topic: Topic): Map[String, AnyRef] = {
if (add.getReplyto != null) {
val ignoreList = currentUser.map(user => ignoreListDao.get(user.user.getId)).getOrElse(Set.empty)
val ignoreList = currentUser.opt.map(user => ignoreListDao.get(user.user.getId)).getOrElse(Set.empty)

val preparedComment = commentPrepareService.prepareCommentOnly(add.getReplyto, currentUser, profile, topic, ignoreList)

Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/ru/org/linux/comment/CommentPrepareService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package ru.org.linux.comment
import com.google.common.base.Strings
import org.joda.time.{DateTime, Duration}
import org.springframework.stereotype.Service
import ru.org.linux.auth.AuthorizedSession
import ru.org.linux.auth.AnySession
import ru.org.linux.group.{Group, GroupDao}
import ru.org.linux.markup.MessageTextService
import ru.org.linux.reaction.{PreparedReactions, ReactionService}
Expand All @@ -39,7 +39,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase

private def prepareComment(messageText: MessageText, author: User, remark: Option[String], comment: Comment,
comments: Option[CommentList], profile: Profile, topic: Topic,
hideSet: Set[Int], samePageComments: Set[Int], currentUser: Option[AuthorizedSession],
hideSet: Set[Int], samePageComments: Set[Int], currentUser: AnySession,
group: Group, ignoreList: Set[Int], filterShow: Boolean, warnings: Seq[Warning]) = {
val processedMessage = textService.renderCommentText(messageText, !topicPermissionService.followAuthorLinks(author))

Expand Down Expand Up @@ -98,15 +98,15 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
val apiDeleteInfo = deleteInfo.map(i => new ApiDeleteInfo(userService.getUserCached(i.userid).getNick, i.getReason))
val editSummary = loadEditSummary(comment)

val (postIP, userAgent) = if (currentUser != null && currentUser.exists(_.moderator)) {
val (postIP, userAgent) = if (currentUser != null && currentUser.moderator) {
(Option(comment.postIP), userAgentDao.getUserAgentById(comment.userAgentId).toScala)
} else {
(None, None)
}

val undeletable = topicPermissionService.isUndeletable(topic, comment, currentUser.map(_.user).orNull, deleteInfo)
val deletable = topicPermissionService.isCommentDeletableNow(comment, currentUser.map(_.user).orNull, topic, hasAnswers)
val editable = topicPermissionService.isCommentEditableNow(comment, currentUser.map(_.user).orNull, hasAnswers, topic, messageText.markup)
val undeletable = topicPermissionService.isUndeletable(topic, comment, currentUser.userOpt.orNull, deleteInfo)
val deletable = topicPermissionService.isCommentDeletableNow(comment, currentUser.userOpt.orNull, topic, hasAnswers)
val editable = topicPermissionService.isCommentEditableNow(comment, currentUser.userOpt.orNull, hasAnswers, topic, messageText.markup)
val warningsAllowed = topicPermissionService.canPostWarning(currentUser, topic, Some(comment))

val authorReadonly = !topicPermissionService.isCommentsAllowed(group, topic, Some(author), ignoreFrozen = true)
Expand All @@ -118,7 +118,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
editSummary = editSummary, postIP = postIP, userAgent = userAgent, undeletable = undeletable,
answerCount = answerCount, answerLink = answerLink, answerSamepage = answerSamepage,
authorReadonly = authorReadonly,
reactions = reactionPrepareService.prepare(comment.reactions, ignoreList, currentUser.map(_.user), topic, Some(comment)),
reactions = reactionPrepareService.prepare(comment.reactions, ignoreList, currentUser.userOpt, topic, Some(comment)),
warningsAllowed = warningsAllowed, warnings = preparedWarnings)
}

Expand All @@ -138,7 +138,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
}
}

def prepareCommentOnly(comment: Comment, currentUser: Option[AuthorizedSession], profile: Profile,
def prepareCommentOnly(comment: Comment, currentUser: AnySession, profile: Profile,
topic: Topic, ignoreList: Set[Int]): PreparedComment = {
assert(comment.topicId == topic.id)

Expand Down Expand Up @@ -171,7 +171,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
}

def prepareCommentList(comments: CommentList, list: Seq[Comment], topic: Topic, hideSet: Set[Int],
currentUser: Option[AuthorizedSession], profile: Profile, ignoreList: Set[Int],
currentUser: AnySession, profile: Profile, ignoreList: Set[Int],
filterShow: Boolean): Seq[PreparedComment] = {
if (list.isEmpty) {
Seq.empty
Expand All @@ -180,13 +180,13 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
val users = userService.getUsersCachedMap(list.map(_.userid))
val group = groupDao.getGroup(topic.groupId)

val allWarnings: Map[Int, Seq[Warning]] = if (!topic.expired && currentUser.exists(_.moderator)) {
val allWarnings: Map[Int, Seq[Warning]] = if (!topic.expired && currentUser.moderator) {
warningService.load(list)
} else {
Map.empty
}

val remarks = currentUser.map { user =>
val remarks = currentUser.opt.map { user =>
remarkDao.getRemarks(user.user, users.values)
}.getOrElse(Map.empty[Int, Remark])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class DeleteCommentController(searchQueueSender: SearchQueueSender, commentServi
"comments" -> comments,
"topic" -> topic,
"commentsPrepared" -> prepareService.prepareCommentList(comments, list, topic, Set.empty[Int],
Some(currentUser), tmpl.getProf, ignoreList, filterShow = false).asJava
currentUser, tmpl.getProf, ignoreList, filterShow = false).asJava
).asJava)
}

Expand Down Expand Up @@ -195,7 +195,7 @@ class DeleteCommentController(searchQueueSender: SearchQueueSender, commentServi
val ignoreList = ignoreListDao.get(currentUser.user.getId)

new ModelAndView("undelete_comment", Map[String, Any](
"comment" -> prepareService.prepareCommentOnly(comment, Some(currentUser), tmpl.getProf, topic, ignoreList),
"comment" -> prepareService.prepareCommentOnly(comment, currentUser, tmpl.getProf, topic, ignoreList),
"topic" -> topic
).asJava)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class EditCommentController(commentService: CommentCreateService, msgbaseDao: Ms

val ignoreList = ignoreListDao.get(currentUser.user.getId)

formParams.put("comment", commentPrepareService.prepareCommentOnly(comment, Some(currentUser), tmpl.getProf,
formParams.put("comment", commentPrepareService.prepareCommentOnly(comment, currentUser, tmpl.getProf,
topic, ignoreList))

topicPermissionService.getEditDeadline(comment).foreach(value => formParams.put("deadline", value.toDate))
Expand Down Expand Up @@ -106,7 +106,7 @@ class EditCommentController(commentService: CommentCreateService, msgbaseDao: Ms
val comment = commentService.getComment(commentRequest, user, request)
val tmpl = Template.getTemplate

val formParams = new util.HashMap[String, AnyRef](commentService.prepareReplyto(commentRequest, Some(currentUser),
val formParams = new util.HashMap[String, AnyRef](commentService.prepareReplyto(commentRequest, currentUser,
tmpl.getProf, commentRequest.getTopic).asJava)

val originalMessageText = msgbaseDao.getMessageText(commentRequest.getOriginal.id)
Expand Down
Loading

0 comments on commit 4967d1b

Please sign in to comment.