Merge pull request #980 from jd-erreape/add_roleable_to_user
Added roleable module for User model
This commit is contained in:
commit
f23807af8f
|
@ -6,8 +6,11 @@ require_dependency 'summarize'
|
||||||
require_dependency 'discourse'
|
require_dependency 'discourse'
|
||||||
require_dependency 'post_destroyer'
|
require_dependency 'post_destroyer'
|
||||||
require_dependency 'user_name_suggester'
|
require_dependency 'user_name_suggester'
|
||||||
|
require_dependency 'roleable'
|
||||||
|
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
|
include Roleable
|
||||||
|
|
||||||
has_many :posts
|
has_many :posts
|
||||||
has_many :notifications
|
has_many :notifications
|
||||||
has_many :topic_users
|
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)
|
where("username_lower = :user or lower(username) = :user or email = :email or lower(name) = :user", user: lower_user, email: lower_email)
|
||||||
end
|
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)
|
def enqueue_welcome_message(message_type)
|
||||||
return unless SiteSetting.send_welcome_message?
|
return unless SiteSetting.send_welcome_message?
|
||||||
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
|
Jobs.enqueue(:send_system_message, user_id: id, message_type: message_type)
|
||||||
|
@ -231,7 +206,6 @@ class User < ActiveRecord::Base
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def unread_private_messages
|
def unread_private_messages
|
||||||
@unread_pms ||= notifications.where("read = false AND notification_type = ?", Notification.types[:private_message]).count
|
@unread_pms ||= notifications.where("read = false AND notification_type = ?", Notification.types[:private_message]).count
|
||||||
end
|
end
|
||||||
|
@ -258,15 +232,6 @@ class User < ActiveRecord::Base
|
||||||
User.select(:username).order('last_posted_at desc').limit(20)
|
User.select(:username).order('last_posted_at desc').limit(20)
|
||||||
end
|
end
|
||||||
|
|
||||||
# any user that is either a moderator or an admin
|
|
||||||
def staff?
|
|
||||||
admin || moderator
|
|
||||||
end
|
|
||||||
|
|
||||||
def regular?
|
|
||||||
!staff?
|
|
||||||
end
|
|
||||||
|
|
||||||
def password=(password)
|
def password=(password)
|
||||||
# special case for passwordless accounts
|
# special case for passwordless accounts
|
||||||
@raw_password = password unless password.blank?
|
@raw_password = password unless password.blank?
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue