aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/MyControllerBase.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-03-23 21:30:14 +0800
committercrupest <crupest@outlook.com>2022-03-23 21:30:31 +0800
commitda9139b7bab95f6e5ba5f4bb2d99011c2d6db03a (patch)
tree051fd4ca4bc511db7e04b019a33fddaab2d0cc6b /BackEnd/Timeline/Controllers/MyControllerBase.cs
parent3d6c9fd916e18c99b3a5497b8313672680571b5e (diff)
downloadtimeline-da9139b7bab95f6e5ba5f4bb2d99011c2d6db03a.tar.gz
timeline-da9139b7bab95f6e5ba5f4bb2d99011c2d6db03a.tar.bz2
timeline-da9139b7bab95f6e5ba5f4bb2d99011c2d6db03a.zip
Diffstat (limited to 'BackEnd/Timeline/Controllers/MyControllerBase.cs')
-rw-r--r--BackEnd/Timeline/Controllers/MyControllerBase.cs33
1 files changed, 21 insertions, 12 deletions
diff --git a/BackEnd/Timeline/Controllers/MyControllerBase.cs b/BackEnd/Timeline/Controllers/MyControllerBase.cs
index d4ee9d3e..b74193f4 100644
--- a/BackEnd/Timeline/Controllers/MyControllerBase.cs
+++ b/BackEnd/Timeline/Controllers/MyControllerBase.cs
@@ -1,8 +1,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.DependencyInjection;
using System;
+using System.Threading.Tasks;
using Timeline.Auth;
using Timeline.Models.Http;
+using Timeline.Services;
using Timeline.Services.User;
namespace Timeline.Controllers
@@ -15,24 +18,30 @@ namespace Timeline.Controllers
return User.HasPermission(permission);
}
- protected string? GetOptionalUsername()
+ protected long? GetOptionalAuthUserId()
{
- return User.GetOptionalName();
- }
-
- protected string GetUsername()
- {
- return GetOptionalUsername() ?? throw new InvalidOperationException(Resource.ExceptionNoUsername);
+ return User.GetOptionalUserId();
}
- protected long? GetOptionalUserId()
+ protected long GetAuthUserId()
{
- return User.GetOptionalUserId();
+ return GetOptionalAuthUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
}
- protected long GetUserId()
- {
- return GetOptionalUserId() ?? throw new InvalidOperationException(Resource.ExceptionNoUserId);
+ protected async Task<bool> CheckIsSelf(string username)
+ {
+ var authUserId = GetOptionalAuthUserId();
+ if (!authUserId.HasValue) return false;
+ try
+ {
+ var userService = HttpContext.RequestServices.GetRequiredService<IUserService>();
+ var id = await userService.GetUserIdByUsernameAsync(username);
+ return authUserId == id;
+ }
+ catch (EntityNotExistException)
+ {
+ return false;
+ }
}
#endregion auth