Migrate form old ui to new ui (categories, tags and authorizations)

This commit is contained in:
chris 2013-10-15 23:38:16 +08:00
parent 11b880752d
commit 279ac774d4
9 changed files with 196 additions and 131 deletions

View File

@ -34,38 +34,30 @@ class Admin::AuthorizationsController < OrbitBackendController
end end
def add_roles def add_roles
roles = Role.find(params[:role_ids]) rescue [] roles = Role.find(params[:role_ids]) rescue nil
users = [] unless roles.nil?
roles.each do |role|
users += role.users.all.entries
end
unless roles.blank?
authorization = get_or_create_authorization authorization = get_or_create_authorization
add_roles_to_auth(authorization, roles) unless @error add_roles_to_auth(authorization, roles)
end end
@users = authorization.authorized_users @users = authorization.authorized_users
render 'admin/authorizations/reload_users' render 'admin/authorizations/reload_users'
end end
def add_sub_roles def add_sub_roles
sub_roles = SubRole.find(params[:sub_role_ids]) rescue [] sub_roles = SubRole.find(params[:sub_role_ids]) rescue nil
users = [] unless sub_roles.nil?
sub_roles.each do |sub_role|
users += sub_role.users.all.entries
end
unless sub_roles.blank?
authorization = get_or_create_authorization authorization = get_or_create_authorization
add_sub_roles_to_auth(authorization, sub_roles) unless @error add_sub_roles_to_auth(authorization, sub_roles)
end end
@users = authorization.authorized_users @users = authorization.authorized_users
render 'admin/authorizations/reload_users' render 'admin/authorizations/reload_users'
end end
def add_users def add_users
users = User.find(params[:user_ids]) rescue [] users = User.find(params[:user_ids]) rescue nil
unless users.blank? unless users.nil?
authorization = get_or_create_authorization authorization = get_or_create_authorization
add_users_to_auth(authorization, users) unless @error add_users_to_auth(authorization, users)
end end
@users = authorization.authorized_users @users = authorization.authorized_users
render 'admin/authorizations/reload_users' render 'admin/authorizations/reload_users'
@ -120,10 +112,6 @@ class Admin::AuthorizationsController < OrbitBackendController
protected protected
def add_managers_to_auth(users)
@module_app.add_managers(users)
end
def add_roles_to_auth(authorization, roles) def add_roles_to_auth(authorization, roles)
authorization.add_roles(roles) authorization.add_roles(roles)
end end
@ -151,7 +139,7 @@ class Admin::AuthorizationsController < OrbitBackendController
@error = t(:no_data) @error = t(:no_data)
end end
when nil when nil
@module_app.auth_manager || @module_app.create_auth_manager(module_app_id: @module_app.id, title: "#{@type}_#{@module_app.key}") @module_app.auth_manager || @module_app.create_auth_manager(module_app_id: @module_app.id, title: @module_app.key)
else else
auth = @object.get_authorization_by_title("#{@type}_#{@module_app.key}") auth = @object.get_authorization_by_title("#{@type}_#{@module_app.key}")
unless auth unless auth
@ -162,10 +150,6 @@ class Admin::AuthorizationsController < OrbitBackendController
end end
end end
def remove_managers_from_auth(users)
@module_app.remove_manager(users)
end
def remove_roles_form_auth(authorization, roles) def remove_roles_form_auth(authorization, roles)
authorization.remove_roles(roles) authorization.remove_roles(roles)
end end

View File

@ -199,7 +199,7 @@ module OrbitBackendHelper
objects.join(' ') objects.join(' ')
else else
objects = Array(objects).map do |object| objects = Array(objects).map do |object|
display_value(object.send(field)) display_value(object.send(field)) rescue ''
end end
objects.join(', ') objects.join(', ')
end end

View File

@ -2,8 +2,8 @@ class AdBanner
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include Mongoid::MultiParameterAttributes include Mongoid::MultiParameterAttributes
include OrbitCoreLib::ObjectAuthable
include OrbitTag::Taggable include OrbitTag::Taggable
include OrbitModel::Authorizable
field :title field :title
field :transition_msec,type: Integer field :transition_msec,type: Integer

View File

@ -1,4 +1,3 @@
class AuthManager < Authorization class AuthManager < Authorization
# belongs_to :manager_authorizable, polymorphic: true
has_and_belongs_to_many :authorized_users, class_name: 'User', inverse_of: 'managing_apps' has_and_belongs_to_many :authorized_users, class_name: 'User', inverse_of: 'managing_apps'
end end

View File

@ -7,6 +7,7 @@ class ModuleApp
field :auth_approval_users, type: Array, default: nil field :auth_approval_users, type: Array, default: nil
field :auth_manager_users, type: Array, default: nil field :auth_manager_users, type: Array, default: nil
field :auth_sub_manager_users, type: Array, default: nil field :auth_sub_manager_users, type: Array, default: nil
field :has_category, type: Boolean, default: false
field :key field :key
field :title field :title
field :sidebar_order,type: Integer,default: 0 field :sidebar_order,type: Integer,default: 0
@ -189,7 +190,8 @@ class ModuleApp
end end
def update_auth_manager_users def update_auth_manager_users
self.update_attribute(:auth_manager_users, self.auth_manager_authorized_users.map{|user| user.id}) users = self.auth_manager_authorized_users
self.update_attribute(:auth_manager_users, users.map{|user| user.id}) if users
end end
def update_auth_sub_manager_users def update_auth_sub_manager_users
@ -207,21 +209,6 @@ class ModuleApp
managers.include?(user) managers.include?(user)
end end
def add_managers(users)
users = Array(users)
if auth_manager = self.auth_manager
auth_manager.update_attribute(:authorized_users, (auth_manager.authorized_users + users).uniq)
else
self.create_auth_manager(authorized_users: users)
end
end
def remove_managers(users)
users = Array(users)
users = users.delete_if{|user| user == current_user || is_admin?}
self.auth_manager.update_attribute(:authorized_users, auth_manager.authorized_users - users)
end
def is_manager?(user) def is_manager?(user)
if user && !auth_manager_users.blank? if user && !auth_manager_users.blank?
auth_manager_users.include?(user.id) auth_manager_users.include?(user.id)

View File

@ -1,8 +1,3 @@
# class Tag < ProtoTag
# belongs_to :module_app
# end
class Tag class Tag
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps

View File

@ -7,6 +7,7 @@ module OrbitModel
has_one :auth_approval, as: :approval_authorizable, dependent: :destroy has_one :auth_approval, as: :approval_authorizable, dependent: :destroy
has_one :auth_sub_manager, as: :sub_manager_authorizable, dependent: :destroy has_one :auth_sub_manager, as: :sub_manager_authorizable, dependent: :destroy
delegate :authorized_users, to: :auth_approval, prefix: true, allow_nil: true delegate :authorized_users, to: :auth_approval, prefix: true, allow_nil: true
delegate :authorized_users, to: :auth_manager, prefix: true, allow_nil: true
delegate :authorized_users, to: :auth_sub_manager, prefix: true, allow_nil: true delegate :authorized_users, to: :auth_sub_manager, prefix: true, allow_nil: true
send :include, InstanceMethods send :include, InstanceMethods
end end

View File

@ -1,17 +1,12 @@
namespace :new_ui do namespace :new_ui do
# :category_name is optional, depends on the naming of the category model: if no conventional, specify it MODULE_APPS = [ %w(announcement bulletin),
task :migrate_categories, [:app_key, :model_name, :category_name] => :environment do |t, args|
@db = Mongoid.database
if args[:app_key] && args[:model_name]
migrate_category(args[:app_key], args[:model_name], args[:category_name])
else
module_apps = [ %w(announcement bulletin),
%w(archive archive_file), %w(archive archive_file),
%w(ask ask_question ask), %w(ask ask_question ask_category),
%w(asset asset), %w(asset asset),
%w(faq qa), %w(faq qa),
%w(gallery gallery_album gallery), %w(gallery gallery_album gallery_category),
%w(location location),
%w(personal_book writing_book), %w(personal_book writing_book),
%w(personal_conference writing_conference), %w(personal_conference writing_conference),
%w(personal_experience experience), %w(personal_experience experience),
@ -20,56 +15,99 @@ namespace :new_ui do
%w(personal_patent writing_patent), %w(personal_patent writing_patent),
%w(personal_project project), %w(personal_project project),
%w(web_resource web_link) %w(web_resource web_link)
] ] # app_key, model_name, category_name
module_apps.each{|value_array| migrate_category(value_array[0], value_array[1], value_array[2])}
task :migrate => :environment do
@db = Mongoid.database
migrate_tags
migrate_categories
migrate_approval_on_object
migrate_managers
end
# :category_name is optional, depends on the naming of the category model: if no conventional, specify it
task :migrate_categories, [:app_key, :model_name, :category_name] => :environment do |t, args|
migrate_categories(args)
end
task :migrate_tags => :environment do
migrate_tags
end
task :fix_tagged_ids => :environment do
fix_tagged_ids
end
def migrate_categories(args = nil)
if args && args[:app_key] && args[:model_name]
migrate_category(args[:app_key], args[:model_name], args[:category_name])
else
MODULE_APPS.each{|value_array| migrate_category(value_array[0], value_array[1], value_array[2])}
end end
end end
def migrate_category(app_key, model_name, category_name=nil) def migrate_category(app_key, model_name, category_name = nil)
category_name = model_name unless category_name @db ||= Mongoid.database
# # coll_cat = @db["#{category_name}_categories"] category_name = "#{model_name}_category" unless category_name
# # categories = coll_cat.find if coll_cat collection_category = @db[category_name.pluralize]
module_app = ModuleApp.where(key: app_key)[0] categories = collection_category.find.entries if collection_category
if module_app.has_category collection_model = @db[model_name.pluralize]
categories = "#{category_name}_categories".classify.constantize.admin_manager_all rescue nil collection_buffer_category = @db['buffer_categories']
categories = "#{category_name}_categories".classify.constantize.all if categories.blank? collection_prototype_auth = @db['prototype_auths']
if categories.present?
module_app = ModuleApp.where(key: app_key).first
if module_app.try(:has_category)
categories.each do |category| categories.each do |category|
new_category = module_app.categories.build new_category = module_app.categories.build
new_category.title_translations = category['title'] new_category.title_translations = category['title']
new_category.disable = category['disable'] new_category.disable = category['disable']
new_category.save new_category.save
model_name.classify.constantize.where("#{category_name}_category_id" => category['_id']).each do |object| collection_model.find("#{category_name}_id" => category['_id']).each do |object|
if object.id.is_a?(String) object['category_id'] = new_category.id
object.destroy object.delete("#{category_name}_id")
else collection_model.save object
object.category = new_category.id collection_buffer_category.save ({ '_type' => 'BufferCategory', 'category_id' => new_category.id, 'categorizable_type' => model_name.camelize, 'categorizable_id' => object['_id'] })
object.remove_attribute("#{category_name}_category_id")
object.save
end end
end object_auths = collection_prototype_auth.find(_type: 'ObjectAuth', obj_authable_id: category['_id'], '$or' => [{privilege_user_ids: {'$not' => {'$size' => 0}}}, {role_ids: {'$not' => {'$size' => 0}}}, {sub_role_ids: {'$not' => {'$size' => 0}}}]).entries
if object_auths = ObjectAuth.where(obj_authable_type: "#{category_name.classify}Category") if object_auths.present?
object_auths.each do |object_auth| object_auths.each do |object_auth|
case object_auth.title if object_auth['title'].eql?('fact_check')
when 'fact_check' authorization = new_category.auth_approval || new_category.create_auth_approval(module_app_id: module_app.id, title: "category_approval_#{module_app.key}")
new_object_auth = new_category.get_object_auth_by_title("approval_#{module_app.key}")
else else
new_object_auth = new_category.get_object_auth_by_title("category_#{module_app.key}") authorization = new_category.auth_sub_manager || new_category.create_auth_sub_manager(module_app_id: module_app.id, title: "category_authorization_#{module_app.key}")
end end
object_auth.auth_users.each do |user| add_users(object_auth, authorization)
new_object_auth.add_user_to_privilege_list(user) collection_prototype_auth.remove(object_auth)
end
object_auth.destroy
end end
end end
category.destroy end
collection_category.drop
collection_prototype_auth.remove(obj_authable_type: category_name.classify)
end
else
collection_category.drop
collection_prototype_auth.remove(obj_authable_type: category_name.classify)
end
end
def fix_tagged_ids
Tag.all.each do |tag|
tag.taggings.each do |tagging|
tagged_ids = tagging.taggable.tagged_ids
unless tagged_ids.include?(tag.id.to_s)
tagged_ids << tag.id.to_s
tagging.taggable.update_attribute(:tagged_ids, tagged_ids)
end
end end
end end
end end
task :migrate_tags => :environment do def migrate_tags
tags = Tag.where(_type: 'Tag') @db ||= Mongoid.database
tags.each do |tag| tags = Tag.where(_type: 'Tag').entries
module_app = ModuleApp.find(tag.module_tag_id) tags.each_with_index do |tag, i|
unless tag.tag_lease
if module_app = ModuleApp.find(tag[:module_tag_id])
new_tag = module_app.module_tags.build new_tag = module_app.module_tags.build
new_tag.name_translations = tag['name'] new_tag.name_translations = tag['name']
new_tag.is_default = tag['is_default'] new_tag.is_default = tag['is_default']
@ -79,16 +117,75 @@ namespace :new_ui do
n_tag.view_count = tag['view_count'] n_tag.view_count = tag['view_count']
n_tag.save n_tag.save
tag.taggings.each do |tagging| tag.taggings.each do |tagging|
tagged_ids = tagging.taggable.tagged_ids
unless tagged_ids.include?(tag.id.to_s)
tagged_ids << tag.id.to_s
tagging.taggable.update_attribute(:tagged_ids, tagged_ids)
end
tagging.taggable.taggings.create(tag_id: n_tag.id) tagging.taggable.taggings.create(tag_id: n_tag.id)
end end
end
tag.destroy tag.destroy
end end
end end
old_tags = Tag.where(_type: {'$ne' => 'Tag'}).delete
task :change_auth_naming => :environment do @db = Mongoid.database
ObjectAuth.where(title: /submit_/).each do |auth| @db['proto_tags'].drop
auth.update_attribute(:title, auth.title.gsub('submit_', 'category_'))
end end
def migrate_approval_on_object
@db ||= Mongoid.database
collection_prototype_auth = @db['prototype_auths']
object_auths = collection_prototype_auth.find({_type: 'ObjectAuth', obj_authable_type: {'$in' => ["AdBanner", "PageContext"]}, '$or' => [{privilege_user_ids: {'$not' => {'$size' => 0}}}, {role_ids: {'$not' => {'$size' => 0}}}, {sub_role_ids: {'$not' => {'$size' => 0}}}]}).entries
object_auths.each do |object_auth|
case object_auth['obj_authable_type']
when 'AdBanner'
module_app = ModuleApp.where(key: 'ad_banner').first
when 'PageContext'
module_app = ModuleApp.where(key: 'page_content').first
end
if module_app
object = object_auth['obj_authable_type'].constantize.find(object_auth['obj_authable_id']) rescue nil
if object
authorization = object.create_auth_sub_manager(module_app_id: module_app.id, title: "#{object_auth['obj_authable_type'].underscore}_authorization_#{module_app.key}")
add_users(object_auth, authorization) if authorization
end
end
collection_prototype_auth.remove(object_auth)
end
collection_prototype_auth.remove(obj_authable_type: {'$in' => ["AdBanner", "PageContext"]})
end
def add_users(object_auth, authorization)
if authorization
if object_auth['role_ids'].present?
roles = Role.find(object_auth['role_ids']) rescue nil
authorization.add_roles(roles) if roles
end
if object_auth['sub_role_ids'].present?
sub_roles = SubRole.find(object_auth['sub_role_ids']) rescue nil
authorization.add_sub_roles(sub_roles) if sub_roles
end
if object_auth['privilege_user_ids'].present?
users = User.find(object_auth['privilege_user_ids']) rescue nil
authorization.add_users(users) if users
end
end
end
def migrate_managers
@db ||= Mongoid.database
collection_app_manager = @db['app_managers']
app_managers = collection_app_manager.find(managing_app_id: {'$not' => {'$size' => 0}}).entries
app_managers.each do |app_manager|
module_app = ModuleApp.find(app_manager['managing_app_id']) rescue nil
user = User.find(app_manager['user_id']) rescue nil
if module_app && user
authorization = module_app.auth_manager || module_app.create_auth_manager(module_app_id: module_app.id, title: module_app.key)
authorization.add_users(user)
end
end
collection_app_manager.drop
end end
task :reset_user_position => :environment do task :reset_user_position => :environment do

View File

@ -11,7 +11,9 @@ module AdBanner
update_info 'some update_info' update_info 'some update_info'
taggable taggable
authorizable authorizable do
authorizable_on('AdBanner')
end
# front_end do # front_end do
# app_page 'bulletins' # app_page 'bulletins'