Commit 92e948d5 by Tran Hoang Viet

VietTH: Apply permission to order resource. Use gem cancan

parent f308e16c
...@@ -68,6 +68,8 @@ gem 'settingslogic', '~> 2.0.9' ...@@ -68,6 +68,8 @@ gem 'settingslogic', '~> 2.0.9'
gem 'draper', '~> 2.1.0' gem 'draper', '~> 2.1.0'
gem 'devise', '~> 3.5.1' gem 'devise', '~> 3.5.1'
gem 'rolify', '~> 4.0.0'
gem 'cancan', '~> 1.6.10'
gem 'carrierwave', '~> 0.10.0' gem 'carrierwave', '~> 0.10.0'
gem 'mini_magick', '~> 3.8.1' gem 'mini_magick', '~> 3.8.1'
...@@ -97,5 +99,3 @@ gem 'cartman', '~> 2.1.2' ...@@ -97,5 +99,3 @@ gem 'cartman', '~> 2.1.2'
gem 'font-awesome-rails', '~> 4.3.0.0' gem 'font-awesome-rails', '~> 4.3.0.0'
gem 'meta-tags', '~> 2.0.0' gem 'meta-tags', '~> 2.0.0'
\ No newline at end of file
gem 'rolify', '~> 4.0.0'
\ No newline at end of file
...@@ -47,6 +47,7 @@ GEM ...@@ -47,6 +47,7 @@ GEM
builder (3.2.2) builder (3.2.2)
byebug (5.0.0) byebug (5.0.0)
columnize (= 0.9.0) columnize (= 0.9.0)
cancan (1.6.10)
capistrano (3.4.0) capistrano (3.4.0)
i18n i18n
rake (>= 10.0.0) rake (>= 10.0.0)
...@@ -326,6 +327,7 @@ PLATFORMS ...@@ -326,6 +327,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
byebug byebug
cancan (~> 1.6.10)
capistrano-rails capistrano-rails
capistrano-rvm capistrano-rvm
capistrano-sidekiq (~> 0.5.2) capistrano-sidekiq (~> 0.5.2)
......
...@@ -2,6 +2,10 @@ class ApplicationController < ActionController::Base ...@@ -2,6 +2,10 @@ class ApplicationController < ActionController::Base
include MetaManagement include MetaManagement
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
layout Proc.new { |controller| controller.devise_controller? ? 'devise' : 'application' } layout Proc.new { |controller| controller.devise_controller? ? 'devise' : 'application' }
# Prevent CSRF attacks by raising an exception. # Prevent CSRF attacks by raising an exception.
......
class OrdersController < ApplicationController class OrdersController < ApplicationController
load_and_authorize_resource :order, only: [:show, :update]
before_action :authenticate_user! before_action :authenticate_user!
before_action :set_order, only: [:show, :update] before_action :set_order, only: [:show, :update]
......
class Ability
include CanCan::Ability
attr_reader :user
def initialize(current_user)
@user = current_user || User.new # guest user (not logged in)
if user.admin?
permission_admin
else
permission_user
end
end
def permission_admin
can :manage, :all
end
def permission_user
can :read, Order do |order|
order.user == user
end
end
end
class Role < ActiveRecord::Base class Role < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :users_roles has_many :users, through: :users_roles
belongs_to :resource, :polymorphic => true belongs_to :resource, :polymorphic => true
validates :resource_type, validates :resource_type,
......
...@@ -24,12 +24,12 @@ ...@@ -24,12 +24,12 @@
.form-group .form-group
%label.col-md-2.col-sm-3.col-xs-3.control-label Status %label.col-md-2.col-sm-3.col-xs-3.control-label Status
.col-md-2.col-sm-9.col-xs-9.value .col-md-2.col-sm-9.col-xs-9.value
- if current_user.admin? - if can?(:update, f.object)
= f.select :status, Order.statuses.keys.map { |status| [status.titleize, status] }, {}, class: 'form-control' = f.select :status, Order.statuses.keys.map { |status| [status.titleize, status] }, {}, class: 'form-control'
- else - else
= f.object.status.titleize = f.object.status.titleize
- if current_user.admin? - if can?(:update, f.object)
.form-group .form-group
%label.col-md-2.col-sm-3.col-xs-3.control-label %label.col-md-2.col-sm-3.col-xs-3.control-label
.col-md-2.col-sm-4.col-xs-9 .col-md-2.col-sm-4.col-xs-9
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment