category export: include category permissions and custom groups
This commit is contained in:
parent
106e3c897f
commit
3053c1ebd8
|
@ -8,6 +8,7 @@ module ImportExport
|
|||
@subcategories = Category.where(parent_category_id: category_id)
|
||||
@export_data = {
|
||||
users: [],
|
||||
groups: [],
|
||||
category: nil,
|
||||
subcategories: [],
|
||||
topics: []
|
||||
|
@ -24,14 +25,43 @@ module ImportExport
|
|||
|
||||
CATEGORY_ATTRS = [:id, :name, :color, :created_at, :user_id, :slug, :description, :text_color,
|
||||
:auto_close_hours, :logo_url, :background_url, :auto_close_based_on_last_post,
|
||||
:topic_template, :suppress_from_homepage]
|
||||
:topic_template, :suppress_from_homepage, :permissions_params]
|
||||
|
||||
def export_categories
|
||||
# description
|
||||
@export_data[:category] = CATEGORY_ATTRS.inject({}) { |h,a| h[a] = @category.send(a); h }
|
||||
@subcategories.find_each do |subcat|
|
||||
@export_data[:subcategories] << CATEGORY_ATTRS.inject({}) { |h,a| h[a] = subcat.send(a); h }
|
||||
end
|
||||
|
||||
# export groups that are mentioned in category permissions
|
||||
group_names = []
|
||||
auto_group_names = Group::AUTO_GROUPS.keys
|
||||
|
||||
([@export_data[:category]] + @export_data[:subcategories]).each do |c|
|
||||
c[:permissions_params].each do |group_name, _|
|
||||
group_names << group_name unless auto_group_names.include?(group_name)
|
||||
end
|
||||
end
|
||||
|
||||
group_names.uniq!
|
||||
export_groups(group_names) unless group_names.empty?
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
GROUP_ATTRS = [ :id, :name, :created_at, :alias_level, :visible,
|
||||
:automatic_membership_email_domains, :automatic_membership_retroactive,
|
||||
:primary_group, :title, :grant_trust_level, :incoming_email]
|
||||
|
||||
def export_groups(group_names)
|
||||
group_names.each do |name|
|
||||
group = Group.find_by_name(name)
|
||||
group_attrs = GROUP_ATTRS.inject({}) { |h,a| h[a] = group.send(a); h }
|
||||
group_attrs[:user_ids] = group.users.pluck(:id)
|
||||
@export_data[:groups] << group_attrs
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ module ImportExport
|
|||
RateLimiter.disable
|
||||
|
||||
import_users
|
||||
import_groups
|
||||
import_categories
|
||||
import_topics
|
||||
self
|
||||
|
@ -18,24 +19,43 @@ module ImportExport
|
|||
RateLimiter.enable
|
||||
end
|
||||
|
||||
def import_groups
|
||||
return if @export_data[:groups].empty?
|
||||
|
||||
@export_data[:groups].each do |group_data|
|
||||
g = group_data.dup
|
||||
user_ids = g.delete(:user_ids)
|
||||
external_id = g.delete(:id)
|
||||
new_group = Group.find_by_name(g[:name]) || Group.create!(g)
|
||||
user_ids.each do |external_user_id|
|
||||
new_group.add( User.find(@topic_importer.new_user_id(external_user_id)) )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def import_users
|
||||
@topic_importer.import_users
|
||||
end
|
||||
|
||||
def import_categories
|
||||
id = @export_data[:category].delete(:id)
|
||||
permissions = @export_data[:category].delete(:permissions_params)
|
||||
|
||||
parent = Category.new(@export_data[:category])
|
||||
parent.user_id = @topic_importer.new_user_id(@export_data[:category][:user_id]) # imported user's new id
|
||||
parent.custom_fields["import_id"] = id
|
||||
parent.permissions = permissions if permissions
|
||||
parent.save!
|
||||
set_category_description(parent, @export_data[:category][:description])
|
||||
|
||||
@export_data[:subcategories].each do |cat_attrs|
|
||||
id = cat_attrs.delete(:id)
|
||||
permissions = cat_attrs.delete(:permissions_params)
|
||||
subcategory = Category.new(cat_attrs)
|
||||
subcategory.parent_category_id = parent.id
|
||||
subcategory.user_id = @topic_importer.new_user_id(cat_attrs[:user_id])
|
||||
subcategory.custom_fields["import_id"] = id
|
||||
subcategory.permissions = permissions if permissions
|
||||
subcategory.save!
|
||||
set_category_description(subcategory, cat_attrs[:description])
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue