/// <summary> /// Сохранить аккаунт /// </summary> /// <param name="Account"></param> /// <param name="Message"></param> /// <returns></returns> public static bool SaveAccount(ApplicationAccount Account, out string Message) { if (Account == null) { throw new ArgumentNullException(); } if (Account.Id == null) { return(CreateAccount(Account, out Message)); } else { return(UpdateAccount(Account, out Message)); } }
/// <summary> /// Обновление аккаунта /// </summary> /// <param name="Account"></param> /// <param name="Message"></param> /// <returns></returns> private static bool UpdateAccount(ApplicationAccount Account, out string Message) { if (Account == null) { throw new ArgumentNullException(); } var membershipUser = Membership.GetUser(Account.Id); membershipUser.Email = Account.Email; //сохранение пользователя Membership.UpdateUser(membershipUser); if (Account is ApplicationUser) { // изменение роли var curentRoles = Roles.GetRolesForUser(membershipUser.UserName); if (curentRoles.Length > 0) { Roles.RemoveUserFromRoles(membershipUser.UserName, curentRoles); } Roles.AddUserToRole(Account.Login, ((ApplicationAccountRoles)Account.IdRole).ToString()); } // изменение пароля if (!membershipUser.IsLockedOut && !string.IsNullOrEmpty(Account.Password)) { string oldPassword = membershipUser.GetPassword(); if (!oldPassword.Equals(Account.Password)) { try { membershipUser.ChangePassword(oldPassword, Account.Password); } catch (ArgumentException) { Message = OperationResult.BadPassword.GetDescription(); return(false); } }
/// <summary> /// Создание нового аккаунта /// </summary> /// <param name="Account"></param> /// <param name="Message"></param> /// <returns></returns> private static bool CreateAccount(ApplicationAccount Account, out string Message) { if (Account == null) { throw new ArgumentNullException(); } if (Account is ApplicationUser && (string.IsNullOrEmpty(Account.Login) || string.IsNullOrEmpty(Account.Password))) { Message = OperationResult.ArgumentsIsNull.GetDescription(); return(false); } if (Account is SalesPoint) { if (string.IsNullOrEmpty(Account.Login)) { Regex regex = new Regex("GS[0-9]{6}$"); var login_max_id = GetAccounts().Where(x => regex.IsMatch(x.Login)).OrderByDescending(x => x.Login).ToArray()[0].Login; Account.Login = $"GS{string.Format("{0:000000}", Convert.ToInt32(login_max_id.OnlyDigital()) + 1)}"; } if (string.IsNullOrEmpty(Account.Password)) { Account.Password = Membership.GeneratePassword(7, 1); } Account.IdRole = (int)ApplicationAccountRoles.Diler; } try { MembershipCreateStatus status; var NewAccount = Membership.CreateUser( Account.Login, Account.Password, Account.Email, !string.IsNullOrEmpty(Account.PasswordQuestion) ? Account.PasswordQuestion : "Yes", !string.IsNullOrEmpty(Account.PasswordAnswer) ? Account.PasswordAnswer : "No", true, out status); if (NewAccount == null || status != MembershipCreateStatus.Success) { Message = $"{OperationResult.CreateAccountError.GetDescription()} [{status}]"; return(false); } //установка роли Roles.AddUserToRole(Account.Login, Account.Role); //сохранение профиля if (!SaveProfile((int)NewAccount.ProviderUserKey, Account.Profile, out Message)) { return(false); } //блокирование аккаунта при необходимости if (Account.IsLockedOut.Equals((byte)ApplicationAccountStatus.locked)) { NewAccount.LockUser(); } Message = OperationResult.OK.GetDescription(); return(true); } catch (Exception e) { GoldenSIM.Core.Logs.RegisterError(Const.DatabaseConnectionString, MethodBase.GetCurrentMethod().DeclaringType, ref e, new StackTrace(false).GetFrame(0).GetMethod().Name); Message = OperationResult.InternalServerError.GetDescription(); return(false); } }