2016-06-23 11:14:31 -04:00
|
|
|
module API
|
|
|
|
class Members < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
helpers ::API::Helpers::MembersHelpers
|
|
|
|
|
|
|
|
%w[group project].each do |source_type|
|
|
|
|
resource source_type.pluralize do
|
|
|
|
# Get a list of group/project members viewable by the authenticated user.
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The group/project ID
|
|
|
|
# query - Query string
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups/:id/members
|
|
|
|
# GET /projects/:id/members
|
|
|
|
get ":id/members" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
|
2016-09-15 12:34:57 -04:00
|
|
|
users = source.users
|
|
|
|
users = users.merge(User.search(params[:query])) if params[:query]
|
|
|
|
users = paginate(users)
|
2016-06-23 11:14:31 -04:00
|
|
|
|
2016-09-15 12:34:57 -04:00
|
|
|
present users, with: Entities::Member, source: source
|
2016-06-23 11:14:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get a group/project member
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The group/project ID
|
|
|
|
# user_id (required) - The user ID of the member
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# GET /groups/:id/members/:user_id
|
|
|
|
# GET /projects/:id/members/:user_id
|
|
|
|
get ":id/members/:user_id" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
|
|
|
|
members = source.members
|
|
|
|
member = members.find_by!(user_id: params[:user_id])
|
|
|
|
|
|
|
|
present member.user, with: Entities::Member, member: member
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add a new group/project member
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The group/project ID
|
|
|
|
# user_id (required) - The user ID of the new member
|
|
|
|
# access_level (required) - A valid access level
|
2016-08-18 12:01:50 -04:00
|
|
|
# expires_at (optional) - Date string in the format YEAR-MONTH-DAY
|
2016-06-23 11:14:31 -04:00
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# POST /groups/:id/members
|
|
|
|
# POST /projects/:id/members
|
|
|
|
post ":id/members" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
authorize_admin_source!(source_type, source)
|
|
|
|
required_attributes! [:user_id, :access_level]
|
|
|
|
|
|
|
|
access_requester = source.requesters.find_by(user_id: params[:user_id])
|
|
|
|
if access_requester
|
|
|
|
# We pass current_user = access_requester so that the requester doesn't
|
|
|
|
# receive a "access denied" email
|
|
|
|
::Members::DestroyService.new(access_requester, access_requester.user).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
member = source.members.find_by(user_id: params[:user_id])
|
2016-08-09 06:14:11 -04:00
|
|
|
|
|
|
|
# This is to ensure back-compatibility but 409 behavior should be used
|
|
|
|
# for both project and group members in 9.0!
|
|
|
|
conflict!('Member already exists') if source_type == 'group' && member
|
|
|
|
|
|
|
|
unless member
|
2016-08-18 12:01:50 -04:00
|
|
|
source.add_user(params[:user_id], params[:access_level], current_user: current_user, expires_at: params[:expires_at])
|
2016-08-09 06:14:11 -04:00
|
|
|
member = source.members.find_by(user_id: params[:user_id])
|
|
|
|
end
|
|
|
|
|
2016-06-23 11:14:31 -04:00
|
|
|
if member
|
|
|
|
present member.user, with: Entities::Member, member: member
|
|
|
|
else
|
2016-08-09 06:14:11 -04:00
|
|
|
# Since `source.add_user` doesn't return a member object, we have to
|
|
|
|
# build a new one and populate its errors in order to render them.
|
2016-08-18 12:01:50 -04:00
|
|
|
member = source.members.build(attributes_for_keys([:user_id, :access_level, :expires_at]))
|
2016-08-09 06:14:11 -04:00
|
|
|
member.valid? # populate the errors
|
|
|
|
|
|
|
|
# This is to ensure back-compatibility but 400 behavior should be used
|
|
|
|
# for all validation errors in 9.0!
|
|
|
|
render_api_error!('Access level is not known', 422) if member.errors.key?(:access_level)
|
|
|
|
render_validation_error!(member)
|
2016-06-23 11:14:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update a group/project member
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The group/project ID
|
|
|
|
# user_id (required) - The user ID of the member
|
|
|
|
# access_level (required) - A valid access level
|
2016-08-18 12:01:50 -04:00
|
|
|
# expires_at (optional) - Date string in the format YEAR-MONTH-DAY
|
2016-06-23 11:14:31 -04:00
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# PUT /groups/:id/members/:user_id
|
|
|
|
# PUT /projects/:id/members/:user_id
|
|
|
|
put ":id/members/:user_id" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
authorize_admin_source!(source_type, source)
|
|
|
|
required_attributes! [:user_id, :access_level]
|
|
|
|
|
|
|
|
member = source.members.find_by!(user_id: params[:user_id])
|
2016-08-18 12:01:50 -04:00
|
|
|
attrs = attributes_for_keys [:access_level, :expires_at]
|
2016-06-23 11:14:31 -04:00
|
|
|
|
2016-08-18 10:49:32 -04:00
|
|
|
if member.update_attributes(attrs)
|
2016-06-23 11:14:31 -04:00
|
|
|
present member.user, with: Entities::Member, member: member
|
|
|
|
else
|
2016-08-09 06:14:11 -04:00
|
|
|
# This is to ensure back-compatibility but 400 behavior should be used
|
|
|
|
# for all validation errors in 9.0!
|
|
|
|
render_api_error!('Access level is not known', 422) if member.errors.key?(:access_level)
|
2016-06-23 11:14:31 -04:00
|
|
|
render_validation_error!(member)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Remove a group/project member
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The group/project ID
|
|
|
|
# user_id (required) - The user ID of the member
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /groups/:id/members/:user_id
|
|
|
|
# DELETE /projects/:id/members/:user_id
|
|
|
|
delete ":id/members/:user_id" do
|
|
|
|
source = find_source(source_type, params[:id])
|
|
|
|
required_attributes! [:user_id]
|
|
|
|
|
2016-08-09 06:14:11 -04:00
|
|
|
# This is to ensure back-compatibility but find_by! should be used
|
|
|
|
# in that casse in 9.0!
|
|
|
|
member = source.members.find_by(user_id: params[:user_id])
|
|
|
|
|
|
|
|
# This is to ensure back-compatibility but this should be removed in
|
|
|
|
# favor of find_by! in 9.0!
|
|
|
|
not_found!("Member: user_id:#{params[:user_id]}") if source_type == 'group' && member.nil?
|
|
|
|
|
|
|
|
# This is to ensure back-compatibility but 204 behavior should be used
|
|
|
|
# for all DELETE endpoints in 9.0!
|
|
|
|
if member.nil?
|
|
|
|
{ message: "Access revoked", id: params[:user_id].to_i }
|
|
|
|
else
|
|
|
|
::Members::DestroyService.new(member, current_user).execute
|
2016-06-23 11:14:31 -04:00
|
|
|
|
2016-08-09 06:14:11 -04:00
|
|
|
present member.user, with: Entities::Member, member: member
|
|
|
|
end
|
2016-06-23 11:14:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|