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
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
render :json = > response . to_json
2014-10-28 08:57:06 +00:00
if ! site . nil?
p1 = fork { register_client_site ( site . site_token ) }
Process . detach ( p1 )
end
2014-10-27 11:01:37 +00:00
end
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 )
render :json = > { " success " = > true } . to_json
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
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 )
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