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

added strict param. It's able to you use strict has_role? #349

Merged
merged 4 commits into from
Jul 30, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage/*
log*/*
.rbx/*
.rspec
*.swp
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# rolify [![Gem Version](https://badge.fury.io/rb/rolify.svg)](http://badge.fury.io/rb/rolify) [![build status](https://secure.travis-ci.org/RolifyCommunity/rolify.png)](http://travis-ci.org/RolifyCommunity/rolify) [![Dependency Status](https://gemnasium.com/RolifyCommunity/rolify.svg)](https://gemnasium.com/RolifyCommunity/rolify) [![Code Climate](https://codeclimate.com/github/RolifyCommunity/rolify.png)](https://codeclimate.com/github/RolifyCommunity/rolify) [![Coverage Status](https://img.shields.io/coveralls/RolifyCommunity/rolify.svg)](https://coveralls.io/r/RolifyCommunity/rolify?branch=master)


##This is a fork the rolify gem version 4.0.0. It's able to you use strict has_role?

```ruby
class User < ActiveRecord::Base
rolify strict: true
end

@user = User.first

@user.add_role(:forum, Forum)
@user.add_role(:forum, Forum.first)

@user.has_role?(:forum, Froum) #=> true
@user.has_role?(:forum, Froum.first) #=> true
@user.has_role?(:forum, Froum.last) #=> false
```
I.e. you get true only on a role that you manually add.


Very simple Roles library without any authorization enforcement supporting scope on resource object.

Let's see an example:
Expand Down
6 changes: 5 additions & 1 deletion lib/rolify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
module Rolify
extend Configure

attr_accessor :role_cname, :adapter, :resource_adapter, :role_join_table_name, :role_table_name
attr_accessor :role_cname, :adapter, :resource_adapter, :role_join_table_name, :role_table_name, :strict_rolify
@@resource_types = []

def rolify(options = {})
Expand All @@ -31,6 +31,9 @@ def rolify(options = {})

self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)
load_dynamic_methods if Rolify.dynamic_shortcuts

#use strict roles
self.strict_rolify = true if options[:strict]
end

def adapter
Expand Down Expand Up @@ -70,4 +73,5 @@ def role_class
def self.resource_types
@@resource_types
end

end
10 changes: 10 additions & 0 deletions lib/rolify/adapters/active_record/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ def where(relation, *args)
relation.where(conditions, *values)
end

def where_strict(relation, args)
resource = if args[:resource].is_a?(Class)
{class: args[:resource].to_s, id: nil}
else
{class: args[:resource].class.name, id: args[:resource].id}
end

relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
end

def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
role_class.where(:name => role_name, :resource_type => resource_type, :resource_id => resource_id).first_or_create
end
Expand Down
10 changes: 10 additions & 0 deletions lib/rolify/adapters/mongoid/role_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ def where(relation, *args)
relation.any_of(*conditions)
end

def where_strict(relation, args)
resource = if args[:resource].is_a?(Class)
{class: args[:resource].to_s, id: nil}
else
{class: args[:resource].class.name, id: args[:resource].id}
end

relation.where(:name => args[:name], :resource_type => resource[:class], :resource_id => resource[:id])
end

def find_or_create_by(role_name, resource_type = nil, resource_id = nil)
self.role_class.find_or_create_by(:name => role_name,
:resource_type => resource_type,
Expand Down
6 changes: 6 additions & 0 deletions lib/rolify/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ def add_role(role_name, resource = nil)
alias_method :grant, :add_role

def has_role?(role_name, resource = nil)
return has_strict_role?(role_name, resource) if self.class.strict_rolify and resource and resource != :any

if new_record?
role_array = self.roles.detect { |r|
r.name.to_s == role_name.to_s &&
Expand All @@ -38,6 +40,10 @@ def has_role?(role_name, resource = nil)
role_array != []
end

def has_strict_role?(role_name, resource)
self.class.adapter.where_strict(self.roles, name: role_name, resource: resource).any?
end

def has_all_roles?(*args)
args.each do |arg|
if arg.is_a? Hash
Expand Down
33 changes: 33 additions & 0 deletions spec/rolify/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,44 @@
end
end


describe '.resource_types' do

it 'include all models that call resourcify' do
Rolify.resource_types.should include("HumanResource", "Forum", "Group",
"Team", "Organization")
end
end


describe "#strict" do
context "strict user" do
before(:all) do
@strict_user = StrictUser.first
@strict_user.add_role(:forum, Forum.first)
@strict_user.add_role(:forum, Forum)
end

it "should return only strict forum" do
@strict_user.has_role?(:forum, Forum.first).should be true
end

it "should return false on strict another forum" do
@strict_user.has_role?(:forum, Forum.last).should be false
end

it "should return true if user has role on Forum model" do
@strict_user.has_role?(:forum, Forum).should be true
end

it "should return true if user has role any forum name" do
@strict_user.has_role?(:forum, :any).should be true
end

it "should return false when deleted role on Forum model" do
@strict_user.remove_role(:forum, Forum)
@strict_user.has_role?(:forum, Forum).should be false
end
end
end
end
7 changes: 7 additions & 0 deletions spec/support/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ class User < ActiveRecord::Base

class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
has_and_belongs_to_many :strict_users, :join_table => :strict_users_roles

belongs_to :resource, :polymorphic => true

extend Rolify::Adapter::Scopes
end

# Strict user and role classes
class StrictUser < ActiveRecord::Base
rolify strict: true
end

# Resourcifed and rolifed at the same time
class HumanResource < ActiveRecord::Base
resourcify :resources
Expand Down
11 changes: 10 additions & 1 deletion spec/support/adapters/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ class User
field :login, :type => String
end

# Standard user and role classes
class StrictUser
include Mongoid::Document
rolify strict: true

field :login, :type => String
end

class Role
include Mongoid::Document
has_and_belongs_to_many :users
has_and_belongs_to_many :strict_users
belongs_to :resource, :polymorphic => true

field :name, :type => String
Expand Down Expand Up @@ -148,4 +157,4 @@ class Organization

class Company < Organization

end
end
4 changes: 2 additions & 2 deletions spec/support/data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Users
[ User, Customer, Admin::Moderator ].each do |user|
[ User, Customer, Admin::Moderator, StrictUser ].each do |user|
user.destroy_all

user.create(:login => "admin")
user.create(:login => "moderator")
user.create(:login => "god")
Expand Down
7 changes: 6 additions & 1 deletion spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end
end

[ :users, :human_resources, :customers, :admin_moderators ].each do |table|
[ :users, :human_resources, :customers, :admin_moderators, :strict_users ].each do |table|
create_table(table) do |t|
t.string :login
end
Expand All @@ -21,6 +21,11 @@
t.references :role
end

create_table(:strict_users_roles, :id => false) do |t|
t.references :strict_user
t.references :role
end

create_table(:human_resources_roles, :id => false) do |t|
t.references :human_resource
t.references :role
Expand Down