Skip to content

Commit

Permalink
add team checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 13, 2021
1 parent 2f934b1 commit e090d98
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
36 changes: 22 additions & 14 deletions app/controllers/Team.scala
Expand Up @@ -84,7 +84,7 @@ final class Team(
)

private def usersExport(teamId: String, me: Option[lila.user.User], req: RequestHeader) = {
api.team(teamId) flatMap {
api teamEnabled teamId flatMap {
_ ?? { team =>
val canView: Fu[Boolean] =
if (team.publicMembers) fuccess(true)
Expand All @@ -108,7 +108,7 @@ final class Team(

def tournaments(teamId: String) =
Open { implicit ctx =>
env.team.teamRepo.enabled(teamId) flatMap {
api teamEnabled teamId flatMap {
_ ?? { team =>
env.teamInfo.tournaments(team, 30, 30) map { tours =>
Ok(html.team.tournaments.page(team, tours))
Expand All @@ -119,14 +119,14 @@ final class Team(

def edit(id: String) =
Auth { implicit ctx => _ =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
fuccess(html.team.form.edit(team, forms edit team))
}
}

def update(id: String) =
AuthBody { implicit ctx => me =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
implicit val req = ctx.body
forms
.edit(team)
Expand All @@ -140,7 +140,7 @@ final class Team(

def kickForm(id: String) =
Auth { implicit ctx => me =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
env.team.memberRepo userIdsByTeam team.id map { userIds =>
html.team.admin.kick(team, userIds.filter(me.id !=))
}
Expand All @@ -149,7 +149,7 @@ final class Team(

def kick(id: String) =
AuthBody { implicit ctx => me =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
implicit val req = ctx.body
forms.selectMember.bindFromRequest().value ?? { api.kick(team, _, me) } inject Redirect(
routes.Team.kickForm(team.id)
Expand All @@ -158,7 +158,7 @@ final class Team(
}
def kickUser(teamId: String, userId: String) =
Scoped(_.Team.Write) { _ => me =>
api team teamId flatMap {
api teamEnabled teamId flatMap {
_ ?? { team =>
if (team leaders me.id) api.kick(team, userId, me) inject jsonOkResult
else Forbidden(jsonError("Not your team")).fuccess
Expand All @@ -168,14 +168,14 @@ final class Team(

def leadersForm(id: String) =
Auth { implicit ctx => _ =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
Ok(html.team.admin.leaders(team, forms leaders team)).fuccess
}
}

def leaders(id: String) =
AuthBody { implicit ctx => me =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
implicit val req = ctx.body
forms.leaders(team).bindFromRequest().value ?? {
api.setLeaders(team, _, me, isGranted(_.ManageTeam))
Expand Down Expand Up @@ -256,7 +256,7 @@ final class Team(
AuthOrScopedBody(_.Team.Write)(
auth = implicit ctx =>
me =>
api.team(id) flatMap {
api.teamEnabled(id) flatMap {
_ ?? { team =>
api hasJoinedTooManyTeams me flatMap { tooMany =>
if (tooMany)
Expand Down Expand Up @@ -428,7 +428,7 @@ final class Team(

def pmAll(id: String) =
Auth { implicit ctx => _ =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
env.tournament.api
.visibleByTeam(team.id, 0, 20)
.dmap(_.next)
Expand All @@ -442,7 +442,7 @@ final class Team(
AuthOrScopedBody(_.Team.Write)(
auth = implicit ctx =>
me =>
WithOwnedTeam(id) { team =>
WithOwnedTeamEnabled(id) { team =>
doPmAll(team, me)(ctx.body).fold(
err =>
env.tournament.api
Expand All @@ -456,7 +456,7 @@ final class Team(
},
scoped = implicit req =>
me =>
api team id flatMap {
api teamEnabled id flatMap {
_.filter(_ leaders me.id) ?? { team =>
doPmAll(team, me).fold(
err => BadRequest(errorsAsJson(err)(reqLang)).fuccess,
Expand All @@ -482,7 +482,7 @@ final class Team(
def apiShow(id: String) =
Open { ctx =>
JsonOptionOk {
api team id flatMap {
api teamEnabled id flatMap {
_ ?? { team =>
for {
joined <- ctx.userId.?? { api.belongsTo(id, _) }
Expand Down Expand Up @@ -565,4 +565,12 @@ You received this because you are subscribed to messages of the team $url."""
if (ctx.userId.exists(team.leaders.contains) || isGranted(_.ManageTeam)) f(team)
else renderTeam(team) map { Forbidden(_) }
}

private def WithOwnedTeamEnabled(
teamId: String
)(f: TeamModel => Fu[Result])(implicit ctx: Context): Fu[Result] =
WithOwnedTeam(teamId) { team =>
if (team.enabled) f(team)
else notFound
}
}
4 changes: 3 additions & 1 deletion modules/team/src/main/TeamApi.scala
Expand Up @@ -34,6 +34,8 @@ final class TeamApi(

def team(id: Team.ID) = teamRepo byId id

def teamEnabled(id: Team.ID) = teamRepo enabled id

def leaderTeam(id: Team.ID) = teamRepo.coll.byId[LeaderTeam](id, $doc("name" -> true))

def lightsByLeader = teamRepo.lightsByLeader _
Expand Down Expand Up @@ -143,7 +145,7 @@ final class TeamApi(

def requestable(teamId: Team.ID, user: User): Fu[Option[Team]] =
for {
teamOption <- teamRepo.coll.byId[Team](teamId)
teamOption <- teamEnabled(teamId)
able <- teamOption.??(requestable(_, user))
} yield teamOption ifTrue able

Expand Down

0 comments on commit e090d98

Please sign in to comment.