Module: NexusCli::UserActions

Included in:
OSSRemote, ProRemote
Defined in:
lib/nexus_cli/mixins/user_actions.rb

Overview

Author:

Instance Method Summary collapse

Instance Method Details

#change_password(params) ⇒ type

Changes the password of a user

Parameters:

  • params (Hash)

    a hash given to update the users password

Returns:

  • (type)
    description


86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nexus_cli/mixins/user_actions.rb', line 86

def change_password(params)
  response = nexus.post(nexus_url("service/local/users_changepw"), :body => create_change_password_json(params), :header => DEFAULT_CONTENT_TYPE_HEADER)
  case response.status
  when 202
    return true
  when 400
    raise InvalidCredentialsException
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#create_user(params) ⇒ Boolean

Creates a User.

Parameters:

  • params (Hash)

    a Hash of parameters to use during user creation

Returns:

  • (Boolean)

    true if the user is created, false otherwise



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nexus_cli/mixins/user_actions.rb', line 27

def create_user(params)
  response = nexus.post(nexus_url("service/local/users"), :body => create_user_json(params), :header => DEFAULT_CONTENT_TYPE_HEADER)
  case response.status
  when 201
    return true
  when 400
    raise CreateUserException.new(response.content)
  else
    raise UnexpectedStatusCodeException.new(reponse.code)
  end
end

#delete_user(user_id) ⇒ Boolean

Deletes the Nexus user with the given id.

Parameters:

  • user_id (String)

    the Nexus user to delete

Returns:

  • (Boolean)

    true if the user is deleted, false otherwise



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/nexus_cli/mixins/user_actions.rb', line 103

def delete_user(user_id)
  response = nexus.delete(nexus_url("service/local/users/#{user_id}"))
  case response.status
  when 204
    return true
  when 404
    raise UserNotFoundException.new(user_id)
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#get_user(user) ⇒ Hash

Gets a user

Parameters:

  • user (String)

    the name of the user to get

Returns:

  • (Hash)

    a parsed Ruby object representing the user’s JSON



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nexus_cli/mixins/user_actions.rb', line 69

def get_user(user)
  response = nexus.get(nexus_url("service/local/users/#{user}"), :header => DEFAULT_ACCEPT_HEADER)
  case response.status
  when 200
    return JSON.parse(response.content)
  when 404
    raise UserNotFoundException.new(user)
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#get_usersString

Gets information about the current Nexus users.

Returns:

  • (String)

    a String of XML with data about Nexus users



12
13
14
15
16
17
18
19
20
# File 'lib/nexus_cli/mixins/user_actions.rb', line 12

def get_users
  response = nexus.get(nexus_url("service/local/users"))
  case response.status
  when 200
    return response.content
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end

#update_user(params) ⇒ Boolean

Updates a user by changing parts of that user’s data.

Parameters:

  • params (Hash)

    a Hash of parameters to update

Returns:

  • (Boolean)

    true if the user is updated, false otherwise



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nexus_cli/mixins/user_actions.rb', line 44

def update_user(params)
  params[:roles] = [] if params[:roles] == [""]
  user_json = get_user(params[:userId])

  modified_json = JsonPath.for(user_json)
  params.each do |key, value|
    modified_json.gsub!("$..#{key}"){|v| value} unless key == "userId" || value.blank?
  end

  response = nexus.put(nexus_url("service/local/users/#{params[:userId]}"), :body => JSON.dump(modified_json.to_hash), :header => DEFAULT_CONTENT_TYPE_HEADER)
  case response.status
  when 200
    return true
  when 400
    raise UpdateUserException.new(response.content)
  else
    raise UnexpectedStatusCodeException.new(response.status)
  end
end