Allow admins to choose if groups are visible or not.

This commit is contained in:
Robin Ward 2014-04-22 16:43:46 -04:00
parent 8538e31fb4
commit af877781b7
11 changed files with 43 additions and 19 deletions

View File

@ -20,7 +20,7 @@ Discourse.AdminGroupsController = Ember.Controller.extend({
}, },
newGroup: function(){ newGroup: function(){
var group = Discourse.Group.create({ loadedUsers: true }), var group = Discourse.Group.create({ loadedUsers: true, visible: true }),
model = this.get("model"); model = this.get("model");
model.addObject(group); model.addObject(group);
model.select(group); model.select(group);

View File

@ -30,6 +30,11 @@
{{userSelector usernames=usernames id="group-users" placeholderKey="admin.groups.selector_placeholder" tabindex="1" disabledBinding="automatic"}} {{userSelector usernames=usernames id="group-users" placeholderKey="admin.groups.selector_placeholder" tabindex="1" disabledBinding="automatic"}}
</div> </div>
</div> </div>
<div class="control-group">
<div class="controls">
{{input type="checkbox" checked=visible}} {{i18n groups.visible}}
</div>
</div>
<div class="control-group"> <div class="control-group">
<label class="control-label">{{i18n groups.alias_levels.title}}</label> <label class="control-label">{{i18n groups.alias_levels.title}}</label>
<div class="controls"> <div class="controls">

View File

@ -83,17 +83,19 @@ Discourse.Group = Discourse.Model.extend({
}); });
}, },
asJSON: function() {
return { group: {
name: this.get('name'),
alias_level: this.get('alias_level'),
visible: !!this.get('visible'),
usernames: this.get('usernames') } };
},
create: function(){ create: function(){
var self = this; var self = this;
self.set('disableSave', true); self.set('disableSave', true);
return Discourse.ajax("/admin/groups", {type: "POST", data: { return Discourse.ajax("/admin/groups", {type: "POST", data: this.asJSON()}).then(function(resp) {
group: {
name: this.get('name'),
alias_level: this.get('alias_level'),
usernames: this.get('usernames')
}
}}).then(function(resp) {
self.set('disableSave', false); self.set('disableSave', false);
self.set('id', resp.id); self.set('id', resp.id);
}, function (error) { }, function (error) {
@ -113,13 +115,7 @@ Discourse.Group = Discourse.Model.extend({
return Discourse.ajax("/admin/groups/" + this.get('id'), { return Discourse.ajax("/admin/groups/" + this.get('id'), {
type: "PUT", type: "PUT",
data: { data: this.asJSON()
group: {
name: this.get('name'),
alias_level: this.get('alias_level'),
usernames: this.get('usernames')
}
}
}).then(function(){ }).then(function(){
self.set('disableSave', false); self.set('disableSave', false);
}, function(e){ }, function(e){

View File

@ -20,6 +20,7 @@ class Admin::GroupsController < Admin::AdminController
group.alias_level = params[:group][:alias_level] group.alias_level = params[:group][:alias_level]
group.name = params[:group][:name] if params[:group][:name] group.name = params[:group][:name] if params[:group][:name]
end end
group.visible = params[:group][:visible] == "true"
if group.save if group.save
render json: success_json render json: success_json
@ -32,6 +33,7 @@ class Admin::GroupsController < Admin::AdminController
group = Group.new group = Group.new
group.name = params[:group][:name].strip group.name = params[:group][:name].strip
group.usernames = params[:group][:usernames] if params[:group][:usernames] group.usernames = params[:group][:usernames] if params[:group][:usernames]
group.visible = params[:group][:visible] == "true"
if group.save if group.save
render_serialized(group, BasicGroupSerializer) render_serialized(group, BasicGroupSerializer)
else else

View File

@ -109,7 +109,7 @@ class User < ActiveRecord::Base
end end
def custom_groups def custom_groups
groups.where(automatic: false) groups.where(automatic: false, visible: true)
end end
def self.username_available?(username) def self.username_available?(username)

View File

@ -1,3 +1,3 @@
class BasicGroupSerializer < ApplicationSerializer class BasicGroupSerializer < ApplicationSerializer
attributes :id, :automatic, :name, :user_count, :alias_level attributes :id, :automatic, :name, :user_count, :alias_level, :visible
end end

View File

@ -177,6 +177,7 @@ en:
sent_by_you: "Sent by <a href='{{userUrl}}'>you</a>" sent_by_you: "Sent by <a href='{{userUrl}}'>you</a>"
groups: groups:
visible: "Group is visible to all users"
title: title:
one: "group" one: "group"
other: "groups" other: "groups"

View File

@ -0,0 +1,5 @@
class AddVisibileToGroups < ActiveRecord::Migration
def change
add_column :groups, :visible, :boolean, default: true, null: false
end
end

View File

@ -109,6 +109,10 @@ class Guardian
alias :can_send_activation_email? :can_moderate? alias :can_send_activation_email? :can_moderate?
alias :can_grant_badges? :can_moderate? alias :can_grant_badges? :can_moderate?
def can_see_group?(group)
group.present? && group.visible?
end
# Can we impersonate this user? # Can we impersonate this user?

View File

@ -238,6 +238,16 @@ describe Guardian do
Guardian.new.can_see?(nil).should be_false Guardian.new.can_see?(nil).should be_false
end end
describe 'a Group' do
it "returns true when the group is visible" do
Guardian.new.can_see?(Group.new).should be_true
end
it "returns false when the group is invisible" do
Guardian.new.can_see?(Group.new(visible: false)).should be_false
end
end
describe 'a Topic' do describe 'a Topic' do
it 'allows non logged in users to view topics' do it 'allows non logged in users to view topics' do
Guardian.new.can_see?(topic).should be_true Guardian.new.can_see?(topic).should be_true

View File

@ -22,7 +22,8 @@ describe Admin::GroupsController do
"name"=>group.name, "name"=>group.name,
"user_count"=>1, "user_count"=>1,
"automatic"=>false, "automatic"=>false,
"alias_level"=>0 "alias_level"=>0,
"visible"=>true
}] }]
end end