Skip to content

Commit

Permalink
Add methods to User model for verifying user roles (#2448)
Browse files Browse the repository at this point in the history
* Add methods to User model for user roles

* Add a comment
  • Loading branch information
seafr authored and jywarren committed Mar 10, 2018
1 parent 07221c7 commit ca820a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ def has_role(r)
role == r
end

def admin?
role == 'admin'
end

def moderator?
role == 'moderator'
end

def can_moderate?
# use instead of "user.role == 'admin' || user.role == 'moderator'"
admin? || moderator?
end

def tags(limit = 10)
Tag.where('name in (?)', tagnames).limit(limit)
end
Expand Down Expand Up @@ -356,4 +369,4 @@ def self.contributor_count_for(start_time,end_time)
contributors
end

end
end
15 changes: 15 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,19 @@ class UserTest < ActiveSupport::TestCase
assert_equal 1, drupal_user.user.status
end

test 'user roles' do
admin = users(:admin)
assert admin.admin?
assert admin.can_moderate?

moderator = users(:moderator)
assert moderator.moderator?
assert moderator.can_moderate?

basic_user = users(:newcomer)
assert_not basic_user.admin?
assert_not basic_user.moderator?
assert_not basic_user.can_moderate?
end

end

0 comments on commit ca820a8

Please sign in to comment.