| | |
| | | using MailKit.Search; |
| | | using OrderByType = SqlSugar.OrderByType; |
| | | using System.Drawing.Printing; |
| | | using WIDESEA_Model.Models.Config; |
| | | using WIDESEA_IBasicService; |
| | | //using WIDESEA_Core.HostedService; |
| | | |
| | | namespace WIDESEA_SystemService |
| | |
| | | private readonly ICacheService _cacheService; |
| | | private readonly ISys_MenuService _menuService; |
| | | private readonly ISys_RoleService _roleService; |
| | | private readonly IPasswordPolicyConfigService _passwordPolicyConfigService; |
| | | |
| | | public IRepository<Sys_User> Repository => BaseDal; |
| | | |
| | | public Sys_UserService(IRepository<Sys_User> repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService roleService) : base(repository) |
| | | public Sys_UserService(IRepository<Sys_User> repository, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService, ISys_MenuService menuService, ISys_RoleService roleService, IPasswordPolicyConfigService passwordPolicyConfigService) : base(repository) |
| | | { |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | _cacheService = cacheService; |
| | | _menuService = menuService; |
| | | _roleService = roleService; |
| | | _passwordPolicyConfigService = passwordPolicyConfigService; |
| | | } |
| | | |
| | | public WebResponseContent Login(LoginInfo loginInfo) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | PasswordPolicyConfig passwordPolicy = null; |
| | | string token = null; |
| | | try |
| | | { |
| | | //BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName); |
| | | |
| | | passwordPolicy = _passwordPolicyConfigService.GetConfigValue("") ?? new PasswordPolicyConfig(); |
| | | |
| | | string msg = string.Empty; |
| | | |
| | | #region 临æ¶ä½¿ç¨ |
| | |
| | | } |
| | | #endregion |
| | | |
| | | UserInfo user = BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password, x => new UserInfo { HeadImageUrl = x.HeadImageUrl, RoleId = x.RoleId, TenantId = x.TenantId, UserId = x.UserId, UserName = x.UserName, UserTrueName = x.UserTrueName }); |
| | | UserInfo user = BaseDal.QueryFirst(x => x.UserName == loginInfo.UserName && x.UserPwd == loginInfo.Password, x => new UserInfo { HeadImageUrl = x.HeadImageUrl, RoleId = x.RoleId, TenantId = x.TenantId, UserId = x.UserId, UserName = x.UserName, UserTrueName = x.UserTrueName,PwdLastModifyTime = x.LastModifyPwdDate }); |
| | | if (user != null) |
| | | { |
| | | // 3. å¯ç è¿æçç¥æ£æ¥ï¼ä»
å¯ç¨æ¶æ§è¡ï¼ |
| | | if (passwordPolicy.EnablePasswordExpire) |
| | | { |
| | | DateTime pwdModifyTime = user.PwdLastModifyTime ?? DateTime.Now.AddYears(-1); |
| | | TimeSpan passwordAge = DateTime.Now - pwdModifyTime; |
| | | int daysToExpire = passwordPolicy.PasswordExpireDays - (int)passwordAge.TotalDays; |
| | | |
| | | // å¯ç å·²è¿æï¼å¼ºå¶æ¹å¯ |
| | | if (daysToExpire <= 0) |
| | | { |
| | | return WebResponseContent.Instance.Error( |
| | | "æ¨çå¯ç å·²è¿æï¼è¯·å
ä¿®æ¹å¯ç ååç»å½", |
| | | data: new { needChangePwd = true, userId = user.UserId }); |
| | | } |
| | | |
| | | // å¯ç å³å°è¿æï¼ç»å½æåå¹¶æé |
| | | if (daysToExpire <= passwordPolicy.RemindBeforeExpireDays) |
| | | { |
| | | token = JwtHelper.IssueJwt(new TokenModelJwt() |
| | | { |
| | | UserId = user.UserId, |
| | | RoleId = user.RoleId, |
| | | UserName = user.UserName, |
| | | TenantId = user.TenantId, |
| | | }); |
| | | App.User.UpdateToke(token, user.UserId); |
| | | |
| | | content = WebResponseContent.Instance.OK( |
| | | message: $"æ¨çå¯ç å°å¨{daysToExpire}天åè¿æï¼è¯·åæ¶ä¿®æ¹", |
| | | data: new |
| | | { |
| | | token, |
| | | userName = user.UserName, |
| | | img = user.HeadImageUrl, |
| | | UserTrueName = user.UserTrueName, |
| | | needChangePwd = false, |
| | | pwdWillExpire = true, |
| | | daysToExpire = daysToExpire |
| | | }); |
| | | return content; |
| | | } |
| | | } |
| | | object obj = _menuService.GetMenuActionList(user.RoleId); |
| | | if (obj is not IEnumerable<object> list) |
| | | { |
| | |
| | | return WebResponseContent.Instance.Error("æ ç»å½æé"); |
| | | } |
| | | |
| | | string token = JwtHelper.IssueJwt(new TokenModelJwt() |
| | | token = JwtHelper.IssueJwt(new TokenModelJwt() |
| | | { |
| | | UserId = user.UserId, |
| | | RoleId = user.RoleId, |
| | |
| | | //if (PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == user.UserId) == null) |
| | | // PermissionDataHostService.UserRoles.AddRange(PermissionDataHostService.GetUserRoles(Db, user.UserId)); |
| | | |
| | | content = WebResponseContent.Instance.OK(data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName }); |
| | | content = WebResponseContent.Instance.OK(message: "ç»å
¥æå,æ£å¨è·³è½¬é¡µé¢", data: new { token, userName = user.UserName, img = user.HeadImageUrl, user.UserTrueName, needChangePwd = false, |
| | | pwdWillExpire = false, |
| | | daysToExpire = passwordPolicy.EnablePasswordExpire |
| | | ? passwordPolicy.PasswordExpireDays - (int)(DateTime.Now - (user.PwdLastModifyTime ?? DateTime.Now)).TotalDays |
| | | : 0 |
| | | }); |
| | | } |
| | | else |
| | | { |
| | |
| | | string pwd = "123456"; |
| | | string uesrName = saveModel.MainData[nameof(Sys_User.UserName).FirstLetterToLower()].ToString(); |
| | | saveModel.MainData[nameof(Sys_User.UserPwd).FirstLetterToLower()] = pwd.EncryptDES(AppSecret.User); |
| | | |
| | | string pwdModifyTimeField = nameof(Sys_User.LastModifyPwdDate).FirstLetterToLower(); |
| | | saveModel.MainData[pwdModifyTimeField] = DateTime.Now; |
| | | |
| | | WebResponseContent content = base.AddData(saveModel); |
| | | if (content.Status) |
| | |
| | | Sys_User user = BaseDal.QueryFirst(x => x.UserName == userName); |
| | | if (user == null) return WebResponseContent.Instance.Error("ç¨æ·ä¸åå¨"); |
| | | user.UserPwd = password.EncryptDES(AppSecret.User); |
| | | user.LastModifyPwdDate = DateTime.Now; |
| | | BaseDal.UpdateData(user); |
| | | if (App.User.UserId == user.UserId) |
| | | { |
| | |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | public WebResponseContent ModifyUserNamePwd(string userName,string oldPwd, string password) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | string message = ""; |
| | | // å»é¤é¦å°¾ç©ºæ ¼ï¼ç©ºå¼å¤ç |
| | | oldPwd = oldPwd?.Trim(); |
| | | password = password?.Trim(); |
| | | userName = userName?.Trim(); |
| | | |
| | | try |
| | | { |
| | | // 1. åºç¡åæ°æ ¡éª |
| | | if (string.IsNullOrEmpty(userName)) return WebResponseContent.Instance.Error("ç¨æ·åä¸è½ä¸ºç©º"); |
| | | if (string.IsNullOrEmpty(oldPwd)) return WebResponseContent.Instance.Error("æ§å¯ç ä¸è½ä¸ºç©º"); |
| | | if (string.IsNullOrEmpty(password)) return WebResponseContent.Instance.Error("æ°å¯ç ä¸è½ä¸ºç©º"); |
| | | if (oldPwd == password) return WebResponseContent.Instance.Error("æ°å¯ç ä¸è½ä¸æ§å¯ç ç¸å"); |
| | | if (password.Length < 6) return WebResponseContent.Instance.Error("æ°å¯ç é¿åº¦ä¸è½å°äº6ä½"); |
| | | |
| | | // 2. è·åç¨æ·ä¿¡æ¯ |
| | | Sys_User user = BaseDal.QueryFirst(x => x.UserName == userName); |
| | | if (user == null) return WebResponseContent.Instance.Error("ç¨æ·ä¸åå¨"); |
| | | |
| | | // 3. æ ¡éªæ§å¯ç ï¼è§£å¯å对æ¯ï¼ |
| | | string decryptedOldPwd = user.UserPwd.DecryptDES(AppSecret.User); // è§£å¯æ°æ®åºä¸çå¯ç |
| | | if (decryptedOldPwd != oldPwd) // 对æ¯åå§æ§å¯ç |
| | | { |
| | | return WebResponseContent.Instance.Error("æ§å¯ç è¾å
¥é误"); |
| | | } |
| | | |
| | | // 4. æ´æ°å¯ç åç¸å
³ä¿¡æ¯ |
| | | user.UserPwd = password.EncryptDES(AppSecret.User); // å 坿°å¯ç |
| | | user.LastModifyPwdDate = DateTime.Now; |
| | | BaseDal.UpdateData(user); |
| | | |
| | | // 5. 妿æ¯å½åç»å½ç¨æ·ï¼éæ°çæJWT Tokenå¹¶æ´æ°ç¼å |
| | | if (App.User.UserId == user.UserId) |
| | | { |
| | | string token = JwtHelper.IssueJwt(new TokenModelJwt() |
| | | { |
| | | UserId = user.UserId, |
| | | RoleId = user.RoleId, |
| | | UserName = user.UserName, |
| | | TenantId = user.TenantId, |
| | | }); |
| | | _cacheService.AddOrUpdate(user.UserId.ToString(), token); |
| | | } |
| | | |
| | | // 6. è¿åæåç»æ |
| | | return content.OK("å¯ç ä¿®æ¹æå"); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | message = ex.Message; // è®°å½å¼å¸¸ä¿¡æ¯ï¼å»ºè®®è¡¥å
æ¥å¿æ¡æ¶è®°å½ï¼ |
| | | content.Error("æå¡å¨åºäºç¹é®é¢,请ç¨ååè¯"); |
| | | } |
| | | return content; |
| | | } |
| | | } |
| | | } |