socialcast 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/config/ldap.yml +0 -2
- data/lib/socialcast.rb +28 -16
- data/lib/socialcast/cli.rb +18 -58
- data/lib/socialcast/message.rb +6 -0
- data/lib/socialcast/net_ldap_ext.rb +43 -0
- data/lib/socialcast/version.rb +1 -1
- data/spec/fixtures/ldap.yml +0 -3
- metadata +5 -5
data/config/ldap.yml
CHANGED
data/lib/socialcast.rb
CHANGED
@@ -2,22 +2,34 @@ require 'yaml'
|
|
2
2
|
require 'fileutils'
|
3
3
|
|
4
4
|
module Socialcast
|
5
|
-
|
6
|
-
config_dir
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
5
|
+
class << self
|
6
|
+
def config_dir
|
7
|
+
config_dir = File.expand_path '~/.socialcast'
|
8
|
+
FileUtils.mkdir config_dir, :mode => 0700 unless File.exist?(config_dir)
|
9
|
+
config_dir
|
10
|
+
end
|
11
|
+
def credentials_file
|
12
|
+
File.join config_dir, 'credentials.yml'
|
13
|
+
end
|
14
|
+
def credentials
|
15
|
+
fail 'Unknown Socialcast credentials. Run `socialcast authenticate` to initialize' unless File.exist?(credentials_file)
|
16
|
+
@@credentials ||= YAML.load_file(credentials_file)
|
17
|
+
end
|
18
|
+
def credentials=(options)
|
19
|
+
File.open(credentials_file, "w") do |f|
|
20
|
+
f.write(options.to_yaml)
|
21
|
+
end
|
22
|
+
File.chmod 0600, credentials_file
|
23
|
+
end
|
24
|
+
def basic_auth_options
|
25
|
+
{:user => credentials[:user], :password => credentials[:password]}
|
26
|
+
end
|
27
|
+
# configure restclient for api call
|
28
|
+
def resource_for_path(path, options = {}, debug = true)
|
29
|
+
RestClient.log = Logger.new(STDOUT) if debug
|
30
|
+
RestClient.proxy = credentials[:proxy] if credentials[:proxy]
|
31
|
+
url = ['https://', credentials[:domain], path].join
|
32
|
+
RestClient::Resource.new url, options.merge(basic_auth_options)
|
16
33
|
end
|
17
|
-
File.chmod 0600, credentials_file
|
18
|
-
end
|
19
|
-
def credentials
|
20
|
-
raise 'Unknown Socialcast credentials. Run `socialcast authenticate` to initialize' unless File.exist?(credentials_file)
|
21
|
-
YAML.load_file(credentials_file)
|
22
34
|
end
|
23
35
|
end
|
data/lib/socialcast/cli.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
1
3
|
require "thor"
|
2
4
|
require 'json'
|
3
5
|
require 'rest_client'
|
@@ -14,12 +16,14 @@ require 'net/ldap'
|
|
14
16
|
module Socialcast
|
15
17
|
class CLI < Thor
|
16
18
|
include Thor::Actions
|
17
|
-
|
19
|
+
|
20
|
+
method_option :trace, :type => :boolean, :aliases => '-v'
|
21
|
+
def initialize(*args); super(*args) end
|
18
22
|
|
19
23
|
desc "authenticate", "Authenticate using your Socialcast credentials"
|
20
24
|
method_option :user, :type => :string, :aliases => '-u', :desc => 'email address for the authenticated user'
|
21
|
-
method_option :domain, :type => :string, :default => 'api.socialcast.com', :desc => '
|
22
|
-
method_option :
|
25
|
+
method_option :domain, :type => :string, :default => 'api.socialcast.com', :desc => 'Socialcast community domain'
|
26
|
+
method_option :proxy, :type => :string, :desc => 'HTTP proxy options for connecting to Socialcast server'
|
23
27
|
def authenticate
|
24
28
|
user = options[:user] || ask('Socialcast username: ')
|
25
29
|
password = HighLine.new.ask("Socialcast password: ") { |q| q.echo = false }
|
@@ -28,19 +32,20 @@ module Socialcast
|
|
28
32
|
url = ['https://', domain, '/api/authentication.json'].join
|
29
33
|
say "Authenticating #{user} to #{url}"
|
30
34
|
params = {:email => user, :password => password }
|
31
|
-
resource = RestClient::Resource.new url
|
32
35
|
RestClient.log = Logger.new(STDOUT) if options[:trace]
|
36
|
+
RestClient.proxy = options[:proxy] if options[:proxy]
|
37
|
+
resource = RestClient::Resource.new url
|
33
38
|
response = resource.post params
|
34
39
|
say "API response: #{response.body.to_s}" if options[:trace]
|
35
40
|
communities = JSON.parse(response.body.to_s)['communities']
|
36
41
|
domain = communities.detect {|c| c['domain'] == domain} ? domain : communities.first['domain']
|
37
42
|
|
38
|
-
|
43
|
+
Socialcast.credentials = {:user => user, :password => password, :domain => domain, :proxy => options[:proxy]}
|
39
44
|
say "Authentication successful for #{domain}"
|
40
45
|
end
|
41
46
|
|
42
47
|
desc "share MESSAGE", "Posts a new message into socialcast"
|
43
|
-
method_option :url, :type => :string
|
48
|
+
method_option :url, :type => :string, :desc => '(optional) url to associate to the message'
|
44
49
|
method_option :attachments, :type => :array, :default => []
|
45
50
|
def share(message = nil)
|
46
51
|
message ||= $stdin.read_nonblock(100_000) rescue nil
|
@@ -48,10 +53,9 @@ module Socialcast
|
|
48
53
|
attachment_ids = []
|
49
54
|
options[:attachments].each do |path|
|
50
55
|
Dir[File.expand_path(path)].each do |attachment|
|
51
|
-
attachment_url = ['https://', credentials[:domain], '/api/attachments.json'].join
|
52
56
|
say "Uploading attachment #{attachment}..."
|
53
|
-
|
54
|
-
|
57
|
+
uploader = Socialcast.resource_for_path '/api/attachments.json', {}, options[:trace]
|
58
|
+
uploader.post :attachment => File.new(attachment) do |response, request, result|
|
55
59
|
if response.code == 201
|
56
60
|
attachment_ids << JSON.parse(response.body)['attachment']['id']
|
57
61
|
else
|
@@ -61,10 +65,7 @@ module Socialcast
|
|
61
65
|
end
|
62
66
|
end
|
63
67
|
|
64
|
-
Socialcast::Message.
|
65
|
-
Socialcast::Message.user = credentials[:user]
|
66
|
-
Socialcast::Message.password = credentials[:password]
|
67
|
-
|
68
|
+
Socialcast::Message.configure_from_credentials
|
68
69
|
Socialcast::Message.create :body => message, :url => options[:url], :attachment_ids => attachment_ids
|
69
70
|
|
70
71
|
say "Message has been shared"
|
@@ -123,46 +124,8 @@ module Socialcast
|
|
123
124
|
next if entry.grab(mappings["email"]).blank? || (mappings.has_key?("unique_identifier") && entry.grab(mappings["unique_identifier"]).blank?)
|
124
125
|
|
125
126
|
users.user do |user|
|
126
|
-
|
127
|
-
|
128
|
-
next unless mappings.has_key?(attribute)
|
129
|
-
user.tag! attribute, entry.grab(mappings[attribute])
|
130
|
-
end
|
131
|
-
|
132
|
-
contact_attributes = %w{email location cell_phone office_phone}
|
133
|
-
user.tag! 'contact-info' do |contact_info|
|
134
|
-
contact_attributes.each do |attribute|
|
135
|
-
next unless mappings.has_key?(attribute)
|
136
|
-
contact_info.tag! attribute, entry.grab(mappings[attribute])
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
custom_attributes = mappings.keys - (primary_attributes + contact_attributes)
|
141
|
-
user.tag! 'custom-fields', :type => "array" do |custom_fields|
|
142
|
-
custom_attributes.each do |attribute|
|
143
|
-
custom_fields.tag! 'custom-field' do |custom_field|
|
144
|
-
custom_field.id attribute
|
145
|
-
custom_field.label attribute
|
146
|
-
custom_field.value entry.grab(mappings[attribute])
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
memberships = entry[membership_attribute]
|
152
|
-
external_ldap_group = permission_mappings.fetch('account_types', {})['external']
|
153
|
-
if external_ldap_group && memberships.include?(external_ldap_group)
|
154
|
-
user.tag! 'account-type', 'external'
|
155
|
-
else
|
156
|
-
user.tag! 'account-type', 'member'
|
157
|
-
if permission_roles_mappings = permission_mappings['roles']
|
158
|
-
user.tag! 'roles', :type => 'array' do |roles|
|
159
|
-
permission_roles_mappings.each_pair do |socialcast_role, ldap_group|
|
160
|
-
roles.role socialcast_role if entry[membership_attribute].include?(ldap_group)
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end # user
|
127
|
+
entry.build_xml_from_mappings user, mappings, permission_mappings
|
128
|
+
end
|
166
129
|
count += 1
|
167
130
|
say "Scanned #{count} users..." if ((count % 100) == 0)
|
168
131
|
end # search
|
@@ -175,15 +138,12 @@ module Socialcast
|
|
175
138
|
say "Sending to Socialcast"
|
176
139
|
|
177
140
|
http_config = config.fetch('http', {})
|
178
|
-
|
179
|
-
RestClient.proxy = http_config['proxy'] if http_config['proxy']
|
180
|
-
url = ['https://', credentials[:domain], '/api/users/provision'].join
|
181
|
-
private_resource = RestClient::Resource.new url, :user => credentials[:user], :password => credentials[:password], :timeout => http_config['timeout']
|
141
|
+
resource = Socialcast.resource_for_path '/api/users/provision', http_config
|
182
142
|
File.open(output_file, 'r') do |file|
|
183
143
|
request_params = {:file => file}
|
184
144
|
request_params[:skip_emails] = 'true' if (config['options']["skip_emails"] || options[:skip_emails])
|
185
145
|
request_params[:test] = 'true' if (config['options']["test"] || options[:test])
|
186
|
-
|
146
|
+
resource.post request_params
|
187
147
|
end
|
188
148
|
|
189
149
|
File.delete(output_file) if (config['options']['delete_users_file'] || options[:delete_users_file])
|
data/lib/socialcast/message.rb
CHANGED
@@ -2,6 +2,12 @@ require 'active_resource'
|
|
2
2
|
|
3
3
|
module Socialcast
|
4
4
|
class Message < ActiveResource::Base
|
5
|
+
def self.configure_from_credentials
|
6
|
+
Socialcast::Message.site = ['https://', Socialcast.credentials[:domain], '/api'].join
|
7
|
+
Socialcast::Message.proxy = credentials[:proxy] if Socialcast.credentials[:proxy]
|
8
|
+
Socialcast::Message.user = Socialcast.credentials[:user]
|
9
|
+
Socialcast::Message.password = Socialcast.credentials[:password]
|
10
|
+
end
|
5
11
|
end
|
6
12
|
end
|
7
13
|
|
@@ -7,4 +7,47 @@ class Net::LDAP::Entry
|
|
7
7
|
def grab(attribute)
|
8
8
|
Array.wrap(self[attribute]).compact.first
|
9
9
|
end
|
10
|
+
|
11
|
+
def build_xml_from_mappings(user, mappings = {}, permission_mappings = {})
|
12
|
+
primary_attributes = %w{unique_identifier first_name last_name employee_number}
|
13
|
+
primary_attributes.each do |attribute|
|
14
|
+
next unless mappings.has_key?(attribute)
|
15
|
+
user.tag! attribute, grab(mappings[attribute])
|
16
|
+
end
|
17
|
+
|
18
|
+
contact_attributes = %w{email location cell_phone office_phone}
|
19
|
+
user.tag! 'contact-info' do |contact_info|
|
20
|
+
contact_attributes.each do |attribute|
|
21
|
+
next unless mappings.has_key?(attribute)
|
22
|
+
contact_info.tag! attribute, grab(mappings[attribute])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
custom_attributes = mappings.keys - (primary_attributes + contact_attributes)
|
27
|
+
user.tag! 'custom-fields', :type => "array" do |custom_fields|
|
28
|
+
custom_attributes.each do |attribute|
|
29
|
+
custom_fields.tag! 'custom-field' do |custom_field|
|
30
|
+
custom_field.id attribute
|
31
|
+
custom_field.label attribute
|
32
|
+
custom_field.value grab(mappings[attribute])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
membership_attribute = permission_mappings.fetch 'attribute_name', 'memberof'
|
38
|
+
memberships = self[membership_attribute]
|
39
|
+
external_ldap_group = permission_mappings.fetch('account_types', {})['external']
|
40
|
+
if external_ldap_group && memberships.include?(external_ldap_group)
|
41
|
+
user.tag! 'account-type', 'external'
|
42
|
+
else
|
43
|
+
user.tag! 'account-type', 'member'
|
44
|
+
if permission_roles_mappings = permission_mappings['roles']
|
45
|
+
user.tag! 'roles', :type => 'array' do |roles|
|
46
|
+
permission_roles_mappings.each_pair do |socialcast_role, ldap_group|
|
47
|
+
roles.role socialcast_role if memberships.include?(ldap_group)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
10
53
|
end
|
data/lib/socialcast/version.rb
CHANGED
data/spec/fixtures/ldap.yml
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: socialcast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ryan Sonnek
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-07-
|
19
|
+
date: 2011-07-15 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: rest-client
|
@@ -190,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
190
190
|
requirements: []
|
191
191
|
|
192
192
|
rubyforge_project: socialcast
|
193
|
-
rubygems_version: 1.8.
|
193
|
+
rubygems_version: 1.8.5
|
194
194
|
signing_key:
|
195
195
|
specification_version: 3
|
196
196
|
summary: command line interface to socialcast api
|