From 8a40d7cc738f5511313841e42161d71bb71e8133 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Fri, 12 Apr 2024 16:44:27 +0200 Subject: [PATCH] Granter no longer needs parameterization --- app/http/CtrlFilters.scala | 6 ++-- app/mashup/UserInfo.scala | 4 +-- app/templating/SecurityHelper.scala | 2 +- app/views/mod/permissions.scala | 4 +-- modules/api/src/main/AccountClosure.scala | 2 +- modules/api/src/main/ForumAccess.scala | 6 ++-- modules/api/src/main/RoundApi.scala | 4 +-- modules/chat/src/main/ChatApi.scala | 4 +-- modules/clas/src/main/Env.scala | 2 +- modules/core/src/main/perm.scala | 31 +++++++------------ modules/core/src/main/user.scala | 8 ----- modules/forum/src/main/ForumDelete.scala | 2 +- modules/forum/src/main/ForumPost.scala | 2 +- modules/forum/src/main/ForumPostApi.scala | 4 +-- modules/forum/src/main/ForumTopicApi.scala | 6 ++-- modules/insight/src/main/Share.scala | 4 +-- modules/mod/src/main/Inquiry.scala | 2 +- modules/mod/src/main/IpRender.scala | 2 +- modules/mod/src/main/ModApi.scala | 4 +-- modules/mod/src/main/Presets.scala | 2 +- modules/perfStat/src/main/PerfStatApi.scala | 2 +- modules/relay/src/main/RelayApi.scala | 6 ++-- modules/relay/src/main/RelayTourForm.scala | 8 ++--- modules/report/src/main/Reason.scala | 8 ++--- modules/report/src/main/Room.scala | 12 +++---- modules/security/src/main/Granter.scala | 13 ++++---- modules/study/src/main/ServerEval.scala | 2 +- modules/study/src/main/StudyInvite.scala | 8 ++--- modules/team/src/main/TeamApi.scala | 6 ++-- modules/team/src/main/TeamSecurity.scala | 6 ++-- .../tournament/src/main/TournamentForm.scala | 2 +- modules/ublog/src/main/UblogBlog.scala | 6 ++-- 32 files changed, 82 insertions(+), 98 deletions(-) diff --git a/app/http/CtrlFilters.scala b/app/http/CtrlFilters.scala index ae8eb84bad487..50c1044ef241f 100644 --- a/app/http/CtrlFilters.scala +++ b/app/http/CtrlFilters.scala @@ -11,13 +11,13 @@ import lila.core.perm.{ Granter, Permission } trait CtrlFilters extends ControllerHelpers with ResponseBuilder with CtrlConversions: def isGranted(permission: Permission.Selector)(using Me): Boolean = - Granter[Me](permission) + Granter(permission) def isGrantedOpt(permission: Permission.Selector)(using Option[Me]): Boolean = - Granter.opt[Me](permission) + Granter.opt(permission) // def isGranted(permission: Permission)(using me: Option[Me]): Boolean = - // Granter.opt[Me](permission) + // Granter.opt(permission) def NoCurrentGame(a: => Fu[Result])(using ctx: Context)(using Executor): Fu[Result] = ctx.me diff --git a/app/mashup/UserInfo.scala b/app/mashup/UserInfo.scala index 2b3d1d1bce0c4..e0cd748836618 100644 --- a/app/mashup/UserInfo.scala +++ b/app/mashup/UserInfo.scala @@ -61,9 +61,9 @@ object UserInfo: ).mapN(Social.apply) def fetchNotes(u: User)(using Me) = - noteApi.get(u, Granter[Me](_.ModNote)).dmap { + noteApi.get(u, Granter(_.ModNote)).dmap { _.filter: n => - (!n.dox || Granter[Me](_.Admin)) + (!n.dox || Granter(_.Admin)) } case class NbGames( diff --git a/app/templating/SecurityHelper.scala b/app/templating/SecurityHelper.scala index 3fbacc867dc21..31c91a1ba4cb4 100644 --- a/app/templating/SecurityHelper.scala +++ b/app/templating/SecurityHelper.scala @@ -8,7 +8,7 @@ import lila.core.user.User trait SecurityHelper: def isGranted(f: Permission.Selector)(using Option[Me]): Boolean = - Granter.opt[Me](f) + Granter.opt(f) def isGranted(permission: Permission.Selector, user: User): Boolean = Granter.ofUser(permission)(user) diff --git a/app/views/mod/permissions.scala b/app/views/mod/permissions.scala index 6e8ced4acd4e9..de5cffbf02f86 100644 --- a/app/views/mod/permissions.scala +++ b/app/views/mod/permissions.scala @@ -26,12 +26,12 @@ object permissions: p(cls := "granted")("In green, permissions enabled manually or by a package."), div(cls := "permission-list")( lila.security.Permission.categorized - .filter { (_, ps) => ps.exists(canGrant[Me](_)) } + .filter { (_, ps) => ps.exists(canGrant(_)) } .map: (categ, perms) => st.section( h2(categ), perms - .filter(canGrant[Me]) + .filter(canGrant) .map: perm => val id = s"permission-${perm.dbKey}" div( diff --git a/modules/api/src/main/AccountClosure.scala b/modules/api/src/main/AccountClosure.scala index 4fd9189560497..609cc2a6ac862 100644 --- a/modules/api/src/main/AccountClosure.scala +++ b/modules/api/src/main/AccountClosure.scala @@ -35,7 +35,7 @@ final class AccountClosure( def close(u: User)(using me: Me): Funit = for playbanned <- playbanApi.HasCurrentPlayban(u.id) selfClose = me.is(u) - modClose = !selfClose && Granter[Me](_.CloseAccount) + modClose = !selfClose && Granter(_.CloseAccount) badApple = u.lameOrTrollOrAlt || modClose _ <- userRepo.disable(u, keepEmail = badApple || playbanned) _ <- relationApi.unfollowAll(u.id) diff --git a/modules/api/src/main/ForumAccess.scala b/modules/api/src/main/ForumAccess.scala index 770f7a159c1de..93449be1d0d8c 100644 --- a/modules/api/src/main/ForumAccess.scala +++ b/modules/api/src/main/ForumAccess.scala @@ -32,13 +32,13 @@ final class ForumAccess( } def isGrantedRead(categId: ForumCategId)(using me: Option[Me]): Fu[Boolean] = - if Granter.opt[Me](_.Shusher) then fuTrue + if Granter.opt(_.Shusher) then fuTrue else isGranted(categId, Operation.Read) def isGrantedWrite(categId: ForumCategId, tryingToPostAsMod: Boolean = false)(using me: Option[Me] ): Fu[Boolean] = - if tryingToPostAsMod && Granter.opt[Me](_.Shusher) then fuTrue + if tryingToPostAsMod && Granter.opt(_.Shusher) then fuTrue else canWriteInAnyForum.so(isGranted(categId, Operation.Write)) private def canWriteInAnyForum(using me: Option[Me]) = me.exists: me => @@ -47,7 +47,7 @@ final class ForumAccess( } def isGrantedMod(categId: ForumCategId)(using meOpt: Option[Me]): Fu[Boolean] = meOpt.so: me => - if Granter.opt[Me](_.ModerateForum) then fuTrue + if Granter.opt(_.ModerateForum) then fuTrue else ForumCateg.toTeamId(categId).so(teamApi.hasPerm(_, me, _.Comm)) def isReplyBlockedOnUBlog(topic: ForumTopic, canModCateg: Boolean)(using me: Me): Fu[Boolean] = diff --git a/modules/api/src/main/RoundApi.scala b/modules/api/src/main/RoundApi.scala index 2fd314e8f6204..aa84a2248f0b5 100644 --- a/modules/api/src/main/RoundApi.scala +++ b/modules/api/src/main/RoundApi.scala @@ -99,7 +99,7 @@ final private[api] class RoundApi( private def ctxFlags(using ctx: Context) = ExportOptions( - blurs = Granter.opt[Me](_.ViewBlurs), + blurs = Granter.opt(_.ViewBlurs), rating = ctx.pref.showRatings, nvui = ctx.blind, lichobileCompat = HTTPRequest.isLichobile(ctx.req) @@ -123,7 +123,7 @@ final private[api] class RoundApi( ctx.me, tv, initialFen = initialFen, - flags = withFlags.copy(blurs = Granter.opt[Me](_.ViewBlurs)) + flags = withFlags.copy(blurs = Granter.opt(_.ViewBlurs)) ), tourApi.gameView.analysis(pov.game), pov.game.simulId.so(simulApi.find), diff --git a/modules/chat/src/main/ChatApi.scala b/modules/chat/src/main/ChatApi.scala index 571bfcd47f2a6..bed4b08527e9a 100644 --- a/modules/chat/src/main/ChatApi.scala +++ b/modules/chat/src/main/ChatApi.scala @@ -222,8 +222,8 @@ final class ChatApi( } .inject(change) - private def isMod(using Me) = Granter[Me](_.ChatTimeout) - private def isRelayMod(using Me) = Granter[Me](_.BroadcastTimeout) + private def isMod(using Me) = Granter(_.ChatTimeout) + private def isRelayMod(using Me) = Granter(_.BroadcastTimeout) def reinstate(list: List[ChatTimeout.Reinstate]) = list.foreach: r => diff --git a/modules/clas/src/main/Env.scala b/modules/clas/src/main/Env.scala index d3177cb77f6b6..216cff6b27be1 100644 --- a/modules/clas/src/main/Env.scala +++ b/modules/clas/src/main/Env.scala @@ -39,7 +39,7 @@ final class Env( lazy val markup = wire[ClasMarkup] def hasClas(using me: Me) = - lila.core.perm.Granter[Me](_.Teacher) || studentCache.isStudent(me) + lila.core.perm.Granter(_.Teacher) || studentCache.isStudent(me) lila.common.Bus.subscribeFuns( "finishGame" -> { case lila.game.actorApi.FinishGame(game, _) => diff --git a/modules/core/src/main/perm.scala b/modules/core/src/main/perm.scala index 7ccaf837dccb8..65f516daf1e4a 100644 --- a/modules/core/src/main/perm.scala +++ b/modules/core/src/main/perm.scala @@ -1,33 +1,26 @@ package lila.core package perm -import lila.core.user.{ User, UserEnabled } - -trait Grantable[U]: - def enabled(u: U): UserEnabled - def roles(u: U): List[String] - extension (u: U) - inline def isEnabled: Boolean = enabled(u).yes - inline def dbKeys: List[String] = roles(u) +import lila.core.user.{ User, Me } object Granter: - def apply[U: Grantable](permission: Permission)(using me: U): Boolean = - me.isEnabled && ofDbKeys(permission, me.dbKeys) + def apply(permission: Permission)(using me: Me): Boolean = + me.enabled.yes && ofDbKeys(permission, me.roles) - def apply[U: Grantable](f: Permission.Selector)(using me: U): Boolean = - me.isEnabled && ofDbKeys(f(Permission), me.dbKeys) + def apply(f: Permission.Selector)(using me: Me): Boolean = + me.enabled.yes && ofDbKeys(f(Permission), me.roles) - def opt[U: Grantable](f: Permission.Selector)(using me: Option[U]): Boolean = + def opt(f: Permission.Selector)(using me: Option[Me]): Boolean = me.exists(of(f)) - def of[U: Grantable](permission: Permission)(user: U): Boolean = - user.isEnabled && ofDbKeys(permission, user.dbKeys) + def of(permission: Permission)(user: User): Boolean = + user.enabled.yes && ofDbKeys(permission, user.roles) - def of[U: Grantable](f: Permission.Selector)(user: U): Boolean = - user.isEnabled && ofDbKeys(f(Permission), user.dbKeys) + def of(f: Permission.Selector)(user: User): Boolean = + user.enabled.yes && ofDbKeys(f(Permission), user.roles) - def ofUser(f: Permission.Selector)(user: User): Boolean = of[User](f)(user) + def ofUser(f: Permission.Selector)(user: User): Boolean = of(f)(user) def ofDbKeys(permission: Permission, dbKeys: Seq[String]): Boolean = Permission.ofDbKeys(dbKeys).exists(_.grants(permission)) @@ -248,6 +241,6 @@ object Permission: val allByDbKey: Map[String, Permission] = all.mapBy(_.dbKey) - def apply[U: Grantable](u: U): Set[Permission] = ofDbKeys(u.dbKeys) + def apply(u: User): Set[Permission] = ofDbKeys(u.roles) def ofDbKey(dbKey: String): Option[Permission] = allByDbKey.get(dbKey) def ofDbKeys(dbKeys: Seq[String]): Set[Permission] = dbKeys.flatMap(allByDbKey.get).toSet diff --git a/modules/core/src/main/user.scala b/modules/core/src/main/user.scala index c63ca4281c727..8c6702b4752a9 100644 --- a/modules/core/src/main/user.scala +++ b/modules/core/src/main/user.scala @@ -12,7 +12,6 @@ import lila.core.perf.{ PerfKey, UserPerfs, UserWithPerfs } import lila.core.userId.* import lila.core.email.* import lila.core.id.Flair -import lila.core.perm.Grantable object user: @@ -161,9 +160,6 @@ object user: object User: given UserIdOf[User] = _.id - given perm.Grantable[User] = new: - def enabled(u: User) = u.enabled - def roles(u: User) = u.roles case class Count( ai: Int, @@ -291,15 +287,11 @@ object user: given (using me: Me): LightUser.Me = me.lightMe given [M[_]]: Conversion[M[Me], M[User]] = Me.raw(_) given (using me: Me): Option[Me] = Some(me) - given Grantable[Me] = new Grantable[User]: - def enabled(me: Me) = me.enabled - def roles(me: Me) = me.roles extension (me: Me) def userId: UserId = me.id def lightMe: LightUser.Me = LightUser.Me(me.value.light) inline def modId: ModId = userId.into(ModId) inline def myId: MyId = userId.into(MyId) - // given (using me: Me): LightUser.Me = LightUser.Me(me.light) final class Flag(val code: Flag.Code, val name: Flag.Name, val abrev: Option[String]): def shortName = abrev | name diff --git a/modules/forum/src/main/ForumDelete.scala b/modules/forum/src/main/ForumDelete.scala index bbe96614f58ad..28b54490e0a6b 100644 --- a/modules/forum/src/main/ForumDelete.scala +++ b/modules/forum/src/main/ForumDelete.scala @@ -44,4 +44,4 @@ final class ForumDelete( } private def publishDelete(p: ForumPost)(using Me) = - Bus.chan.forumPost(RemovePost(p.id, p.userId, p.text, asAdmin = MasterGranter[Me](_.ModerateForum))) + Bus.chan.forumPost(RemovePost(p.id, p.userId, p.text, asAdmin = MasterGranter(_.ModerateForum))) diff --git a/modules/forum/src/main/ForumPost.scala b/modules/forum/src/main/ForumPost.scala index 3f9a2b9ac3dd8..652e5a67419f0 100644 --- a/modules/forum/src/main/ForumPost.scala +++ b/modules/forum/src/main/ForumPost.scala @@ -44,7 +44,7 @@ case class ForumPost( def canBeEditedByMe(using me: Me): Boolean = userId match case Some(userId) if me.is(userId) => true - case None if (Granter[Me](_.PublicMod) || Granter[Me](_.SeeReport)) && isAnonModPost => + case None if (Granter(_.PublicMod) || Granter(_.SeeReport)) && isAnonModPost => true case _ => false diff --git a/modules/forum/src/main/ForumPostApi.scala b/modules/forum/src/main/ForumPostApi.scala index 55fb558cbb842..25b5a73a8e05f 100644 --- a/modules/forum/src/main/ForumPostApi.scala +++ b/modules/forum/src/main/ForumPostApi.scala @@ -32,8 +32,8 @@ final class ForumPostApi( data: ForumForm.PostData )(using me: Me): Fu[ForumPost] = detectLanguage(data.text).zip(recentUserIds(topic, topic.nbPosts)).flatMap { (lang, topicUserIds) => - val publicMod = MasterGranter[Me](_.PublicMod) - val modIcon = ~data.modIcon && (publicMod || MasterGranter[Me](_.SeeReport)) + val publicMod = MasterGranter(_.PublicMod) + val modIcon = ~data.modIcon && (publicMod || MasterGranter(_.SeeReport)) val anonMod = modIcon && !publicMod val post = ForumPost.make( topicId = topic.id, diff --git a/modules/forum/src/main/ForumTopicApi.scala b/modules/forum/src/main/ForumTopicApi.scala index 5ff2fa06cad40..f75b26efc03b3 100644 --- a/modules/forum/src/main/ForumTopicApi.scala +++ b/modules/forum/src/main/ForumTopicApi.scala @@ -102,7 +102,7 @@ final private class ForumTopicApi( lang = lang.map(_.language), number = 1, categId = categ.id, - modIcon = (~data.post.modIcon && MasterGranter[Me](_.PublicMod)).option(true) + modIcon = (~data.post.modIcon && MasterGranter(_.PublicMod)).option(true) ) findDuplicate(topic).flatMap { case Some(dup) => fuccess(dup) @@ -172,14 +172,14 @@ final private class ForumTopicApi( def toggleClose(categ: ForumCateg, topic: ForumTopic)(using me: Me): Funit = topicRepo.close(topic.id, topic.open) >> { - (MasterGranter[Me](_.ModerateForum) || topic.isAuthor(me.value)).so { + (MasterGranter(_.ModerateForum) || topic.isAuthor(me.value)).so { modLog.toggleCloseTopic(categ.id, topic.slug, topic.open) } } def toggleSticky(categ: ForumCateg, topic: ForumTopic)(using Me): Funit = topicRepo.sticky(topic.id, !topic.isSticky) >> { - MasterGranter[Me](_.ModerateForum).so(modLog.toggleStickyTopic(categ.id, topic.slug, !topic.isSticky)) + MasterGranter(_.ModerateForum).so(modLog.toggleStickyTopic(categ.id, topic.slug, !topic.isSticky)) } def denormalize(topic: ForumTopic): Funit = for diff --git a/modules/insight/src/main/Share.scala b/modules/insight/src/main/Share.scala index 2a91c9b2d3eef..87dcf3f502cfa 100644 --- a/modules/insight/src/main/Share.scala +++ b/modules/insight/src/main/Share.scala @@ -1,6 +1,6 @@ package lila.insight -import lila.core.perm.{ Granter, Grantable } +import lila.core.perm.Granter final class Share( prefApi: lila.core.pref.PrefApi, @@ -10,7 +10,7 @@ final class Share( def getPrefId(insighted: User) = prefApi.getInsightShare(insighted.id) def grant(insighted: User)(using to: Option[User]): Fu[Boolean] = - if to.soUse(Granter[User](_.SeeInsight)) then fuTrue + if to.exists(Granter.of(_.SeeInsight)) then fuTrue else getPrefId(insighted).flatMap: case _ if to.contains(insighted) => fuTrue diff --git a/modules/mod/src/main/Inquiry.scala b/modules/mod/src/main/Inquiry.scala index 25f5f9ff7b7db..7b5ce2bb4931c 100644 --- a/modules/mod/src/main/Inquiry.scala +++ b/modules/mod/src/main/Inquiry.scala @@ -27,7 +27,7 @@ final class InquiryApi( ): def forMod(using mod: Me)(using Executor): Fu[Option[Inquiry]] = - lila.core.perm.Granter[Me](_.SeeReport).so { + lila.core.perm.Granter(_.SeeReport).so { reportApi.inquiries .ofModId(mod) .flatMapz: report => diff --git a/modules/mod/src/main/IpRender.scala b/modules/mod/src/main/IpRender.scala index 1a42729386acc..d05fdb68caa5f 100644 --- a/modules/mod/src/main/IpRender.scala +++ b/modules/mod/src/main/IpRender.scala @@ -20,7 +20,7 @@ final class IpRender(using Executor): import IpRender.* - def apply(using Me): RenderIp = if Granter[Me](_.Admin) then visible else encrypted + def apply(using Me): RenderIp = if Granter(_.Admin) then visible else encrypted private val visible = (ip: IpAddress) => ip.value diff --git a/modules/mod/src/main/ModApi.scala b/modules/mod/src/main/ModApi.scala index 0d0b57d1c4638..812731d80bfbe 100644 --- a/modules/mod/src/main/ModApi.scala +++ b/modules/mod/src/main/ModApi.scala @@ -140,10 +140,10 @@ final class ModApi( withUser(username): user => val finalPermissions = Permission(user).filter { p => // only remove permissions the mod can actually grant - permissions.contains(p) || !lila.security.Granter.canGrant[Me](p) + permissions.contains(p) || !lila.security.Granter.canGrant(p) } ++ // only add permissions the mod can actually grant - permissions.filter(lila.security.Granter.canGrant[Me]) + permissions.filter(lila.security.Granter.canGrant) userRepo.setRoles(user.id, finalPermissions.map(_.dbKey).toList) >> logApi.setPermissions( user.id, diff --git a/modules/mod/src/main/Presets.scala b/modules/mod/src/main/Presets.scala index a14946398e33b..78d4003724a62 100644 --- a/modules/mod/src/main/Presets.scala +++ b/modules/mod/src/main/Presets.scala @@ -18,7 +18,7 @@ final class ModPresetsApi(settingStore: lila.memo.SettingStore.Builder): case _ => none def getPmPresets(using Me): ModPresets = - ModPresets(pmPresets.get().value.filter(_.permissions.exists(Granter[Me](_)))) + ModPresets(pmPresets.get().value.filter(_.permissions.exists(Granter(_)))) def getPmPresetsOpt(using mod: Option[Me]): ModPresets = mod.map(getPmPresets(using _)).getOrElse(ModPresets(Nil)) diff --git a/modules/perfStat/src/main/PerfStatApi.scala b/modules/perfStat/src/main/PerfStatApi.scala index d72f0f3788787..94bd2dfcc59d7 100644 --- a/modules/perfStat/src/main/PerfStatApi.scala +++ b/modules/perfStat/src/main/PerfStatApi.scala @@ -35,7 +35,7 @@ final class PerfStatApi( .so: perfKey => userApi.withPerfs(name.id).flatMap { _.filter: u => - (u.enabled.yes && (!u.lame || me.exists(_.is(u.user)))) || me.soUse(Granter[Me](_.UserModView)) + (u.enabled.yes && (!u.lame || me.exists(_.is(u.user)))) || me.soUse(Granter(_.UserModView)) .filter: u => !u.isBot || (perfKey != PerfKey.ultraBullet) .soFu: u => diff --git a/modules/relay/src/main/RelayApi.scala b/modules/relay/src/main/RelayApi.scala index dc1f75cbbba79..62d89b653cfe0 100644 --- a/modules/relay/src/main/RelayApi.scala +++ b/modules/relay/src/main/RelayApi.scala @@ -181,8 +181,8 @@ final class RelayApi( (tour.id :: data.grouping.so(_.tourIds)).foreach(withTours.invalidate) private def updateGrouping(tour: RelayTour, data: RelayGroup.form.Data)(using me: Me): Funit = - Granter[Me](_.Relay).so: - val canGroup = fuccess(Granter[Me](_.StudyAdmin)) >>| tourRepo.isOwnerOfAll(me.userId, data.tourIds) + Granter(_.Relay).so: + val canGroup = fuccess(Granter(_.StudyAdmin)) >>| tourRepo.isOwnerOfAll(me.userId, data.tourIds) canGroup.flatMapz(groupRepo.update(tour.id, data)) def create(data: RelayRoundForm.Data, tour: RelayTour)(using me: Me): Fu[RelayRound.WithTourAndStudy] = @@ -287,7 +287,7 @@ final class RelayApi( tourById(relay.tourId).map2(relay.withTour) def canUpdate(tour: RelayTour)(using me: Me): Fu[Boolean] = - fuccess(Granter[Me](_.StudyAdmin) || me.is(tour.ownerId)) >>| + fuccess(Granter(_.StudyAdmin) || me.is(tour.ownerId)) >>| roundRepo .studyIdsOf(tour.id) .flatMap: ids => diff --git a/modules/relay/src/main/RelayTourForm.scala b/modules/relay/src/main/RelayTourForm.scala index 1838f86908a02..f4cee46f12212 100644 --- a/modules/relay/src/main/RelayTourForm.scala +++ b/modules/relay/src/main/RelayTourForm.scala @@ -63,7 +63,7 @@ object RelayTourForm: name = name, description = description, markup = markup, - tier = tier.ifTrue(Granter[Me](_.Relay)), + tier = tier.ifTrue(Granter(_.Relay)), autoLeaderboard = autoLeaderboard, teamTable = teamTable, players = players, @@ -71,7 +71,7 @@ object RelayTourForm: spotlight = spotlight.filterNot(_.isEmpty), pinnedStreamer = pinnedStreamer ) - .giveOfficialToBroadcasterIf(Granter[Me](_.StudyAdmin)) + .giveOfficialToBroadcasterIf(Granter(_.StudyAdmin)) def make(using me: Me) = RelayTour( @@ -80,7 +80,7 @@ object RelayTourForm: description = description, markup = markup, ownerId = me, - tier = tier.ifTrue(Granter[Me](_.Relay)), + tier = tier.ifTrue(Granter(_.Relay)), active = false, createdAt = nowInstant, syncedAt = none, @@ -90,7 +90,7 @@ object RelayTourForm: teams = teams, spotlight = spotlight.filterNot(_.isEmpty), pinnedStreamer = pinnedStreamer - ).giveOfficialToBroadcasterIf(Granter[Me](_.StudyAdmin)) + ).giveOfficialToBroadcasterIf(Granter(_.StudyAdmin)) object Data: diff --git a/modules/report/src/main/Reason.scala b/modules/report/src/main/Reason.scala index 23ea53d102f11..648f7709459d5 100644 --- a/modules/report/src/main/Reason.scala +++ b/modules/report/src/main/Reason.scala @@ -46,7 +46,7 @@ object Reason: def isGranted(reason: Reason)(using Me) = import lila.core.perm.Granter reason match - case Cheat => Granter[Me](_.MarkEngine) - case Comm | Sexism => Granter[Me](_.Shadowban) - case Boost => Granter[Me](_.MarkBooster) - case AltPrint | CheatPrint | Playbans | Username | Other => Granter[Me](_.Admin) + case Cheat => Granter(_.MarkEngine) + case Comm | Sexism => Granter(_.Shadowban) + case Boost => Granter(_.MarkBooster) + case AltPrint | CheatPrint | Playbans | Username | Other => Granter(_.Admin) diff --git a/modules/report/src/main/Room.scala b/modules/report/src/main/Room.scala index 90afb5250e2e0..381e18cff88ab 100644 --- a/modules/report/src/main/Room.scala +++ b/modules/report/src/main/Room.scala @@ -37,12 +37,12 @@ object Room: def isGranted(room: Room)(using Me) = import lila.core.perm.Granter room match - case Cheat => Granter[Me](_.MarkEngine) - case Boost => Granter[Me](_.MarkBooster) - case Print => Granter[Me](_.Admin) - case Comm => Granter[Me](_.Shadowban) - case Other => Granter[Me](_.Admin) - case Xfiles => Granter[Me](_.MarkEngine) + case Cheat => Granter(_.MarkEngine) + case Boost => Granter(_.MarkBooster) + case Print => Granter(_.Admin) + case Comm => Granter(_.Shadowban) + case Other => Granter(_.Admin) + case Xfiles => Granter(_.MarkEngine) def filterGranted(reports: List[Report])(using mod: Me) = reports.filter: r => isGranted(r.room) && (r.user.isnt(mod) || mod.isSuperAdmin) diff --git a/modules/security/src/main/Granter.scala b/modules/security/src/main/Granter.scala index f8ea1bb646775..6190bb12d857b 100644 --- a/modules/security/src/main/Granter.scala +++ b/modules/security/src/main/Granter.scala @@ -1,6 +1,5 @@ package lila.security -import lila.core.perm.Grantable import lila.core.perm.Permission object Granter: @@ -8,15 +7,15 @@ object Granter: export lila.core.perm.Granter.* def canViewAltUsername(user: User)(using Option[Me]): Boolean = - opt[Me](_.Admin) || { - (opt[Me](_.CheatHunter) && user.marks.engine) || - (opt[Me](_.BoostHunter) && user.marks.boost) || - (opt[Me](_.Shusher) && user.marks.troll) + opt(_.Admin) || { + (opt(_.CheatHunter) && user.marks.engine) || + (opt(_.BoostHunter) && user.marks.boost) || + (opt(_.Shusher) && user.marks.troll) } - def canCloseAlt(using Me) = apply[Me](_.CloseAccount) && apply[Me](_.ViewPrintNoIP) + def canCloseAlt(using Me) = apply(_.CloseAccount) && apply(_.ViewPrintNoIP) - def canGrant[U: Grantable](permission: Permission)(using me: U): Boolean = + def canGrant(permission: Permission)(using me: Me): Boolean = apply(_.SuperAdmin) || { apply(_.ChangePermission) && Permission.nonModPermissions(permission) } || { diff --git a/modules/study/src/main/ServerEval.scala b/modules/study/src/main/ServerEval.scala index 3d351c374ee94..32c61a35c2c04 100644 --- a/modules/study/src/main/ServerEval.scala +++ b/modules/study/src/main/ServerEval.scala @@ -28,7 +28,7 @@ object ServerEval: .so: val unlimitedFu = fuccess(unlimited) >>| - fuccess(userId == UserId.lichess) >>| userRepo.me(userId).map(_.soUse(Granter.opt[Me](_.Relay))) + fuccess(userId == UserId.lichess) >>| userRepo.me(userId).map(_.soUse(Granter.opt(_.Relay))) unlimitedFu.flatMap: unlimited => chapterRepo .startServerEval(chapter) diff --git a/modules/study/src/main/StudyInvite.scala b/modules/study/src/main/StudyInvite.scala index 6062446d18f48..6adc6c0f330eb 100644 --- a/modules/study/src/main/StudyInvite.scala +++ b/modules/study/src/main/StudyInvite.scala @@ -30,13 +30,13 @@ final private class StudyInvite( _ <- (study.nbMembers >= maxMembers).so(fufail[Unit](s"Max study members reached: $maxMembers")) inviter <- userRepo.me(byUserId).orFail("No such inviter") given Me = inviter - _ <- (!study.isOwner(inviter) && !Granter[Me](_.StudyAdmin)).so: + _ <- (!study.isOwner(inviter) && !Granter(_.StudyAdmin)).so: fufail[Unit]("Only the study owner can invite") invited <- userRepo .enabledById(invitedUsername) .map( - _.filterNot(u => UserId.lichess.is(u) && !Granter[Me](_.StudyAdmin)) + _.filterNot(u => UserId.lichess.is(u) && !Granter(_.StudyAdmin)) ) .orFail("No such invited") _ <- study.members.contains(invited).so(fufail[Unit]("Already a member")) @@ -44,7 +44,7 @@ final private class StudyInvite( _ <- relation.has(Block).so(fufail[Unit]("This user does not want to join")) isPresent <- getIsPresent(invited.id) _ <- - if isPresent || Granter[Me](_.StudyAdmin) then funit + if isPresent || Granter(_.StudyAdmin) then funit else prefApi .getStudyInvite(invited.id) @@ -57,7 +57,7 @@ final private class StudyInvite( _ <- studyRepo.addMember(study, StudyMember.make(invited)) shouldNotify = !isPresent && (!inviter.marks.troll || relation.has(Follow)) rateLimitCost = - if Granter[Me](_.StudyAdmin) then 1 + if Granter(_.StudyAdmin) then 1 else if relation.has(Follow) then 5 else if inviter.hasTitle then 10 else 100 diff --git a/modules/team/src/main/TeamApi.scala b/modules/team/src/main/TeamApi.scala index 3367a3866bb61..5ebf40bc34ff1 100644 --- a/modules/team/src/main/TeamApi.scala +++ b/modules/team/src/main/TeamApi.scala @@ -107,7 +107,7 @@ final class TeamApi( .teamIdsList(member.id) .map(_.take(lila.team.Team.maxJoinCeiling)) .flatMap { allIds => - if viewer.exists(_.is(member)) || Granter.opt[Me](_.UserModView) then fuccess(allIds) + if viewer.exists(_.is(member)) || Granter.opt(_.UserModView) then fuccess(allIds) else allIds.nonEmpty.so: teamRepo.filterHideMembers(allIds).flatMap { hiddenIds => @@ -274,7 +274,7 @@ final class TeamApi( myself <- memberRepo.get(team.id, me) allowed = userId.isnt(team.createdBy) && kicked.exists: kicked => myself.exists: myself => - kicked.perms.isEmpty || myself.hasPerm(_.Admin) || Granter[Me](_.ManageTeam) + kicked.perms.isEmpty || myself.hasPerm(_.Admin) || Granter(_.ManageTeam) _ <- allowed.so: // create a request to set declined in order to prevent kicked use to rejoin val request = TeamRequest.make(team.id, userId, "Kicked from team", declined = true) @@ -319,7 +319,7 @@ final class TeamApi( def toggleEnabled(team: Team, explain: String)(using me: Me): Funit = isCreatorGranted(team, _.Admin).flatMap: activeCreator => - if Granter[Me](_.ManageTeam) || me.is(team.createdBy) || !activeCreator + if Granter(_.ManageTeam) || me.is(team.createdBy) || !activeCreator then logger.info(s"toggleEnabled ${team.id}: ${!team.enabled} by @${me}: $explain") if team.enabled then diff --git a/modules/team/src/main/TeamSecurity.scala b/modules/team/src/main/TeamSecurity.scala index 5b33d64be9ce5..5a88275f7cbcc 100644 --- a/modules/team/src/main/TeamSecurity.scala +++ b/modules/team/src/main/TeamSecurity.scala @@ -66,7 +66,7 @@ final class TeamSecurity(memberRepo: TeamMemberRepo, userRepo: UserRepo, cached: ) .verifying( "You can't make Lichess a leader", - n => Granter[Me](_.ManageTeam) || n.isnt(UserId.lichess) + n => Granter(_.ManageTeam) || n.isnt(UserId.lichess) ) .verifying( "This user is already a team leader", @@ -90,7 +90,7 @@ final class TeamSecurity(memberRepo: TeamMemberRepo, userRepo: UserRepo, cached: single("leaders" -> seq(permissionsForm)) .verifying( "You can't make Lichess a leader", - Granter[Me](_.ManageTeam) || + Granter(_.ManageTeam) || !_.exists(_.name.is(UserId.lichess)) || t.leaders.exists(_.is(UserId.lichess)) ) @@ -113,7 +113,7 @@ final class TeamSecurity(memberRepo: TeamMemberRepo, userRepo: UserRepo, cached: .verifying( "You cannot evict the team creator", d => - Granter[Me](_.ManageTeam) || !t.hasAdminCreator || d.exists: l => + Granter(_.ManageTeam) || !t.hasAdminCreator || d.exists: l => l.name.is(t.createdBy) && l.perms(Permission.Admin) ) .verifying( diff --git a/modules/tournament/src/main/TournamentForm.scala b/modules/tournament/src/main/TournamentForm.scala index 437de8d30d5a9..fdcd30308ff8d 100644 --- a/modules/tournament/src/main/TournamentForm.scala +++ b/modules/tournament/src/main/TournamentForm.scala @@ -83,7 +83,7 @@ final class TournamentForm: "clockTime" -> numberInDouble(timeChoices), "clockIncrement" -> numberIn(incrementChoices).into[IncrementSeconds], "minutes" -> { - if lila.core.perm.Granter[Me](_.ManageTournament) then number + if lila.core.perm.Granter(_.ManageTournament) then number else numberIn(minuteChoicesKeepingCustom(prev)) }, "waitMinutes" -> optional(numberIn(waitMinuteChoices)), diff --git a/modules/ublog/src/main/UblogBlog.scala b/modules/ublog/src/main/UblogBlog.scala index 1e8170ccc339e..89ee9d472de1a 100644 --- a/modules/ublog/src/main/UblogBlog.scala +++ b/modules/ublog/src/main/UblogBlog.scala @@ -35,9 +35,9 @@ object UblogBlog: ) class Allows(creator: UserId): - def moderate(using Option[Me]): Boolean = Granter.opt[Me](_.ModerateBlog) + def moderate(using Option[Me]): Boolean = Granter.opt(_.ModerateBlog) def edit(using me: Option[Me]): Boolean = me.exists(creator.is(_)) || moderate def create(using me: Option[Me]): Boolean = - edit || (creator.is(UserId.lichess) && Granter.opt[Me](_.Pages)) + edit || (creator.is(UserId.lichess) && Granter.opt(_.Pages)) def draft(using me: Option[Me]): Boolean = - create || (Granter.opt[Me](_.LichessTeam) && creator.is(UserId.lichess)) + create || (Granter.opt(_.LichessTeam) && creator.is(UserId.lichess))