Module: CFoundryHelper::Helpers::UserHelper

Defined in:
lib/cfoundry_helper/helpers/user_helper.rb

Class Method Summary collapse

Class Method Details

.create_user(attributes) ⇒ Object

creates a user with the given email and password



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 20

def self.create_user(attributes)
  raise "The given attributes hash is nil!" if attributes.nil?
  raise "No user email given!" if attributes[:email].nil?
  raise "No password attribute given!" if attributes[:password].nil?

  # no admin user is created as a default
  admin = false
  unless attributes[:admin].nil?
    admin = attributes[:admin]
  end

  user = CFoundryHelper::Helpers::ClientHelper.cloud_controller_client.register(attributes[:email], attributes[:password])

  if admin
    user.admin = true
    user.update!
  end

  user
end

.delete_user(user) ⇒ Object

deletes a user from the cc and the uaa returns true if the user was deleted throws an exception on errors



44
45
46
47
48
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 44

def self.delete_user(user)
  self.delete_user_from_uaa user
  self.delete_user_from_cc  user
  return true
end

.delete_user_by_email(email) ⇒ Object



50
51
52
53
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 50

def self.delete_user_by_email(email)
  user = self.get_user_by_email email
  self.delete_user user
end

.email_exists?(email) ⇒ Boolean

checks whether a user with the given email address exists

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 79

def self.email_exists?(email)
  begin
    u = self.get_user_by_email email
      return true
  rescue
    return false
  end
end

.get_cc_usersObject

returns an array of CFoundry::V2::User registered within the cloud controller



56
57
58
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 56

def self.get_cc_users
  CFoundryHelper::Helpers::ClientHelper.cloud_controller_client.users
end

.get_email_for_user(user) ⇒ Object

returns the associated email address for the given cc user



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 5

def self.get_email_for_user(user)
  guid = user.guid

  # fetch the data from the uaa
  scim_cl = CFoundryHelper::Helpers::ClientHelper.scim_client
  query_result = scim_cl.query(:user, {:filter => "id eq '#{guid}'"})

  if query_result["resources"].nil? || query_result["resources"].empty?
    raise "No user with the guid: #{guid} could be found on the uaa!"
  end

  query_result["resources"].first["emails"].first["value"]
end

.get_user_by_email(email) ⇒ Object

returns the CFoundry::V2::User with the given email address if present throws an exception if the given user is not present in the system



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 90

def self.get_user_by_email(email)

  # fetch the data from the uaa
  scim_cl = CFoundryHelper::Helpers::ClientHelper.scim_client
  query_result = scim_cl.query(:user, {:filter => "email eq '#{email.strip}'"})

  if query_result["resources"].nil? || query_result["resources"].empty?
    raise "No user with the email address: #{email} could be found!"
  end

  user_guid = query_result["resources"].first["id"]

  # retrieve the user from the CC
  user = nil
  cc_cl = CFoundryHelper::Helpers::ClientHelper.cloud_controller_client
  cc_cl.users.each do |u|
    if u.guid.eql? user_guid
      user = u
      break
    end
  end

  if user.nil?
    raise "Could not find the user with the email address: #{email} in the Cloud Controller!"
  end
  user
end

.get_user_emailsObject

returns an array of all user emails registered within the uaa



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 61

def self.get_user_emails
  scim_cl = CFoundryHelper::Helpers::ClientHelper.scim_client
  query_result = scim_cl.query(:user)

  if query_result["resources"].nil? || query_result["resources"].empty?
    raise "No user emails could be found in the uaa!"
  end

  res_array = Array.new
  user_guid = query_result["resources"].each do |r|
    res_array << r["emails"].first["value"]
    res_array.sort!
  end

  res_array
end

.set_user_password(user, newpass) ⇒ Object

sets a new password for the given user returns the user



120
121
122
123
124
# File 'lib/cfoundry_helper/helpers/user_helper.rb', line 120

def self.set_user_password(user, newpass)
  scim_cl = CFoundryHelper::Helpers::ClientHelper.scim_client
  scim_cl.change_password user.guid, newpass, nil
  return user
end