-
Notifications
You must be signed in to change notification settings - Fork 109
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
11 changed files
with
129 additions
and
95 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/java/ru/org/linux/user/UserPermissionService.scala
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,79 @@ | ||
/* | ||
* Copyright 1998-2024 Linux.org.ru | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ru.org.linux.user | ||
|
||
import org.springframework.stereotype.Service | ||
import ru.org.linux.auth.{AuthorizedSession, IPBlockDao} | ||
import ru.org.linux.spring.dao.DeleteInfoDao | ||
import ru.org.linux.user.UserPermissionService.* | ||
|
||
import java.time.Duration | ||
|
||
object UserPermissionService { | ||
val MaxTotalInvites = 15 | ||
val MaxUserInvites = 1 | ||
val MaxInviteScoreLoss = 10 | ||
val InviteScore = 200 | ||
val MaxUnactivatedPerIp = 2 | ||
val MaxUserpicScoreLoss = 20 | ||
} | ||
|
||
@Service | ||
class UserPermissionService(userLogDao: UserLogDao, userInvitesDao: UserInvitesDao, deleteInfoDao: DeleteInfoDao, | ||
ipBlockDao: IPBlockDao, userDao: UserDao) { | ||
def canResetPassword(user: User): Boolean = { | ||
!userLogDao.hasRecentSelfEvent(user, Duration.ofDays(7), UserLogAction.SENT_PASSWORD_RESET) | ||
} | ||
|
||
def canInvite(implicit session: AuthorizedSession): Boolean = session.moderator || { | ||
lazy val (totalInvites, userInvites) = userInvitesDao.countValidInvites(session.user) | ||
lazy val userScoreLoss = deleteInfoDao.getRecentScoreLoss(session.user) | ||
|
||
!session.user.isFrozen && session.user.getScore > InviteScore && | ||
totalInvites < MaxTotalInvites && | ||
userInvites < MaxUserInvites && | ||
userScoreLoss < MaxInviteScoreLoss | ||
} | ||
|
||
def canRegister(remoteAddr: String): Boolean = !ipBlockDao.getBlockInfo(remoteAddr).isBlocked && | ||
userDao.countUnactivated(remoteAddr) < MaxUnactivatedPerIp | ||
|
||
def canLoadUserpic(implicit session: AuthorizedSession): Boolean = { | ||
def userpicSetCount = userLogDao.getUserpicSetCount(session.user, Duration.ofHours(1)) | ||
|
||
def wasReset = userLogDao.hasRecentModerationEvent(session.user, Duration.ofDays(30), UserLogAction.RESET_USERPIC) | ||
|
||
def userScoreLoss = deleteInfoDao.getRecentScoreLoss(session.user) | ||
|
||
session.user.getScore >= 45 && | ||
!session.user.isFrozen && | ||
(userpicSetCount < 3) && | ||
!wasReset && | ||
(userScoreLoss < MaxUserpicScoreLoss) | ||
} | ||
|
||
def canEditProfileInfo(implicit session: AuthorizedSession): Boolean = { | ||
import session.user | ||
|
||
!user.isFrozen && | ||
!userLogDao.hasRecentModerationEvent(user, Duration.ofDays(1), UserLogAction.RESET_INFO) && | ||
!userLogDao.hasRecentModerationEvent(user, Duration.ofDays(1), UserLogAction.RESET_URL) && | ||
!userLogDao.hasRecentModerationEvent(user, Duration.ofDays(1), UserLogAction.RESET_TOWN) | ||
} | ||
|
||
def canResetPasswordByCode(user: User): Boolean = | ||
!user.isBlocked && user.isActivated && !user.isAnonymous && !user.isAdministrator | ||
} | ||
|
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
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
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.