Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
include Pundit::Authorization

before_action :configure_permitted_parameters, if: :devise_controller?

# Pundit: allow-list approach
after_action :verify_authorized, except: :index, unless: :skip_pundit?
after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
redirect_to(root_path)
end

def configure_permitted_parameters
# For additional fields in app/views/devise/registrations/new.html.erb
devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :address, :phone_number])

# For additional in app/views/devise/registrations/edit.html.erb
devise_parameter_sanitizer.permit(:account_update, keys: [:name, :address, :phone_number])
end

private

def skip_pundit?
devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)/
end
end
4 changes: 4 additions & 0 deletions app/controllers/credit_cards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class CreditCardsController < ApplicationController

def index
# @credit_cards = CreditCard.all
# @credit_cards = policy_scope(CreditCard)
# add a filter for user, when more than 1 credit_card
end

def new
@credit_card = CreditCard.new
authorize @credit_card
end

def create
Expand All @@ -20,6 +22,7 @@ def create
else
render :new, status: :unprocessable_entity
end
authorize @credit_card
end

def show
Expand Down Expand Up @@ -50,6 +53,7 @@ def destroy

def set_credit_card
@credit_card = CreditCard.find(params[:id])
authorize @credit_card
end

def credit_card_params
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/goals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ class GoalsController < ApplicationController
before_action :set_goal, only: %i[show edit destroy update]

def index
@goals = Goal.where(user: current_user)
# @goals = Goal.where(user: current_user)
goals = policy_scope(Goal)
end

def new
@goal = Goal.new
authorize @goal
end

def create
Expand All @@ -17,6 +19,7 @@ def create
else
render :new, status: :unprocessable_entity, notice: 'Failed to create'
end
authorize @goal
end

def show
Expand Down Expand Up @@ -46,6 +49,7 @@ def goal_params

def set_goal
@goal = Goal.find(params[:id])
authorize @goal
end

end
3 changes: 2 additions & 1 deletion app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class PagesController < ApplicationController
skip_before_action :authenticate_user!, only: [ :home ]

def profile
@user = current_user
# @user = current_user
authorize @user
end
end
4 changes: 4 additions & 0 deletions app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class TransactionsController < ApplicationController

def index
@transactions = Transaction.where(credit_card: current_user.credit_cards.first)
authorize @transaction
end

def create
Expand All @@ -15,9 +16,12 @@ def create
else
render :new, status: :unprocessable_entity
end

authorize @transaction
end

def show
authorize @transaction
end

private
Expand Down
15 changes: 10 additions & 5 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,36 @@ def initialize(user, record)
end

def index?
false
is_owner?
end

def show?
false
is_owner?
end

def create?
false
true
end

def new?
create?
end

def update?
false
is_owner?
end

def edit?
update?
end

def destroy?
false
is_owner?
end

private
def is_owner?
record.user == user
end

class Scope
Expand Down
8 changes: 8 additions & 0 deletions app/policies/credit_card_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreditCardPolicy < ApplicationPolicy
class Scope < Scope
# NOTE: Be explicit about which records you allow access to!
def resolve
scope.where(user:user)
end
end
end
8 changes: 8 additions & 0 deletions app/policies/goal_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class GoalPolicy < ApplicationPolicy
class Scope < Scope
# NOTE: Be explicit about which records you allow access to!
def resolve
scope.where(user: user)
end
end
end
8 changes: 8 additions & 0 deletions app/policies/transaction_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class TransactionPolicy < ApplicationPolicy
class Scope < Scope
# NOTE: Be explicit about which records you allow access to!
def resolve
scope.where(user:user)
end
end
end
18 changes: 18 additions & 0 deletions test/policies/application_record_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class ApplicationRecordPolicyTest < ActiveSupport::TestCase
def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end
18 changes: 18 additions & 0 deletions test/policies/credit_card_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class CreditCardPolicyTest < ActiveSupport::TestCase
def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end
18 changes: 18 additions & 0 deletions test/policies/goal_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class GoalPolicyTest < ActiveSupport::TestCase
def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end
18 changes: 18 additions & 0 deletions test/policies/transaction_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class TransactionPolicyTest < ActiveSupport::TestCase
def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end
18 changes: 18 additions & 0 deletions test/policies/user_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

class UserPolicyTest < ActiveSupport::TestCase
def test_scope
end

def test_show
end

def test_create
end

def test_update
end

def test_destroy
end
end