Merge pull request #980 from jd-erreape/add_roleable_to_user

Added roleable module for User model
This commit is contained in:
Robin Ward 2013-06-07 07:24:08 -07:00
commit f23807af8f
2 changed files with 46 additions and 38 deletions

View File

@ -6,8 +6,11 @@ require_dependency 'summarize'
require_dependency 'discourse'
require_dependency 'post_destroyer'
require_dependency 'user_name_suggester'
require_dependency 'roleable'
class User < ActiveRecord::Base
include Roleable
has_many :posts
has_many :notifications
has_many :topic_users
@ -136,34 +139,6 @@ class User < ActiveRecord::Base
where("username_lower = :user or lower(username) = :user or email = :email or lower(name) = :user", user: lower_user, email: lower_email)
end
def save_and_refresh_staff_groups!
transaction do
self.save!
Group.refresh_automatic_groups!(:admins, :moderators, :staff)
end
end
def grant_moderation!
self.moderator = true
save_and_refresh_staff_groups!
end
def revoke_moderation!
self.moderator = false
save_and_refresh_staff_groups!
end
def grant_admin!
self.admin = true
save_and_refresh_staff_groups!
end
def revoke_admin!
self.admin = false
save_and_refresh_staff_groups!
end
def enqueue_welcome_message(message_type)
return unless SiteSetting.send_welcome_message?
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
@ -231,7 +206,6 @@ class User < ActiveRecord::Base
super
end
def unread_private_messages
@unread_pms ||= notifications.where("read = false AND notification_type = ?", Notification.types[:private_message]).count
end
@ -258,15 +232,6 @@ class User < ActiveRecord::Base
User.select(:username).order('last_posted_at desc').limit(20)
end
# any user that is either a moderator or an admin
def staff?
admin || moderator
end
def regular?
!staff?
end
def password=(password)
# special case for passwordless accounts
@raw_password = password unless password.blank?

43
lib/roleable.rb Normal file
View File

@ -0,0 +1,43 @@
require 'active_support/concern'
module Roleable
extend ActiveSupport::Concern
# any user that is either a moderator or an admin
def staff?
admin || moderator
end
def regular?
!staff?
end
def grant_moderation!
set_permission('moderator', true)
end
def revoke_moderation!
set_permission('moderator', false)
end
def grant_admin!
set_permission('admin', true)
end
def revoke_admin!
set_permission('admin', false)
end
def save_and_refresh_staff_groups!
transaction do
self.save!
Group.refresh_automatic_groups!(:admins, :moderators, :staff)
end
end
def set_permission(permission_name, value)
self.send("#{permission_name}=", value)
save_and_refresh_staff_groups!
end
end