public UserDetail GetByLoginName(string loginName) { loginName = loginName.ToLower(); var q = this.DbContext.Query <UserDetail>(); if (AceUtils.IsMobilePhone(loginName)) { q = q.Where(a => a.MobilePhone == loginName); } else if (AceUtils.IsEmail(loginName)) { q = q.Where(a => a.Email == loginName); } else { q = q.Where(a => a.AccountName == loginName); } return(q.FirstOrDefault()); }
public void Add(AddUserInput input) { this.Trim(input); input.Validate(); if (input.AccountName.IsNullOrEmpty() && input.MobilePhone.IsNullOrEmpty() && input.Email.IsNullOrEmpty()) { throw new InvalidInputException("用户名/手机号码/邮箱至少填一个"); } string accountName = null; if (input.AccountName.IsNotNullOrEmpty()) { accountName = input.AccountName.ToLower(); AceUtils.EnsureAccountNameLegal(accountName); bool exists = this.DbContext.Query <SysUser>().Where(a => a.AccountName == accountName).Any(); if (exists) { throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName)); } } string mobilePhone = null; if (input.MobilePhone.IsNotNullOrEmpty()) { mobilePhone = input.MobilePhone; if (AceUtils.IsMobilePhone(mobilePhone) == false) { throw new InvalidInputException("请输入正确的手机号码"); } bool exists = this.DbContext.Query <SysUser>().Where(a => a.MobilePhone == mobilePhone).Any(); if (exists) { throw new InvalidInputException("手机号码[{0}]已存在".ToFormat(mobilePhone)); } } string email = null; if (input.Email.IsNotNullOrEmpty()) { email = input.Email.ToLower(); if (AceUtils.IsEmail(email) == false) { throw new InvalidInputException("请输入正确的邮箱地址"); } bool exists = this.DbContext.Query <SysUser>().Where(a => a.Email == email).Any(); if (exists) { throw new InvalidInputException("邮箱地址[{0}]已存在".ToFormat(input.Email)); } } SysUser user = this.CreateEntity <SysUser>(null, input.CreatorId); user.AccountName = accountName; user.Name = input.Name; user.Gender = input.Gender; user.MobilePhone = mobilePhone; user.Birthday = input.Birthday; user.WeChat = input.WeChat; user.Email = email; user.Description = input.Description; user.State = AccountState.Normal; string userSecretkey = UserHelper.GenUserSecretkey(); string encryptedPassword = PasswordHelper.Encrypt(input.Password, userSecretkey); SysUserLogOn logOnEntity = new SysUserLogOn(); logOnEntity.Id = IdHelper.CreateStringSnowflakeId(); logOnEntity.UserId = user.Id; logOnEntity.UserSecretkey = userSecretkey; logOnEntity.UserPassword = encryptedPassword; List <string> roleIds = input.GetRoles(); List <SysUserRole> userRoles = roleIds.Select(a => { return(new SysUserRole() { Id = IdHelper.CreateStringSnowflakeId(), UserId = user.Id, RoleId = a, }); }).ToList(); user.RoleIds = string.Join(",", roleIds); List <string> orgIds = input.GetOrgs(); List <SysUserOrg> userOrgs = orgIds.Select(a => { return(new SysUserOrg() { Id = IdHelper.CreateStringSnowflakeId(), UserId = user.Id, OrgId = a, DisablePermission = false }); }).ToList(); user.OrgIds = string.Join(",", orgIds); List <string> postIds = input.GetPosts(); List <SysUserPost> userPosts = postIds.Select(a => { return(new SysUserPost() { Id = IdHelper.CreateStringSnowflakeId(), UserId = user.Id, PostId = a }); }).ToList(); user.PostIds = string.Join(",", postIds); this.DbContext.DoWithTransaction(() => { this.DbContext.Insert(user); this.DbContext.Insert(logOnEntity); this.DbContext.InsertRange(userRoles); this.DbContext.InsertRange(userOrgs); this.DbContext.InsertRange(userPosts); }); }
public void Update(UpdateUserInput input) { this.Trim(input); input.Validate(); SysUser user = this.Query.Where(a => a.Id == input.Id).AsTracking().First(); user.EnsureIsNotAdmin(); if (user.State == AccountState.Closed) { throw new InvalidInputException("无法修改已注销用户"); } string accountName = null; if (user.AccountName.IsNullOrEmpty()) { //用户名设置后不能修改 if (input.AccountName.IsNotNullOrEmpty()) { accountName = input.AccountName.ToLower(); AceUtils.EnsureAccountNameLegal(accountName); bool exists = this.DbContext.Query <SysUser>().Where(a => a.AccountName == accountName).Any(); if (exists) { throw new InvalidInputException("用户名[{0}]已存在".ToFormat(input.AccountName)); } } } else { accountName = user.AccountName; } string mobilePhone = null; if (user.MobilePhone.IsNotNullOrEmpty() && input.MobilePhone.IsNullOrEmpty()) { //手机号码设置后不能再改为空 throw new InvalidInputException("请输入手机号码"); } if (input.MobilePhone.IsNotNullOrEmpty()) { mobilePhone = input.MobilePhone; if (AceUtils.IsMobilePhone(mobilePhone) == false) { throw new InvalidInputException("请输入正确的手机号码"); } if (user.MobilePhone != mobilePhone)//不等说明手机号码有变 { bool exists = this.DbContext.Query <SysUser>().Where(a => a.MobilePhone == mobilePhone).Any(); if (exists) { throw new InvalidInputException("手机号码[{0}]已存在".ToFormat(mobilePhone)); } } } string email = null; if (user.Email.IsNotNullOrEmpty() && input.Email.IsNullOrEmpty()) { //邮箱地址设置后不能再改为空 throw new InvalidInputException("请输入邮箱地址"); } if (input.Email.IsNotNullOrEmpty()) { email = input.Email.ToLower(); if (AceUtils.IsEmail(email) == false) { throw new InvalidInputException("请输入正确的邮箱地址"); } if (user.Email != email)//不等说明邮箱有变 { bool exists = this.DbContext.Query <SysUser>().Where(a => a.Email == email).Any(); if (exists) { throw new InvalidInputException("邮箱地址[{0}]已存在".ToFormat(input.Email)); } } } user.AccountName = accountName; user.Name = input.Name; user.Gender = input.Gender; user.MobilePhone = mobilePhone; user.Birthday = input.Birthday; user.WeChat = input.WeChat; user.Email = email; user.Description = input.Description; List <string> roleIds = input.GetRoles(); List <SysUserRole> userRoles = this.DbContext.Query <SysUserRole>().Where(a => a.UserId == input.Id).ToList(); List <string> userRolesToDelete = userRoles.Where(a => !roleIds.Contains(a.Id)).Select(a => a.Id).ToList(); List <SysUserRole> userRolesToAdd = roleIds.Where(a => !userRoles.Any(r => r.Id == a)).Select(a => { return(new SysUserRole() { Id = IdHelper.CreateStringSnowflakeId(), UserId = input.Id, RoleId = a, }); }).ToList(); user.RoleIds = string.Join(",", roleIds); List <string> orgIds = input.GetOrgs(); List <SysUserOrg> userOrgs = this.DbContext.Query <SysUserOrg>().Where(a => a.UserId == input.Id).ToList(); List <string> userOrgsToDelete = userOrgs.Where(a => !orgIds.Contains(a.Id)).Select(a => a.Id).ToList(); List <SysUserOrg> userOrgsToAdd = orgIds.Where(a => !userOrgs.Any(r => r.Id == a)).Select(a => { return(new SysUserOrg() { Id = IdHelper.CreateStringSnowflakeId(), UserId = input.Id, OrgId = a, DisablePermission = false }); }).ToList(); user.OrgIds = string.Join(",", orgIds); List <string> postIds = input.GetPosts(); List <SysUserPost> userPosts = postIds.Select(a => { return(new SysUserPost() { Id = IdHelper.CreateStringSnowflakeId(), UserId = input.Id, PostId = a }); }).ToList(); user.PostIds = string.Join(",", postIds); this.DbContext.DoWithTransaction(() => { this.DbContext.Delete <SysUserRole>(a => a.Id.In(userRolesToDelete)); this.DbContext.InsertRange(userRolesToAdd); this.DbContext.Delete <SysUserOrg>(a => a.Id.In(userOrgsToDelete)); this.DbContext.InsertRange(userOrgsToAdd); this.DbContext.Delete <SysUserPost>(a => a.UserId == input.Id); this.DbContext.InsertRange(userPosts); this.DbContext.Update <SysUser>(user); }); }
/// <summary> /// /// </summary> /// <param name="loginName"></param> /// <param name="password">前端传过来的是经过md5加密后的密码</param> /// <param name="user"></param> /// <param name="msg"></param> /// <returns></returns> public bool CheckLogin(string loginName, string password, out SysUser user, out string msg) { user = null; msg = null; loginName.NotNullOrEmpty(); password.NotNullOrEmpty(); var view = this.DbContext.JoinQuery <SysUser, SysUserLogOn>((u, userLogOn) => new object[] { JoinType.InnerJoin, u.Id == userLogOn.UserId }) .Select((u, userLogOn) => new { User = u, UserLogOn = userLogOn }); loginName = loginName.ToLower(); if (AceUtils.IsMobilePhone(loginName)) { view = view.Where(a => a.User.MobilePhone == loginName); } else if (AceUtils.IsEmail(loginName)) { view = view.Where(a => a.User.Email == loginName); } else { view = view.Where(a => a.User.AccountName == loginName); } view = view.Where(a => a.User.State != AccountState.Closed); var viewEntity = view.FirstOrDefault(); if (viewEntity == null) { msg = "账户不存在,请重新输入"; return(false); } if (!viewEntity.User.IsAdmin()) { if (viewEntity.User.State == AccountState.Disabled) { msg = "账户被禁用,请联系管理员"; return(false); } } SysUser userEntity = viewEntity.User; SysUserLogOn userLogOnEntity = viewEntity.UserLogOn; string dbPassword = PasswordHelper.EncryptMD5Password(password, userLogOnEntity.UserSecretkey); if (dbPassword != userLogOnEntity.UserPassword) { msg = "密码不正确,请重新输入"; return(false); } DateTime lastVisitTime = DateTime.Now; this.DbContext.Update <SysUserLogOn>(a => a.Id == userLogOnEntity.Id, a => new SysUserLogOn() { LogOnCount = a.LogOnCount + 1, PreviousVisitTime = userLogOnEntity.LastVisitTime, LastVisitTime = lastVisitTime }); user = userEntity; return(true); }