Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch auth system to cancan WIP #69

Merged
merged 31 commits into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4432b07
Remove the_role and add cancancan as dep
henrikssn Mar 19, 2015
4783ae8
Remove some mentions of TheRole
henrikssn Mar 19, 2015
111c219
Add cancan ability model
henrikssn Mar 19, 2015
ddf8018
Remove references to the_role in code.
jforberg Mar 21, 2015
f6c9b52
Handle access errors centrally in appcontroller.
jforberg Mar 21, 2015
a97af7a
Replace current_user.moderator with can? in views
henrikssn Mar 21, 2015
172e63c
Add pry so we can debug code.
jforberg Mar 21, 2015
243f4a1
Port the news controller to cancan.
jforberg Mar 21, 2015
64be0c8
Merge branch 'cancan' of github.com:fsek/web into cancan
jforberg Mar 21, 2015
ccf2593
Cancan dynamic permission model
henrikssn Mar 21, 2015
cd1f6a6
Small fix use single quotes for constant string.
jforberg Mar 21, 2015
d0163da
Merge branch 'master' into cancan
henrikssn Mar 21, 2015
d6d93bd
Merge branch 'cancan' of github.com:fsek/web into cancan
henrikssn Mar 21, 2015
b822543
Forgot to add some files
henrikssn Mar 21, 2015
7544d99
Site should now start properly
henrikssn Mar 23, 2015
f07c536
Regenerated binstubs
henrikssn Mar 23, 2015
2ccba80
Site now kind-of works with cancan
henrikssn Mar 23, 2015
a80bc9e
Permissions model now working
henrikssn Mar 25, 2015
4bb22b5
Rake tasks for generating permissions
henrikssn Mar 25, 2015
17d6385
Hound fixes
henrikssn Mar 25, 2015
67953fc
Factory for constant
henrikssn Mar 25, 2015
77cb3ec
Fixed all tests
henrikssn Mar 26, 2015
b93b0b2
Merge remote-tracking branch 'origin/master' into cancan
henrikssn Mar 26, 2015
b3be67d
Missed a merge conflict
henrikssn Mar 26, 2015
72032e4
All tests pass, ready to merge
henrikssn Mar 26, 2015
0a4720d
Test config
henrikssn Mar 26, 2015
0110d26
Forgot two files
henrikssn Mar 26, 2015
47eb1a5
Removed some old things in seeds.rb, changed validate-syntax in Permi…
davidwessman Mar 26, 2015
ff7ff47
Fix post edit
henrikssn Mar 26, 2015
ff9cc73
Merge branch 'cancan' of github.com:fsek/web into cancan
henrikssn Mar 26, 2015
ee8fdb4
Hound fixes
henrikssn Mar 26, 2015
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
10 changes: 6 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source 'https://rubygems.org'

gem 'rails', '~> 4.2.0'

gem 'cancancan'

# Capistrano stuff
gem 'capistrano', '~> 3.1.0'
gem 'capistrano-bundler', '~> 1.1.2'
Expand All @@ -15,6 +17,7 @@ gem 'devise'
gem 'fancybox2-rails'
gem 'font-awesome-rails'
gem 'fullcalendar-rails'
gem 'haml'
gem 'icalendar'
gem 'jbuilder'
gem 'jquery-rails'
Expand All @@ -27,8 +30,6 @@ gem 'prawn-rails'
gem 'responders', '~> 2.0'
gem 'sass-rails'
gem 'sqlite3'
gem 'the_role_bootstrap3_ui'
gem 'the_role', github: 'TheRole/TheRoleApi', tag: 'v2.6'
gem 'turbolinks'
gem 'uglifier'
gem 'wice_grid'
Expand All @@ -42,10 +43,11 @@ group :development, :test do
gem 'factory_girl_rails'
gem 'mailcatcher'
gem 'poltergeist'
gem 'pry-byebug', '2.0.0'
gem 'pry-rails'
gem 'rspec-rails'
gem 'web-console', '~> 2.0'
gem 'pry-rails'
gem 'pry-byebug'
gem 'better_errors'
end

group :test do
Expand Down
47 changes: 16 additions & 31 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
GIT
remote: git://github.com/TheRole/TheRoleApi.git
revision: 3f2a247bd922e3280ae0233b8ea439a6e5e79ca4
tag: v2.6
specs:
the_role (2.6)
multi_json
rails (>= 3, < 5)
the_string_to_slug (~> 1.2)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -48,13 +38,16 @@ GEM
tzinfo (~> 1.1)
arel (6.0.0)
bcrypt (3.1.10)
better_errors (2.1.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (3.5.1)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
slop (~> 3.6)
byebug (4.0.3)
columnize (= 0.9.0)
cancancan (1.10.1)
capistrano (3.1.0)
i18n
rake (>= 10.0.0)
Expand Down Expand Up @@ -91,10 +84,9 @@ GEM
coffee-script-source (1.8.0)
colorize (0.7.5)
columnize (0.9.0)
daemons (1.2.1)
daemons (1.2.2)
database_cleaner (1.4.1)
debug_inspector (0.0.2)
debugger-linecache (1.2.0)
devise (3.4.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
Expand Down Expand Up @@ -192,8 +184,8 @@ GEM
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (2.0.0)
byebug (~> 3.4)
pry-byebug (3.1.0)
byebug (~> 4.0)
pry (~> 0.10)
pry-rails (0.3.3)
pry (>= 0.9.10)
Expand Down Expand Up @@ -221,9 +213,6 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rails-i18n (4.0.4)
i18n (~> 0.6)
railties (~> 4.0)
railties (4.2.1)
actionpack (= 4.2.1)
activesupport (= 4.2.1)
Expand Down Expand Up @@ -264,10 +253,10 @@ GEM
multi_json (~> 1.0)
simplecov-html (~> 0.9.0)
simplecov-html (0.9.0)
sinatra (1.4.5)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (>= 1.3, < 3)
skinny (0.2.3)
eventmachine (~> 1.0.0)
thin (~> 1.5.0)
Expand All @@ -286,11 +275,6 @@ GEM
colorize (>= 0.7.0)
net-scp (>= 1.1.2)
net-ssh (>= 2.8.0)
the_role_bootstrap3_ui (1.1)
haml
rails (>= 3, < 5)
the_string_to_slug (1.2)
rails-i18n (~> 4.0.0.pre)
therubyracer (0.12.1)
libv8 (~> 3.16.14.0)
ref
Expand Down Expand Up @@ -329,6 +313,8 @@ PLATFORMS
ruby

DEPENDENCIES
better_errors
cancancan
capistrano (~> 3.1.0)
capistrano-bundler (~> 1.1.2)
capistrano-rails (~> 1.1.1)
Expand All @@ -343,6 +329,7 @@ DEPENDENCIES
fancybox2-rails
font-awesome-rails
fullcalendar-rails
haml
icalendar
jbuilder
jquery-rails
Expand All @@ -354,16 +341,14 @@ DEPENDENCIES
paperclip
poltergeist
prawn-rails
pry-byebug (= 2.0.0)
pry-byebug
pry-rails
rails (~> 4.2.0)
responders (~> 2.0)
rspec-rails
sass-rails
shoulda-matchers
sqlite3
the_role!
the_role_bootstrap3_ui
therubyracer
turbolinks
uglifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//= require jquery_ujs
//= require bootstrap
//= require_self
//= require the_role_editinplace
//= require jquery-ui/datepicker
//= require moment
//= require fullcalendar
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/admin/elections_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: UTF-8
class Admin::ElectionsController < ApplicationController
before_action :login_required
before_action :authenticate
load_permissions_and_authorize_resource

before_action :set_election, only: [:show,:edit,:update,:destroy,:candidates,:nominations]

def new
Expand Down Expand Up @@ -59,7 +59,7 @@ def candidates
end
private
def authenticate
flash[:error] = t('the_role.access_denied')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

redirect_to(:back) unless (current_user) && (current_user.moderator?(:val))
rescue ActionController::RedirectBackError
redirect_to root_path
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/admin/rents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Admin::RentsController < ApplicationController
before_action :login_required
before_action :authenticate
load_permissions_and_authorize_resource
before_action :set_rent, only: [:show, :update, :destroy, :preview]
before_action :set_councils, only: [:new, :show]

Expand Down
8 changes: 3 additions & 5 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# encoding:UTF-8
class AlbumsController < ApplicationController

before_filter :login_required
before_filter :authenticate, only: [:new, :create,:edit,:destroy,:update,:settings,:destroy_images]
load_permissions_and_authorize_resource
before_action :set_edit
before_action :set_album, except: [:index,:new,:create,:settings]
before_action :categories
Expand Down Expand Up @@ -187,7 +185,7 @@ def update
end
private
def authenticate
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are using this method everywhere. Can't we move it to application_controller?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as it is now - we wanted to specify which rule was used. For example in the album controller we wanted to check if user was current_user.moderator?(:album).

If we change it to application controller with a variable for the :rule_nameit can work with the_role.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cancan does this as part of load_and_authorize_resource. So it is already in ApplicationController.

We do need to move the access rules to Ability though.

flash[:error] = t('the_role.access_denied')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected.

redirect_to(:back) unless (current_user) && (current_user.moderator?(:galleri))

rescue ActionController::RedirectBackError
Expand All @@ -213,4 +211,4 @@ def image_params
def album_params
params.fetch(:album,{}).permit(:title,:description,:author,:location,:public,:start_date,:end_date,:album_category_ids => [],:subcategory_ids => [],images_parameters: [:id, :foto])
end
end
end
57 changes: 42 additions & 15 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# encoding:UTF-8
class ApplicationController < ActionController::Base
include TheRole::Controller
protect_from_forgery

before_filter :configure_permitted_devise_parameters, if: :devise_controller?
before_filter :set_locale

def access_denied
flash[:error] = t('the_role.access_denied')
redirect_to(:back)
rescue_from CanCan::AccessDenied do |ex|
flash[:error] = ex.message
render text: '', layout: true, status: :forbidden
end

rescue_from ActionController::RedirectBackError do
redirect_to root_path
end
rescue_from ActiveRecord::RecordInvalid do |ex|
flash[:error] =
"Fel i formulär: #{ex.record.errors.full_messages.join '; '}"
render referring_action, status: :unprocessable_entity
end

rescue_from ActiveRecord::RecordNotFound do
# translate record not found -> HTTP 404
Expand All @@ -23,24 +24,51 @@ def access_denied
protected

def configure_permitted_devise_parameters
devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:username, :password, :remember_me)}
devise_parameter_sanitizer.for(:sign_up) {|u| u.permit(:username, :email, :password, :password_confirmation) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :password, :remember_me) }
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [115/100]

devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:password, :password_confirmation, :current_password) }
end

def self.permission
name.gsub('Controller', '').singularize.split('::').last.constantize.name rescue nil
end

def current_ability
@current_ability ||= Ability.new(current_user)
end

# load the permissions for the current user so that UI can be manipulated
def load_permissions
return unless current_user
@current_permissions = current_user.profile.posts.each do |post|
post.permissions.map { |i| [i.subject_class, i.action] }
end
end

# Enables authentication and
def self.load_permissions_and_authorize_resource(*args)
load_and_authorize_resource(*args)
before_filter(:load_permissions, *args)
end

def self.skip_authorization(*args)
skip_authorization_check(*args)
skip_before_filter(:load_permissions, *args)
end

def set_locale
locale = 'sv'
langs = %w{ sv en }
langs = %w{ sv en }

if params[:locale]
lang = params[:locale]
if langs.include? lang
locale = lang
locale = lang
cookies[:locale] = lang
end
else
if cookies[:locale]
lang = cookies[:locale]
lang = cookies[:locale]
locale = lang if langs.include? lang
end
end
Expand All @@ -49,8 +77,7 @@ def set_locale
redirect_to(:back) if params[:locale]
end

def verify_admin
flash[:error] = t('the_role.access_denied')
redirect_to(:root) unless (current_user) && (current_user.admin?)
def referring_action
Rails.application.routes.recognize_path(request.referer)[:action]
end
end
39 changes: 11 additions & 28 deletions app/controllers/constants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
class ConstantsController < ApplicationController
before_action :login_required
before_action :verify_admin
load_permissions_and_authorize_resource

before_action :set_constant, only: [:show, :edit, :update, :destroy]

# GET /constants
# GET /constants.json
def index
@constants = Constant.all
end

# GET /constants/1
# GET /constants/1.json
def show
end

Expand All @@ -24,43 +22,28 @@ def edit
end

# POST /constants
# POST /constants.json
def create
@constant = Constant.new(constant_params)

respond_to do |format|
if @constant.save
format.html { redirect_to @constant, notice: 'Constant was successfully created.' }
format.json { render action: 'show', status: :created, location: @constant }
else
format.html { render action: 'new' }
format.json { render json: @constant.errors, status: :unprocessable_entity }
end
if @constant.save
redirect_to @constant, notice: 'Constant was successfully created.'
else
render action: 'new'
end
end

# PATCH/PUT /constants/1
# PATCH/PUT /constants/1.json
def update
respond_to do |format|
if @constant.update(constant_params)
format.html { redirect_to @constant, notice: 'Constant was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @constant.errors, status: :unprocessable_entity }
end
if @constant.update(constant_params)
redirect_to @constant, notice: 'Constant was successfully updated.'
else
render action: 'edit'
end
end

# DELETE /constants/1
# DELETE /constants/1.json
def destroy
@constant.destroy
respond_to do |format|
format.html { redirect_to constants_url }
format.json { head :no_content }
end
redirect_to constants_url
end

private
Expand Down
Loading