Module: Cyclid::API::Users::Document

Defined in:
app/cyclid/controllers/users/document.rb

Overview

API endpoints for a single Organization document

Users collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



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
89
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/cyclid/controllers/users/document.rb', line 64

def self.registered(app)
  include Errors::HTTPErrors

  # Get a specific user.
  app.get do
    authorized_as!(params[:username], Operations::READ)

    user = User.find_by(username: params[:username])
    halt_with_json_response(404, INVALID_USER, 'user does not exist') \
      if user.nil?

    Cyclid.logger.debug user.organizations

    # Convert to a Hash and inject the User data
    user_hash = user.serializable_hash
    user_hash['organizations'] = user.organizations.map(&:name)

    # DO provide the users HMAC secret, in this instance
    user_hash = sanitize_user(user_hash, ['password'])

    return user_hash.to_json
  end

  # Modify a specific user.
  app.put do
    authorized_as!(params[:username], Operations::WRITE)

    payload = parse_request_body
    Cyclid.logger.debug payload

    user = User.find_by(username: params[:username])
    halt_with_json_response(404, INVALID_USER, 'user does not exist') \
      if user.nil?

    begin
      user.name = payload['name'] if payload.key? 'name'
      user.email = payload['email'] if payload.key? 'email'
      user.password = payload['password'] if payload.key? 'password'
      user.secret = payload['secret'] if payload.key? 'secret'
      user.new_password = payload['new_password'] if payload.key? 'new_password'
      user.save!
    rescue ActiveRecord::ActiveRecordError => ex
      Cyclid.logger.debug ex.message
      halt_with_json_response(400, INVALID_JSON, ex.message)
    end

    return json_response(NO_ERROR, "user #{payload['username']} modified")
  end

  # Delete a specific user.
  app.delete do
    authorized_as!(params[:username], Operations::ADMIN)

    user = User.find_by(username: params[:username])
    halt_with_json_response(404, INVALID_USER, 'user does not exist') \
      if user.nil?

    begin
      user.delete
    rescue ActiveRecord::ActiveRecordError => ex
      Cyclid.logger.debug ex.message
      halt_with_json_response(400, INVALID_JSON, ex.message)
    end

    return json_response(NO_ERROR, "user #{params['username']} deleted")
  end
end

Instance Method Details

#DELETE(/users/: username) ⇒ 200, 404

Delete a specific user.

Parameters:

  • username (String)

    Username of the user.

Returns:

  • (200)

    User was deleted successfully

  • (404)

    The user does not exist



# File 'app/cyclid/controllers/users/document.rb', line 52

#GET(/users/: username) ⇒ Object

Get a specific user.

Parameters:

  • username (String)

    Username of the user.

Returns:

  • The requested user.

  • (404)

    The user does not exist



# File 'app/cyclid/controllers/users/document.rb', line 27

#PUT(/users/: username) ⇒ 200, ...

Modify a specific user.

Parameters:

  • username (String)

    Username of the user.

  • body (JSON)

    User information

Options Hash (body):

  • name (String)

    Users real name

  • email (String)

    Users new email address

  • password (String)

    New Bcrypt2 encrypted password

  • new_password (String)

    New password in plain text, which will be encrypted before being stored in the databaase.

  • secret (String)

    New HMAC signing secret. This should be a suitably long random string.

Returns:

  • (200)

    User was modified successfully

  • (400)

    The user definition is invalid

  • (404)

    The user does not exist



# File 'app/cyclid/controllers/users/document.rb', line 35