registered-sites/app/controllers/admin/register_api_controller.rb

148 lines
5.0 KiB
Ruby

require "uri"
require "net/http"
class Admin::RegisterApiController < ApplicationController
def register
response = {}
response["success"] = false
domain = params[:site_domain]
email = params[:admin_email]
title = params[:site_title]
site = RegisteredSite.where(:site_domain => domain).first rescue nil
if site.nil?
site = RegisteredSite.new
site.site_domain = domain
site.admin_email = email
site.title_translations = title
site.generate_confirmation_token
site.save
cp = CPanelUser.where(:email => email).first rescue nil
if cp.nil?
cp = CPanelUser.new(:email => email, :password => SecureRandom.hex(5))
end
if !cp.first_time_run
cp.registered_site_ids << site.id.to_s
end
cp.save
response["success"] = true
else
response["message"] = "Site already registered."
response["error"] = "SITE_ALREADY_REGISTERED"
end
if !site.nil?
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
p1 = fork {register_client_site(site,site.site_token) }
Process.detach(p1)
end
render :json => ((params[:success] == 'error') ? response.merge({'success'=>params[:success]}).to_json : response.to_json)
end
def send_store_token
site = RegisteredSite.where(:site_domain => params[:site_domain]).first
uid = site.uid if !site.nil?
render :json => {'store_token'=>uid,}
end
def send_confirmation_email(email, confirmation_token, site_token)
url = "#{request.protocol}#{request.host_with_port}/register/confirm_email?confirmation=#{confirmation_token}&site=#{site_token}"
email = Email.new(:mail_to => email, :mail_subject => "Confirmation email for orbit store.", :template => "email/site_confirmation_email.html.erb", :template_data => {"url" => url})
email.deliver
end
def send_email
site_token = params[:store_token]
site = RegisteredSite.find_by(:uid => site_token) rescue nil
if !site.nil?
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
end
render :body => "ok"
end
def re_register_url
site_token = params[:store_token]
site = RegisteredSite.find_by(:uid => site_token) rescue nil
if !site.nil?
site.site_domain = params[:site_domain]
site.save
watch_list_save_flag = (params['user']!= 'rulingcom' && params['user'].to_s != '')
user_name = params['user']
puts ['user_name',user_name]
site_anncs = SiteAnnc.where(visible: true)
.select{|v| v.send_all || v.recieve_sites.include?(site)}.collect do |v|
if watch_list_save_flag
watch_list = v.watch_list
if watch_list.select{|v| v[0]==site.id && v[1]==user_name}.count == 0
watch_list << [site.id,user_name]
end
v.update_attributes(watch_list: watch_list)
end
{title: v.title,content: v.content,see_more_link: v.see_more_link}
end
render :json => {"success" => true,'site_anncs'=>site_anncs}.to_json
else
render :json => {"success" => false}.to_json
end
end
def check_permissions
response = {}
response["success"] = false
store_token = params[:store_token]
site = RegisteredSite.find_by(:uid => store_token) rescue nil
if site.nil?
response["message"] = "Invalid site token."
response["error"] = "INVALID_SITE_TOKEN"
elsif !site.site_confirmed
response["message"] = "Site not confirmed, please confirm the email, sent to #{site.admin_email} to proceed."
response["error"] = "SITE_NOT_CONFIRMED"
elsif !site.access_granted
response["message"] = "Store permissions revoked."
response["error"] = "SITE_PERMISSION_DENIED"
else
response["success"] = true
end
render :json => ((params[:success] == 'error') ? response.merge(params).to_json : response.to_json)
end
def register_client_site(site=nil,store_token)
if !site.nil?
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
end
params_to_send = {'store_token' => store_token, "site_token" => params[:site_token]}
uri = URI.parse("http://#{params[:site_domain]}/")
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Post.new("/store/confirmation")
request.body = params_to_send.to_query
begin
success_flag = false
begin
response = http.request(request)
success_flag = (response.code == '200')
rescue => e
end
if success_flag
return response
else
response = Net::HTTP.post_form(URI("https://#{params[:site_domain]}/store/confirmation"),params_to_send) #if response.code == 301,use https to resend /store/confirmation
return response
end
rescue => e
puts ['e', e, e.backtrace]
params[:success] = 'error'
end
end
def confirm_email
@confirmation = false
site_token = params["site"]
confirmation_token = params["confirmation"]
site = RegisteredSite.where(:uid => site_token, :confirmation_token => confirmation_token).first rescue nil
if !site.nil?
site.confirmation_token = nil
site.site_confirmed = true
site.access_granted = true
site.save
@confirmation = true
end
end
end