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

148 lines
5.0 KiB
Ruby
Raw Normal View History

2014-10-27 11:01:37 +00:00
require "uri"
require "net/http"
class Admin::RegisterApiController < ApplicationController
def register
response = {}
response["success"] = false
domain = params[:site_domain]
email = params[:admin_email]
2014-12-02 13:15:21 +00:00
title = params[:site_title]
2014-10-27 11:01:37 +00:00
site = RegisteredSite.where(:site_domain => domain).first rescue nil
if site.nil?
site = RegisteredSite.new
site.site_domain = domain
site.admin_email = email
2014-12-02 13:15:21 +00:00
site.title_translations = title
2014-10-27 11:01:37 +00:00
site.generate_confirmation_token
site.save
2017-04-12 10:29:23 +00:00
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
2014-10-28 08:57:06 +00:00
response["success"] = true
2014-10-27 11:01:37 +00:00
else
response["message"] = "Site already registered."
response["error"] = "SITE_ALREADY_REGISTERED"
end
2014-10-28 08:57:06 +00:00
if !site.nil?
2022-03-08 08:08:40 +00:00
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
2021-09-09 06:44:46 +00:00
p1 = fork {register_client_site(site,site.site_token) }
2014-10-28 08:57:06 +00:00
Process.detach(p1)
end
render :json => ((params[:success] == 'error') ? response.merge({'success'=>params[:success]}).to_json : response.to_json)
2014-10-28 08:57:06 +00:00
2014-10-27 11:01:37 +00:00
end
def send_store_token
site = RegisteredSite.where(:site_domain => params[:site_domain]).first
2020-04-01 03:44:59 +00:00
uid = site.uid if !site.nil?
render :json => {'store_token'=>uid,}
end
2014-10-27 11:01:37 +00:00
def send_confirmation_email(email, confirmation_token, site_token)
2014-10-28 10:09:17 +00:00
url = "#{request.protocol}#{request.host_with_port}/register/confirm_email?confirmation=#{confirmation_token}&site=#{site_token}"
2014-10-27 11:01:37 +00:00
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
2014-10-28 08:57:06 +00:00
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
2023-05-08 14:49:53 +00:00
render :body => "ok"
2014-10-28 08:57:06 +00:00
end
2016-04-15 10:18:58 +00:00
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]
2016-04-15 10:19:38 +00:00
site.save
2020-04-01 03:44:59 +00:00
watch_list_save_flag = (params['user']!= 'rulingcom' && params['user'].to_s != '')
user_name = params['user']
puts ['user_name',user_name]
2021-09-09 06:44:46 +00:00
site_anncs = SiteAnnc.where(visible: true)
2020-04-01 03:44:59 +00:00
.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
2016-04-15 10:18:58 +00:00
else
render :json => {"success" => false}.to_json
end
end
2014-10-27 11:01:37 +00:00
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
2014-10-28 08:57:06 +00:00
response["message"] = "Site not confirmed, please confirm the email, sent to #{site.admin_email} to proceed."
2014-10-27 11:01:37 +00:00
response["error"] = "SITE_NOT_CONFIRMED"
elsif !site.access_granted
2015-08-19 05:28:50 +00:00
response["message"] = "Store permissions revoked."
2014-10-27 11:01:37 +00:00
response["error"] = "SITE_PERMISSION_DENIED"
else
response["success"] = true
end
render :json => ((params[:success] == 'error') ? response.merge(params).to_json : response.to_json)
2014-10-27 11:01:37 +00:00
end
2021-09-09 06:44:46 +00:00
def register_client_site(site=nil,store_token)
if !site.nil?
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
end
2014-10-27 11:01:37 +00:00
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
2023-05-08 14:38:15 +00:00
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
2023-05-08 14:38:15 +00:00
rescue => e
puts ['e', e, e.backtrace]
params[:success] = 'error'
end
2014-10-27 11:01:37 +00:00
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
2014-12-02 13:15:21 +00:00
end