Skip to content

Commit

Permalink
Granter no longer needs parameterization
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 12, 2024
1 parent 6bb24b6 commit 8a40d7c
Show file tree
Hide file tree
Showing 32 changed files with 82 additions and 98 deletions.
6 changes: 3 additions & 3 deletions app/http/CtrlFilters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/mashup/UserInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion app/templating/SecurityHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/views/mod/permissions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion modules/api/src/main/AccountClosure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions modules/api/src/main/ForumAccess.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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] =
Expand Down
4 changes: 2 additions & 2 deletions modules/api/src/main/RoundApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions modules/chat/src/main/ChatApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion modules/clas/src/main/Env.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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, _) =>
Expand Down
31 changes: 12 additions & 19 deletions modules/core/src/main/perm.scala
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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
8 changes: 0 additions & 8 deletions modules/core/src/main/user.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/forum/src/main/ForumDelete.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
2 changes: 1 addition & 1 deletion modules/forum/src/main/ForumPost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions modules/forum/src/main/ForumPostApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions modules/forum/src/main/ForumTopicApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions modules/insight/src/main/Share.scala
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/mod/src/main/Inquiry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
2 changes: 1 addition & 1 deletion modules/mod/src/main/IpRender.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions modules/mod/src/main/ModApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion modules/mod/src/main/Presets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion modules/perfStat/src/main/PerfStatApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
6 changes: 3 additions & 3 deletions modules/relay/src/main/RelayApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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] =
Expand Down Expand Up @@ -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 =>
Expand Down
8 changes: 4 additions & 4 deletions modules/relay/src/main/RelayTourForm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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,
teams = teams,
spotlight = spotlight.filterNot(_.isEmpty),
pinnedStreamer = pinnedStreamer
)
.giveOfficialToBroadcasterIf(Granter[Me](_.StudyAdmin))
.giveOfficialToBroadcasterIf(Granter(_.StudyAdmin))

def make(using me: Me) =
RelayTour(
Expand All @@ -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,
Expand All @@ -90,7 +90,7 @@ object RelayTourForm:
teams = teams,
spotlight = spotlight.filterNot(_.isEmpty),
pinnedStreamer = pinnedStreamer
).giveOfficialToBroadcasterIf(Granter[Me](_.StudyAdmin))
).giveOfficialToBroadcasterIf(Granter(_.StudyAdmin))

object Data:

Expand Down
8 changes: 4 additions & 4 deletions modules/report/src/main/Reason.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading

0 comments on commit 8a40d7c

Please sign in to comment.