public async Task <UsuarioDTO> Insert(UsuarioDTO loggedUser, UsuarioDTO dto)
        {
            try
            {
                var tempPsw = Guid.NewGuid().ToString().Substring(0, 8);
                var entity  = _mapper.Map <Usuario>(dto);
                entity.UpdateDate   = entity.CreationDate = DateTime.Now;
                entity.UpdateUserId = entity.CreationUserId = loggedUser.Id;
                entity.Active       = true;
                entity.Contrasenia  = tempPsw.ToHash();
                entity = await _userRepository.Insert(entity);

                dto = _mapper.Map <UsuarioDTO>(entity);
                var nombre = dto.Nombre + ", " + dto.Apellido;
                var body   = $"Estimado {nombre}:" + Environment.NewLine +
                             "Le damos la bienvenida a Inforap" + Environment.NewLine +
                             "Se le ha asignado un usuario de acceso a Inforap"
                             + Environment.NewLine +
                             $"  - Su usuario es: {dto.Email}" + Environment.NewLine +
                             $"  - Su contraseña es: {tempPsw} ";
                _mailClient.Send(dto.Email, nombre, "Bienvenido a InfoRap", body);
                return(dto);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Ejemplo n.º 2
0
        public CommandResult <ChangeUserEmailCommand> Execute(ChangeUserEmailCommand command)
        {
            var result       = new CommandResult <ChangeUserEmailCommand>();
            var user         = _repository.GetActiveByEmail(command.CurrentEmail);
            var existingUser = _repository.TryGetByEmail(command.NewEmail);

            if (!user.Password.Verify(command.Password))
            {
                result.AddError(c => c.Password, "Invalid password");
            }

            if (existingUser != null)
            {
                result.AddError(c => c.NewEmail, "Email address already exists");
            }

            if (result.Success)
            {
                user.RequestEmailChange(command.NewEmail);
                _mailClient.Send(_template.GetMessage(new ConfirmEmailMailTemplateParameters(user.Email, user.NewEmailConfirmationCode.Value), user.NewEmail));
            }
            else
            {
                user.RequestEmailChangeFailed();
            }

            _repository.Update();

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <Unit> Handle(SendMailsCommand request, CancellationToken cancellationToken)
        {
            // TODO add batching
            var pendingMails = await _dbContext.Mails
                               .Where(m => m.Status == MailStatus.Pending)
                               .ToListAsync(cancellationToken: cancellationToken);

            foreach (var mail in pendingMails)
            {
                try
                {
                    await _mailClient.Send(mail.Sender, mail.Recipients, mail.Subject, mail.Body);

                    mail.Status = MailStatus.Sent;
                }
                catch (Exception)
                {
                    mail.Status = MailStatus.Failed;
                }
            }

            await _dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Ejemplo n.º 4
0
        public Task Handle(NoteDeleted notification, CancellationToken cancellationToken)
        {
            _mailClient.Send(new NoteDeletedMail(
                                 notification.Id
                                 ));

            return(Task.FromResult(true));
        }
Ejemplo n.º 5
0
        public Task Handle(NoteUpdated notification, CancellationToken cancellationToken)
        {
            _mailClient.Send(new NoteUpdatedMail(
                                 notification.Title,
                                 notification.Description
                                 ));

            return(Task.FromResult(true));
        }
Ejemplo n.º 6
0
        public void SendEmailReport(string subject, string message)
        {
            MailMessage email  = new MailMessage();
            var         sender = (_smtpClient.Credentials as NetworkCredential)?.UserName;

            email.From = new MailAddress(sender);
            _recipients.ToList().ForEach(r => email.To.Add(r));
            email.Subject = subject;
            email.Body    = message;
            _smtpClient.Send(email);
        }
Ejemplo n.º 7
0
        public CommandResult <SendUserActivationCommand> Execute(SendUserActivationCommand parameter)
        {
            var result = new CommandResult <SendUserActivationCommand>();
            var user   = _repository.TryGetByEmail(parameter.Email);

            // Don't return errors to prevent leaking information
            if (user != null && user.Status == UserStatus.New)
            {
                user.GenerateActivationCode();

                _repository.Update();

                _mailClient.Send(_template.GetMessage(new ActivationMailTemplateParameters(user.Email, user.ActivationCode.Value), user.Email));
            }

            return(result);
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> RecoveryPassword(string userName)
        {
            var user = await _usuarioService.GetBy(userName);

            if (user != null)
            {
                var nwPsw = Guid.NewGuid().ToString().Substring(0, 8);
                await _usuarioService.UpdatePassword(user.Id, nwPsw);

                var nombre = user.Nombre + ", " + user.Apellido;
                var body   = $"Estimado {nombre}:" + Environment.NewLine +
                             "Se ha cambiado la contraseña de acceso a Inforap"
                             + Environment.NewLine +
                             $"Su nueva contraseña es: {nwPsw} ";
                _mailClient.Send(user.Email, nombre, "Recuperación de contraseña", body);
            }
            return(Ok());
        }
        public CommandResult <RegisterUserCommand> Execute(RegisterUserCommand parameter)
        {
            var result = new CommandResult <RegisterUserCommand>();
            var user   = new User(parameter.Email, parameter.Password);

            if (_repository.TryGetByEmail(user.Email) != null)
            {
                result.AddError(p => p.Email, "Email address already exists");
            }

            if (result.Success)
            {
                _repository.Add(user);

                _mailClient.Send(_template.GetMessage(new ActivationMailTemplateParameters(user.Email, user.ActivationCode.Value), user.Email));
            }

            return(result);
        }
        public CommandResult <ForgotUserPasswordCommand> Execute(ForgotUserPasswordCommand parameter)
        {
            var result = new CommandResult <ForgotUserPasswordCommand>();
            var user   = _repository.TryGetByEmail(parameter.Email);

            // There is no error reporting to prevent information leaking
            if (user != null && user.Status == UserStatus.Active)
            {
                var token = user.GeneratePasswordResetToken();

                _mailClient.Send(_template.GetMessage(new PasswordResetMailTemplateParameters(user.Email, token), user.Email));
            }
            else
            {
                user?.PasswordResetRequestFailed();
            }

            _repository.Update();

            return(result);
        }
Ejemplo n.º 11
0
        public void Recover(AccountRecoveryView view)
        {
            Account account = UnitOfWork.Repository <Account>().SingleOrDefault(acc => acc.Email.ToLower() == view.Email.ToLower());

            if (account == null)
            {
                return;
            }

            account.RecoveryTokenExpirationDate = DateTime.Now.AddMinutes(30);
            account.RecoveryToken = Guid.NewGuid().ToString();

            UnitOfWork.Repository <Account>().Update(account);
            UnitOfWork.Commit();

            HttpRequest request           = HttpContext.Current.Request;
            UrlHelper   urlHelper         = new UrlHelper(request.RequestContext);
            String      url               = urlHelper.Action("Reset", "Auth", new { token = account.RecoveryToken }, request.Url.Scheme);
            String      recoveryEmailBody = String.Format(Messages.RecoveryEmailBody, url);

            mailClient.Send(account.Email, Messages.RecoveryEmailSubject, recoveryEmailBody);
        }
Ejemplo n.º 12
0
        public PasswordResetRequest IssuePasswordReset(User user, string subject, string content)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var mailContent = "";
            var reset       = new PasswordResetRequest();

            if (content == null)
            {
                reset = GenerateResetRequest(user);
                var resetLink = _baseUrl + "#/reset-password/" + HttpUtility.UrlEncode(reset.Hash);
                mailContent = "<p>Du har bedt om at få nulstillet dit password.</p>" +
                              "<p><a href='" + resetLink +
                              "'>Klik her for at nulstille passwordet for din KITOS profil</a>.</p>" +
                              "<p>Linket udløber om " + _ttl.TotalDays + " dage.</p>" +
                              "<p><a href='https://os2.eu/sites/default/files/documents/generelt_-_login_i_kitos_som_ny_bruger.pdf'>Klik her for at få Hjælp til log ind og brugerkonto</a></p>" +
                              "<p>Bemærk at denne mail ikke kan besvares.</p>";
            }
            var mailSubject = "Nulstilning af dit KITOS password" + _mailSuffix;

            var message = new MailMessage
            {
                Subject      = (subject ?? mailSubject).Replace('\r', ' ').Replace('\n', ' '),
                Body         = content ?? mailContent,
                IsBodyHtml   = true,
                BodyEncoding = Encoding.UTF8,
            };

            message.To.Add(user.Email);

            _mailClient.Send(message);

            return(reset);
        }