Skip to content

Commit fd27b87

Browse files
committed
Adding Pundit to the application to centralize role-based authorization into /app/policies folder.
Updating binstubs with new version of spring and so pundit works.
1 parent 217fdee commit fd27b87

File tree

12 files changed

+119
-12
lines changed

12 files changed

+119
-12
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ gem 'draper'
2929
gem 'simple_form', '3.1.1'
3030
gem 'zeroclipboard-rails'
3131
gem 'responders', '~> 2.0'
32+
gem 'pundit'
3233

3334
group :production do
3435
gem 'rails_12factor'

Gemfile.lock

+8-2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ GEM
230230
interception (>= 0.5)
231231
pry
232232
puma (2.15.3)
233+
pundit (1.1.0)
234+
activesupport (>= 3.0.0)
233235
quiet_assets (1.1.0)
234236
railties (>= 3.1, < 5.0)
235237
rack (1.6.4)
@@ -311,7 +313,7 @@ GEM
311313
actionpack (~> 4.0)
312314
activemodel (~> 4.0)
313315
slop (3.6.0)
314-
spring (1.6.2)
316+
spring (1.7.2)
315317
spring-commands-rspec (1.0.4)
316318
spring (>= 0.9.1)
317319
sprockets (3.6.1)
@@ -381,6 +383,7 @@ DEPENDENCIES
381383
pry-remote
382384
pry-rescue
383385
puma (~> 2.13)
386+
pundit
384387
quiet_assets
385388
rack-mini-profiler
386389
rack-timeout (~> 0.2.4)
@@ -399,5 +402,8 @@ DEPENDENCIES
399402
web-console (~> 2.0)
400403
zeroclipboard-rails
401404

405+
RUBY VERSION
406+
ruby 2.3.0p0
407+
402408
BUNDLED WITH
403-
1.11.2
409+
1.12.5

app/controllers/application_controller.rb

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
class ApplicationController < ActionController::Base
2+
include Pundit
3+
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
4+
25
require "csv"
36
# Prevent CSRF attacks by raising an exception.
47
# For APIs, you may want to use :null_session instead.
@@ -60,6 +63,11 @@ def require_proposal
6063
@proposal = @event.proposals.find_by!(uuid: params[:proposal_uuid] || params[:uuid])
6164
end
6265

66+
def user_not_authorized
67+
flash[:alert] = "You are not authorized to perform this action."
68+
redirect_to(request.referrer || root_path)
69+
end
70+
6371
def event_params
6472
params.require(:event).permit(
6573
:name, :contact_email, :slug, :url, :valid_proposal_tags,

app/policies/application_policy.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ApplicationPolicy
2+
3+
def initialize(user, record)
4+
raise Pundit::NotAuthorizedError, "must be logged in" unless user
5+
@user = user
6+
@record = record
7+
end
8+
9+
end

app/policies/event_policy.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class EventPolicy
2+
attr_reader :current_user, :model
3+
4+
def initialize(current_user, model)
5+
@current_user = current_user
6+
@event = model
7+
end
8+
9+
def index?
10+
@current_user.admin? || @current_user.organizer_for_event?(@event)
11+
end
12+
13+
def show?
14+
@current_user.admin? || @current_user.organizer_for_event?(@event)
15+
end
16+
17+
def update?
18+
@current_user.admin? || @current_user.organizer_for_event?(@event)
19+
end
20+
21+
def destroy?
22+
@current_user.admin? || @current_user.organizer_for_event?(@event)
23+
end
24+
25+
end
26+
27+
28+
# def index?
29+
# false
30+
# end
31+
32+
# def show?
33+
# scope.where(:id => record.id).exists?
34+
# end
35+
36+
# def create?
37+
# false
38+
# end
39+
40+
# def new?
41+
# create?
42+
# end
43+
44+
# def update?
45+
# false
46+
# end
47+
48+
# def edit?
49+
# update?
50+
# end
51+
52+
# def destroy?
53+
# false
54+
# end
55+
56+
# def scope
57+
# Pundit.policy_scope!(user, record.class)
58+
# end
59+
60+
# class Scope
61+
# attr_reader :user, :scope
62+
63+
# def initialize(user, scope)
64+
# @user = user
65+
# @scope = scope
66+
# end
67+
68+
# def resolve
69+
# scope
70+
# end
71+
# end

app/views/admin/events/_form.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@
3636
- if event && event.persisted? && current_user.admin?
3737
= link_to 'Delete Event', admin_event_path(event), method: :delete, data: { confirm: 'Are you sure you want to delete this event?' }, class: 'btn btn-danger pull-left'
3838

39-
=submit_tag("Save", class: "pull-right btn btn-success", type: "submit", disabled: !current_user.organizer_for_event?(f.object))
39+
=submit_tag("Save", class: "pull-right btn btn-success", type: "submit", disabled: !policy(@event).update?)

app/views/admin/events/_tags_form.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
= f.input :valid_review_tags, placeholder: 'Separate multiple tags with commas'
1111
//= f.text_field :valid_review_tags, class: 'form-control'
1212
%p.help-block This is a comma separated list of tags allowed for use during proposal reviews. These limits apply to tags used internally by reviewers and not to publicly displayed tags.
13-
=submit_tag("Save Tags", class: "pull-right btn btn-success", type: "submit", disabled: !current_user.organizer_for_event?(f.object))
13+
=submit_tag("Save Tags", class: "pull-right btn btn-success", type: "submit", disabled: !policy(@event).update?)

bin/rails

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/usr/bin/env ruby
2+
begin
3+
load File.expand_path('../spring', __FILE__)
4+
rescue LoadError => e
5+
raise unless e.message.include?('spring')
6+
end
27
APP_PATH = File.expand_path('../../config/application', __FILE__)
38
require_relative '../config/boot'
49
require 'rails/commands'

bin/rake

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#!/usr/bin/env ruby
2+
begin
3+
load File.expand_path('../spring', __FILE__)
4+
rescue LoadError => e
5+
raise unless e.message.include?('spring')
6+
end
27
require_relative '../config/boot'
38
require 'rake'
49
Rake.application.run

bin/rspec

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env ruby
22
begin
3-
load File.expand_path("../spring", __FILE__)
4-
rescue LoadError
3+
load File.expand_path('../spring', __FILE__)
4+
rescue LoadError => e
5+
raise unless e.message.include?('spring')
56
end
67
require 'bundler/setup'
78
load Gem.bin_path('rspec', 'rspec')

bin/spring

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# It gets overwritten when you run the `spring binstub` command.
55

66
unless defined?(Spring)
7-
require "rubygems"
8-
require "bundler"
7+
require 'rubygems'
8+
require 'bundler'
99

10-
if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m)
11-
Gem.paths = { "GEM_PATH" => [Bundler.bundle_path.to_s, *Gem.path].uniq }
12-
gem "spring", match[1]
13-
require "spring/binstub"
10+
if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m))
11+
Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(Gem.path_separator) }
12+
gem 'spring', match[1]
13+
require 'spring/binstub'
1414
end
1515
end

spec/rails_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require File.expand_path("../../config/environment", __FILE__)
55
require 'rspec/rails'
66
require 'capybara/rspec'
7+
require 'pundit/rspec'
78

89
# Requires supporting ruby files with custom matchers and macros, etc, in
910
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are

0 commit comments

Comments
 (0)