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

85 lines
2.8 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]
site = RegisteredSite.where(:site_domain => domain).first rescue nil
if site.nil?
site = RegisteredSite.new
site.site_domain = domain
site.admin_email = email
site.generate_confirmation_token
site.save
if register_client_site(site.site_token)
response["success"] = true
send_confirmation_email(site.admin_email, site.confirmation_token, site.site_token)
else
response["message"] = "Invalid site token."
response["error"] = "INVALID_SITE_TOKEN"
end
else
response["message"] = "Site already registered."
response["error"] = "SITE_ALREADY_REGISTERED"
end
render :json => response.to_json
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 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 reovked."
response["error"] = "SITE_PERMISSION_DENIED"
else
response["success"] = true
end
render :json => response.to_json
end
def register_client_site(store_token)
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
response = http.request(request)
data = JSON.parse(response.body)
if data["success"] == true
return true
else
return false
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