Skip to content

Commit

Permalink
Merge pull request #160 from chef/tball/add_user_to_org
Browse files Browse the repository at this point in the history
Adding back logic to delete the association request when adding a user to an org (as well as adding the user to the groups)
  • Loading branch information
tyler-ball committed Aug 20, 2015
2 parents 34ade96 + 4223892 commit d08bed6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ module Endpoints
# /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)
Expand Down
14 changes: 0 additions & 14 deletions lib/chef_zero/endpoints/organization_user_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def self.get(obj, request, &block)
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
27 changes: 26 additions & 1 deletion lib/chef_zero/endpoints/organization_users_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,32 @@ module Endpoints
# /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)
Expand Down

0 comments on commit d08bed6

Please sign in to comment.