Class: Socialcast::CommandLine::Provision
- Inherits:
-
Object
- Object
- Socialcast::CommandLine::Provision
- Defined in:
- lib/socialcast/command_line/provision.rb
Defined Under Namespace
Classes: ProvisionError
Constant Summary collapse
- DEFAULT_OUTPUT_FILE =
'users.xml.gz'
Instance Method Summary collapse
- #each_user_hash ⇒ Object
- #fetch_user_hash(identifier, options = {}) ⇒ Object
-
#initialize(ldap_config, options = {}) ⇒ Provision
constructor
A new instance of Provision.
- #provision ⇒ Object
- #sync_photos ⇒ Object
Constructor Details
#initialize(ldap_config, options = {}) ⇒ Provision
Returns a new instance of Provision.
15 16 17 18 19 20 |
# File 'lib/socialcast/command_line/provision.rb', line 15 def initialize(ldap_config, = {}) @ldap_config = ldap_config.dup = .dup [:output] ||= DEFAULT_OUTPUT_FILE end |
Instance Method Details
#each_user_hash ⇒ Object
22 23 24 25 26 |
# File 'lib/socialcast/command_line/provision.rb', line 22 def each_user_hash each_ldap_entry do |ldap, entry, attr_mappings, perm_mappings| yield build_user_hash_from_mappings(ldap, entry, attr_mappings, perm_mappings) end end |
#fetch_user_hash(identifier, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/socialcast/command_line/provision.rb', line 28 def fetch_user_hash(identifier, = {}) = .delete(:identifying_field) || 'unique_identifier' each_ldap_connection do |connection_name, connection_config, ldap| filter = if connection_config['filter'].present? Net::LDAP::Filter.construct(connection_config['filter']) else Net::LDAP::Filter.pres("objectclass") end attr_mappings = attribute_mappings(connection_name) filter = filter & Net::LDAP::Filter.construct("#{attr_mappings[identifying_field]}=#{identifier}") search(ldap, :base => connection_config['basedn'], :filter => filter, :attributes => ldap_search_attributes(connection_name), :size => 1) do |entry, connection| return build_user_hash_from_mappings(ldap, entry, attr_mappings, (connection_name)) end end nil end |
#provision ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/socialcast/command_line/provision.rb', line 49 def provision http_config = @ldap_config.fetch 'http', {} user_whitelist = Set.new output_file = File.join Dir.pwd, [:output] Zlib::GzipWriter.open(output_file) do |gz| xml = Builder::XmlMarkup.new(:target => gz, :indent => 1) xml.instruct! xml.export do |export| export.users(:type => "array") do |users| each_user_hash do |user_hash| users << user_hash.to_xml(:skip_instruct => true, :root => 'user') user_whitelist << [user_hash['contact_info']['email'], user_hash['unique_identifier'], user_hash['employee_number']] end end # users end # export end # gzip if [:sanity_check] puts "Sanity checking users currently marked as needing to be terminated" each_ldap_connection do |connection_name, connection_config, ldap| attr_mappings = attribute_mappings(connection_name) ((http_config) - user_whitelist).each do |user_identifiers| combined_filters = [] ['email', 'unique_identifier', 'employee_number'].each_with_index do |identifier, index| combined_filters << ((attr_mappings[identifier].blank? || user_identifiers[index].nil?) ? nil : Net::LDAP::Filter.eq(attr_mappings[identifier], user_identifiers[index])) end combined_filters.compact! filter = ((combined_filters.size > 1) ? '(|%s)' : '%s') % combined_filters.join(' ') filter = Net::LDAP::Filter.construct(filter) & Net::LDAP::Filter.construct(connection_config["filter"]) ldap_result = ldap.search(:return_result => true, :base => connection_config["basedn"], :filter => filter, :attributes => ldap_search_attributes(connection_name)) raise ProvisionError.new "Found user marked for termination that should not be terminated: #{user_identifiers}" unless ldap_result.blank? end end end if user_whitelist.empty? && ![:force] raise ProvisionError.new "Skipping upload to Socialcast since no users were found" else puts "Uploading dataset to Socialcast..." resource = Socialcast::CommandLine.resource_for_path '/api/users/provision', http_config begin File.open(output_file, 'r') do |file| request_params = {:file => file} request_params[:skip_emails] = 'true' if (@ldap_config['options']["skip_emails"] || [:skip_emails]) request_params[:test] = 'true' if (@ldap_config['options']["test"] || [:test]) resource.post request_params, :accept => :json end rescue RestClient:: => e raise ProvisionError.new "Authenticated user either does not have administration privileges or the community is not configured to allow provisioning. Please contact Socialcast support to if you need help." if e.http_code == 401 end puts "Finished" end File.delete(output_file) if (@ldap_config['options']['delete_users_file'] || [:delete_users_file]) end |
#sync_photos ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/socialcast/command_line/provision.rb', line 106 def sync_photos http_config = @ldap_config.fetch 'http', {} @ldap_config["connections"].keys.each do |connection_name| attribute_mappings(connection_name).fetch('profile_photo') end search_users_resource = Socialcast::CommandLine.resource_for_path '/api/users/search', http_config each_ldap_entry do |ldap, entry, attr_mappings, _| email = grab(entry, attr_mappings['email']) if profile_photo_data = grab(entry, attr_mappings['profile_photo']) profile_photo_data = profile_photo_data.force_encoding('binary') user_search_response = search_users_resource.get(:params => { :q => email, :per_page => 1 }, :accept => :json) user_info = JSON.parse(user_search_response)['users'].first if user_info && user_info['avatars'] && user_info['avatars']['is_system_default'] puts "Uploading photo for #{email}" user_resource = Socialcast::CommandLine.resource_for_path "/api/users/#{user_info['id']}", http_config content_type = case profile_photo_data when Regexp.new("\AGIF8", nil, 'n') 'gif' when Regexp.new('\A\x89PNG', nil, 'n') 'png' when Regexp.new("\A\xff\xd8\xff\xe0\x00\x10JFIF", nil, 'n'), Regexp.new("\A\xff\xd8\xff\xe1(.*){2}Exif", nil, 'n') 'jpg' else puts "Skipping photo for #{email}: unknown image format (supports .gif, .png, .jpg)" next end tempfile = Tempfile.new(["photo_upload", ".#{content_type}"]) tempfile.write(profile_photo_data) tempfile.rewind begin user_resource.put({ :user => { :profile_photo => { :data => tempfile } } }) ensure tempfile.unlink end end end end end |