Add pundit authorization policies
This commit is contained in:
parent
3aa396959b
commit
737068de5f
@ -33,6 +33,8 @@ a
|
|||||||
text-decoration: none
|
text-decoration: none
|
||||||
color: white
|
color: white
|
||||||
outline: none
|
outline: none
|
||||||
|
&:hover
|
||||||
|
color: yellow
|
||||||
|
|
||||||
h1
|
h1
|
||||||
font-size: 7vmin
|
font-size: 7vmin
|
||||||
@ -67,6 +69,7 @@ header
|
|||||||
|
|
||||||
|
|
||||||
#home-headline
|
#home-headline
|
||||||
|
margin-top: 6em
|
||||||
flex: 1 1 auto
|
flex: 1 1 auto
|
||||||
text-align: center
|
text-align: center
|
||||||
h1
|
h1
|
||||||
@ -89,13 +92,24 @@ header
|
|||||||
border: 2px solid black
|
border: 2px solid black
|
||||||
text-align: center
|
text-align: center
|
||||||
&:hover
|
&:hover
|
||||||
background: #bada55
|
background: #ffffff44
|
||||||
color: black
|
color: yellow
|
||||||
&:active
|
&:active
|
||||||
background: (#bada55 + 100)
|
background: (#bada55 + 100)
|
||||||
|
|
||||||
*, *:before, *:after
|
*, *:before, *:after
|
||||||
box-sizing: inherit
|
box-sizing: inherit
|
||||||
|
font-family: fantasy
|
||||||
|
font-weight: lighter
|
||||||
|
|
||||||
li
|
li
|
||||||
list-style: none
|
list-style: none
|
||||||
|
|
||||||
|
.alert
|
||||||
|
padding: 1em
|
||||||
|
background: #ffffff77
|
||||||
|
font-size: 1.2rem
|
||||||
|
color: #ffffff
|
||||||
|
text-shadow: 0 0 3px black
|
||||||
|
font-family: fantasy
|
||||||
|
letter-spacing: 0.3
|
||||||
|
|||||||
@ -10,14 +10,16 @@
|
|||||||
|
|
||||||
#qrcode
|
#qrcode
|
||||||
float: right
|
float: right
|
||||||
margin-top: -.5em
|
margin-top: -7em
|
||||||
margin-right: 1em
|
margin-right: 1.25em
|
||||||
transform: scale(10%)
|
transform: scale(10%)
|
||||||
transition: all 0.5s ease-in-out
|
transition: all 0.25s ease-out
|
||||||
width: 38px
|
width: 38px
|
||||||
height: 38px
|
height: 38px
|
||||||
&.full
|
&.full
|
||||||
margin-right: 2em
|
margin-top: -6em
|
||||||
|
margin-right: 0
|
||||||
|
margin-bottom: 1em
|
||||||
transform: scale(100%)
|
transform: scale(100%)
|
||||||
width: 384px
|
width: 384px
|
||||||
height: 384px
|
height: 384px
|
||||||
|
|||||||
@ -5,18 +5,16 @@ class ApplicationController < ActionController::Base
|
|||||||
after_action :flash_to_http_header
|
after_action :flash_to_http_header
|
||||||
|
|
||||||
if (Rails.env.development? || Rails.env.test?)
|
if (Rails.env.development? || Rails.env.test?)
|
||||||
# https://github.com/RailsApps/rails-devise-pundit/issues/10
|
|
||||||
include Pundit
|
include Pundit
|
||||||
# https://github.com/elabs/pundit#ensuring-policies-are-used
|
after_action :verify_authorized, unless: -> { devise_controller? }
|
||||||
#after_action :verify_authorized, except: :index
|
# after_action :verify_policy_scoped, only: :index
|
||||||
#after_action :verify_policy_scoped, only: :index
|
|
||||||
|
|
||||||
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def user_not_authorized
|
def user_not_authorized(exception)
|
||||||
flash[:alert] = 'Access denied.'
|
policy_name = exception.policy.class.to_s.underscore
|
||||||
|
flash[:alert] = "#{policy_name}.#{exception.query}"
|
||||||
redirect_to (request.referrer || root_path)
|
redirect_to (request.referrer || root_path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -28,6 +28,7 @@ class GamesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def play_hand
|
def play_hand
|
||||||
|
authorize @game
|
||||||
# TODO: move this logic out of controller maybe to model(s) ?
|
# TODO: move this logic out of controller maybe to model(s) ?
|
||||||
|
|
||||||
# validate player's turn
|
# validate player's turn
|
||||||
@ -124,6 +125,7 @@ class GamesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
authorize @game
|
||||||
begin
|
begin
|
||||||
@game.start
|
@game.start
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
|
|||||||
@ -44,6 +44,7 @@ class SeatsController < ApplicationController
|
|||||||
# PATCH/PUT /seats/1
|
# PATCH/PUT /seats/1
|
||||||
# PATCH/PUT /seats/1.json
|
# PATCH/PUT /seats/1.json
|
||||||
def update
|
def update
|
||||||
|
authorize @seat
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @seat.update(seat_params)
|
if @seat.update(seat_params)
|
||||||
format.html { redirect_to @seat, notice: 'Seat was successfully updated.' }
|
format.html { redirect_to @seat, notice: 'Seat was successfully updated.' }
|
||||||
@ -58,6 +59,7 @@ class SeatsController < ApplicationController
|
|||||||
# DELETE /seats/1
|
# DELETE /seats/1
|
||||||
# DELETE /seats/1.json
|
# DELETE /seats/1.json
|
||||||
def destroy
|
def destroy
|
||||||
|
authorize @seat
|
||||||
@seat.destroy
|
@seat.destroy
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to seats_url, notice: 'Seat was successfully destroyed.' }
|
format.html { redirect_to seats_url, notice: 'Seat was successfully destroyed.' }
|
||||||
@ -67,6 +69,7 @@ class SeatsController < ApplicationController
|
|||||||
|
|
||||||
# POST /seats/1
|
# POST /seats/1
|
||||||
def sit
|
def sit
|
||||||
|
authorize @seat
|
||||||
@message = @seat.sit current_user
|
@message = @seat.sit current_user
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @seat.save
|
if @seat.save
|
||||||
@ -78,11 +81,13 @@ class SeatsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def stand
|
def stand
|
||||||
|
authorize @seat
|
||||||
@message = @seat.stand current_user
|
@message = @seat.stand current_user
|
||||||
redirect_to @seat.table, notice: @message
|
redirect_to @seat.table, notice: @message
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_bot
|
def add_bot
|
||||||
|
authorize @seat
|
||||||
@message = @seat.add_bot
|
@message = @seat.add_bot
|
||||||
redirect_to @seat.table, notice: @message
|
redirect_to @seat.table, notice: @message
|
||||||
end
|
end
|
||||||
@ -93,11 +98,13 @@ class SeatsController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def empty
|
def empty
|
||||||
|
authorize @seat
|
||||||
@message = @seat.empty
|
@message = @seat.empty
|
||||||
redirect_to @seat.table, notice: @message
|
redirect_to @seat.table, notice: @message
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_player
|
def remove_player
|
||||||
|
authorize @seat
|
||||||
@message = @seat.remove_player
|
@message = @seat.remove_player
|
||||||
redirect_to @seat.table, notice: @message
|
redirect_to @seat.table, notice: @message
|
||||||
end
|
end
|
||||||
|
|||||||
@ -7,9 +7,10 @@ class TablesController < ApplicationController
|
|||||||
# First empty seat
|
# First empty seat
|
||||||
@seat = Seat.where(user_soft_token: nil).select{|s| !s.player.try("is_bot?")}.first
|
@seat = Seat.where(user_soft_token: nil).select{|s| !s.player.try("is_bot?")}.first
|
||||||
if @seat
|
if @seat
|
||||||
|
authorize @seat
|
||||||
@seat.sit current_user
|
@seat.sit current_user
|
||||||
else
|
else
|
||||||
@table = Table.create(title: "Table " + current_user.soft_token[0..4])
|
authorize @table = Table.create(title: "Table " + current_user.soft_token[0..4])
|
||||||
@seat = @table.seats.first
|
@seat = @table.seats.first
|
||||||
@seat.sit current_user
|
@seat.sit current_user
|
||||||
end
|
end
|
||||||
@ -24,12 +25,13 @@ class TablesController < ApplicationController
|
|||||||
# GET /tables
|
# GET /tables
|
||||||
# GET /tables.json
|
# GET /tables.json
|
||||||
def index
|
def index
|
||||||
@tables = Table.all
|
@tables = authorize Table.all
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /tables/1
|
# GET /tables/1
|
||||||
# GET /tables/1.json
|
# GET /tables/1.json
|
||||||
def show
|
def show
|
||||||
|
authorize @game
|
||||||
# TODO refactor to ensure play for 2nd and 3rd place
|
# TODO refactor to ensure play for 2nd and 3rd place
|
||||||
|
|
||||||
if @game.controlling_player.try("is_bot?")
|
if @game.controlling_player.try("is_bot?")
|
||||||
@ -41,6 +43,7 @@ class TablesController < ApplicationController
|
|||||||
end
|
end
|
||||||
|
|
||||||
def new_game
|
def new_game
|
||||||
|
authorize @table
|
||||||
@game = @table.add_game
|
@game = @table.add_game
|
||||||
begin
|
begin
|
||||||
@game.start
|
@game.start
|
||||||
|
|||||||
@ -1,2 +1,3 @@
|
|||||||
class VisitorsController < ApplicationController
|
class VisitorsController < ApplicationController
|
||||||
|
before_action :skip_authorization
|
||||||
end
|
end
|
||||||
|
|||||||
@ -17,7 +17,7 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def display_name
|
def display_name
|
||||||
self.email || "Guest#{self.soft_token[0..5]}"
|
self.email.split("@")[0] || "Guest#{self.soft_token[0..5]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def sit_in(seat)
|
def sit_in(seat)
|
||||||
@ -40,6 +40,6 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
# Include default devise modules. Others available are:
|
# Include default devise modules. Others available are:
|
||||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||||
devise :invitable, :database_authenticatable, :registerable, :confirmable,
|
devise :invitable, :database_authenticatable, :registerable,
|
||||||
:recoverable, :rememberable, :trackable, :validatable
|
:recoverable, :rememberable, :trackable, :validatable
|
||||||
end
|
end
|
||||||
|
|||||||
@ -11,7 +11,8 @@ class GamePolicy
|
|||||||
end
|
end
|
||||||
|
|
||||||
def show?
|
def show?
|
||||||
@current_user.admin? || @current_user == @user
|
true
|
||||||
|
# @current_user.admin? || @game.players.find_by_soft_token(soft_token: @current_user.soft_token)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update?
|
def update?
|
||||||
@ -22,4 +23,12 @@ class GamePolicy
|
|||||||
return false if @current_user == @user
|
return false if @current_user == @user
|
||||||
@current_user.admin?
|
@current_user.admin?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def start?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def play_hand?
|
||||||
|
true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
44
app/policies/seat_policy.rb
Normal file
44
app/policies/seat_policy.rb
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
class SeatPolicy
|
||||||
|
attr_reader :current_user, :model
|
||||||
|
|
||||||
|
def initialize(current_user, model)
|
||||||
|
@current_user = current_user || User.new
|
||||||
|
@seat = model
|
||||||
|
end
|
||||||
|
|
||||||
|
def index?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def show?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def update?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy?
|
||||||
|
@current_user.admin?
|
||||||
|
end
|
||||||
|
|
||||||
|
def play_now?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def sit?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_bot?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_player?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -22,4 +22,12 @@ class TablePolicy
|
|||||||
return false if @current_user == @user
|
return false if @current_user == @user
|
||||||
@current_user.admin?
|
@current_user.admin?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def play_now?
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_game?
|
||||||
|
true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<ul>
|
||||||
<li><%= link_to 'Main Menu', root_path %></li>
|
<%# <li><%= link_to 'Main Menu', root_path %1></li> %>
|
||||||
<li><%= link_to 'Tables', tables_path %></li>
|
<% if policy(:table).index? %>
|
||||||
|
<li><%= link_to 'Tables', tables_path %></li>
|
||||||
|
<% end %>
|
||||||
<% if current_user.signed_in? %>
|
<% if current_user.signed_in? %>
|
||||||
<li><%= link_to 'Edit account', edit_user_registration_path %></li>
|
<li><%= link_to 'My account', edit_user_registration_path %></li>
|
||||||
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
|
<li><%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %></li>
|
||||||
<% else %>
|
<% else %>
|
||||||
<li><%= link_to 'Sign in', new_user_session_path %></li>
|
<li><%= link_to 'Sign in', new_user_session_path %></li>
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
- else
|
- else
|
||||||
-# = button_to empty_seat_path(seat), :data => { :confirm => 'Are you sure you want to empty this seat?' } do
|
-# = button_to empty_seat_path(seat), :data => { :confirm => 'Are you sure you want to empty this seat?' } do
|
||||||
= button_to empty_seat_path(seat) do
|
= button_to empty_seat_path(seat) do
|
||||||
- "Empty"
|
- "Clear Seat"
|
||||||
- else
|
- else
|
||||||
= button_to sit_seat_path(seat) do
|
= button_to sit_seat_path(seat) do
|
||||||
- "Sit"
|
- "Sit"
|
||||||
|
|||||||
@ -19,8 +19,8 @@
|
|||||||
#table-controls
|
#table-controls
|
||||||
#newgame= button_to 'New Game', new_game_table_path(@table)
|
#newgame= button_to 'New Game', new_game_table_path(@table)
|
||||||
|
|
||||||
#qrcode
|
-# #qrcode
|
||||||
:javascript
|
-# :javascript
|
||||||
function gen_qrcode(){
|
function gen_qrcode(){
|
||||||
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
var qrcode = new QRCode(document.getElementById("qrcode"), {
|
||||||
text: "#{request.original_url}",
|
text: "#{request.original_url}",
|
||||||
|
|||||||
@ -2,7 +2,10 @@
|
|||||||
%h1 Thirteen <br/>Tien Len
|
%h1 Thirteen <br/>Tien Len
|
||||||
|
|
||||||
#main-menu
|
#main-menu
|
||||||
= link_to 'Play Now', play_now_path
|
- if policy(:table).play_now?
|
||||||
= link_to 'Browse Tables', tables_path
|
= link_to 'Play', play_now_path
|
||||||
= link_to 'Tutorial', tutorial_path
|
- else
|
||||||
= link_to 'About', page_path('about')
|
Soon
|
||||||
|
-# = link_to 'Browse Tables', tables_path
|
||||||
|
-# = link_to 'Tutorial', tutorial_path
|
||||||
|
-# = link_to 'About', page_path('about')
|
||||||
|
|||||||
@ -10,7 +10,7 @@ feature 'Home page' do
|
|||||||
scenario 'visit the home page' do
|
scenario 'visit the home page' do
|
||||||
visit root_path
|
visit root_path
|
||||||
expect(page).to have_link 'Play Now'
|
expect(page).to have_link 'Play Now'
|
||||||
expect(page).to have_link 'Browse Tables'
|
# expect(page).to have_link 'Browse Tables'
|
||||||
expect(page).to have_link 'Tutorial'
|
# expect(page).to have_link 'Tutorial'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user