Module: Cyclid::Client::User

Included in:
Tilapia
Defined in:
lib/cyclid/client/user.rb

Overview

User related methods

Instance Method Summary collapse

Instance Method Details

#user_add(username, email, name = nil, password = nil, secret = nil) ⇒ Hash

Create a new user

Parameters:

  • username (String)

    User name of the new user.

  • name (String) (defaults to: nil)

    Users real name.

  • email (String)

    Users email address.

  • password (String) (defaults to: nil)

    Unencrypted initial password OR a BCrypt2 encrypted password string.

  • secret (String) (defaults to: nil)

    Initial HMAC signing secret

Returns:

  • (Hash)

    Decoded server response object.

See Also:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cyclid/client/user.rb', line 59

def user_add(username, email, name = nil, password = nil, secret = nil)
  # Create the user object
  user = { 'username' => username, 'email' => email }

  # Add the real name is one was supplied
  user['name'] = name unless name.nil?

  # Add the HMAC secret if one was supplied
  user['secret'] = secret unless secret.nil?

  # Add the password if one was supplied
  unless password.nil?
    user['password'] = if password =~ /\A\$2a\$.+\z/
                         # Password is already encrypted
                         password
                       else
                         # Encrypt the plaintext password
                         BCrypt::Password.create(password).to_s
                       end
  end

  @logger.debug user

  # Sign & send the request
  uri = server_uri('/users')
  res_data = api_json_post(uri, user)
  @logger.debug res_data

  return res_data
end

#user_delete(username) ⇒ Hash

Delete a user

Parameters:

  • username (String)

    User name of the user to delete.

Returns:

  • (Hash)

    Decoded server response object.

See Also:



145
146
147
148
149
150
151
# File 'lib/cyclid/client/user.rb', line 145

def user_delete(username)
  uri = server_uri("/users/#{username}")
  res_data = api_delete(uri)
  @logger.debug res_data

  return res_data
end

#user_get(username) ⇒ Hash

Get details of a specific user

Parameters:

  • username (String)

    User name of the user to retrieve.

Returns:

  • (Hash)

    Decoded server response object.



41
42
43
44
45
46
47
# File 'lib/cyclid/client/user.rb', line 41

def user_get(username)
  uri = server_uri("/users/#{username}")
  res_data = api_get(uri)
  @logger.debug res_data

  return res_data
end

#user_listArray

Retrieve the list of users from a server

Returns:

  • (Array)

    List of user names.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cyclid/client/user.rb', line 25

def user_list
  uri = server_uri('/users')
  res_data = api_get(uri)
  @logger.debug res_data

  users = []
  res_data.each do |item|
    users << item['username']
  end

  return users
end

#user_modify(username, args) ⇒ Hash

Modify a user

Examples:

Change the email address of the user ‘leslie’

user_modify('leslie', email: '[email protected]')

Change the password & secret of the user ‘bob’

user_modify('bob', secret: 'sekrit', password: 'm1lkb0ne')

Parameters:

  • username (String)

    User name of the new user.

  • args (Hash)

    options to modify the user.

Options Hash (args):

  • name (String)

    Users real name.

  • email (String)

    Users email address.

  • secret (String)

    Initial HMAC signing secret

  • password (String)

    Unencrypted initial password

  • password (String)

    Unencrypted initial password OR a BCrypt2 encrypted password string.

Returns:

  • (Hash)

    Decoded server response object.

See Also:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cyclid/client/user.rb', line 106

def user_modify(username, args)
  # Create the user object
  user = {}

  # Add the real name is one was supplied
  user['name'] = args[:name] if args.key? :name and args[:name]

  # Add the email address if one was supplied
  user['email'] = args[:email] if args.key? :email and args[:email]

  # Add the HMAC secret if one was supplied
  user['secret'] = args[:secret] if args.key? :secret and args[:secret]

  # Add the password if one was supplied
  if args.key? :password and args[:password]
    user['password'] = if args[:password] =~ /\A\$2a\$.+\z/
                         # Password is already encrypted
                         args[:password]
                       else
                         # Encrypt the plaintext password
                         BCrypt::Password.create(args[:password]).to_s
                       end
  end

  @logger.debug user

  # Sign & send the request
  uri = server_uri("/users/#{username}")
  res_data = api_json_put(uri, user)
  @logger.debug res_data

  return res_data
end