From 1bd0f5b015dca764d2f6d91aa62f6a269ee96003 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 1 Sep 2015 16:52:05 -0400 Subject: [PATCH] FEATURE: group can grant a trust level when a user is added --- .../javascripts/admin/controllers/admin-group.js.es6 | 7 +++++++ app/assets/javascripts/admin/templates/group.hbs | 5 +++++ app/assets/javascripts/discourse/models/group.js.es6 | 3 ++- app/controllers/admin/groups_controller.rb | 2 ++ app/models/group.rb | 1 + app/models/group_user.rb | 11 +++++++++++ app/serializers/basic_group_serializer.rb | 3 ++- config/locales/client.en.yml | 3 +++ ...20150901192313_add_grant_trust_level_to_groups.rb | 5 +++++ spec/controllers/admin/groups_controller_spec.rb | 3 ++- spec/models/group_spec.rb | 12 ++++++++++++ 11 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20150901192313_add_grant_trust_level_to_groups.rb diff --git a/app/assets/javascripts/admin/controllers/admin-group.js.es6 b/app/assets/javascripts/admin/controllers/admin-group.js.es6 index dee7c45bb84..f16151ed29e 100644 --- a/app/assets/javascripts/admin/controllers/admin-group.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-group.js.es6 @@ -27,6 +27,13 @@ export default Ember.Controller.extend({ ]; }.property(), + trustLevelOptions: function() { + return [ + { name: I18n.t("groups.trust_levels.none"), value: 0 }, + { name: 1, value: 1 }, { name: 2, value: 2 }, { name: 3, value: 3 }, { name: 4, value: 4 } + ]; + }.property(), + actions: { next() { if (this.get("showingLast")) { return; } diff --git a/app/assets/javascripts/admin/templates/group.hbs b/app/assets/javascripts/admin/templates/group.hbs index 6260d12aab3..3da80d9af7d 100644 --- a/app/assets/javascripts/admin/templates/group.hbs +++ b/app/assets/javascripts/admin/templates/group.hbs @@ -70,6 +70,11 @@ {{input value=model.title}} + +
+ + {{combo-box name="grant_trust_level" valueAttribute="value" value=model.grant_trust_level content=trustLevelOptions}} +
{{/unless}}
diff --git a/app/assets/javascripts/discourse/models/group.js.es6 b/app/assets/javascripts/discourse/models/group.js.es6 index b79db164f48..f90aee368dd 100644 --- a/app/assets/javascripts/discourse/models/group.js.es6 +++ b/app/assets/javascripts/discourse/models/group.js.es6 @@ -62,7 +62,8 @@ const Group = Discourse.Model.extend({ automatic_membership_email_domains: this.get('emailDomains'), automatic_membership_retroactive: !!this.get('automatic_membership_retroactive'), title: this.get('title'), - primary_group: !!this.get('primary_group') + primary_group: !!this.get('primary_group'), + grant_trust_level: this.get('grant_trust_level') }; }, diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb index 42ed206bfdb..35f3295d273 100644 --- a/app/controllers/admin/groups_controller.rb +++ b/app/controllers/admin/groups_controller.rb @@ -37,6 +37,8 @@ class Admin::GroupsController < Admin::AdminController def save_group(group) group.alias_level = params[:alias_level].to_i if params[:alias_level].present? group.visible = params[:visible] == "true" + grant_trust_level = params[:grant_trust_level].to_i + group.grant_trust_level = (grant_trust_level > 0 && grant_trust_level <= 4) ? grant_trust_level : nil group.automatic_membership_email_domains = params[:automatic_membership_email_domains] unless group.automatic group.automatic_membership_retroactive = params[:automatic_membership_retroactive] == "true" unless group.automatic diff --git a/app/models/group.rb b/app/models/group.rb index b36f5cee20d..bdd3ca82e31 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -372,6 +372,7 @@ end # visible :boolean default(TRUE), not null # automatic_membership_email_domains :text # automatic_membership_retroactive :boolean default(FALSE) +# grant_trust_level :integer # # Indexes # diff --git a/app/models/group_user.rb b/app/models/group_user.rb index 38351f52b1e..5febb87f3b7 100644 --- a/app/models/group_user.rb +++ b/app/models/group_user.rb @@ -8,6 +8,8 @@ class GroupUser < ActiveRecord::Base after_save :set_primary_group after_destroy :remove_primary_group + after_save :grant_trust_level + protected def set_primary_group @@ -44,6 +46,15 @@ class GroupUser < ActiveRecord::Base title: group.title) end end + + 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 + end end # == Schema Information diff --git a/app/serializers/basic_group_serializer.rb b/app/serializers/basic_group_serializer.rb index 9f6ad6b7a2f..38644aff4da 100644 --- a/app/serializers/basic_group_serializer.rb +++ b/app/serializers/basic_group_serializer.rb @@ -8,5 +8,6 @@ class BasicGroupSerializer < ApplicationSerializer :automatic_membership_email_domains, :automatic_membership_retroactive, :primary_group, - :title + :title, + :grant_trust_level end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index a8958b9035a..7fe190f1573 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -343,6 +343,9 @@ en: mods_and_admins: "Only moderators and Admins" members_mods_and_admins: "Only group members, moderators and admins" everyone: "Everyone" + trust_levels: + title: "Trust level automatically granted to members when they're added:" + none: "None" user_action_groups: "1": "Likes Given" diff --git a/db/migrate/20150901192313_add_grant_trust_level_to_groups.rb b/db/migrate/20150901192313_add_grant_trust_level_to_groups.rb new file mode 100644 index 00000000000..a654369a2e2 --- /dev/null +++ b/db/migrate/20150901192313_add_grant_trust_level_to_groups.rb @@ -0,0 +1,5 @@ +class AddGrantTrustLevelToGroups < ActiveRecord::Migration + def change + add_column :groups, :grant_trust_level, :integer + end +end diff --git a/spec/controllers/admin/groups_controller_spec.rb b/spec/controllers/admin/groups_controller_spec.rb index b7710e7007f..75bc9ddac12 100644 --- a/spec/controllers/admin/groups_controller_spec.rb +++ b/spec/controllers/admin/groups_controller_spec.rb @@ -29,7 +29,8 @@ describe Admin::GroupsController do "automatic_membership_email_domains"=>nil, "automatic_membership_retroactive"=>false, "title"=>nil, - "primary_group"=>false + "primary_group"=>false, + "grant_trust_level"=>nil }]) end diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index c1b42d643c5..b1d484d9633 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -311,4 +311,16 @@ describe Group do end end + it "correctly grants a trust level to members" do + group = Fabricate(:group, grant_trust_level: 2) + u0 = Fabricate(:user, trust_level: 0) + u3 = Fabricate(:user, trust_level: 3) + + group.add(u0) + expect(u0.reload.trust_level).to eq(2) + + group.add(u3) + expect(u3.reload.trust_level).to eq(3) + end + end