Esempio n. 1
0
        public bool SendPassword(string email, Guid id, string keySubject, string keyObject)
        {
            var result = true;

            try
            {
                var user = _fomMonitoringEntities.Set <Users>().AsNoTracking().FirstOrDefault(u => u.ID == id);
                if (user == null)
                {
                    return(false);
                }
                var subject = LocalizationService.GetResource(keySubject, new CultureInfo(user.Languages.DotNetCulture)) + " " + user.Username;
                var ruolo   = _fomMonitoringEntities.Set <Roles_Users>().FirstOrDefault()?.Roles.Description;
                var idRuolo = user.Roles_Users.First().Roles.IdRole;

                switch (idRuolo)
                {
                case 1:
                    ruolo = LocalizationService.GetResource($"Operator", new CultureInfo(user.Languages.DotNetCulture));
                    break;

                case 2:
                    ruolo = LocalizationService.GetResource($"HeadWorkshop", new CultureInfo(user.Languages.DotNetCulture));
                    break;
                }



                var firstPart = LocalizationService.GetResource($"{keyObject}_FirstPart", new CultureInfo(user.Languages.DotNetCulture))
                                .Replace("[TIPO_USER]", ruolo);

                var footer = LocalizationService.GetResource($"{keyObject}_Footer", new CultureInfo(user.Languages.DotNetCulture));

                var usernameLabel = LocalizationService.GetResource($"{keyObject}_UsernameLabel", new CultureInfo(user.Languages.DotNetCulture));

                var passwordLabel = LocalizationService.GetResource($"{keyObject}_PasswordLabel", new CultureInfo(user.Languages.DotNetCulture));

                var lastPart = LocalizationService.GetResource($"{keyObject}_LastPart", new CultureInfo(user.Languages.DotNetCulture))
                               .Replace("[TIPO_USER]", ruolo);

                var modelEmail = new Renderer.EmailChangedPasswordDto
                {
                    FirstPart     = firstPart,
                    Username      = user.Username,
                    Password      = _loginServices.DecryptPassword(user.Password),
                    FooterText    = footer,
                    UsernameLabel = usernameLabel,
                    PasswordLabel = passwordLabel,
                    LastPart      = lastPart
                };


                var body    = Renderer.RazorViewToString.RenderRazorEmailChangedPasswordViewToString(modelEmail);
                var message = new MailMessage(ApplicationSettingService.GetWebConfigKey("EmailFromAddress"),
                                              email, subject, body)
                {
                    IsBodyHtml = true
                };
                EmailSender.SendEmail(message);
                return(true);
            }
            catch (Exception ex)
            {
                var errMessage = ex.GetStringLog();
                LogService.WriteLog(errMessage, LogService.TypeLevel.Error, ex);
                result = false;
            }

            return(result);
        }