/// <summary>
        /// Changes pass from reset key
        /// </summary>
        /// <param name="idUser"></param>
        /// <param name="newPass"></param>
        /// <param name="passResetToken"></param>
        /// <returns></returns>
        public static async Task <ChangePasswordFromResetKeyOutput> ChangePasswordFromResetKeyAsync(MapHiveIdentityUser idUser, string newPass, string passResetToken)
        {
            var output = new ChangePasswordFromResetKeyOutput();

            try
            {
                var userManager = MapHive.Core.Identity.UserManagerUtils.GetUserManager();

                if (idUser != null)
                {
                    if (await userManager.CheckPasswordAsync(idUser, newPass))
                    {
                        output.FailureReason = "new_pass_same_as_old_pass";
                        output.Success       = false;
                    }
                    else
                    {
                        await userManager.ResetPasswordAsync(idUser, passResetToken, newPass);

                        output.Success = true;
                    }
                }
            }
            catch (Exception ex)
            {
                output.FailureReason = "unknown_error";
            }

            return(output);
        }
Exemple #2
0
        /// <summary>
        /// Creates a new user account in both Identity database and in the MapHive meta database;
        /// sends out a confirmation email if email account and template are provided
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dbCtx"></param>
        /// <param name="emailSender"></param>
        /// <param name="emailAccount"></param>
        /// <param name="emailTemplate"></param>
        /// <returns></returns>
        protected internal virtual async Task <T> CreateAsync <T>(DbContext dbCtx, IEmailSender emailSender, IEmailAccount emailAccount, IEmailTemplate emailTemplate)
            where T : Base
        {
            T output;

            //need to validate the model first
            await ValidateAsync(dbCtx);

            //make sure the email is ALWAYS lower case
            Email = Email.ToLower();

            //grab user manager
            var userManager = MapHive.Core.Identity.UserManagerUtils.GetUserManager();

            //check if the email is already used or not; throw validation feedback exception if so
            //Note - could do it in the mh meta, but both dbs must be in sync anyway
            var emailInUse = await userManager.FindByEmailAsync(Email) != null;

            if (emailInUse)
            {
                throw Validation.Utils.GenerateValidationFailedException(nameof(Email), ValidationErrors.EmailInUse);
            }

            try
            {
                var rndPass = Cartomatic.Utils.Crypto.Generator.GenerateRandomString(10);
                var idUser  = new MapHiveIdentityUser
                {
                    Id       = Guid.NewGuid(),
                    UserName = Email.ToLower(),
                    Email    = Email.ToLower()
                };
                var result = await userManager.CreateAsync(idUser, rndPass);

                //so can next pass some data to the mh meta user object
                this.Uuid = idUser.Id;

                //identity work done, so can create the user within the mh metadata db
                output = await base.CreateAsync <T>(dbCtx);


                var opFeedback = new Dictionary <string, object>
                {
                    { "InitialPassword", rndPass },
                    {
                        "VerificationKey",
                        Auth.MergeIdWithToken(
                            idUser.Id,
                            await userManager.GenerateEmailConfirmationTokenAsync(idUser)
                            )
                    }
                };

                //if email related objects have been provided, send the account created email
                if (emailAccount != null && emailTemplate != null)
                {
                    emailSender.Send(
                        emailAccount, emailTemplate.Prepare(opFeedback), Email
                        );
                }

                //finally the user created event
                UserCreated?.Invoke(
                    this,
                    new Events.OpFeedbackEventArgs
                {
                    OperationFeedback = opFeedback
                }
                    );
            }
            catch (Exception ex)
            {
                throw Validation.Utils.GenerateValidationFailedException(ex);
            }

            return(output);
        }