Skip to content

Commit 1b13ee3

Browse files
committed
apply tournament creation rate limits to API calls
1 parent dc54f3c commit 1b13ee3

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

app/controllers/Tournament.scala

+41-32
Original file line numberDiff line numberDiff line change
@@ -263,31 +263,38 @@ final class Tournament(
263263
fuccess(Redirect(routes.Tournament.home(1)))
264264
}
265265

266+
private def rateLimitCreation(me: UserModel, password: Option[String], req: RequestHeader)(
267+
create: => Fu[Result]
268+
): Fu[Result] = {
269+
val cost =
270+
if (me.hasTitle ||
271+
env.streamer.liveStreamApi.isStreaming(me.id) ||
272+
isGranted(_.ManageTournament, me) ||
273+
password.isDefined) 1
274+
else 4
275+
CreateLimitPerUser(me.id, cost = cost) {
276+
CreateLimitPerIP(HTTPRequest lastRemoteAddress req, cost = cost) {
277+
create
278+
}(rateLimited)
279+
}(rateLimited)
280+
}
281+
266282
def create = AuthBody { implicit ctx => me =>
267283
NoLameOrBot {
268284
teamC.teamsIBelongTo(me) flatMap { teams =>
269285
implicit val req = ctx.body
270286
negotiate(
271287
html = forms(me).bindFromRequest.fold(
272288
err => BadRequest(html.tournament.form(err, me, teams)).fuccess,
273-
setup => {
274-
val cost =
275-
if (me.hasTitle ||
276-
env.streamer.liveStreamApi.isStreaming(me.id) ||
277-
isGranted(_.ManageTournament) ||
278-
setup.password.isDefined) 1
279-
else 4
280-
CreateLimitPerUser(me.id, cost = cost) {
281-
CreateLimitPerIP(HTTPRequest lastRemoteAddress ctx.req, cost = cost) {
282-
api.createTournament(setup, me, teams, getUserTeamIds) map { tour =>
283-
Redirect {
284-
if (tour.isTeamBattle) routes.Tournament.teamBattleEdit(tour.id)
285-
else routes.Tournament.show(tour.id)
286-
}.flashSuccess
287-
}
288-
}(rateLimited)
289-
}(rateLimited)
290-
}
289+
setup =>
290+
rateLimitCreation(me, setup.password, ctx.req) {
291+
api.createTournament(setup, me, teams, getUserTeamIds) map { tour =>
292+
Redirect {
293+
if (tour.isTeamBattle) routes.Tournament.teamBattleEdit(tour.id)
294+
else routes.Tournament.show(tour.id)
295+
}.flashSuccess
296+
}
297+
}
291298
),
292299
api = _ => doApiCreate(me)
293300
)
@@ -304,21 +311,23 @@ final class Tournament(
304311
forms(me).bindFromRequest.fold(
305312
jsonFormErrorDefaultLang,
306313
setup =>
307-
teamC.teamsIBelongTo(me) flatMap { teams =>
308-
api.createTournament(setup, me, teams, getUserTeamIds) flatMap { tour =>
309-
jsonView(
310-
tour,
311-
none,
312-
none,
313-
getUserTeamIds,
314-
env.team.getTeamName,
315-
none,
316-
none,
317-
partial = false,
318-
lila.i18n.defaultLang
319-
)
314+
rateLimitCreation(me, setup.password, req) {
315+
teamC.teamsIBelongTo(me) flatMap { teams =>
316+
api.createTournament(setup, me, teams, getUserTeamIds) flatMap { tour =>
317+
jsonView(
318+
tour,
319+
none,
320+
none,
321+
getUserTeamIds,
322+
env.team.getTeamName,
323+
none,
324+
none,
325+
partial = false,
326+
lila.i18n.defaultLang
327+
) map { Ok(_) }
328+
}
320329
}
321-
} map { Ok(_) }
330+
}
322331
)
323332

324333
def teamBattleEdit(id: String) = Auth { implicit ctx => me =>

0 commit comments

Comments
 (0)