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])
|
|
|
|
|
Improve the performance of the GET /:sources/:id/{access_requests,members} API endpoints
The performance was greatly improved by removing two N+1 queries issues
for each endpoint.
For comparison:
1. `GET /projects/:id/members`
With two N+1 queries issues (one was already fxed in the following
snippet):
```
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 5]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 22]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 16]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 13]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0 [["source_type",
"Project"], ["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
```
2. `GET /projects/:id/access_requests`
With two N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 8], ["source_type", "Project"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 24]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 23]]
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.1ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 24]]
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 23]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0
[["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(24, 23) ORDER BY "users"."id" DESC
```
Signed-off-by: Rémy Coutable <remy@rymai.me>
2016-08-09 06:20:09 -04:00
|
|
|
members = source.members.includes(:user)
|
2016-06-23 11:14:31 -04:00
|
|
|
members = members.joins(:user).merge(User.search(params[:query])) if params[:query]
|
Improve the performance of the GET /:sources/:id/{access_requests,members} API endpoints
The performance was greatly improved by removing two N+1 queries issues
for each endpoint.
For comparison:
1. `GET /projects/:id/members`
With two N+1 queries issues (one was already fxed in the following
snippet):
```
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 5]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 22]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 16]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 13]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0 [["source_type",
"Project"], ["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
```
2. `GET /projects/:id/access_requests`
With two N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 8], ["source_type", "Project"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 24]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 23]]
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.1ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 24]]
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 23]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0
[["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(24, 23) ORDER BY "users"."id" DESC
```
Signed-off-by: Rémy Coutable <remy@rymai.me>
2016-08-09 06:20:09 -04:00
|
|
|
members = paginate(members)
|
2016-06-23 11:14:31 -04:00
|
|
|
|
Improve the performance of the GET /:sources/:id/{access_requests,members} API endpoints
The performance was greatly improved by removing two N+1 queries issues
for each endpoint.
For comparison:
1. `GET /projects/:id/members`
With two N+1 queries issues (one was already fxed in the following
snippet):
```
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 5]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 22]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 16]]
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL AND "members"."user_id" = $4 ORDER BY "members"."id" DESC LIMIT 1
[["source_type", "Project"], ["source_id", 1], ["source_type",
"Project"], ["user_id", 13]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND "members"."requested_at" IS
NULL ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0 [["source_type",
"Project"], ["source_id", 1], ["source_type", "Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(5, 22, 16, 13) ORDER BY "users"."id" DESC
```
2. `GET /projects/:id/access_requests`
With two N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC [["source_type", "Project"],
["source_id", 8], ["source_type", "Project"]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 24]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" =
$1 ORDER BY "users"."id" DESC LIMIT 1 [["id", 23]]
ActiveRecord::SchemaMigration Load (0.2ms) SELECT
"schema_migrations".* FROM "schema_migrations"
ProjectMember Load (0.1ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 24]]
ProjectMember Load (0.2ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) AND "members"."user_id" = $4 ORDER BY "members"."id" DESC
LIMIT 1 [["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"], ["user_id", 23]]
```
Without the N+1 queries issues:
```
ProjectMember Load (0.3ms) SELECT "members".* FROM "members" WHERE
"members"."source_type" = $1 AND "members"."type" IN ('ProjectMember')
AND "members"."source_id" = $2 AND "members"."source_type" = $3 AND
"members"."type" IN ('ProjectMember') AND ("members"."requested_at" IS
NOT NULL) ORDER BY "members"."id" DESC LIMIT 20 OFFSET 0
[["source_type", "Project"], ["source_id", 8], ["source_type",
"Project"]]
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN
(24, 23) ORDER BY "users"."id" DESC
```
Signed-off-by: Rémy Coutable <remy@rymai.me>
2016-08-09 06:20:09 -04:00
|
|
|
present members.map(&:user), with: Entities::Member, members: members
|
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
|