Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Final right on clean #13

Merged
merged 7 commits into from
Apr 13, 2018
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This changelog adheres to [Keep a CHANGELOG](http://keepachangelog.com/).
- Internal improvement of RightOn::ByGroup
- Internal extraction of 'allowed?' feature for failure message
- CanCanRight functionality merged into RightOn
- Cleanup of CanCanRight/RightOn merge

### Fixed
- [TT-3352] Ensure roles currently in use cannot be deleted
Expand Down
69 changes: 0 additions & 69 deletions lib/right_on/action_controller_extensions.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/right_on/by_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def action_rights(controller, actions)
end

def rights_by_name!(name)
@rights_by_name[name] or fail name.inspect
@rights_by_name[name] or fail RightOn::RightNotFound, name.inspect

Choose a reason for hiding this comment

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

Style/AndOr: Use || instead of or.

end
end

RightNotFound = Class.new(RightOn::Error)
end
41 changes: 35 additions & 6 deletions lib/right_on/controller_additions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module RightOn
module ControllerAdditions
def self.included(base)
base.module_eval do
class_attribute :rights_from
class_attribute :permission_denied_layout
end
end

private

def authorize_action!
controller = (self.rights_from || params[:controller]).to_s
action = params[:action].to_s
Expand All @@ -10,14 +19,34 @@ def authorize_action!
end

def can_access_controller_action?(controller, action)
(can?(:access, controller) && !Right.where(subject: controller + '#' + action).exists?) ||
(can?(:access, controller) && !Right.where(ccr_subject: controller + '#' + action).exists?) ||

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [100/80]

can?(:access, controller + '#' + action)
end
end
end

if defined? ActionController::Base
ActionController::Base.class_eval do
include RightOn::ControllerAdditions
def access_granted?
can? :access, [params[:controller], params[:action]].join('#')
end

def rescue_access_denied(exception)

Choose a reason for hiding this comment

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

Lint/UnusedMethodArgument: Unused method argument - exception. If it's necessary, use _ or _exception as an argument name to indicate that it won't be used. You can also write as rescue_access_denied(*) if you want the method to accept any arguments but don't care about them.

@permission_denied_response = RightOn::PermissionDeniedResponse.new(params, controller_action_options)

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [108/80]


respond_to do |format|
format.html do
render status: :unauthorized,
template: 'permission_denied',
layout: ( permission_denied_layout || false )

Choose a reason for hiding this comment

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

Layout/SpaceInsideParens: Space inside parentheses detected.

end

format.json do
render status: :unauthorized, json: @permission_denied_response.to_json

Choose a reason for hiding this comment

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

Metrics/LineLength: Line is too long. [81/80]

end
end
end

def controller_action_options
opts = params.slice(:controller, :action)
opts[:controller] = rights_from.to_s if rights_from
opts
end
end
end
6 changes: 5 additions & 1 deletion lib/right_on/rails.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
require 'right_on/role_model'
require 'right_on/ability'
require 'right_on/rule'
require 'right_on/error'
require 'right_on/right'
require 'right_on/role'
require 'right_on/right_allowed'
require 'right_on/by_group'
require 'right_on/action_controller_extensions'
require 'cancan/exceptions'
require 'right_on/controller_additions'
require 'right_on/permission_denied_response'
34 changes: 0 additions & 34 deletions spec/action_controller_extensions_spec.rb

This file was deleted.

7 changes: 2 additions & 5 deletions spec/controller_additions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def self.where(args)
describe RightOn::ControllerAdditions do
let(:rule_override) { false }
before do
rule_class = class_double('RightOn::ControllerAdditions::Model')
allow(RightOn::Right).to receive(:where).and_return(double(exists?: rule_override))
end

Expand All @@ -36,6 +35,8 @@ def initialize(user)
end

class Controller < ActionController::Base
include RightOn::ControllerAdditions

def rights_from
nil
end
Expand All @@ -54,10 +55,6 @@ def current_user
Controller.new
}

it 'should respond to authorize_action!' do
expect(controller.respond_to? :authorize_action!).to be_truthy
end

describe 'private #authorize_action!' do
context 'when the ability has a matching rule' do
let(:right) {
Expand Down
35 changes: 0 additions & 35 deletions spec/right_on_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,3 @@
end
end
end

describe 'when checking accessibility to a controller' do

let(:test_controller_right) { RightOn::Right.new(name: 'test', controller: 'test') }
let(:user) { double(rights: [test_controller_right]) }
let(:controller) { 'test' }
let(:action) { 'index' }
let(:params) { {controller: 'test', action: 'index'} }

before do
stub_const 'TestController', double(current_user: user, params: params)
TestController.extend RightOn::ActionControllerExtensions
allow(TestController).to receive(:rights_from).and_return(nil)
end

specify { expect(TestController.access_allowed?(controller)).to be_truthy }
specify { expect(TestController.access_allowed?('other')).to be_falsey }
specify { expect(TestController.access_allowed_to_controller?(controller)).to be_truthy }
specify { expect(TestController.access_allowed_to_controller?('other')).to be_falsey }

describe 'when inheriting rights' do
let(:controller) { 'test_inherited' }

before do
stub_const 'TestInheritedController', double(current_user: user, params: params)
TestInheritedController.extend RightOn::ActionControllerExtensions
allow(TestInheritedController).to receive(:rights_from).and_return(:test)
end

specify { expect(TestInheritedController.access_allowed?(controller)).to be_falsey }
specify { expect(TestInheritedController.access_allowed?('other')).to be_falsey }
specify { expect(TestInheritedController.access_allowed_to_controller?(controller)).to be_truthy }
specify { expect(TestInheritedController.access_allowed_to_controller?('other')).to be_falsey }
end
end
14 changes: 0 additions & 14 deletions spec/support/bootstrap.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,4 @@
class Bootstrap
def self.reset_database
RightOn::Right.delete_all
RightOn::Role.delete_all
User.delete_all

basic_right = RightOn::Right.create!(name: 'basic', controller: 'basic')
admin_right = RightOn::Right.create!(name: 'admin', controller: 'admin')
basic_role = RightOn::Role.create!(title: 'Basic', rights: [basic_right])
admin_role = RightOn::Role.create!(title: 'Admin', rights: [admin_right])

User.create!(name: 'basic', roles: [basic_role])
User.create!(name: 'admin', roles: [basic_role, admin_role])
end

def self.various_rights_with_actions
RightOn::Right.delete_all
{
Expand Down
2 changes: 1 addition & 1 deletion spec/support/coverage_loader.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'simplecov-rcov'
require 'coveralls'
require 'coverage/kit'
Coverage::Kit.setup(minimum_coverage: 92.2)
Coverage::Kit.setup(minimum_coverage: 92.8)