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 response["success"] = true else response["message"] = "Site already registered." response["error"] = "SITE_ALREADY_REGISTERED" end render :json => response.to_json if !site.nil? p1 = fork {register_client_site(site.site_token) } Process.detach(p1) end 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) render :json => {"success" => true}.to_json else render :json => {"success" => false}.to_json end 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] render :json => {"success" => true}.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 => 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) return response 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