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:



7
8
9
# File 'lib/sfrest/user.rb', line 7

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



84
85
86
87
88
89
# File 'lib/sfrest/user.rb', line 84

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



104
105
106
107
# File 'lib/sfrest/user.rb', line 104

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)


74
75
76
# File 'lib/sfrest/user.rb', line 74

def get_user_data(uid)
  @conn.get("/api/v1/users/#{uid}")
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



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

def get_user_id(username)
  pglimit = 100
  res = @conn.get("/api/v1/users&limit=#{pglimit}")
  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}?page=#{i}")
    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



111
112
113
114
# File 'lib/sfrest/user.rb', line 111

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

#regenerate_apikeysObject

Regenerata all users apikeys.



117
118
119
120
# File 'lib/sfrest/user.rb', line 117

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



96
97
98
99
100
# File 'lib/sfrest/user.rb', line 96

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



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

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})


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

def user_list
  page = 1
  not_done = true
  count = 0
  while not_done
    current_path = "/api/v1/users?page=#{page}"
    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