FIX: grant trust level when bulk adding users to group
This commit is contained in:
parent
7496f373cd
commit
d5bcc70e9c
|
@ -0,0 +1,17 @@
|
|||
module Jobs
|
||||
|
||||
class BulkGrantTrustLevel < Jobs::Base
|
||||
|
||||
def execute(args)
|
||||
trust_level = args[:trust_level]
|
||||
user_ids = args[:user_ids]
|
||||
|
||||
raise Discourse::InvalidParameters.new(:trust_level) if trust_level.blank?
|
||||
raise Discourse::InvalidParameters.new(:user_ids) if user_ids.blank?
|
||||
|
||||
User.where(id: user_ids).find_each do |user|
|
||||
TrustLevelGranter.grant(trust_level, user)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -414,6 +414,10 @@ class Group < ActiveRecord::Base
|
|||
if self.title.present?
|
||||
User.where(id: user_ids).update_all(title: self.title)
|
||||
end
|
||||
|
||||
if self.grant_trust_level.present?
|
||||
Jobs.enqueue(:bulk_grant_trust_level, user_ids: user_ids, trust_level: self.grant_trust_level)
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
|
|
|
@ -55,11 +55,7 @@ class GroupUser < ActiveRecord::Base
|
|||
|
||||
def grant_trust_level
|
||||
return if group.grant_trust_level.nil?
|
||||
if user.trust_level < group.grant_trust_level
|
||||
user.change_trust_level!(group.grant_trust_level)
|
||||
user.trust_level_locked = true
|
||||
user.save
|
||||
end
|
||||
TrustLevelGranter.grant(group.grant_trust_level, user)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
class TrustLevelGranter
|
||||
|
||||
def initialize(trust_level, user)
|
||||
@trust_level, @user = trust_level, user
|
||||
end
|
||||
|
||||
def self.grant(trust_level, user)
|
||||
TrustLevelGranter.new(trust_level, user).grant
|
||||
end
|
||||
|
||||
def grant
|
||||
if @user.trust_level < @trust_level
|
||||
@user.change_trust_level!(@trust_level)
|
||||
@user.trust_level_locked = true
|
||||
@user.save!
|
||||
end
|
||||
end
|
||||
end
|
|
@ -51,9 +51,9 @@ describe Admin::GroupsController do
|
|||
|
||||
context ".bulk" do
|
||||
it "can assign users to a group by email or username" do
|
||||
group = Fabricate(:group, name: "test", primary_group: true, title: 'WAT')
|
||||
user = Fabricate(:user)
|
||||
user2 = Fabricate(:user)
|
||||
group = Fabricate(:group, name: "test", primary_group: true, title: 'WAT', grant_trust_level: 3)
|
||||
user = Fabricate(:user, trust_level: 2)
|
||||
user2 = Fabricate(:user, trust_level: 4)
|
||||
|
||||
xhr :put, :bulk_perform, group_id: group.id, users: [user.username.upcase, user2.email, 'doesnt_exist']
|
||||
|
||||
|
@ -62,10 +62,12 @@ describe Admin::GroupsController do
|
|||
user.reload
|
||||
expect(user.primary_group).to eq(group)
|
||||
expect(user.title).to eq("WAT")
|
||||
expect(user.trust_level).to eq(3)
|
||||
|
||||
user2.reload
|
||||
expect(user2.primary_group).to eq(group)
|
||||
|
||||
expect(user2.title).to eq("WAT")
|
||||
expect(user2.trust_level).to eq(4)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
require 'rails_helper'
|
||||
require_dependency 'jobs/regular/bulk_grant_trust_level'
|
||||
|
||||
describe Jobs::BulkGrantTrustLevel do
|
||||
|
||||
it "raises an error when trust_level is missing" do
|
||||
expect { Jobs::BulkGrantTrustLevel.new.execute(user_ids: [1,2]) }.to raise_error(Discourse::InvalidParameters)
|
||||
end
|
||||
|
||||
it "raises an error when user_ids are missing" do
|
||||
expect { Jobs::BulkGrantTrustLevel.new.execute(trust_level: 0) }.to raise_error(Discourse::InvalidParameters)
|
||||
end
|
||||
|
||||
it "updates the trust_level" do
|
||||
user1 = Fabricate(:user, email: "foo@wat.com", trust_level: 0)
|
||||
user2 = Fabricate(:user, email: "foo@bar.com", trust_level: 2)
|
||||
|
||||
Jobs::BulkGrantTrustLevel.new.execute(trust_level: 3, user_ids: [user1.id,user2.id])
|
||||
|
||||
user1.reload
|
||||
user2.reload
|
||||
expect(user1.trust_level).to eq(3)
|
||||
expect(user2.trust_level).to eq(3)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe TrustLevelGranter do
|
||||
|
||||
describe 'grant' do
|
||||
|
||||
it 'grants trust level' do
|
||||
user = Fabricate(:user, email: "foo@bar.com", trust_level: 0)
|
||||
TrustLevelGranter.grant(3, user)
|
||||
|
||||
user.reload
|
||||
expect(user.trust_level).to eq(3)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue