Add auth in all controllers
This commit is contained in:
parent
5472a400d1
commit
6b02eecb9b
@ -3,8 +3,7 @@ class ApplicationController < ActionController::Base
|
|||||||
include Pundit::Authorization
|
include Pundit::Authorization
|
||||||
|
|
||||||
before_action :set_title, :set_current_user
|
before_action :set_title, :set_current_user
|
||||||
# TODO: add later
|
after_action :verify_authorized
|
||||||
# after_action :verify_authorized
|
|
||||||
|
|
||||||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
|
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
|
||||||
allow_browser versions: :modern
|
allow_browser versions: :modern
|
||||||
|
@ -4,10 +4,14 @@ class CompletionsController < ApplicationController
|
|||||||
before_action :set_completion, only: %i[ destroy edit update ]
|
before_action :set_completion, only: %i[ destroy edit update ]
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@title = "Edit completion"
|
@title = "Edit completion"
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@completion = Completion.new
|
@completion = Completion.new
|
||||||
if params[:contestant_id]
|
if params[:contestant_id]
|
||||||
@completion.contestant_id = params[:contestant_id]
|
@completion.contestant_id = params[:contestant_id]
|
||||||
@ -16,6 +20,8 @@ class CompletionsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@completion = Completion.new(completion_params)
|
@completion = Completion.new(completion_params)
|
||||||
@completion.contest_id = @contest.id
|
@completion.contest_id = @contest.id
|
||||||
if @completion.save
|
if @completion.save
|
||||||
@ -29,6 +35,8 @@ class CompletionsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
if params[:contestant_id]
|
if params[:contestant_id]
|
||||||
@completion.contestant_id = params[:contestant_id]
|
@completion.contestant_id = params[:contestant_id]
|
||||||
end
|
end
|
||||||
@ -41,6 +49,8 @@ class CompletionsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@completion.destroy
|
@completion.destroy
|
||||||
if params[:contestant_id]
|
if params[:contestant_id]
|
||||||
redirect_to contest_contestant_path(@contest, params[:contestant_id])
|
redirect_to contest_contestant_path(@contest, params[:contestant_id])
|
||||||
|
@ -4,15 +4,21 @@ class ContestantsController < ApplicationController
|
|||||||
before_action :set_completions, only: %i[edit update ]
|
before_action :set_completions, only: %i[edit update ]
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@title = "Contestant"
|
@title = "Contestant"
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@contestant = Contestant.new
|
@contestant = Contestant.new
|
||||||
@title = "New contestant"
|
@title = "New contestant"
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@contestant = Contestant.new(contestant_params)
|
@contestant = Contestant.new(contestant_params)
|
||||||
@contestant.contest_id = @contest.id
|
@contestant.contest_id = @contest.id
|
||||||
if @contestant.save
|
if @contestant.save
|
||||||
@ -24,6 +30,8 @@ class ContestantsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
if @contestant.update(contestant_params)
|
if @contestant.update(contestant_params)
|
||||||
redirect_to @contest
|
redirect_to @contest
|
||||||
else
|
else
|
||||||
@ -33,6 +41,8 @@ class ContestantsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@contestant.destroy
|
@contestant.destroy
|
||||||
redirect_to contest_path(@contest)
|
redirect_to contest_path(@contest)
|
||||||
end
|
end
|
||||||
|
@ -2,11 +2,15 @@ class ContestsController < ApplicationController
|
|||||||
before_action :set_contest, only: %i[ destroy edit show update ]
|
before_action :set_contest, only: %i[ destroy edit show update ]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
authorize :contest
|
||||||
|
|
||||||
@contests = current_user.contests
|
@contests = current_user.contests
|
||||||
@title = "Welcome #{current_user.username}!"
|
@title = "Welcome #{current_user.username}!"
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@title = @contest.name
|
@title = @contest.name
|
||||||
@contestants = @contest.contestants.order(:name)
|
@contestants = @contest.contestants.order(:name)
|
||||||
@puzzles = @contest.puzzles.order(:id)
|
@puzzles = @contest.puzzles.order(:id)
|
||||||
@ -14,15 +18,21 @@ class ContestsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@title = "Edit contest settings"
|
@title = "Edit contest settings"
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
authorize :contest
|
||||||
|
|
||||||
@contest = Contest.new
|
@contest = Contest.new
|
||||||
@title = "New jigsaw puzzle competition"
|
@title = "New jigsaw puzzle competition"
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
authorize :contest
|
||||||
|
|
||||||
@contest = Contest.new(contest_params)
|
@contest = Contest.new(contest_params)
|
||||||
@contest.user_id = current_user.id
|
@contest.user_id = current_user.id
|
||||||
if @contest.save
|
if @contest.save
|
||||||
@ -33,6 +43,8 @@ class ContestsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
if @contest.update(contest_params)
|
if @contest.update(contest_params)
|
||||||
redirect_to @contest
|
redirect_to @contest
|
||||||
else
|
else
|
||||||
@ -41,6 +53,7 @@ class ContestsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize @contest
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -3,15 +3,21 @@ class PuzzlesController < ApplicationController
|
|||||||
before_action :set_puzzle, only: %i[ destroy edit update]
|
before_action :set_puzzle, only: %i[ destroy edit update]
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@title = "Edit contest puzzle"
|
@title = "Edit contest puzzle"
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@puzzle = Puzzle.new
|
@puzzle = Puzzle.new
|
||||||
@title = "New contest puzzle"
|
@title = "New contest puzzle"
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@puzzle = Puzzle.new(puzzle_params)
|
@puzzle = Puzzle.new(puzzle_params)
|
||||||
@puzzle.contest_id = @contest.id
|
@puzzle.contest_id = @contest.id
|
||||||
if @puzzle.save
|
if @puzzle.save
|
||||||
@ -23,6 +29,8 @@ class PuzzlesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
if @puzzle.update(puzzle_params)
|
if @puzzle.update(puzzle_params)
|
||||||
redirect_to @contest
|
redirect_to @contest
|
||||||
else
|
else
|
||||||
@ -32,6 +40,8 @@ class PuzzlesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize @contest
|
||||||
|
|
||||||
@puzzle.destroy
|
@puzzle.destroy
|
||||||
redirect_to contest_path(@contest)
|
redirect_to contest_path(@contest)
|
||||||
end
|
end
|
||||||
|
@ -3,9 +3,11 @@ class SessionsController < ApplicationController
|
|||||||
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
|
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." }
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
skip_authorization
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
skip_authorization
|
||||||
if user = User.authenticate_by(params.permit(:email_address, :password))
|
if user = User.authenticate_by(params.permit(:email_address, :password))
|
||||||
start_new_session_for user
|
start_new_session_for user
|
||||||
redirect_to after_authentication_url
|
redirect_to after_authentication_url
|
||||||
|
9
app/policies/completion_policy.rb
Normal file
9
app/policies/completion_policy.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class CompletionPolicy < ContestPolicy
|
||||||
|
def index?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
29
app/policies/contest_policy.rb
Normal file
29
app/policies/contest_policy.rb
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
class ContestPolicy < ApplicationPolicy
|
||||||
|
def index?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
record.user.id == user.id || user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def new?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def create?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def edit?
|
||||||
|
record.user.id == user.id || user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
record.user.id == user.id || user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
record.user.id == user.id || user.admin?
|
||||||
|
end
|
||||||
|
end
|
9
app/policies/contestant_policy.rb
Normal file
9
app/policies/contestant_policy.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class ContestantPolicy < ContestPolicy
|
||||||
|
def index?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
9
app/policies/puzzle_policy.rb
Normal file
9
app/policies/puzzle_policy.rb
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
class PuzzlePolicy < ContestPolicy
|
||||||
|
def index?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
@ -15,9 +15,10 @@
|
|||||||
.card-header
|
.card-header
|
||||||
= contest.name
|
= contest.name
|
||||||
.card-body
|
.card-body
|
||||||
|
.card-text.mb-2 = "#{contest.puzzles.length} puzzles - #{contest.contestants.length} participants"
|
||||||
.row
|
.row
|
||||||
- contest.puzzles.each do |puzzle|
|
.col
|
||||||
- if puzzle.image.attached?
|
- contest.puzzles.each do |puzzle|
|
||||||
.col
|
- if puzzle.image.attached?
|
||||||
= image_tag puzzle.image, style: "max-height: 80px;"
|
= image_tag puzzle.image, style: "max-height: 50px;", class: "mb-2 me-2"
|
||||||
a.stretched-link href=contest_path(contest)
|
a.stretched-link href=contest_path(contest)
|
Loading…
x
Reference in New Issue
Block a user