Module: Cyclid::API::Organizations::Members

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

Overview

API endpoints for Organization members

Organizations collapse

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

Sinatra callback



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
131
132
133
134
135
136
137
138
139
140
141
# File 'app/cyclid/controllers/organizations/members.rb', line 67

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

  # Get the details of the specified user within the organization.
  app.get '/:username' 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?

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

    begin
      perms = user.userpermissions.find_by(organization: org)

      user_hash = user.serializable_hash
      user_hash.delete_if do |key, _value|
        key == 'password' || key == 'secret'
      end

      perms_hash = perms.serializable_hash
      perms_hash.delete_if do |key, _value|
        key == 'id' || key == 'user_id' || key == 'organization_id'
      end

      user_hash['permissions'] = perms_hash

      return user_hash.to_json
    rescue ActiveRecord::ActiveRecordError, \
           ActiveRecord::UnknownAttributeError => ex

      Cyclid.logger.debug ex.message
      halt_with_json_response(500, INTERNAL_ERROR, ex.message)
    end
  end

  # Modify the specified user within the organization.
  app.put '/:username' 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?

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

    begin
      perms = user.userpermissions.find_by(organization: org)

      payload_perms = payload['permissions'] if payload.key? 'permissions'
      unless payload_perms.nil?
        perms.admin = payload_perms['admin'] if payload_perms.key? 'admin'
        perms.write = payload_perms['write'] if payload_perms.key? 'write'
        perms.read = payload_perms['read'] if payload_perms.key? 'read'

        Cyclid.logger.debug perms.serializable_hash

        perms.save!
      end
    rescue ActiveRecord::ActiveRecordError, \
           ActiveRecord::UnknownAttributeError => ex

      Cyclid.logger.debug ex.message
      halt_with_json_response(500, INTERNAL_ERROR, ex.message)
    end
  end
end

Instance Method Details

#GET(/organizations/: organization/members/:username) ⇒ Object

Get the details of the specified user within the organization.

Examples:

Get the ‘user1’ user from the ‘example’ organization

GET /organizations/example/members/user1 => {"id": 1,
                                             "username": "user1",
                                             "email":"[email protected]",
                                             "permissions":{
                                               "admin":true,
                                               "write":true,
                                               "read":true
                                              }}

Parameters:

  • organization (String)

    Name of the organization.

  • username (String)

    Username of the member.

Returns:

  • The requested member.

  • (404)

    The organization or user does not exist, or the user is not a member of the organization.



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

#PUT(/organizations/: name/members/:username) ⇒ 200, 404

Modify the permissions of specified user within the organization.

Examples:

Give the member ‘user1’ write & read permissions for the ‘example’ organization

PUT /organizations/example/members/user1 <= {"permissions": {
                                               "admin":false,
                                               "write":true,
                                               "read":true
                                              }}

Parameters:

  • organization (String)

    Name of the organization.

  • username (String)

    Username of the member.

  • body (JSON)

    User permissions.

Returns:

  • (200)

    The member was modified successfully.

  • (404)

    The user does not exist, or is not a member of the organization.



# File 'app/cyclid/controllers/organizations/members.rb', line 46