Class: SFRest::User

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrest/user.rb

Overview

Do User actions within the Site Factory

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ User

Returns a new instance of User.

Parameters:



5
6
7
# File 'lib/sfrest/user.rb', line 5

def initialize(conn)
  @conn = conn
end

Instance Method Details

#create_user(name, email, datum = nil) ⇒ Object

Creates a user.

Parameters:

  • name (String)
  • email (String)
  • datum (Hash) (defaults to: nil)

    hash with elements :pass => string, :status => 0|1, :roles => Array



82
83
84
85
86
87
# File 'lib/sfrest/user.rb', line 82

def create_user(name, email, datum = nil)
  current_path = '/api/v1/users'
  payload = { name: name, mail: email }
  payload.merge!(datum) unless datum.nil?
  @conn.post(current_path, payload.to_json)
end

#delete_user(uid) ⇒ Object

Delete a user.

Parameters:

  • uid (integer)

    Uid of the user to be deleted



102
103
104
105
# File 'lib/sfrest/user.rb', line 102

def delete_user(uid)
  current_path = "/api/v1/users/#{uid}"
  @conn.delete(current_path)
end

#get_user_data(uid) ⇒ Hash

Gets the data for user UID

Parameters:

  • uid (int)

    site id

Returns:

  • (Hash)


72
73
74
# File 'lib/sfrest/user.rb', line 72

def get_user_data(uid)
  @conn.get('/api/v1/users/' + uid.to_s)
end

#get_user_id(username) ⇒ Integer

gets the site ID for the site named sitename will page through all the sites available searching for the site

Parameters:

  • username (String)

    drupal username (not email)

Returns:

  • (Integer)

    the uid of the drupal user



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sfrest/user.rb', line 40

def get_user_id(username)
  pglimit = 100
  res = @conn.get('/api/v1/users&limit=' + pglimit.to_s)
  usercount = res['count'].to_i
  id = user_data_from_results(res, username, 'uid')
  return id if id

  pages = (usercount / pglimit) + 1
  2.upto(pages) do |i|
    res = @conn.get('/api/v1/users&limit=' + pglimit.to_s + '?page=' + i.to_s)
    id = user_data_from_results(res, username, 'uid')
    return id if id
  end
  nil
end

#regenerate_apikey(uid) ⇒ Object

Regenerata a users apikey.

Parameters:

  • uid (integer)

    Uid of the user



109
110
111
112
# File 'lib/sfrest/user.rb', line 109

def regenerate_apikey(uid)
  current_path = "/api/v1/users/#{uid}/api-keys"
  @conn.delete(current_path)
end

#regenerate_apikeysObject

Regenerata all users apikeys.



115
116
117
118
# File 'lib/sfrest/user.rb', line 115

def regenerate_apikeys
  current_path = '/api/v1/users/all/api-keys'
  @conn.delete(current_path)
end

#update_user(uid, datum = nil) ⇒ Object

Updates a user.

Parameters:

  • uid (Integer)

    user id of the drupal user to update

  • datum (Hash) (defaults to: nil)

    hash with elements :name => string, :pass => string, :status => 0|1, :roles => Array, :mail => string@string, :tfa_status => 0|1



94
95
96
97
98
# File 'lib/sfrest/user.rb', line 94

def update_user(uid, datum = nil)
  current_path = "/api/v1/users/#{uid}/update"
  payload = datum.to_json unless datum.nil?
  @conn.put(current_path, payload)
end

#user_data_from_results(res, username, key) ⇒ Object

Extract the user data for ‘key’ based on the user result object

Parameters:

  • res (Hash)

    result from a request to /users

  • username (String)
  • key (String)

    one of the user data returned (uid, mail, tfa_status…)

Returns:

  • (Object)

    Integer, String, Array, Hash depending on the user data



61
62
63
64
65
66
67
# File 'lib/sfrest/user.rb', line 61

def user_data_from_results(res, username, key)
  users = res['users']
  users.each do |user|
    return user[key] if user['name'] == username
  end
  nil
end

#user_listHash{'count' => Integer, 'users' => Hash}

Gets the complete list of users Makes multiple requests to the factory to get all the users on the factory

Returns:

  • (Hash{'count' => Integer, 'users' => Hash})


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sfrest/user.rb', line 12

def user_list
  page = 1
  not_done = true
  count = 0
  while not_done
    current_path = '/api/v1/users?page=' << page.to_s
    res = @conn.get(current_path)
    if res['users'] == []
      not_done = false
    elsif !res['message'].nil?
      return { 'message' => res['message'] }
    elsif page == 1
      count = res['count']
      users = res['users']
    else
      res['users'].each do |user|
        users << user
      end
    end
    page += 1
  end
  { 'count' => count, 'users' => users }
end