Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding back logic to delete the association request when adding a user to an org (as well as adding the user to the groups) #160

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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