Module: Cyclid::API::Organizations::Document

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

Overview

API endpoints for a single Organization document

Organizations collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



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
131
# File 'app/cyclid/controllers/organizations/document.rb', line 68

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

  # Get a specific organization.
  app.get do
    authorized_for!(params[:name], Operations::READ)

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    # Base64 encode the public key
    public_key = Base64.strict_encode64(org.rsa_public_key)

    # Convert to a Hash, sanitize and inject the Users data & encoded
    # RSA key
    org_hash = sanitize_organization(org.serializable_hash)
    org_hash['users'] = org.users.map(&:username)
    org_hash['public_key'] = public_key

    return org_hash.to_json
  end

  # Modify a specific organization.
  app.put do
    authorized_for!(params[:name], Operations::WRITE)

    payload = parse_request_body
    Cyclid.logger.debug payload

    org = Organization.find_by(name: params[:name])
    halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
      if org.nil?

    begin
      # Change the owner email if one is provided
      org['owner_email'] = payload['owner_email'] if payload.key? 'owner_email'

      # Change the users if a list of users was provided
      if payload.key? 'users'
        # Add each provided user to the Organization
        org.users = payload['users'].map do |username|
          user = User.find_by(username: username)

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

          user
        end
      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 #{params['name']} updated")
  end
end

Instance Method Details

#GET(/organizations/: organization) ⇒ Object

Get a specific organization. The RSA public key is in Base64 encoded DER format, and can be used to encrypt secrets that can be decrypted only by the server.

Examples:

Get the ‘example’ organization

GET /organizations/example => [{"id": 1,
                                "name": "example",
                                "owner_email": "[email protected]",
                                "users": ["user1", "user2"],
                                "public_key": "<RSA public key>"}]

Parameters:

  • organization (String)

    Name of the organization.

Returns:

  • The organization object.

  • (404)

    The requested organization does not exist.

See Also:

  • get_organizations


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

#PUT(/organizations/: organization) ⇒ 200, 404

Modify an organization. The organizations name or public key can not be changed. If a list of users is provided, the current list will be replaced, so clients should first retrieve the full list of users, modify it, and then use this API to set the final list of users.

Examples:

Modify the ‘example’ organization to have user1 & user2 as members

POST /organizations/example <= {"users": ["user1", "user2"]}

Modify the ‘example’ organization to change the owner email

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

Parameters:

  • organization (String)

    Name of the organization.

  • body (JSON)

    New organization data.

Options Hash (body):

  • owner_email (String)

    Email address of the organization owner

  • users (Array<String>)

    List of users who are organization members.

Returns:

  • (200)

    The organization was changed successfully.

  • (404)

    The organization does not exist

  • (404)

    A user in the list of members does not exist



# File 'app/cyclid/controllers/organizations/document.rb', line 44