Class: SimpleCrowd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_crowd/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|@options| ... } ⇒ Client

Returns a new instance of Client.

Yields:



7
8
9
10
11
# File 'lib/simple_crowd/client.rb', line 7

def initialize options = {}
  @options = SimpleCrowd.options options
  yield(@options) if block_given?
  self.cache_store = @options.delete(:cache_store)
end

Instance Attribute Details

#app_tokenObject

Returns the value of attribute app_token.



5
6
7
# File 'lib/simple_crowd/client.rb', line 5

def app_token
  @app_token
end

#cache_storeObject Also known as: cache

Returns the value of attribute cache_store.



5
6
7
# File 'lib/simple_crowd/client.rb', line 5

def cache_store
  @cache_store
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/simple_crowd/client.rb', line 4

def options
  @options
end

Instance Method Details

#add_user(user, credential) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/simple_crowd/client.rb', line 167

def add_user user, credential
  return if user.nil? || credential.nil?
  [:email, :first_name, :last_name].each do |k|
    user.send(:"#{k}=", "") if user.send(k).nil?
  end
  soap_user = user.to_soap
  # We don't use these attributes when creating
  soap_user.delete(:id)
  soap_user.delete(:directory_id)
  # Add blank attributes if missing

  # Declare require namespaces
  soap_user = add_soap_namespace(soap_user)

  SimpleCrowd::User.from_soap simple_soap_call(:add_principal, soap_user, {'auth:credential' => credential, 'auth:encryptedCredential' => false})
end

#add_user_to_group(user, group) ⇒ Object



97
98
99
100
# File 'lib/simple_crowd/client.rb', line 97

def add_user_to_group user, group
  simple_soap_call :add_principal_to_group, user, group
  true
end

#authenticate_application(name = , password = ) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simple_crowd/client.rb', line 28

def authenticate_application(name = @options[:app_name], password = @options[:app_password])
  response = convert_soap_errors do
    client.request :authenticate_application do |soap|
      prepare soap
      soap.body = {:in0 => {
        'auth:name' => name,
        'auth:credential' => {'auth:credential' => password}
      }.merge(no_validation_factors)}
    end
  end
  clean_response(response.to_hash[:authenticate_application_response][:out])[:token].to_s.dup
end

#authenticate_user(name, password, factors = nil) ⇒ String

Authenticate user by name/pass and retrieve login token

Returns:

  • (String)

    user token



43
44
45
46
47
48
49
50
51
52
# File 'lib/simple_crowd/client.rb', line 43

def authenticate_user name, password, factors = nil
  if factors
    factors = prepare_validation_factors(factors)
    simple_soap_call :authenticate_principal, {'auth:application' => @options[:app_name], 'auth:name' => name,
        'auth:credential' => {'auth:credential' => password},
        'auth:validationFactors' => factors}
  else
    simple_soap_call :authenticate_principal_simple, name, password
  end
end

#create_user_token(name) ⇒ Object



54
55
56
# File 'lib/simple_crowd/client.rb', line 54

def create_user_token name
  simple_soap_call :create_principal_token, name, nil
end

#find_all_group_namesObject



88
89
90
# File 'lib/simple_crowd/client.rb', line 88

def find_all_group_names
  (simple_soap_call :find_all_group_names)[:string]
end

#find_all_user_namesObject



112
113
114
# File 'lib/simple_crowd/client.rb', line 112

def find_all_user_names
  (simple_soap_call :find_all_principal_names)[:string]
end

#find_group_by_name(name) ⇒ Object



84
85
86
# File 'lib/simple_crowd/client.rb', line 84

def find_group_by_name name
  SimpleCrowd::Group.from_soap simple_soap_call(:find_group_by_name, name)
end

#find_group_memberships(user) ⇒ Object



79
80
81
82
# File 'lib/simple_crowd/client.rb', line 79

def find_group_memberships user
  groups = simple_soap_call :find_group_memberships, user
  groups[:string] unless groups.nil?
end

#find_user_by_email(email) ⇒ Object

Exact email match



134
135
136
# File 'lib/simple_crowd/client.rb', line 134

def find_user_by_email email
  search_users_by_email(email).find{|u| u.email.casecmp(email) == 0}
end

#find_user_by_name(name) ⇒ Object



116
117
118
# File 'lib/simple_crowd/client.rb', line 116

def find_user_by_name name
  SimpleCrowd::User.from_soap simple_soap_call(:find_principal_by_name, name) rescue nil
end

#find_user_by_token(token) ⇒ Object



124
125
126
# File 'lib/simple_crowd/client.rb', line 124

def find_user_by_token token
  SimpleCrowd::User.from_soap simple_soap_call(:find_principal_by_token, token) rescue nil
end

#find_user_with_attributes_by_name(name) ⇒ Object



120
121
122
# File 'lib/simple_crowd/client.rb', line 120

def find_user_with_attributes_by_name name
  SimpleCrowd::User.from_soap simple_soap_call(:find_principal_with_attributes_by_name, name) rescue nil
end

#find_username_by_token(token) ⇒ Object



128
129
130
131
# File 'lib/simple_crowd/client.rb', line 128

def find_username_by_token token
  user = find_user_by_token token
  user && user[:username]
end


13
14
15
16
17
18
19
20
21
# File 'lib/simple_crowd/client.rb', line 13

def get_cookie_info
  @cookie_info ||= cache.fetch(cache_key(:cookie_info)) do
    # Remove custom SOAP attributes from the strings
    simple_soap_call(:get_cookie_info).inject({}) do |cookie_info, (key, val)|
      cookie_info[key] = val ? val.to_s : val
      cookie_info
    end
  end
end

#get_granted_authoritiesObject



23
24
25
26
# File 'lib/simple_crowd/client.rb', line 23

def get_granted_authorities
  groups = simple_soap_call :get_granted_authorities
  groups[:string] unless groups.nil?
end

#invalidate_user_token(token) ⇒ Boolean

Invalidate an existing user token (log out) NOTE: call will return true even if token is invalid

Returns:

  • (Boolean)

    success (does not guarantee valid token)



61
62
63
64
# File 'lib/simple_crowd/client.rb', line 61

def invalidate_user_token token
  simple_soap_call :invalidate_principal_token, token
  true
end

#is_cache_enabled?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/simple_crowd/client.rb', line 71

def is_cache_enabled?
  simple_soap_call :is_cache_enabled
end

#is_group_member?(group, user) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/simple_crowd/client.rb', line 75

def is_group_member? group, user
  simple_soap_call :is_group_member, group, user
end

#is_valid_user_token?(token, factors = nil) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/simple_crowd/client.rb', line 66

def is_valid_user_token? token, factors = nil
  factors = prepare_validation_factors(factors)
  simple_soap_call :is_valid_principal_token, token, factors
end

#remove_user(name) ⇒ Object



184
185
186
187
# File 'lib/simple_crowd/client.rb', line 184

def remove_user name
  simple_soap_call :remove_principal, name
  true
end

#remove_user_from_group(user, group) ⇒ Object



102
103
104
105
# File 'lib/simple_crowd/client.rb', line 102

def remove_user_from_group user, group
  simple_soap_call :remove_principal_from_group, user, group
  true
end

#reset_cacheObject



237
238
239
240
241
# File 'lib/simple_crowd/client.rb', line 237

def reset_cache
  [:app_token, :cookie_info].each do |key|
    cache.delete(cache_key(key))
  end
end

#reset_user_password(name) ⇒ Object



107
108
109
110
# File 'lib/simple_crowd/client.rb', line 107

def reset_user_password name
  simple_soap_call :reset_principal_credential, name
  true
end

#search_users(criteria, limit = 0, start = 0) ⇒ Object

Search Crowd users using the given criteria.

critieria should be a hash of SimpleCrowd::User properties or attributes. Not all properties are supported, see (developer.atlassian.com/display/CROWDDEV/Using+the+Search+API)

NOTE: Atlassian Crowd contains a bug that ignores the limit and start parameters

For example:

client.search_users(:email => 'foo', :display_name => 'bar')


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/simple_crowd/client.rb', line 153

def search_users criteria, limit=0, start=0
  # Convert search criteria to Crowd search restrictions
  restrictions = criteria.inject({}) do |h, (key, val)|
    key = User.search_restriction_for(key).to_s
    h[key] = val
    h
  end
  soap_restrictions = add_soap_namespace(prepare_search_restrictions(restrictions, limit, start))
  users = simple_soap_call :search_principals, soap_restrictions rescue []
  return [] if users.nil? || users[:soap_principal].nil?
  users = users[:soap_principal].is_a?(Array) ? users[:soap_principal] : [users[:soap_principal]]
  users.map{|u| SimpleCrowd::User.from_soap u}
end

#search_users_by_email(email) ⇒ Object

Partial email match



139
140
141
# File 'lib/simple_crowd/client.rb', line 139

def search_users_by_email email
  search_users({'email' => email})
end

#update_group(group, description, active) ⇒ Object



92
93
94
95
# File 'lib/simple_crowd/client.rb', line 92

def update_group group, description, active
  simple_soap_call :update_group, group, description, active
  true
end

#update_user(user) ⇒ Object

Parameters:



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/simple_crowd/client.rb', line 209

def update_user user
  return unless user.dirty?
  # Exclude non-attribute properties (only attributes can be updated in crowd)
  attrs_to_update = user.dirty_attributes
  return if attrs_to_update.empty?

  attrs_to_update.each do |a|
    key = SimpleCrowd::User.soap_key_for(a)
    self.update_user_attribute user.username, key, user.send(a)
  end
  user.clean
  user
end

#update_user_attribute(user, name, value) ⇒ Object Also known as: add_user_attribute

Only supports single value attributes TODO: Allow value arrays

Parameters:

  • user (String)

    name of user to update

  • name (String)

    of attribute to update

  • value (String)

    of attribute to update



200
201
202
203
204
205
# File 'lib/simple_crowd/client.rb', line 200

def update_user_attribute user, name, value
  return unless (name.is_a?(String) || name.is_a?(Symbol)) && (value.is_a?(String) || value.is_a?(Array))
  soap_attr = add_soap_namespace({:name => name, :values => {:string => value}})
  simple_soap_call :update_principal_attribute, user, soap_attr
  true
end

#update_user_credential(user, credential, encrypted = false) ⇒ Object



189
190
191
192
193
# File 'lib/simple_crowd/client.rb', line 189

def update_user_credential user, credential, encrypted = false
  simple_soap_call :update_principal_credential, user,
                   {'auth:credential' => credential, 'auth:encryptedCredential' => encrypted}
  true
end