aboutsummaryrefslogtreecommitdiff
path: root/Plugins.Essentials/src/Users
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins.Essentials/src/Users')
-rw-r--r--Plugins.Essentials/src/Users/IUser.cs74
-rw-r--r--Plugins.Essentials/src/Users/IUserManager.cs103
-rw-r--r--Plugins.Essentials/src/Users/UserCreationFailedException.cs47
-rw-r--r--Plugins.Essentials/src/Users/UserDeleteException.cs44
-rw-r--r--Plugins.Essentials/src/Users/UserExistsException.cs49
-rw-r--r--Plugins.Essentials/src/Users/UserStatus.cs50
-rw-r--r--Plugins.Essentials/src/Users/UserUpdateException.cs43
7 files changed, 410 insertions, 0 deletions
diff --git a/Plugins.Essentials/src/Users/IUser.cs b/Plugins.Essentials/src/Users/IUser.cs
new file mode 100644
index 0000000..28c5305
--- /dev/null
+++ b/Plugins.Essentials/src/Users/IUser.cs
@@ -0,0 +1,74 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: IUser.cs
+*
+* IUser.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.Collections.Generic;
+
+using VNLib.Utils;
+using VNLib.Utils.Async;
+using VNLib.Utils.Memory;
+
+#nullable enable
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ /// <summary>
+ /// Represents an abstract user account
+ /// </summary>
+ public interface IUser : IAsyncExclusiveResource, IDisposable, IObjectStorage, IEnumerable<KeyValuePair<string, string>>, IIndexable<string, string>
+ {
+ /// <summary>
+ /// The user's privilage level
+ /// </summary>
+ ulong Privilages { get; }
+ /// <summary>
+ /// The user's ID
+ /// </summary>
+ string UserID { get; }
+ /// <summary>
+ /// Date the user's account was created
+ /// </summary>
+ DateTimeOffset Created { get; }
+ /// <summary>
+ /// The user's password hash if retreived from the backing store, otherwise null
+ /// </summary>
+ PrivateString? PassHash { get; }
+ /// <summary>
+ /// Status of account
+ /// </summary>
+ UserStatus Status { get; set; }
+ /// <summary>
+ /// Is the account only usable from local network?
+ /// </summary>
+ bool LocalOnly { get; set; }
+ /// <summary>
+ /// The user's email address
+ /// </summary>
+ string EmailAddress { get; set; }
+ /// <summary>
+ /// Marks the user for deletion on release
+ /// </summary>
+ void Delete();
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/IUserManager.cs b/Plugins.Essentials/src/Users/IUserManager.cs
new file mode 100644
index 0000000..dd521e4
--- /dev/null
+++ b/Plugins.Essentials/src/Users/IUserManager.cs
@@ -0,0 +1,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);
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/UserCreationFailedException.cs b/Plugins.Essentials/src/Users/UserCreationFailedException.cs
new file mode 100644
index 0000000..9f509ac
--- /dev/null
+++ b/Plugins.Essentials/src/Users/UserCreationFailedException.cs
@@ -0,0 +1,47 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: UserCreationFailedException.cs
+*
+* UserCreationFailedException.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.Runtime.Serialization;
+using VNLib.Utils.Resources;
+
+#nullable enable
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ /// <summary>
+ /// Raised when a user creation operation has failed and could not be created
+ /// </summary>
+ public class UserCreationFailedException : ResourceUpdateFailedException
+ {
+ public UserCreationFailedException()
+ {}
+ public UserCreationFailedException(string message) : base(message)
+ {}
+ public UserCreationFailedException(string message, Exception innerException) : base(message, innerException)
+ {}
+ protected UserCreationFailedException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {}
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/UserDeleteException.cs b/Plugins.Essentials/src/Users/UserDeleteException.cs
new file mode 100644
index 0000000..cd26543
--- /dev/null
+++ b/Plugins.Essentials/src/Users/UserDeleteException.cs
@@ -0,0 +1,44 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: UserDeleteException.cs
+*
+* UserDeleteException.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 VNLib.Utils.Resources;
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ /// <summary>
+ /// Raised when a user flagged for deletion could not be deleted. See the <see cref="Exception.InnerException"/>
+ /// for the Exception that cause the opertion to fail
+ /// </summary>
+ public class UserDeleteException : ResourceDeleteFailedException
+ {
+ public UserDeleteException(string message, Exception cause) : base(message, cause) { }
+
+ public UserDeleteException()
+ {}
+
+ public UserDeleteException(string message) : base(message)
+ {}
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/UserExistsException.cs b/Plugins.Essentials/src/Users/UserExistsException.cs
new file mode 100644
index 0000000..5c63547
--- /dev/null
+++ b/Plugins.Essentials/src/Users/UserExistsException.cs
@@ -0,0 +1,49 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: UserExistsException.cs
+*
+* UserExistsException.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.Runtime.Serialization;
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ /// <summary>
+ /// Raised when an <see cref="IUserManager"/> operation
+ /// fails because the user account already exists
+ /// </summary>
+ public class UserExistsException : UserCreationFailedException
+ {
+ ///<inheritdoc/>
+ public UserExistsException()
+ {}
+ ///<inheritdoc/>
+ public UserExistsException(string message) : base(message)
+ {}
+ ///<inheritdoc/>
+ public UserExistsException(string message, Exception innerException) : base(message, innerException)
+ {}
+ ///<inheritdoc/>
+ protected UserExistsException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {}
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/UserStatus.cs b/Plugins.Essentials/src/Users/UserStatus.cs
new file mode 100644
index 0000000..32aa63d
--- /dev/null
+++ b/Plugins.Essentials/src/Users/UserStatus.cs
@@ -0,0 +1,50 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: UserStatus.cs
+*
+* UserStatus.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/.
+*/
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ public enum UserStatus
+ {
+ /// <summary>
+ /// Unverified account state
+ /// </summary>
+ Unverified,
+ /// <summary>
+ /// Active account state. The account is fully functional
+ /// </summary>
+ Active,
+ /// <summary>
+ /// The account is suspended
+ /// </summary>
+ Suspended,
+ /// <summary>
+ /// The account is inactive as marked by the system
+ /// </summary>
+ Inactive,
+ /// <summary>
+ /// The account has been locked from access
+ /// </summary>
+ Locked
+ }
+} \ No newline at end of file
diff --git a/Plugins.Essentials/src/Users/UserUpdateException.cs b/Plugins.Essentials/src/Users/UserUpdateException.cs
new file mode 100644
index 0000000..391bb05
--- /dev/null
+++ b/Plugins.Essentials/src/Users/UserUpdateException.cs
@@ -0,0 +1,43 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Plugins.Essentials
+* File: UserUpdateException.cs
+*
+* UserUpdateException.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 VNLib.Utils.Resources;
+
+namespace VNLib.Plugins.Essentials.Users
+{
+ /// <summary>
+ /// Raised when a user-data object was modified and an update operation failed
+ /// </summary>
+ public class UserUpdateException : ResourceUpdateFailedException
+ {
+ public UserUpdateException(string message, Exception cause) : base(message, cause) { }
+
+ public UserUpdateException()
+ {}
+
+ public UserUpdateException(string message) : base(message)
+ {}
+ }
+} \ No newline at end of file