Browse Source

UserProfile: Change my password

HOME 2 years ago
parent
commit
3daf5846cf

+ 13 - 0
VCommonCoreExample/AppServices/UserProfiles/Dto/ChangeMyPasswordInput.cs

@@ -0,0 +1,13 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace VCommonCoreExample.AppServices.UserProfiles.Dto
+{
+    public class ChangeMyPasswordInput
+    {
+        [Required]
+        public string OldPassword { get; set; }
+
+        [Required]
+        public string NewPassword { get; set; }
+    }
+}

+ 35 - 3
VCommonCoreExample/AppServices/UserProfiles/UserProfileService.cs

@@ -1,14 +1,46 @@
-using VCommon.VApplication.Authorization;
+using Microsoft.AspNetCore.Identity;
+using VCommon.VApplication.Authorization;
+using VCommon.VOpenApi.VAspNetCore;
+using VCommonCoreExample.AppServices.Basic;
+using VCommonCoreExample.AppServices.UserProfiles.Dto;
+using VCommonCoreExample.Caching;
+using VCommonCoreExample.Entity;
 
 namespace VCommonCoreExample.AppServices.UserProfiles
 {
     [VServiceAuthorize]
     public interface IUserProfileService
     {
-        //TODO: change my password
+        void ChangeMyPassword(ChangeMyPasswordInput input);
     }
 
-    public class UserProfileService : IUserProfileService
+    public class UserProfileService : DbTableAppServiceBase<User>, IUserProfileService
     {
+        private readonly AuthCache _authCache;
+        private readonly SessionCache _sessionCache;
+
+        public UserProfileService(AuthCache authCache, SessionCache sessionCache)
+        {
+            _authCache = authCache;
+            _sessionCache = sessionCache;
+        }
+
+        public void ChangeMyPassword(ChangeMyPasswordInput input)
+        {
+            using var repo = GetRepository();
+            var usr = repo.GetEntityOrDefault(VSession.GetUserId());
+            
+            var ph = new PasswordHasher<User>();
+            if (ph.VerifyHashedPassword(usr, usr.Password, input.OldPassword) == PasswordVerificationResult.Failed) throw new VFriendlyException("原始密码不正确");
+            
+            usr.Password = ph.HashPassword(usr, input.NewPassword);
+            repo.SaveChanges();
+
+            var scm = _sessionCache.Get(VSession.Token);
+            scm.HashedPassword = usr.Password;
+
+            _sessionCache.Set(VSession.Token, scm);
+            _authCache.ClearUser(usr.TenantId, usr.Id);
+        }
     }
 }

+ 10 - 10
VCommonCoreExample/DataStore/UserStore.cs

@@ -62,10 +62,10 @@ namespace VCommonCoreExample.DataStore
 
         public void ValidateToken(string token, out Guid? tenantId, out Guid userId)
         {
-            var c = _sessionCache.Get(token);
-            if (c == null) throw new VFriendlyException("会话已失效");
-            var ek = _sessionCache.GetKickSign(c.TenantId, c.UserId);
-            if (c.KickSign != ek)
+            var scm = _sessionCache.Get(token);
+            if (scm == null) throw new VFriendlyException("会话已失效");
+            var ks = _sessionCache.GetKickSign(scm.TenantId, scm.UserId);
+            if (scm.KickSign != ks)
             {
                 _sessionCache.Clear(token);
                 throw new VFriendlyException("会话已失效:用户已重新登录");
@@ -73,13 +73,13 @@ namespace VCommonCoreExample.DataStore
 
             //Check exist and tenant/user delete/disable/passChanged
 
-            if (c.TenantId.HasValue && true != _authCache.CheckTenantEnable(c.TenantId.Value))
+            if (scm.TenantId.HasValue && true != _authCache.CheckTenantEnable(scm.TenantId.Value))
             {
                 _sessionCache.Clear(token);
                 throw new VFriendlyException("会话已失效:租户不存在或未启用");
             }
 
-            var ucm = _authCache.GetUser(c.TenantId, c.UserId);
+            var ucm = _authCache.GetUser(scm.TenantId, scm.UserId);
             if (null == ucm)
             {
                 _sessionCache.Clear(token);
@@ -92,17 +92,17 @@ namespace VCommonCoreExample.DataStore
                 throw new VFriendlyException("会话已失效:用户未启用");
             }
 
-            if (c.HashedPassword != ucm.Password)
+            if (scm.HashedPassword != ucm.Password)
             {
                 _sessionCache.Clear(token);
                 throw new VFriendlyException("会话已失效:密码已变更");
             }
 
             // extend expire
-            _sessionCache.ExtendExpire(token, c.TenantId, c.UserId, c.Remember ? RememberMeSessionExpireDays : NormalSessionExpireDays);
+            _sessionCache.ExtendExpire(token, scm.TenantId, scm.UserId, scm.Remember ? RememberMeSessionExpireDays : NormalSessionExpireDays);
 
-            tenantId = c.TenantId;
-            userId = c.UserId;
+            tenantId = scm.TenantId;
+            userId = scm.UserId;
         }
     }
 }

+ 2 - 0
VCommonCoreExample/Startup.cs

@@ -18,6 +18,7 @@ using VCommonCoreExample.AppServices.Platform;
 using VCommonCoreExample.AppServices.Session;
 using VCommonCoreExample.AppServices.System.Roles;
 using VCommonCoreExample.AppServices.System.Users;
+using VCommonCoreExample.AppServices.UserProfiles;
 using VCommonCoreExample.Configuration;
 using VCommonCoreExample.EntityFrameworkCore;
 
@@ -77,6 +78,7 @@ namespace VCommonCoreExample
                 {"SystemUser",typeof(IUserService) },
                 {"SystemRole",typeof(IRoleService) },
                 {"PlatformTenant",typeof(ITenantService) },
+                {"UserProfile",typeof(IUserProfileService) },
             }, iocManager: rootContainer, docGen: dbg, isDebuggingEnabled: dbg);
 
             app.UseMiddleware<ApiMiddleware>();

+ 1 - 1
VCommonCoreExample/appsettings.json

@@ -1,6 +1,6 @@
 {
   "ConnectionStrings": {
-    "Default": "server=DevEnvMySQL;port=3308;uid=root;database=VCommonCoreExample"
+    "Default": "server=127.0.0.1;port=3308;uid=root;database=VCommonCoreExample"
   },
   "CacheServer": "127.0.0.1:6377"
 }