aboutsummaryrefslogtreecommitdiff
path: root/Plugins.Essentials/src/Users/IUserManager.cs
blob: dd521e43cebdb1b1baa34b1cfaf2e8972715f1f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials
* File: IUserManager.cs 
*
* IUserManager.cs is part of VNLib.Plugins.Essentials which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials is free software: you can redistribute it and/or modify 
* it under the terms of the GNU Affero General Public License as 
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Essentials is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program.  If not, see https://www.gnu.org/licenses/.
*/

using System;
using System.Threading;
using System.Threading.Tasks;

using VNLib.Utils;
using VNLib.Utils.Memory;

#nullable enable

namespace VNLib.Plugins.Essentials.Users
{
    /// <summary>
    /// A backing store that provides user accounts
    /// </summary>
    public interface IUserManager
    {
        /// <summary>
        /// Attempts to get a user object without their password from the database asynchronously
        /// </summary>
        /// <param name="userId">The id of the user</param>
        /// <param name="cancellationToken">A token to cancel the operation</param>
        /// <returns>The user's <see cref="IUser"/> object, null if the user was not found</returns>
        /// <exception cref="ArgumentNullException"></exception>
        Task<IUser?> GetUserFromIDAsync(string userId, CancellationToken cancellationToken = default);
        /// <summary>
        /// Attempts to get a user object without their password from the database asynchronously
        /// </summary>
        /// <param name="emailAddress">The user's email address</param>
        /// <param name="cancellationToken">A token to cancel the operation</param>
        /// <returns>The user's <see cref="IUser"/> object, null if the user was not found</returns>
        /// <exception cref="ArgumentNullException"></exception>
        Task<IUser?> GetUserFromEmailAsync(string emailAddress, CancellationToken cancellationToken = default);
        /// <summary>
        /// Attempts to get a user object with their password from the database on the current thread
        /// </summary>
        /// <param name="userid">The id of the user</param>
        /// <param name="cancellation">A token to cancel the operation</param>
        /// <returns>The user's <see cref="IUser"/> object, null if the user was not found</returns>
        /// <exception cref="ArgumentNullException"></exception>
        Task<IUser?> GetUserAndPassFromIDAsync(string userid, CancellationToken cancellation = default);
        /// <summary>
        /// Attempts to get a user object with their password from the database asynchronously
        /// </summary>
        /// <param name="emailAddress">The user's email address</param>
        /// <param name="cancellationToken">A token to cancel the operation</param>
        /// <returns>The user's <see cref="IUser"/> object, null if the user was not found</returns>
        /// <exception cref="ArgumentNullException"></exception>
        Task<IUser?> GetUserAndPassFromEmailAsync(string emailAddress, CancellationToken cancellationToken = default);
        /// <summary>
        /// Creates a new user in the current user's table and if successful returns the new user object (without password)
        /// </summary>
        /// <param name="userid">The user id</param>
        /// <param name="privilages">A number representing the privilage level of the account</param>
        /// <param name="passHash">Value to store in the password field</param>
        /// <param name="cancellation">A token to cancel the operation</param>
        /// <param name="emailAddress">The account email address</param>
        /// <returns>An object representing a user's account if successful, null otherwise</returns>
        /// <exception cref="UserExistsException"></exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="UserCreationFailedException"></exception>
        Task<IUser> CreateUserAsync(string userid, string emailAddress, ulong privilages, PrivateString passHash, CancellationToken cancellation = default);
        /// <summary>
        /// Updates a password associated with the specified user. If the update fails, the transaction
        /// is rolled back.
        /// </summary>
        /// <param name="user">The user account to update the password of</param>
        /// <param name="newPass">The new password to set</param>
        /// <param name="cancellation">A token to cancel the operation</param>
        /// <returns>The result of the operation, the result should be 1 (aka true)</returns>
        Task<ERRNO> UpdatePassAsync(IUser user, PrivateString newPass, CancellationToken cancellation = default);

        /// <summary>
        /// Gets the number of entries in the current user table
        /// </summary>
        /// <param name="cancellation">A token to cancel the operation</param>
        /// <returns>The number of users in the table, or -1 if the operation failed</returns>
        Task<long> GetUserCountAsync(CancellationToken cancellation = default);
    }
}