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(){
var group = Discourse.Group.create({ loadedUsers: true }),
var group = Discourse.Group.create({ loadedUsers: true, visible: true }),
model = this.get("model");
model.addObject(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"}}
</div>
</div>
<div class="control-group">
<div class="controls">
{{input type="checkbox" checked=visible}} {{i18n groups.visible}}
</div>
</div>
<div class="control-group">
<label class="control-label">{{i18n groups.alias_levels.title}}</label>
<div class="controls">
@ -39,7 +44,7 @@
<div class='controls'>
<button {{action save this}} {{bind-attr disabled="disableSave"}} class='btn'>{{i18n admin.customize.save}}</button>
{{#unless automatic}}
<a {{action destroy this}} class='delete-link'>{{i18n admin.customize.delete}}</a>
<a {{action destroy this}} class='delete-link'>{{i18n admin.customize.delete}}</a>
{{/unless}}
</div>
{{/with}}

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

View File

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

View File

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

View File

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

View File

@ -177,6 +177,7 @@ en:
sent_by_you: "Sent by <a href='{{userUrl}}'>you</a>"
groups:
visible: "Group is visible to all users"
title:
one: "group"
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_grant_badges? :can_moderate?
def can_see_group?(group)
group.present? && group.visible?
end
# Can we impersonate this user?

View File

@ -238,6 +238,16 @@ describe Guardian do
Guardian.new.can_see?(nil).should be_false
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
it 'allows non logged in users to view topics' do
Guardian.new.can_see?(topic).should be_true

View File

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