Module: Cyclid::API::Organizations::Collection
- Defined in:
- app/cyclid/controllers/organizations/collection.rb
Overview
API endpoints for the Organization collection
Organizations collapse
-
#GET(/organizations) ⇒ Object
Get all of the organizations.
-
#POST(/organizations) ⇒ 200, ...
Create a new organization.
Class Method Summary collapse
-
.registered(app) ⇒ Object
Sinatra callback.
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 (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 (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. halt_with_json_response(400, INVALID_JSON, ex.) end return json_response(NO_ERROR, "organization #{payload['name']} created") end end |
Instance Method Details
#GET(/organizations) ⇒ Object
Get all of the organizations.
|
# File 'app/cyclid/controllers/organizations/collection.rb', line 27
|
#POST(/organizations) ⇒ 200, ...
Create a new organization.
|
# File 'app/cyclid/controllers/organizations/collection.rb', line 36
|