Skip to content

Commit 3946156

Browse files
committed
Add pundit gem
1 parent 121d30a commit 3946156

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ gem "font-awesome-sass", "~> 5.15"
3131
gem "simple_form", github: "heartcombo/simple_form"
3232
gem 'cloudinary', '~> 1.16.0'
3333
gem 'devise'
34+
gem 'pundit'
3435

3536
# Reduces boot times through caching; required in config/boot.rb
3637
gem 'bootsnap', '>= 1.4.2', require: false

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ GEM
131131
pg (1.3.5)
132132
puma (4.3.12)
133133
nio4r (~> 2.0)
134+
pundit (2.2.0)
135+
activesupport (>= 3.0.0)
134136
racc (1.6.0)
135137
rack (2.2.3)
136138
rack-proxy (0.7.2)
@@ -257,6 +259,7 @@ DEPENDENCIES
257259
listen (~> 3.2)
258260
pg (>= 0.18, < 2.0)
259261
puma (~> 4.1)
262+
pundit
260263
rails (~> 6.1.1)
261264
rails-controller-testing
262265
rspec-rails

app/controllers/application_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
class ApplicationController < ActionController::Base
22
before_action :configure_permitted_parameters, if: :devise_controller?
3+
before_action :authenticate_user!
4+
include Pundit
5+
6+
after_action :verify_authorized, except: :index, unless: :skip_pundit?
7+
after_action :verify_policy_scoped, only: :index, unless: :skip_pundit?
38

49
def configure_permitted_parameters
510
# For additional fields in app/views/devise/registrations/new.html.erb
@@ -8,5 +13,10 @@ def configure_permitted_parameters
813
# For additional in app/views/devise/registrations/edit.html.erb
914
devise_parameter_sanitizer.permit(:account_update, keys: [:photo, :nickname])
1015
end
16+
17+
private
18+
19+
def skip_pundit?
20+
devise_controller? || params[:controller] =~ /(^(rails_)?admin)|(^pages$)/
21+
end
1122
end
12-

app/controllers/lists_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@ class ListsController < ApplicationController
22
before_action :set_list, only: [:show, :edit, :update]
33

44
def index
5-
@lists = List.all
5+
# @lists = List.all
6+
@lists = policy_scope(List).all
7+
68
if user_signed_in?
79
@self_lists = List.where(user: current_user.id)
810
end
911
end
1012

1113
def show
14+
authorize @list
15+
1216
if user_signed_in?
1317
@user = User.where(id: current_user.id)
1418
end
1519
end
1620

1721
def new
1822
@list = List.new
23+
authorize @list
1924
end
2025

2126
def create
2227
@list = List.new(list_params)
2328
@list.user = current_user.id
29+
authorize @list
2430

2531
if @list.save
2632
redirect_to list_path(@list)
@@ -30,6 +36,7 @@ def create
3036
end
3137

3238
def edit
39+
authorize @list
3340
end
3441

3542
def update

app/policies/application_policy.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
class ApplicationPolicy
4+
attr_reader :user, :record
5+
6+
def initialize(user, record)
7+
@user = user
8+
@record = record
9+
end
10+
11+
def index?
12+
false
13+
end
14+
15+
def show?
16+
false
17+
end
18+
19+
def create?
20+
false
21+
end
22+
23+
def new?
24+
create?
25+
end
26+
27+
def update?
28+
false
29+
end
30+
31+
def edit?
32+
update?
33+
end
34+
35+
def destroy?
36+
false
37+
end
38+
39+
class Scope
40+
def initialize(user, scope)
41+
@user = user
42+
@scope = scope
43+
end
44+
45+
def resolve
46+
raise NotImplementedError, "You must define #resolve in #{self.class}"
47+
end
48+
49+
private
50+
51+
attr_reader :user, :scope
52+
end
53+
end

app/policies/list_policy.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class ListPolicy < ApplicationPolicy
2+
class Scope < Scope
3+
# NOTE: Be explicit about which records you allow access to!
4+
def resolve
5+
scope.all
6+
end
7+
end
8+
9+
def update?
10+
true
11+
end
12+
13+
def create?
14+
true
15+
end
16+
17+
def index?
18+
true
19+
end
20+
21+
def show?
22+
true
23+
end
24+
end

0 commit comments

Comments
 (0)