Module: Cyclid::API::Organizations::Collection

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

Overview

API endpoints for the Organization collection

Organizations collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



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
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/organizations/collection.rb', line 61

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

  # Get all of the organizations.
  app.get do
    authorized_admin!(Operations::READ)

    # Retrieve the organization data in a form we can more easily
    # manipulate so that we can sanitize it
    orgs = Organization.all_as_hash

    # Remove any sensitive data
    orgs.map! do |org|
      sanitize_organization(org)
    end

    return orgs.to_json
  end

  # Create a new organization.
  app.post do
    authorized_admin!(Operations::ADMIN)

    payload = parse_request_body
    Cyclid.logger.debug payload

    begin
      halt_with_json_response(409, \
                              DUPLICATE, \
                              'An organization with that name already exists') \
      if Organization.exists?(name: payload['name'])

      org = Organization.new
      org['name'] = payload['name']
      org['owner_email'] = payload['owner_email']

      # Generate an RSA key-pair and a Salt
      key = OpenSSL::PKey::RSA.new(RSA_KEY_LENGTH)

      org['rsa_private_key'] = key.to_der
      org['rsa_public_key'] = key.public_key.to_der

      org['salt'] = SecureRandom.hex(32)

      # Add each provided user to the Organization
      users = payload['users'] || []

      org.users = users.map do |username|
        user = User.find_by(username: username)

        halt_with_json_response(404, \
                                INVALID_USER, \
                                "user #{user} does not exist") \
        if user.nil?

        user
      end

      org.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, "organization #{payload['name']} created")
  end
end

Instance Method Details

#GET(/organizations) ⇒ Object

Get all of the organizations.

Examples:

Get a list of organizations

GET /organizations => [{"id": 1, "name": "example", "owner_email": "[email protected]"}]

Returns:

  • List of organizations

See Also:

  • get_organizations_organization


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

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

Create a new organization.

Examples:

Create a new organization with user1 & user2 as members

POST /organizations <= {"name": "example",
                        "owner_email": "[email protected]",
                        "users": ["user1", "user2"]}
                        ***

Create a new organization with no users as members

POST /organizations <= {"name": "example",
                        "owner_email": "[email protected]"}
                        ***

Parameters:

  • body (JSON)

    New organization

Options Hash (body):

  • name (String)

    Name of the new organization

  • owner_email (String)

    Email address of the organization owner

  • users (Array<String>) — default: []

    List of users to add to the organization

Returns:

  • (200)

    Organization was created successfully

  • (404)

    A user in the list of members does not exist

  • (409)

    An organization with that name already exists



# File 'app/cyclid/controllers/organizations/collection.rb', line 36