diff --git a/app/jobs/regular/bulk_grant_trust_level.rb b/app/jobs/regular/bulk_grant_trust_level.rb new file mode 100644 index 00000000000..9302dc6bc1f --- /dev/null +++ b/app/jobs/regular/bulk_grant_trust_level.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index 8e457c72de7..56236717738 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/models/group_user.rb b/app/models/group_user.rb index df666a0278f..8d1e3beba59 100644 --- a/app/models/group_user.rb +++ b/app/models/group_user.rb @@ -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 diff --git a/app/services/trust_level_granter.rb b/app/services/trust_level_granter.rb new file mode 100644 index 00000000000..1a826ba18d3 --- /dev/null +++ b/app/services/trust_level_granter.rb @@ -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 diff --git a/spec/controllers/admin/groups_controller_spec.rb b/spec/controllers/admin/groups_controller_spec.rb index 732d07c3ad2..64fd305e0ef 100644 --- a/spec/controllers/admin/groups_controller_spec.rb +++ b/spec/controllers/admin/groups_controller_spec.rb @@ -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 diff --git a/spec/jobs/bulk_grant_trust_level_spec.rb b/spec/jobs/bulk_grant_trust_level_spec.rb new file mode 100644 index 00000000000..0c8112d3863 --- /dev/null +++ b/spec/jobs/bulk_grant_trust_level_spec.rb @@ -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 diff --git a/spec/services/trust_level_granter_spec.rb b/spec/services/trust_level_granter_spec.rb new file mode 100644 index 00000000000..b85222c7bea --- /dev/null +++ b/spec/services/trust_level_granter_spec.rb @@ -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