Module: Cyclid::API::Users::Collection

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

Overview

API endpoints for the User collection

Users collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



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
# File 'app/cyclid/controllers/users/collection.rb', line 71

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

  # @macro [attach] sinatra.get
  #   @overload get "$1"
  # @method get_users
  # @return [String] JSON represention of all of all the users.
  # Get all of the users across all organizations.
  app.get do
    authorized_admin!(Operations::READ)

    # Retrieve the user data in a form we can more easily manipulate so
    # that we can sanitize it
    users = User.all_as_hash

    # Remove any sensitive data
    users.map! do |user|
      sanitize_user(user)
    end

    return users.to_json
  end

  # @macro [attach] sinatra.post
  #   @overload post "$1"
  # @method post_users
  # Create a new user.
  app.post do
    authorized_admin!(Operations::ADMIN)

    payload = parse_request_body
    Cyclid.logger.debug payload

    begin
      halt_with_json_response(409, \
                              DUPLICATE, \
                              'a user with that name already exists') \
      if User.exists?(username: payload['username'])

      user = User.new
      user.username = payload['username']
      user.email = payload['email']
      user.name = payload['name'] if payload.key? 'name'
      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, \
           ActiveRecord::UnknownAttributeError => ex

      Cyclid.logger.debug ex.message
      halt_with_json_response(400, INVALID_JSON, ex.message)
    end

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

Instance Method Details

#POST(/users) ⇒ 200, ...

Create a new user. Note that only one of ‘password’ or ‘new_password’ should be passed.

Examples:

Create a new user with an encrypted password

POST /users <= {"username": "user1",
                "email": "[email protected]",
                "password": "<Bcrypt2 encrypted password>"}

Parameters:

  • body (JSON)

    New user

Options Hash (body):

  • username (String)

    Username of the new user

  • name (String)

    Users real name

  • email (String)

    Users email address

  • password (String)

    Bcrypt2 encrypted password

  • new_password (String)

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

  • secret (String)

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

Returns:

  • (200)

    User was created successfully

  • (400)

    The user definition is invalid

  • (409)

    An user with that name already exists



# File 'app/cyclid/controllers/users/collection.rb', line 45

#GET(/users) ⇒ Object

Get all of the users.

Examples:

Get a list of users

GET /users => [{
                  "id": 1,
                  "username": "user1",
                  "email": "[email protected]"
                },
                {
                  "id": 2,
                  "username": "user2",
                  "email": "[email protected]"
                }]

Returns:

  • List of users

See Also:

  • get_users_user


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