diff options
author | crupest <crupest@outlook.com> | 2020-11-15 20:54:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-15 20:54:33 +0800 |
commit | 515430ecc976008c50cf27a38dc869c2b1aa73c0 (patch) | |
tree | 945016b5c2a911a2a19b8f4f1472ecfdbe32a602 /BackEnd/Timeline/Services | |
parent | fa7b123be84afe020fc582535cc270e8cf24e85b (diff) | |
parent | 45873d9115840c9db596c2dffebc7bb29df13686 (diff) | |
download | timeline-515430ecc976008c50cf27a38dc869c2b1aa73c0.tar.gz timeline-515430ecc976008c50cf27a38dc869c2b1aa73c0.tar.bz2 timeline-515430ecc976008c50cf27a38dc869c2b1aa73c0.zip |
Merge pull request #188 from crupest/root-user
Forbid some operation on root user.
Diffstat (limited to 'BackEnd/Timeline/Services')
3 files changed, 27 insertions, 4 deletions
diff --git a/BackEnd/Timeline/Services/Exceptions/InvalidOperationOnRootUserException.cs b/BackEnd/Timeline/Services/Exceptions/InvalidOperationOnRootUserException.cs new file mode 100644 index 00000000..2bcab316 --- /dev/null +++ b/BackEnd/Timeline/Services/Exceptions/InvalidOperationOnRootUserException.cs @@ -0,0 +1,16 @@ +using System;
+
+namespace Timeline.Services.Exceptions
+{
+
+ [Serializable]
+ public class InvalidOperationOnRootUserException : InvalidOperationException
+ {
+ public InvalidOperationOnRootUserException() { }
+ public InvalidOperationOnRootUserException(string message) : base(message) { }
+ public InvalidOperationOnRootUserException(string message, Exception inner) : base(message, inner) { }
+ protected InvalidOperationOnRootUserException(
+ System.Runtime.Serialization.SerializationInfo info,
+ System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
+ }
+}
diff --git a/BackEnd/Timeline/Services/UserDeleteService.cs b/BackEnd/Timeline/Services/UserDeleteService.cs index b6306682..5365313b 100644 --- a/BackEnd/Timeline/Services/UserDeleteService.cs +++ b/BackEnd/Timeline/Services/UserDeleteService.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using Timeline.Entities;
using Timeline.Helpers;
using Timeline.Models.Validation;
+using Timeline.Services.Exceptions;
using static Timeline.Resources.Services.UserService;
namespace Timeline.Services
@@ -20,6 +21,7 @@ namespace Timeline.Services /// <returns>True if user is deleted, false if user not exist.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="username"/> is null.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="username"/> is of bad format.</exception>
+ /// <exception cref="InvalidOperationOnRootUserException">Thrown when deleting root user.</exception>
Task<bool> DeleteUser(string username);
}
@@ -54,6 +56,9 @@ namespace Timeline.Services if (user == null)
return false;
+ if (user.Id == 1)
+ throw new InvalidOperationOnRootUserException("Can't delete root user.");
+
await _timelineService.DeleteAllPostsOfUser(user.Id);
_databaseContext.Users.Remove(user);
diff --git a/BackEnd/Timeline/Services/UserPermissionService.cs b/BackEnd/Timeline/Services/UserPermissionService.cs index ff09b4ee..42c93283 100644 --- a/BackEnd/Timeline/Services/UserPermissionService.cs +++ b/BackEnd/Timeline/Services/UserPermissionService.cs @@ -127,6 +127,7 @@ namespace Timeline.Services /// <param name="userId">The id of the user.</param>
/// <param name="permission">The new permission.</param>
/// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
+ /// <exception cref="InvalidOperationOnRootUserException">Thrown when change root user's permission.</exception>
Task AddPermissionToUserAsync(long userId, UserPermission permission);
/// <summary>
@@ -136,6 +137,7 @@ namespace Timeline.Services /// <param name="permission">The permission.</param>
/// <param name="checkUserExistence">Whether check the user's existence.</param>
/// <exception cref="UserNotExistException">Thrown when <paramref name="checkUserExistence"/> is true and user does not exist.</exception>
+ /// <exception cref="InvalidOperationOnRootUserException">Thrown when change root user's permission.</exception>
Task RemovePermissionFromUserAsync(long userId, UserPermission permission, bool checkUserExistence = true);
}
@@ -176,8 +178,8 @@ namespace Timeline.Services public async Task AddPermissionToUserAsync(long userId, UserPermission permission)
{
- if (userId == 1) // The init administrator account.
- return;
+ if (userId == 1)
+ throw new InvalidOperationOnRootUserException("Can't change root user's permission.");
await CheckUserExistence(userId, true);
@@ -193,8 +195,8 @@ namespace Timeline.Services public async Task RemovePermissionFromUserAsync(long userId, UserPermission permission, bool checkUserExistence = true)
{
- if (userId == 1) // The init administrator account.
- return;
+ if (userId == 1)
+ throw new InvalidOperationOnRootUserException("Can't change root user's permission.");
await CheckUserExistence(userId, checkUserExistence);
|