register_modules/app/controllers/admin/admin_modules_controller.rb

152 lines
3.7 KiB
Ruby

class Admin::AdminModulesController < OrbitAdminController
include RegisteredModuleHelper
def initialize
super
@app_title = "register_module"
end
def index
@registered_modules = RegisteredModule.where(:completed => true).page(params[:page]).per(10)
@table_fields = table_fields
if request.xhr?
render :partial => "index"
end
end
def approve_modules
@rm = RegisteredModule.find(params[:module_id])
@categories = @module_app.categories.enabled
@tags = @module_app.tags
render :layout => false
end
def install_site_list
rm = RegisteredModule.find(params[:id]) rescue nil
@registered_sites = site_list_without_module(rm)
render :layout => false
end
def uninstall_site_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, nil)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def revoke_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, true)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def grant_list
rm = RegisteredModule.find(params[:id]) rescue nil
slwm = site_list_with_module(rm, false)
@registered_sites = slwm.first
@times = slwm.last
render :layout => false
end
def update
rm = RegisteredModule.find(params[:id])
rm.update_attributes(registered_module_params)
rm.save
render :json => {"success" => true}.to_json
end
def install_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
if !websites.blank?
p1 = fork{install_modules_on_websites(websites,rm)}
Process.detach(p1)
end
render :json=>{"success" => true}.to_json
end
def uninstall_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
if !websites.blank?
# uninstall_modules_on_websites(websites,rm)
p1 = fork{uninstall_modules_on_websites(websites,rm)}
Process.detach(p1)
end
render :json=>{"success" => true}.to_json
end
def revoke_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
websites.each do |website|
im = InstalledModule.where(:registered_site_id => website,:r_module => rm.id).first rescue nil
if !im.nil?
im.permission_granted = false
im.save
end
end
render :json=>{"success" => true}.to_json
end
def grant_module
websites = params[:websites]
rm = RegisteredModule.find(params[:module]) rescue nil
websites.each do |website|
im = InstalledModule.where(:registered_site_id => website,:r_module => rm.id).first rescue nil
if !im.nil?
im.permission_granted = true
im.save
end
end
render :json=>{"success" => true}.to_json
end
private
def site_list_without_module(rm)
registered_sites = []
if !rm.nil?
rss = RegisteredSite.active
rss.each do |rs|
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id)
if im.count == 0
registered_sites << rs
end
end
end
registered_sites
end
def site_list_with_module(rm,access=nil)
registered_sites = []
times = []
if !rm.nil?
rss = RegisteredSite.active
rss.each do |rs|
if access.nil?
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id)
else
im = InstalledModule.where(:r_module => rm.id, :registered_site_id => rs.id,:permission_granted => access)
end
if im.count > 0
registered_sites << rs
times << im.first.created_at.strftime("%Y-%m-%d %H:%M") rescue ""
end
end
end
[registered_sites, times]
end
def registered_module_params
params.require(:registered_module).permit!
end
def table_fields
[:module_name_, :category, :developed_by, :actions]
end
end