debian-mirror-gitlab/app/graphql/resolvers/users_resolver.rb

70 lines
2 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module Resolvers
class UsersResolver < BaseResolver
include Gitlab::Graphql::Authorize::AuthorizeResource
2021-01-29 00:20:46 +05:30
type Types::UserType.connection_type, null: true
2020-06-23 00:09:42 +05:30
description 'Find Users'
2021-10-27 15:23:28 +05:30
argument :ids, [GraphQL::Types::ID],
2020-06-23 00:09:42 +05:30
required: false,
2021-03-08 18:12:59 +05:30
description: 'List of user Global IDs.'
2020-06-23 00:09:42 +05:30
2021-10-27 15:23:28 +05:30
argument :usernames, [GraphQL::Types::String], required: false,
2021-03-08 18:12:59 +05:30
description: 'List of usernames.'
2020-06-23 00:09:42 +05:30
argument :sort, Types::SortEnum,
2021-03-08 18:12:59 +05:30
description: 'Sort users by this criteria.',
2020-06-23 00:09:42 +05:30
required: false,
2021-02-22 17:27:13 +05:30
default_value: :created_desc
2020-06-23 00:09:42 +05:30
2021-10-27 15:23:28 +05:30
argument :search, GraphQL::Types::String,
2021-01-29 00:20:46 +05:30
required: false,
description: "Query to search users by name, username, or primary email."
2021-10-27 15:23:28 +05:30
argument :admins, GraphQL::Types::Boolean,
2021-03-08 18:12:59 +05:30
required: false,
default_value: false,
description: 'Return only admin users.'
def resolve(ids: nil, usernames: nil, sort: nil, search: nil, admins: nil)
2020-06-23 00:09:42 +05:30
authorize!
2021-03-08 18:12:59 +05:30
::UsersFinder.new(context[:current_user], finder_params(ids, usernames, sort, search, admins)).execute
2020-06-23 00:09:42 +05:30
end
def ready?(**args)
args = { ids: nil, usernames: nil }.merge!(args)
return super if args.values.compact.blank?
2021-03-08 18:12:59 +05:30
if args[:usernames]&.present? && args[:ids]&.present?
2020-06-23 00:09:42 +05:30
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-03-08 18:12:59 +05:30
def finder_params(ids, usernames, sort, search, admins)
2020-06-23 00:09:42 +05:30
params = {}
params[:sort] = sort if sort
params[:username] = usernames if usernames
params[:id] = parse_gids(ids) if ids
2021-01-29 00:20:46 +05:30
params[:search] = search if search
2021-03-08 18:12:59 +05:30
params[:admins] = admins if admins
2020-06-23 00:09:42 +05:30
params
end
def parse_gids(gids)
gids.map { |gid| GitlabSchema.parse_gid(gid, expected_type: ::User).model_id }
end
end
end