diff options
author | tyler-ball <tyleraball@gmail.com> | 2015-08-19 17:40:00 -0600 |
---|---|---|
committer | tyler-ball <tyleraball@gmail.com> | 2015-08-19 17:40:00 -0600 |
commit | 4223892e11e9c26cd2e37a254b8e0ca633993227 (patch) | |
tree | 147ef7115044c7decdefa4bf82a01a878bfb2877 /lib | |
parent | 34ade96c42d32943b0881069dfaf2d7f27856593 (diff) | |
download | chef-zero-4223892e11e9c26cd2e37a254b8e0ca633993227.tar.gz |
Adding back logic to delete the association request when adding a user to an org (as well as adding the user to the groups)tball/add_user_to_org
Diffstat (limited to 'lib')
3 files changed, 37 insertions, 16 deletions
diff --git a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb index e1b6a7e..72b0e4d 100644 --- a/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_association_requests_endpoint.rb @@ -6,7 +6,17 @@ module ChefZero # /organizations/ORG/association_requests class OrganizationAssociationRequestsEndpoint < RestBase def post(request) - ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'user') + json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) + username = json['user'] + orgname = request.rest_path[1] + id = "#{username}-#{orgname}" + + if exists_data?(request, [ 'organizations', orgname, 'users', username ]) + raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") + end + + create_data(request, request.rest_path, username, '{}') + json_response(201, { "uri" => build_uri(request.base_uri, request.rest_path + [ id ]) }) end def get(request) diff --git a/lib/chef_zero/endpoints/organization_user_base.rb b/lib/chef_zero/endpoints/organization_user_base.rb index aaa2e3a..d4ccf44 100644 --- a/lib/chef_zero/endpoints/organization_user_base.rb +++ b/lib/chef_zero/endpoints/organization_user_base.rb @@ -10,20 +10,6 @@ module ChefZero obj.json_response(200, result) end - def self.post(obj, request, key) - json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) - username = json[key] - orgname = request.rest_path[1] - id = "#{username}-#{orgname}" - - if obj.exists_data?(request, [ 'organizations', orgname, 'users', username ]) - raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") - end - - obj.create_data(request, request.rest_path, username, '{}') - obj.json_response(201, { "uri" => obj.build_uri(request.base_uri, request.rest_path + [ id ]) }) - end - end end end diff --git a/lib/chef_zero/endpoints/organization_users_endpoint.rb b/lib/chef_zero/endpoints/organization_users_endpoint.rb index e914820..861c670 100644 --- a/lib/chef_zero/endpoints/organization_users_endpoint.rb +++ b/lib/chef_zero/endpoints/organization_users_endpoint.rb @@ -7,7 +7,32 @@ module ChefZero # /organizations/ORG/users class OrganizationUsersEndpoint < RestBase def post(request) - ChefZero::Endpoints::OrganizationUserBase.post(self, request, 'username') + orgname = request.rest_path[1] + json = FFI_Yajl::Parser.parse(request.body, :create_additions => false) + username = json['username'] + + if exists_data?(request, [ 'organizations', orgname, 'users', username ]) + raise RestErrorResponse.new(409, "User #{username} is already in organization #{orgname}") + end + + users = get_data(request, [ 'organizations', orgname, 'groups', 'users' ]) + users = FFI_Yajl::Parser.parse(users, :create_additions => false) + + create_data(request, request.rest_path, 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(201, { "uri" => build_uri(request.base_uri, request.rest_path + [ username ]) }) end def get(request) |