Class: ChefZero::Endpoints::OrganizationUserEndpoint

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/organization_user_endpoint.rb

Overview

/organizations/ORG/users/NAME

Instance Attribute Summary

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestBase

#accepts?, #already_json_response, #build_uri, build_uri, #call, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #initialize, #json_only, #json_response, #list_data, #populate_defaults, #set_data, #text_response

Constructor Details

This class inherits a constructor from ChefZero::RestBase

Instance Method Details

#delete(request) ⇒ Object


16
17
18
19
20
21
# File 'lib/chef_zero/endpoints/organization_user_endpoint.rb', line 16

def delete(request)
  user = get_data(request)
  delete_data(request)
  user = FFI_Yajl::Parser.parse(user, :create_additions => false)
  json_response(200, ChefData::DataNormalizer.normalize_user(user, request.rest_path[3], ['username'], server.options[:osc_compat]))
end

#get(request) ⇒ Object


8
9
10
11
12
13
14
# File 'lib/chef_zero/endpoints/organization_user_endpoint.rb', line 8

def get(request)
  username = request.rest_path[3]
  get_data(request) # 404 if user is not in org
  user = get_data(request, [ 'users', username ])
  user = FFI_Yajl::Parser.parse(user, :create_additions => false)
  json_response(200, ChefData::DataNormalizer.normalize_user(user, username, ['username'], server.options[:osc_compat], request.method))
end

#post(request) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef_zero/endpoints/organization_user_endpoint.rb', line 23

def post(request)
  orgname = request.rest_path[1]
  username = request.rest_path[3]

  users = get_data(request, [ 'organizations', orgname, 'groups', 'users' ])
  users = FFI_Yajl::Parser.parse(users, :create_additions => false)

  create_data(request, [ 'organizations', orgname, 'users' ], username, '{}')

  # /organizations/ORG/association_requests/USERNAME-ORG
  begin
    delete_data(request, [ 'organizations', orgname, 'association_requests', username], :data_store_exceptions)
  rescue DataStore::DataNotFoundError
  end

  # Add the user to the users group if it isn't already there
  if !users['users'] || !users['users'].include?(username)
    users['users'] ||= []
    users['users'] |= [ username ]
    set_data(request, [ 'organizations', orgname, 'groups', 'users' ], FFI_Yajl::Encoder.encode(users, :pretty => true))
  end
  json_response(200, {})
end