internal static int? GetPassword(IDataAccess dataAccess, string identity, string @namespace, out byte[] password, out byte[] passwordSalt, out bool isApproved, out bool isSuspended) { var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); comparer = GetUserIdentityComparer(identity, @namespace, comparer, entity); return GetPasswordCore(dataAccess, comparer, entity, out password, out passwordSalt, out isApproved, out isSuspended); }
internal static bool GetPassword(IDataAccess dataAccess, int userId, out byte[] password, out byte[] passwordSalt, out bool isApproved, out bool isSuspended) { var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); comparer = comparer.Compare(entity.UserId, "=", userId); return GetPasswordCore(dataAccess, comparer, entity, out password, out passwordSalt, out isApproved, out isSuspended) != 0; }
internal static User GetUser(IDataAccess dataAccess, int userId) { if(dataAccess == null) throw new ArgumentNullException("dataAccess"); var user = new UserEntity(); var oql = OQL.From(user).Select().Where(p => p.Property(user.UserId) == userId).End(); user = dataAccess.Find<UserEntity>(oql); return Mapper.Map<UserEntity, User>(user); }
public bool ResetPassword(string identity, string @namespace, string[] passwordAnswers, string newPassword = null) { if(string.IsNullOrWhiteSpace(identity) || passwordAnswers == null || passwordAnswers.Length < 3) return false; var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); var dataAccess = this.EnsureService<IDataAccess>(); comparer = Utility.GetUserIdentityComparer(identity, @namespace, comparer, entity); oql = comparer.OQL .Select(entity.UserId, entity.PasswordAnswer1, entity.PasswordAnswer2, entity.PasswordAnswer3) .Where(comparer) .End(); entity = dataAccess.Find<UserEntity>(oql); if(entity == null) return false; var userId = entity.UserId; var succeed = PasswordUtility.VerifyPassword(passwordAnswers[0], entity.PasswordAnswer1, this.GetPasswordAnswerSalt(userId, 1)) && PasswordUtility.VerifyPassword(passwordAnswers[1], entity.PasswordAnswer2, this.GetPasswordAnswerSalt(userId, 2)) && PasswordUtility.VerifyPassword(passwordAnswers[2], entity.PasswordAnswer3, this.GetPasswordAnswerSalt(userId, 3)); if(succeed && newPassword != null && newPassword.Length > 0) { //重新生成密码随机数 var passwordSalt = Zongsoft.Common.RandomGenerator.Generate(8); entity = new UserEntity { Password = PasswordUtility.HashPassword(newPassword, passwordSalt), PasswordSalt = passwordSalt }; // 更新密码 oql = OQL.From(entity) .Update(entity.Password, entity.PasswordSalt) .Where(p => p.Compare(entity.UserId, CompareOperator.Equal, userId)) .End(); return dataAccess.Execute(oql) > 0; } return succeed; }
public IEnumerable<User> GetAllUsers(string @namespace, int? pageIndex = null, int? pageSize = null) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity(); var oql = OQL.From(entity) .Select() .Where(p => p.Compare(entity.Namespace, "=", Utility.TrimNamespace(@namespace))) .OrderBy(p => p.Desc(entity.UserId)) .End(); if(pageIndex != null && pageSize != null) oql.Limit(pageSize.Value, pageIndex.Value); var entities = dataAccess.Select<UserEntity>(oql); return Mapper.Map<IEnumerable<UserEntity>, IEnumerable<User>>(entities); }
internal static Comparer GetUserIdentityComparer(string identity, string @namespace, Comparer comparer, UserEntity entity, out UserIdentityType identityType) { if(string.IsNullOrWhiteSpace(identity)) throw new ArgumentNullException("identity"); string text; // 默认条件 comparer = comparer & comparer.Compare(entity.Namespace, "=", TrimNamespace(@namespace)); if(Zongsoft.Text.TextRegular.Web.Email.IsMatch(identity, out text)) { identityType = UserIdentityType.Email; comparer = comparer & comparer.Compare(entity.Email, "=", text); } else if(Zongsoft.Text.TextRegular.Chinese.Cellphone.IsMatch(identity, out text)) { identityType = UserIdentityType.Phone; comparer = comparer & comparer.Compare(entity.PhoneNumber, "=", text); } else { identityType = UserIdentityType.Name; comparer = comparer & comparer.Compare(entity.Name, "=", text); } return comparer; }
public bool Suspend(int userId, bool suspended = true) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, Suspended = suspended, SuspendedTime = DateTime.Now }; var oql = OQL.From(entity).Update(entity.Suspended, entity.SuspendedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetPasswordQuestionsAndAnswers(int userId, string password, string[] passwordQuestions, string[] passwordAnswers) { if(passwordQuestions == null || passwordQuestions.Length < 3) throw new ArgumentNullException("passwordQuestions"); if(passwordAnswers == null || passwordAnswers.Length < 3) throw new ArgumentNullException("passwordAnswers"); if(passwordQuestions.Length != passwordAnswers.Length) throw new ArgumentException(); var dataAccess = this.EnsureService<IDataAccess>(); byte[] storedPassword; byte[] storedPasswordSalt; bool isApproved, isSuspended; if(!Utility.GetPassword(dataAccess, userId, out storedPassword, out storedPasswordSalt, out isApproved, out isSuspended)) return false; if(!PasswordUtility.VerifyPassword(password, storedPassword, storedPasswordSalt)) return false; var entity = new UserEntity { UserId = userId, PasswordQuestion1 = passwordQuestions.Length > 0 ? passwordQuestions[0] : null, PasswordAnswer1 = passwordAnswers.Length > 0 ? this.HashPasswordAnswer(passwordAnswers[0], userId, 1) : null, PasswordQuestion2 = passwordQuestions.Length > 1 ? passwordQuestions[1] : null, PasswordAnswer2 = passwordAnswers.Length > 1 ? this.HashPasswordAnswer(passwordAnswers[1], userId, 2) : null, PasswordQuestion3 = passwordQuestions.Length > 2 ? passwordQuestions[2] : null, PasswordAnswer3 = passwordAnswers.Length > 2 ? this.HashPasswordAnswer(passwordAnswers[2], userId, 3) : null }; return dataAccess.Update(entity) > 0; }
public bool SetPrincipalId(int userId, string principalId) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, PrincipalId = string.IsNullOrWhiteSpace(principalId) ? null : principalId.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.PrincipalId, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetFullName(int userId, string fullName) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, FullName = string.IsNullOrWhiteSpace(fullName) ? null : fullName.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.FullName, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetName(int userId, string name) { if(string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException("name"); //确保用户名是审核通过的 this.Censor(name); var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, Name = name.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.Name, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetEmail(int userId, string email) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, Email = string.IsNullOrWhiteSpace(email) ? null : email.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.Email, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetAvatar(int userId, string avatar) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, Avatar = string.IsNullOrWhiteSpace(avatar) ? null : avatar.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.Avatar, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool Exists(string identity, string @namespace) { if(string.IsNullOrWhiteSpace(identity)) return false; var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); var dataAccess = this.EnsureService<IDataAccess>(); Utility.UserIdentityType identityType; comparer = Utility.GetUserIdentityComparer(identity, @namespace, comparer, entity, out identityType); // 确保用户名是审核通过的 if(identityType == Utility.UserIdentityType.Name) this.Censor(identity); oql.Select(entity.UserId) .Where(comparer) .OrderBy(p => p.Desc(entity.UserId)) .End(); return dataAccess.Exists(oql); }
public bool Exists(int userId) { if(userId == 0) return true; var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity(){UserId = userId}; var oql = OQL.From(entity) .Select(entity.UserId) .Where(p => p.EqualValue(entity.UserId)) .OrderBy(p => p.Desc(entity.UserId)) .End(); return dataAccess.Exists(oql); }
public string[] GetPasswordQuestions(int userId) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity(); var oql = OQL.From(entity) .Select(entity.UserId, entity.PasswordQuestion1, entity.PasswordQuestion2, entity.PasswordQuestion3) .Where(p => p.Property(entity.UserId) == userId) .End(); entity = dataAccess.Find<UserEntity>(oql); if(entity == null) return null; var result = new string[] { entity.PasswordQuestion1, entity.PasswordQuestion2, entity.PasswordQuestion3, }; return result; }
public string[] GetPasswordQuestions(string identity, string @namespace) { var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); var dataAccess = this.EnsureService<IDataAccess>(); comparer = Utility.GetUserIdentityComparer(identity, @namespace, comparer, entity); oql = OQL.From(entity) .Select(entity.UserId, entity.PasswordQuestion1, entity.PasswordQuestion2, entity.PasswordQuestion3) .Where(comparer) .End(); entity = dataAccess.Find<UserEntity>(oql); if(entity == null) return null; var result = new string[] { entity.PasswordQuestion1, entity.PasswordQuestion2, entity.PasswordQuestion3, }; return result; }
public bool SetDescription(int userId, string description) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, Description = string.IsNullOrWhiteSpace(description) ? null : description.Trim(), ModifiedTime = DateTime.Now, }; var oql = OQL.From(entity).Update(entity.Description, entity.ModifiedTime).End(); return dataAccess.Execute(oql) > 0; }
public bool SetPasswordOptions(int userId, bool changePasswordOnFirstTime = false, byte maxInvalidPasswordAttempts = 3, byte minRequiredPasswordLength = 6, TimeSpan? passwordAttemptWindow = null, DateTime? passwordExpires = null) { var dataAccess = this.EnsureService<IDataAccess>(); var entity = new UserEntity { UserId = userId, ChangePasswordOnFirstTime = changePasswordOnFirstTime, MaxInvalidPasswordAttempts = maxInvalidPasswordAttempts, MinRequiredPasswordLength = minRequiredPasswordLength }; if(passwordAttemptWindow.HasValue) entity.PasswordAttemptWindow = passwordAttemptWindow.Value.Minutes; if(passwordExpires.HasValue) entity.PasswordExpires = passwordExpires.Value; return dataAccess.Update(entity) > 0; }
public int DeleteUsers(params int[] userIds) { if(userIds == null || userIds.Length < 1) return 0; var entity = new UserEntity(); var dataAccess = this.EnsureService<IDataAccess>(); var oql = OQL.From(entity).Delete().Where(p => p.Compare(entity.UserId, CompareOperator.In, userIds)).End(); return dataAccess.Execute(oql); }
internal static Comparer GetUserIdentityComparer(string identity, string @namespace, Comparer comparer, UserEntity entity) { UserIdentityType identityType; return GetUserIdentityComparer(identity, @namespace, comparer, entity, out identityType); }
public bool HasPassword(int userId) { var entity = new UserEntity(); var oql = new OQL(entity); var dataAccess = this.EnsureService<IDataAccess>(); oql.Select(entity.UserId) .Where(oql.Condition.And(entity.Password, "IS", "NOT NULL")) .OrderBy(p => p.Desc(entity.UserId)) .End(); return dataAccess.Exists(oql); }
private static int? GetPasswordCore(IDataAccess dataAccess, Comparer comparer, UserEntity entity, out byte[] password, out byte[] passwordSalt, out bool isApproved, out bool isSuspended) { if(dataAccess == null) throw new ArgumentNullException("dataAccess"); if(Object.Equals(comparer, null)) throw new ArgumentNullException("comparer"); password = null; passwordSalt = null; isApproved = false; isSuspended = false; var oql = comparer.OQL .Select(entity.UserId, entity.Password, entity.PasswordSalt, entity.Approved, entity.Suspended) .Where(comparer) .End(); entity = dataAccess.Find<UserEntity>(oql); if(entity == null) return null; password = entity.Password; passwordSalt = entity.PasswordSalt; isApproved = entity.Approved; isSuspended = entity.Suspended; return entity.UserId; }
public bool HasPassword(string identity, string @namespace) { var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); var dataAccess = this.EnsureService<IDataAccess>(); comparer = Utility.GetUserIdentityComparer(identity, @namespace, comparer, entity); comparer = comparer & comparer.IsNotNull(entity.Password); oql.Select(entity.UserId) .Where(comparer) .OrderBy(p => p.Desc(entity.UserId)) .End(); return dataAccess.Exists(oql); }
public bool ChangePassword(int userId, string oldPassword, string newPassword) { var dataAccess = this.EnsureService<IDataAccess>(); byte[] storedPassword; byte[] storedPasswordSalt; bool isApproved, isSuspended; if(!Utility.GetPassword(dataAccess, userId, out storedPassword, out storedPasswordSalt, out isApproved, out isSuspended)) return false; if(!PasswordUtility.VerifyPassword(oldPassword, storedPassword, storedPasswordSalt)) return false; // 重新生成密码随机数 storedPasswordSalt = Zongsoft.Common.RandomGenerator.Generate(8); var entity = new UserEntity(); entity.Password = PasswordUtility.HashPassword(newPassword, storedPasswordSalt); entity.PasswordSalt = storedPasswordSalt; // 更新密码 var oql = OQL.From(entity) .Update(entity.Password, entity.PasswordSalt) .Where(p => p.Compare(entity.UserId, CompareOperator.Equal, userId)) .End(); return dataAccess.Execute(oql) > 0; }
public bool ResetPassword(string identity, string @namespace, string secret, string newPassword = null) { if(string.IsNullOrWhiteSpace(identity) || string.IsNullOrWhiteSpace(secret)) return false; var userId = 0; var dataAccess = this.EnsureService<IDataAccess>(); if(!Utility.GetUserId(dataAccess, identity, @namespace, out userId)) return false; var cache = this.EnsureService<Zongsoft.Runtime.Caching.ICache>(); var cachedSecret = cache.GetValue(this.GetCacheKeyOfResetPassword(userId)) as string; var succeed = cachedSecret != null && string.Equals(cachedSecret, secret, StringComparison.Ordinal); if(succeed && newPassword != null && newPassword.Length > 0) { //重新生成密码随机数 var passwordSalt = Zongsoft.Common.RandomGenerator.Generate(8); var entity = new UserEntity { Password = PasswordUtility.HashPassword(newPassword, passwordSalt), PasswordSalt = passwordSalt }; // 更新密码 var oql = OQL.From(entity) .Update(entity.Password, entity.PasswordSalt) .Where(p => p.Compare(entity.UserId, CompareOperator.Equal, userId)) .End(); var affectedRows = dataAccess.Execute(oql); if(affectedRows > 0) cache.Remove(this.GetCacheKeyOfResetPassword(userId)); return affectedRows > 0; } return succeed; }
public static bool GetUserId(IDataAccess dataAccess, string identity, string @namespace, out int userId) { if(dataAccess == null) throw new ArgumentNullException("dataAccess"); if(string.IsNullOrWhiteSpace(identity)) throw new ArgumentNullException("identity"); var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); comparer = GetUserIdentityComparer(identity, @namespace, comparer, entity); oql = oql.Select(entity.UserId) .Where(comparer) .End(); entity = dataAccess.Find<UserEntity>(oql); var result = entity != null && entity.UserId > 0; userId = 0; if(result) userId = entity.UserId; return result; }
public User GetUser(string identity, string @namespace) { var entity = new UserEntity(); var oql = new OQL(entity); var comparer = new Comparer(oql); var dataAccess = this.EnsureService<IDataAccess>(); comparer = Utility.GetUserIdentityComparer(identity, @namespace, comparer, entity); entity = dataAccess.Find<UserEntity>(oql.Select().Where(comparer).End()); return Mapper.Map<UserEntity, User>(entity); }