Skip to content

Commit

Permalink
Add a role? method to the user mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
mfinelli committed Oct 8, 2019
1 parent 1aadc02 commit 6798a11
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
This file keeps track of changes between releases for the ruolo project
which adheres to [semantic versioning](https://semver.org).

## unreleased

* Add a `role?` method to the user mixin to check for given roles instead
of permissions.

## v0.2.0 2019-09-11

Initial cleanup of original code: add tests and documentation.
Expand Down
11 changes: 10 additions & 1 deletion lib/ruolo/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ module Models
# A mixin to include in downstream user classes that adds useful helper
# methods for dealing with roles and permissions.
module User
# Given the name of a permission determin whether the user's role
# Given the name of a permission determine whether the user's role
# membership includes it.
#
# @param permission [String] the name of the permission
# @return [Boolean] if the user has the permission or not
def permission?(permission)
roles.map { |role| role.permissions.map(&:name) }.flatten.uniq.include?(permission)
end

# Given a role name or array of role names determine if the user has
# that/those roles.
#
# @param role [String|Array<String>] role(s) to check
# @return [Boolean] if the user has the given role(s)
def role?(role)
!(roles.map(&:name) & Array(role)).empty?
end
end
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/policies/user_spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ roles:
- PERMISSION_ONE
ROLE_TWO:
- PERMISSION_TWO
ROLE_THREE:
- PERMISSION_THREE
33 changes: 33 additions & 0 deletions spec/ruolo/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,37 @@
expect(user.permission?('PERMISSION_TWO')).to eq(false)
end
end

describe '#role?' do
let(:user) do
u = klass.create(email: Faker::Internet.unique.safe_email, password: 'password', first_name: 'fn', last_name: 'ln')
u.add_role(Ruolo::Models::Role.where(name: 'ROLE_ONE').first)
u.add_role(Ruolo::Models::Role.where(name: 'ROLE_TWO').first)
u
end

context 'with a string' do
it 'returns true if the user has the role' do
expect(user.role?('ROLE_ONE')).to eq(true)
end

it 'returns false if the user doesn\'t have the role' do
expect(user.role?('ROLE_THREE')).to eq(false)
end
end

context 'with an array' do
it 'returns true if the user has any of the roles' do
expect(user.role?(%w[ROLE_ONE])).to eq(true)
end

it 'returns false if the user doesn\'t have any of the roles' do
expect(user.role?(%w[ROLE_THREE])).to eq(false)
end

it 'returns true if the user has all of the roles' do
expect(user.role?(%w[ROLE_ONE ROLE_TWO])).to eq(true)
end
end
end
end

0 comments on commit 6798a11

Please sign in to comment.