mirror of
https://github.com/codeninjasllc/discourse.git
synced 2025-03-24 05:42:03 -04:00
FIX: Don't allow parent categories to be deleted. Also, remove
duplicated logic and rely on the server response for `can_delete` status.
This commit is contained in:
parent
a963dd9081
commit
f73a3f252a
6 changed files with 29 additions and 7 deletions
|
@ -58,10 +58,6 @@ Discourse.EditCategoryController = Discourse.ObjectController.extend(Discourse.M
|
||||||
return false;
|
return false;
|
||||||
}.property('saving', 'name', 'color', 'deleting'),
|
}.property('saving', 'name', 'color', 'deleting'),
|
||||||
|
|
||||||
deleteVisible: function() {
|
|
||||||
return (this.get('id') && this.get('topic_count') === 0 && !this.get("isUncategorizedCategory"));
|
|
||||||
}.property('id', 'topic_count'),
|
|
||||||
|
|
||||||
deleteDisabled: function() {
|
deleteDisabled: function() {
|
||||||
return (this.get('deleting') || this.get('saving') || false);
|
return (this.get('deleting') || this.get('saving') || false);
|
||||||
}.property('disabled', 'saving', 'deleting'),
|
}.property('disabled', 'saving', 'deleting'),
|
||||||
|
|
|
@ -119,7 +119,7 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
<button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action saveCategory}}>{{buttonTitle}}</button>
|
<button class='btn btn-primary' {{bind-attr disabled="disabled"}} {{action saveCategory}}>{{buttonTitle}}</button>
|
||||||
{{#if deleteVisible}}
|
{{#if can_delete}}
|
||||||
<button class='btn btn-danger pull-right' {{bind-attr disabled="deleteDisabled"}} {{action deleteCategory}}><i class="fa fa-trash-o"></i>{{deleteButtonTitle}}</button>
|
<button class='btn btn-danger pull-right' {{bind-attr disabled="deleteDisabled"}} {{action deleteCategory}}><i class="fa fa-trash-o"></i>{{deleteButtonTitle}}</button>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -350,6 +350,10 @@ SQL
|
||||||
self.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
self.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def has_children?
|
||||||
|
id && Category.where(parent_category_id: id).exists?
|
||||||
|
end
|
||||||
|
|
||||||
def uncategorized?
|
def uncategorized?
|
||||||
id == SiteSetting.uncategorized_category_id
|
id == SiteSetting.uncategorized_category_id
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
class CategorySerializer < BasicCategorySerializer
|
class CategorySerializer < BasicCategorySerializer
|
||||||
|
|
||||||
attributes :read_restricted, :available_groups, :auto_close_hours, :group_permissions, :position
|
attributes :read_restricted,
|
||||||
|
:available_groups,
|
||||||
|
:auto_close_hours,
|
||||||
|
:group_permissions,
|
||||||
|
:position,
|
||||||
|
:can_delete
|
||||||
|
|
||||||
def group_permissions
|
def group_permissions
|
||||||
@group_permissions ||= begin
|
@group_permissions ||= begin
|
||||||
|
@ -21,4 +26,13 @@ class CategorySerializer < BasicCategorySerializer
|
||||||
Group.order(:name).pluck(:name) - group_permissions.map{|g| g[:group_name]}
|
Group.order(:name).pluck(:name) - group_permissions.map{|g| g[:group_name]}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
def can_delete
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def include_can_delete?
|
||||||
|
scope && scope.can_delete?(object)
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,10 @@ module CategoryGuardian
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_delete_category?(category)
|
def can_delete_category?(category)
|
||||||
is_admin? && category.topic_count == 0 && !category.uncategorized?
|
is_admin? &&
|
||||||
|
category.topic_count == 0 &&
|
||||||
|
!category.uncategorized? &&
|
||||||
|
!category.has_children?
|
||||||
end
|
end
|
||||||
|
|
||||||
def can_see_category?(category)
|
def can_see_category?(category)
|
||||||
|
|
|
@ -882,6 +882,11 @@ describe Guardian do
|
||||||
Guardian.new(admin).can_delete?(uncategorized_category).should be_false
|
Guardian.new(admin).can_delete?(uncategorized_category).should be_false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can't be deleted if it has children" do
|
||||||
|
category.expects(:has_children?).returns(true)
|
||||||
|
Guardian.new(admin).can_delete?(category).should be_false
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'can_suspend?' do
|
context 'can_suspend?' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue