2020-06-08 20:08:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Resolvers
|
|
|
|
class UsersResolver < BaseResolver
|
|
|
|
include Gitlab::Graphql::Authorize::AuthorizeResource
|
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
type Types::UserType.connection_type, null: true
|
2020-06-08 20:08:47 -04:00
|
|
|
description 'Find Users'
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :ids, [GraphQL::Types::ID],
|
2020-06-08 20:08:47 -04:00
|
|
|
required: false,
|
2021-01-11 19:10:42 -05:00
|
|
|
description: 'List of user Global IDs.'
|
2020-06-08 20:08:47 -04:00
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :usernames, [GraphQL::Types::String], required: false,
|
2021-01-11 19:10:42 -05:00
|
|
|
description: 'List of usernames.'
|
2020-06-08 20:08:47 -04:00
|
|
|
|
|
|
|
argument :sort, Types::SortEnum,
|
2021-01-11 19:10:42 -05:00
|
|
|
description: 'Sort users by this criteria.',
|
2020-06-08 20:08:47 -04:00
|
|
|
required: false,
|
2020-12-01 22:09:38 -05:00
|
|
|
default_value: :created_desc
|
2020-06-08 20:08:47 -04:00
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :search, GraphQL::Types::String,
|
2020-11-03 13:09:22 -05:00
|
|
|
required: false,
|
|
|
|
description: "Query to search users by name, username, or primary email."
|
|
|
|
|
2021-07-22 08:10:04 -04:00
|
|
|
argument :admins, GraphQL::Types::Boolean,
|
2021-01-18 07:10:41 -05:00
|
|
|
required: false,
|
|
|
|
default_value: false,
|
|
|
|
description: 'Return only admin users.'
|
|
|
|
|
|
|
|
def resolve(ids: nil, usernames: nil, sort: nil, search: nil, admins: nil)
|
2020-06-08 20:08:47 -04:00
|
|
|
authorize!
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
::UsersFinder.new(context[:current_user], finder_params(ids, usernames, sort, search, admins)).execute
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready?(**args)
|
|
|
|
args = { ids: nil, usernames: nil }.merge!(args)
|
|
|
|
|
|
|
|
return super if args.values.compact.blank?
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
if args[:usernames]&.present? && args[:ids]&.present?
|
2020-06-08 20:08:47 -04:00
|
|
|
raise Gitlab::Graphql::Errors::ArgumentError, 'Provide either a list of usernames or ids'
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize!
|
|
|
|
Ability.allowed?(context[:current_user], :read_users_list) || raise_resource_not_available_error!
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
def finder_params(ids, usernames, sort, search, admins)
|
2020-06-08 20:08:47 -04:00
|
|
|
params = {}
|
|
|
|
params[:sort] = sort if sort
|
|
|
|
params[:username] = usernames if usernames
|
|
|
|
params[:id] = parse_gids(ids) if ids
|
2020-11-03 13:09:22 -05:00
|
|
|
params[:search] = search if search
|
2021-01-18 07:10:41 -05:00
|
|
|
params[:admins] = admins if admins
|
2020-06-08 20:08:47 -04:00
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_gids(gids)
|
|
|
|
gids.map { |gid| GitlabSchema.parse_gid(gid, expected_type: ::User).model_id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|