trabajo-afectivo/app/controllers/getting_started_controller.rb

857 lines
21 KiB
Ruby
Raw Normal View History

2014-02-03 19:24:49 +00:00
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
2014-11-16 22:45:57 +00:00
require 'resolv'
2012-04-10 19:57:18 +00:00
class GettingStartedController < ApplicationController
=begin
Resource:
2014-11-16 22:45:57 +00:00
GET /api/v1/getting_started
Response:
{
"master_user": 1,
"groups": [
{
"name": "group1",
"active":true
},
{
"name": "group2",
"active":true
}
]
}
Test:
2014-11-16 22:45:57 +00:00
curl http://localhost/api/v1/getting_started -v -u #{login}:#{password}
=end
2012-04-10 19:57:18 +00:00
def index
# check if first user already exists
2014-10-22 21:00:11 +00:00
return if setup_done_response
2012-04-10 19:57:18 +00:00
# if master user already exists, we need to be authenticated
2014-10-22 21:00:11 +00:00
if setup_done
return if !authentication_check
end
2012-04-10 19:57:18 +00:00
# get all groups
groups = Group.where( :active => true )
2012-04-10 19:57:18 +00:00
# return result
2012-04-11 06:34:56 +00:00
render :json => {
2014-10-26 12:13:44 +00:00
:setup_done => setup_done,
:import_mode => Setting.get('import_mode'),
:import_backend => Setting.get('import_backend'),
:groups => groups,
2014-10-22 21:00:11 +00:00
}
end
2014-11-16 22:45:57 +00:00
def base
# check admin permissions
return if deny_if_not_role('Admin')
2014-10-22 21:00:11 +00:00
2014-11-16 22:45:57 +00:00
# validate url
messages = {}
if !params[:url] ||params[:url] !~ /^(http|https):\/\/.+?$/
2014-11-16 22:45:57 +00:00
messages[:url] = 'A URL looks like http://zammad.example.com'
end
# validate organization
if !params[:organization] || params[:organization].empty?
messages[:organization] = 'Invalid!'
end
# validate image
if params[:logo] && !params[:logo].empty?
content_type = nil
content = nil
# data:image/png;base64
if params[:logo] =~ /^data:(.+?);base64,(.+?)$/
content_type = $1
content = $2
end
if !content_type || !content
messages[:logo] = 'Unable to process image upload.'
end
end
if !messages.empty?
2014-10-22 21:00:11 +00:00
render :json => {
2014-11-16 22:45:57 +00:00
:result => 'invalid',
:messages => messages,
2014-10-22 21:00:11 +00:00
}
return
2014-10-22 21:00:11 +00:00
end
# split url in http_type and fqdn
2014-11-16 22:45:57 +00:00
settings = {}
if params[:url] =~ /^(http|https):\/\/(.+?)$/
Setting.set('http_type', $1)
2014-11-16 22:45:57 +00:00
settings[:http_type] = $1
Setting.set('fqdn', $2)
2014-11-16 22:45:57 +00:00
settings[:fqdn] = $2
end
2014-11-16 22:45:57 +00:00
# save organization
Setting.set('organization', params[:organization])
settings[:organization] = params[:organization]
# save image
if params[:logo] && !params[:logo].empty?
content_type = nil
content = nil
# data:image/png;base64
if params[:logo] =~ /^data:(.+?);base64,(.+?)$/
content_type = $1
content = Base64.decode64($2)
end
Store.remove( :object => 'System::Logo', :o_id => 1 )
Store.add(
:object => 'System::Logo',
:o_id => 1,
:data => content,
:filename => 'image',
:preferences => {
'Content-Type' => content_type
},
# :created_by_id => self.updated_by_id,
)
end
2014-10-22 21:00:11 +00:00
render :json => {
2014-11-16 22:45:57 +00:00
:result => 'ok',
:settings => settings,
2014-10-22 21:00:11 +00:00
}
end
2014-11-16 22:45:57 +00:00
def email_probe
2014-10-22 21:00:11 +00:00
2014-11-16 22:45:57 +00:00
# check admin permissions
return if deny_if_not_role('Admin')
# validation
user = nil
domain = nil
if params[:email] =~ /^(.+?)@(.+?)$/
user = $1
domain = $2
end
if !user || !domain
2014-10-22 21:00:11 +00:00
render :json => {
2014-11-16 22:45:57 +00:00
:result => 'invalid',
:messages => {
:email => 'Invalid email.'
},
2014-10-22 21:00:11 +00:00
}
return
end
2014-11-16 22:45:57 +00:00
# check domain based attributes
providerMap = {
:google => {
:domain => 'gmail.com|googlemail.com|gmail.de',
:inbound => {
:adapter => 'imap',
:options => {
:host => 'imap.gmail.com',
:port => '993',
:ssl => true,
:user => params[:email],
:password => params[:password],
2014-10-22 21:00:11 +00:00
},
2014-11-16 22:45:57 +00:00
},
:outbound => {
:adapter => 'smtp',
:options => {
:host => 'smtp.gmail.com',
2014-11-18 11:51:35 +00:00
:port => '25',
2014-11-16 22:45:57 +00:00
:ssl => true,
:user => params[:email],
:password => params[:password],
2014-10-22 21:00:11 +00:00
}
2014-11-16 22:45:57 +00:00
},
},
}
2014-10-22 21:00:11 +00:00
2014-11-16 22:45:57 +00:00
# probe based on email domain and mx
domains = [domain]
mail_exchangers = mxers(domain)
if mail_exchangers && mail_exchangers[0]
puts "MX #{mail_exchangers} - #{mail_exchangers[0][0]}"
end
if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
domains.push mail_exchangers[0][0]
end
providerMap.each {|provider, settings|
domains.each {|domain_to_check|
if domain_to_check =~ /#{settings[:domain]}/i
# probe inbound
result = email_probe_inbound( settings[:inbound] )
2014-11-17 10:35:36 +00:00
if result[:result] != 'ok'
2014-11-16 22:45:57 +00:00
render :json => result
2014-10-22 21:00:11 +00:00
return
end
2014-11-16 22:45:57 +00:00
# probe outbound
result = email_probe_outbound( settings[:outbound], params[:email] )
if result[:result] != 'ok'
render :json => result
return
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
render :json => {
:result => 'ok',
2014-11-18 11:51:35 +00:00
:setting => settings,
2014-11-16 22:45:57 +00:00
}
return
end
}
}
# probe inbound
inboundMap = []
if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
inboundMx = [
{
:adapter => 'imap',
:options => {
:host => mail_exchangers[0][0],
:port => 993,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'imap',
:options => {
:host => mail_exchangers[0][0],
:port => 993,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
]
inboundMap = inboundMap + inboundMx
end
inboundAuto = [
{
:adapter => 'imap',
:options => {
:host => "mail.#{domain}",
:port => 993,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'imap',
:options => {
:host => "mail.#{domain}",
:port => 993,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'imap',
:options => {
:host => "imap.#{domain}",
:port => 993,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'imap',
:options => {
:host => "imap.#{domain}",
:port => 993,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "mail.#{domain}",
:port => 995,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "mail.#{domain}",
:port => 995,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "pop.#{domain}",
:port => 995,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "pop.#{domain}",
:port => 995,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "pop3.#{domain}",
:port => 995,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'pop3',
:options => {
:host => "pop3.#{domain}",
:port => 995,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
]
inboundMap = inboundMap + inboundAuto
settings = {}
success = false
inboundMap.each {|config|
puts "PROBE: #{config.inspect}"
result = email_probe_inbound( config )
puts "RESULT: #{result.inspect}"
2014-11-17 10:35:36 +00:00
if result[:result] == 'ok'
2014-11-16 22:45:57 +00:00
success = true
settings[:inbound] = config
break
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
}
2014-10-22 21:00:11 +00:00
2014-11-16 22:45:57 +00:00
if !success
render :json => {
:result => 'failed',
}
return
end
# probe outbound
outboundMap = []
if mail_exchangers && mail_exchangers[0] && mail_exchangers[0][0]
outboundMx = [
{
:adapter => 'smtp',
:options => {
:host => mail_exchangers[0][0],
:port => 25,
:ssl => true,
:user => user,
:password => params[:password],
2014-10-22 21:00:11 +00:00
},
2014-11-16 22:45:57 +00:00
},
{
:adapter => 'smtp',
:options => {
:host => mail_exchangers[0][0],
:port => 25,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => mail_exchangers[0][0],
:port => 465,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => mail_exchangers[0][0],
:port => 465,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
]
outboundMap = outboundMap + outboundMx
end
outboundAuto = [
{
:adapter => 'smtp',
:options => {
:host => "mail.#{domain}",
:port => 25,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "mail.#{domain}",
:port => 25,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "mail.#{domain}",
:port => 465,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "mail.#{domain}",
:port => 465,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "smtp.#{domain}",
:port => 25,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "smtp.#{domain}",
:port => 25,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "smtp.#{domain}",
:port => 465,
:ssl => true,
:user => user,
:password => params[:password],
},
},
{
:adapter => 'smtp',
:options => {
:host => "smtp.#{domain}",
:port => 465,
:ssl => true,
:user => params[:email],
:password => params[:password],
},
},
]
success = false
outboundMap.each {|config|
puts "PROBE: #{config.inspect}"
result = email_probe_outbound( config, params[:email] )
puts "RESULT: #{result.inspect}"
if result[:result] == 'ok'
success = true
settings[:outbound] = config
break
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
}
if !success
render :json => {
:result => 'failed',
}
return
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
render :json => {
:result => 'ok',
:setting => settings,
}
end
def email_outbound
# check admin permissions
return if deny_if_not_role('Admin')
# validate params
if !params[:adapter]
render :json => {
:result => 'invalid',
}
return
end
# connection test
result = email_probe_outbound( params, params[:email] )
2014-10-22 21:00:11 +00:00
2014-11-18 11:51:35 +00:00
render :json => result
2014-10-22 21:00:11 +00:00
end
2014-11-16 22:45:57 +00:00
def email_inbound
# check admin permissions
return if deny_if_not_role('Admin')
2014-10-22 21:00:11 +00:00
# validate params
if !params[:adapter]
render :json => {
:result => 'invalid',
}
return
end
# connection test
2014-11-16 22:45:57 +00:00
result = email_probe_inbound( params )
2014-10-22 21:00:11 +00:00
2014-11-17 10:35:36 +00:00
render :json => result
return
2014-11-16 22:45:57 +00:00
end
def email_verify
# check admin permissions
return if deny_if_not_role('Admin')
2014-10-22 21:00:11 +00:00
# send verify email to inbox
2014-11-18 11:51:35 +00:00
if !params[:subject]
subject = '#' + rand(99999999999).to_s
else
subject = params[:subject]
end
result = email_probe_outbound( params[:outbound], params[:meta][:email], subject )
(1..7).each {|loop|
sleep 7
2014-10-22 21:00:11 +00:00
# fetch mailbox
found = nil
2014-11-16 22:45:57 +00:00
if params[:inbound][:adapter] =~ /^imap$/i
found = Channel::IMAP.new.fetch( { :options => params[:inbound][:options] }, 'verify', subject )
2014-10-22 21:00:11 +00:00
else
2014-11-16 22:45:57 +00:00
found = Channel::POP3.new.fetch( { :options => params[:inbound][:options] }, 'verify', subject )
2014-10-22 21:00:11 +00:00
end
if found && found == 'verify ok'
# remember address
address = EmailAddress.all.first
if address
address.update_attributes(
2014-11-16 22:45:57 +00:00
:realname => params[:meta][:realname],
:email => params[:meta][:email],
2014-10-22 21:00:11 +00:00
:active => 1,
:updated_by_id => 1,
:created_by_id => 1,
)
else
EmailAddress.create(
2014-11-16 22:45:57 +00:00
:realname => params[:meta][:realname],
:email => params[:meta][:email],
2014-10-22 21:00:11 +00:00
:active => 1,
:updated_by_id => 1,
:created_by_id => 1,
)
end
# store mailbox
Channel.create(
:area => 'Email::Inbound',
2014-11-16 22:45:57 +00:00
:adapter => params[:inbound][:adapter],
:options => params[:inbound][:options],
2014-10-22 21:00:11 +00:00
:group_id => 1,
:active => 1,
:updated_by_id => 1,
:created_by_id => 1,
)
2014-11-16 22:45:57 +00:00
# save settings
if params[:outbound][:adapter] =~ /^smtp$/i
smtp = Channel.where( :adapter => 'SMTP', :area => 'Email::Outbound' ).first
smtp.options = params[:outbound][:options]
smtp.active = true
smtp.save!
sendmail = Channel.where( :adapter => 'Sendmail' ).first
sendmail.active = false
sendmail.save!
else
sendmail = Channel.where( :adapter => 'Sendmail', :area => 'Email::Outbound' ).first
sendmail.options = {}
sendmail.active = true
sendmail.save!
smtp = Channel.where( :adapter => 'SMTP' ).first
smtp.active = false
smtp.save
end
2014-10-22 21:00:11 +00:00
render :json => {
:result => 'ok',
}
return
end
}
# check dilivery for 30 sek.
render :json => {
:result => 'invalid',
:message => 'Verification Email not found in mailbox.',
2014-11-18 11:51:35 +00:00
:subject => subject,
2014-10-22 21:00:11 +00:00
}
end
private
2014-11-18 11:51:35 +00:00
def email_probe_outbound(params, email, subject = nil)
2014-11-16 22:45:57 +00:00
# validate params
if !params[:adapter]
result = {
:result => 'invalid',
2014-11-18 11:51:35 +00:00
:message => 'Invalid, need adapter!',
2014-11-16 22:45:57 +00:00
}
return result
end
2014-11-18 11:51:35 +00:00
if subject
mail = {
:from => email,
:to => email,
:subject => "Zammad Getting started Test Email #{subject}",
:body => '.',
'x-zammad-ignore' => 'true',
}
else
mail = {
:from => email,
:to => 'emailtrytest@znuny.com',
:subject => 'test',
:body => 'test',
}
end
2014-11-16 22:45:57 +00:00
# test connection
translationMap = {
2014-11-17 10:35:36 +00:00
'authentication failed' => 'Authentication failed!',
2014-11-16 22:45:57 +00:00
'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
2014-11-17 10:35:36 +00:00
'No route to host' => 'No route to host!',
'Connection refused' => 'Connection refused!',
2014-11-16 22:45:57 +00:00
}
2014-11-18 11:51:35 +00:00
if params[:adapter] =~ /^smtp$/i
2014-11-16 22:45:57 +00:00
begin
Channel::SMTP.new.send(
2014-11-18 11:51:35 +00:00
mail,
2014-11-16 22:45:57 +00:00
{
:options => params[:options]
}
)
rescue Exception => e
# check if sending email was ok, but mailserver rejected
2014-11-18 11:51:35 +00:00
if !subject
whiteMap = {
'Recipient address rejected' => true,
}
whiteMap.each {|key, message|
if e.message =~ /#{Regexp.escape(key)}/i
result = {
:result => 'ok',
:settings => params,
:notice => e.message,
}
return result
end
}
end
2014-11-16 22:45:57 +00:00
message_human = ''
translationMap.each {|key, message|
if e.message =~ /#{Regexp.escape(key)}/i
message_human = message
end
}
result = {
:result => 'invalid',
2014-11-18 11:51:35 +00:00
:settings => params,
2014-11-16 22:45:57 +00:00
:message => e.message,
:message_human => message_human,
}
return result
end
2014-11-17 10:35:36 +00:00
result = {
:result => 'ok',
}
return result
2014-11-16 22:45:57 +00:00
end
begin
Channel::Sendmail.new.send(
2014-11-18 11:51:35 +00:00
mail,
2014-11-16 22:45:57 +00:00
nil
)
rescue Exception => e
message_human = ''
translationMap.each {|key, message|
if e.message =~ /#{Regexp.escape(key)}/i
message_human = message
end
}
result = {
:result => 'invalid',
2014-11-18 11:51:35 +00:00
:settings => params,
2014-11-16 22:45:57 +00:00
:message => e.message,
:message_human => message_human,
}
return result
end
2014-11-17 10:35:36 +00:00
result = {
:result => 'ok',
}
return result
2014-11-16 22:45:57 +00:00
end
def email_probe_inbound(params)
# validate params
if !params[:adapter]
raise 'need :adapter param'
end
# connection test
translationMap = {
'authentication failed' => 'Authentication failed!',
'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
'No route to host' => 'No route to host!',
'Connection refused' => 'Connection refused!',
}
if params[:adapter] =~ /^imap$/i
begin
Channel::IMAP.new.fetch( { :options => params[:options] }, 'check' )
rescue Exception => e
message_human = ''
translationMap.each {|key, message|
if e.message =~ /#{Regexp.escape(key)}/i
message_human = message
end
}
result = {
:result => 'invalid',
2014-11-18 11:51:35 +00:00
:settings => params,
2014-11-16 22:45:57 +00:00
:message => e.message,
:message_human => message_human,
}
return result
end
2014-11-17 10:35:36 +00:00
result = {
:result => 'ok',
}
return result
2014-11-16 22:45:57 +00:00
end
begin
Channel::POP3.new.fetch( { :options => params[:options] }, 'check' )
rescue Exception => e
message_human = ''
translationMap.each {|key, message|
if e.message =~ /#{Regexp.escape(key)}/i
message_human = message
end
}
result = {
:result => 'invalid',
2014-11-18 11:51:35 +00:00
:settings => params,
2014-11-16 22:45:57 +00:00
:message => e.message,
:message_human => message_human,
}
return result
end
2014-11-17 10:35:36 +00:00
result = {
:result => 'ok',
}
return result
2014-11-16 22:45:57 +00:00
end
def mxers(domain)
mxs = Resolv::DNS.open do |dns|
ress = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
ress.map { |r| [r.exchange.to_s, IPSocket::getaddress(r.exchange.to_s), r.preference] }
end
mxs
end
2014-10-22 21:00:11 +00:00
def setup_done
2014-11-01 10:57:02 +00:00
#return false
2014-10-22 21:00:11 +00:00
count = User.all.count()
done = true
if count <= 2
done = false
end
done
end
def setup_done_response
if !setup_done
return false
end
render :json => {
2014-11-04 09:06:41 +00:00
:setup_done => true,
:import_mode => Setting.get('import_mode'),
:import_backend => Setting.get('import_backend'),
2012-04-11 06:34:56 +00:00
}
2014-10-22 21:00:11 +00:00
true
2012-04-10 19:57:18 +00:00
end
2014-10-22 21:00:11 +00:00
end