2017-05-05 08:58:05 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
require 'ldap'
|
|
|
|
require 'ldap/group'
|
|
|
|
|
|
|
|
module Import
|
2017-05-05 08:58:05 +00:00
|
|
|
class Ldap < Import::Base
|
|
|
|
|
2017-05-10 07:51:19 +00:00
|
|
|
# Checks if the integration is activated and configured.
|
|
|
|
# Otherwise it won't get queued since it will display
|
|
|
|
# an error which is confusing and wrong.
|
2017-05-05 08:58:05 +00:00
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Import::LDAP.queueable?
|
|
|
|
# #=> true
|
|
|
|
#
|
|
|
|
# return [Boolean]
|
|
|
|
def self.queueable?
|
2017-05-10 07:51:19 +00:00
|
|
|
Setting.get('ldap_integration') && Setting.get('ldap_config').present?
|
2017-05-05 08:58:05 +00:00
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
|
2017-05-05 08:58:05 +00:00
|
|
|
# Starts a live or dry run LDAP import.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# instance = Import::LDAP.new(import_job)
|
|
|
|
#
|
|
|
|
# @raise [RuntimeError] Raised if an import should start but the ldap integration is disabled
|
|
|
|
#
|
|
|
|
# return [nil]
|
|
|
|
def start
|
2017-05-10 14:43:44 +00:00
|
|
|
return if !requirements_completed?
|
2017-04-19 10:09:54 +00:00
|
|
|
start_import
|
|
|
|
end
|
|
|
|
|
2017-05-11 15:18:03 +00:00
|
|
|
# Gets called when the Scheduler gets (re-)started and a LDAP ImportJob was still
|
|
|
|
# in the queue. The job will always get restarted to avoid the gap till the next
|
|
|
|
# run triggered by the Scheduler. The result will get updated to inform the user
|
|
|
|
# in the agent interface result view.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# instance = Import::LDAP.new(import_job)
|
|
|
|
# instance.reschedule?(delayed_job)
|
|
|
|
# #=> true
|
|
|
|
#
|
|
|
|
# return [true]
|
|
|
|
def reschedule?(_delayed_job)
|
|
|
|
@import_job.update_attribute(:result, {
|
|
|
|
info: 'Restarting due to scheduler restart.'
|
|
|
|
})
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-04-19 10:09:54 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def start_import
|
|
|
|
Import::Ldap::UserFactory.reset_statistics
|
|
|
|
|
|
|
|
Import::Ldap::UserFactory.import(
|
|
|
|
config: @import_job.payload,
|
|
|
|
dry_run: @import_job.dry_run,
|
|
|
|
import_job: @import_job
|
|
|
|
)
|
|
|
|
|
|
|
|
@import_job.result = Import::Ldap::UserFactory.statistics
|
|
|
|
end
|
2017-05-10 14:43:44 +00:00
|
|
|
|
|
|
|
def requirements_completed?
|
|
|
|
return true if @import_job.dry_run
|
|
|
|
|
|
|
|
if !Setting.get('ldap_integration')
|
|
|
|
message = 'Sync cancelled. LDAP integration deactivated. Activate via the switch.'
|
|
|
|
elsif Setting.get('ldap_config').blank? && @import_job.payload.blank?
|
|
|
|
message = 'Sync cancelled. LDAP configration or ImportJob payload missing.'
|
|
|
|
end
|
|
|
|
|
|
|
|
return true if !message
|
|
|
|
|
|
|
|
@import_job.update_attribute(:result, {
|
|
|
|
info: message
|
|
|
|
})
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
2017-04-19 10:09:54 +00:00
|
|
|
end
|
|
|
|
end
|