// Copyright Epic Games, Inc. All Rights Reserved.
using System.Security.Claims;
using EpicGames.Horde.Jobs;
using EpicGames.Horde.Jobs.Bisect;
using EpicGames.Horde.Users;
using HordeServer.Utilities;
using MongoDB.Bson;
namespace HordeServer.Users
{
///
/// Manages user documents
///
public interface IUserCollection
{
///
/// Gets a user by unique id
///
/// Id of the user
/// Cancellation token for the operation
/// The user information
Task GetUserAsync(UserId id, CancellationToken cancellationToken = default);
///
/// Gets a cached user by unique id
///
/// Id of the user
/// Cancellation token for the operation
/// The user information
ValueTask GetCachedUserAsync(UserId? id, CancellationToken cancellationToken = default);
///
/// Gets a user by unique id
///
/// Ids of the users
/// Name regex to match for the users
/// Maximum number of results
/// Number of results to return
/// Cancellation token for the operation
/// The user information
Task> FindUsersAsync(IEnumerable? ids = null, string? nameRegex = null, int? index = null, int? count = null, CancellationToken cancellationToken = default);
///
/// Gets a user by login
///
/// Login for the user
/// Cancellation token for the operation
/// The user information
Task FindUserByLoginAsync(string login, CancellationToken cancellationToken = default);
///
/// Gets a user by email address
///
/// Email for the user
/// Cancellation token for the operation
/// The user information
Task FindUserByEmailAsync(string email, CancellationToken cancellationToken = default);
///
/// Find or add a user with the given claims. Claims will be updated if the user exists.
///
/// Login id of the user
/// Full name of the user
/// Email address of the user
/// Cancellation token for the operation
/// The user document
Task FindOrAddUserByLoginAsync(string login, string? name = null, string? email = null, CancellationToken cancellationToken = default);
///
/// Gets the claims for a user
///
///
/// Cancellation token for the operation
///
Task GetClaimsAsync(UserId userId, CancellationToken cancellationToken = default);
///
/// Update the claims for a user
///
///
///
/// Cancellation token for the operation
///
Task UpdateClaimsAsync(UserId userId, IEnumerable claims, CancellationToken cancellationToken = default);
///
/// Get settings for a user
///
///
/// Cancellation token for the operation
///
Task GetSettingsAsync(UserId userId, CancellationToken cancellationToken = default);
///
/// Update a user
///
/// The user to update
///
///
/// Opaque settings object for the dashboard
///
///
///
///
///
/// Cancellation token for the operation
/// Updated user object
Task UpdateSettingsAsync(UserId userId, bool? enableExperimentalFeatures = null, bool? alwaysTagPreflightCL = null, BsonValue? dashboardSettings = null, IEnumerable? addPinnedJobIds = null, IEnumerable? removePinnedJobIds = null, UpdateUserJobTemplateOptions? templateOptions = null, IEnumerable? addBisectTaskIds = null, IEnumerable? removeBisectTaskIds = null, CancellationToken cancellationToken = default);
}
///
/// Extension methods for the user collect
///
public static class UserCollectionExtensions
{
///
/// Gets a particular user info from the collection
///
///
///
/// Cancellation token for the operation
///
public static Task GetUserAsync(this IUserCollection userCollection, ClaimsPrincipal principal, CancellationToken cancellationToken = default)
{
UserId? userId = principal.GetUserId();
if (userId != null)
{
return userCollection.GetUserAsync(userId.Value, cancellationToken);
}
else
{
return Task.FromResult(null);
}
}
}
}