public async Task <IActionResult> ResetPassword(string username)
        {
            _logger.LogRequest("Requesting reset password token for user {username}", username);

            var user = await _userService.GetUser(username);

            if (user == null)
            {
                _logger.LogWarning("User {username} was not found.", username);
                return(Ok());
            }

            var token = await _userService.GetResetPasswordToken(user.Id);

            var originUrl = Request.Headers["Origin"];

            if (!string.IsNullOrEmpty(originUrl))
            {
                await _notificationProvider.SendNotification(new SendNotificationRequest
                {
                    MessageType = NotificationConfig.ResetPasswordWeb,
                    Emails      = new List <string>
                    {
                        user.Email
                    }
                }, new Dictionary <string, string>
                {
                    { MessageParameter.ResetPasswordLink, $"{originUrl}/reset-password?username={username}&token={HttpUtility.UrlEncode(token)}" }
                });
            }
            else
            {
                await _notificationProvider.SendNotification(new SendNotificationRequest
                {
                    MessageType = NotificationConfig.ResetPassword,
                    Emails      = new List <string>
                    {
                        user.Email
                    }
                }, new Dictionary <string, string>
                {
                    { MessageParameter.ResetPasswordToken, token }
                });
            }

            _logger.LogResponse("Password reset notification for user {username} sent", username);

            return(Ok());
        }
Esempio n. 2
0
        public async Task <IActionResult> RegisterUser(RegisterUserDto dto)
        {
            _logger.LogInformation("Registering user. Request body: {@dto}", dto);

            int userId = 0;

            try
            {
                var temporaryPassword = await _userService.GeneratePassword();

                var createdUser = await _userService.CreateUser(dto.Email, dto.FirstName, dto.LastName, temporaryPassword);

                if (createdUser != null)
                {
                    userId = createdUser.Id;

                    var token = await _userService.GenerateConfirmationToken(createdUser.Id);

                    string confirmToken = HttpUtility.UrlEncode(token);

                    // TODO: We might need to change the confirm url into the web UI url, when it's ready
                    var confirmUrl = $"{this.Request.Scheme}://{Request.Host}/account/{userId}/confirm?token={confirmToken}";
                    await _notificationProvider.SendNotification(new SendNotificationRequest
                    {
                        MessageType = NotificationConfig.RegistrationCompleted,
                        Emails      = new List <string>
                        {
                            dto.Email
                        }
                    }, new Dictionary <string, string>
                    {
                        { MessageParameter.ConfirmUrl, confirmUrl },
                        { MessageParameter.TemporaryPassword, temporaryPassword }
                    });
                }
            }
            catch (UserCreationFailedException uex)
            {
                _logger.LogWarning(uex, "User creation failed");
                return(BadRequest(uex.GetExceptionMessageList()));
            }

            var user = await _userService.GetUserById(userId);

            var result = _mapper.Map <UserDto>(user);

            return(Ok(result));
        }
Esempio n. 3
0
        public async Task SendNotification(int jobQueueId, string webUrl, CancellationToken cancellationToken = default(CancellationToken))
        {
            var jobQueueSpec = new JobQueueFilterSpecification(0, jobQueueId);

            jobQueueSpec.Includes.Add(q => q.Project.Members);
            var jobQueue = await _jobQueueRepository.GetSingleBySpec(jobQueueSpec, cancellationToken);

            var users = await _userRepository.GetUsersByIds(jobQueue.Project.Members.Select(m => m.UserId).ToArray());

            var jobQueueWebUrl = $"{webUrl}/project/{jobQueue.ProjectId}/job-queue/{jobQueueId}";
            await _notificationProvider.SendNotification(new SendNotificationRequest
            {
                MessageType = NotificationConfig.JobQueueCompleted,
                Emails      = users.Select(u => u.Email).Distinct().ToList()
            }, new Dictionary <string, string>
            {
                { MessageParameter.JobCode, jobQueue.Code },
                { MessageParameter.JobDefinitionName, jobQueue.JobDefinitionName },
                { MessageParameter.ProjectName, jobQueue.Project.Name },
                { MessageParameter.JobStatus, jobQueue.Status },
                { MessageParameter.Remarks, jobQueue.Remarks },
                { MessageParameter.WebUrl, jobQueueWebUrl },
                { MessageParameter.JobTaskStatus, GenerateJobTaskStatusNotificationMessage(jobQueue.JobTasksStatus) }
            });
        }
Esempio n. 4
0
        public async Task <User> CreateUser(string email, string firstName, string lastName, string roleName, Dictionary <string, string> externalAccountIds, string password, string webUrl, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var user = new User
            {
                UserName           = email,
                Email              = email,
                FirstName          = firstName,
                LastName           = lastName,
                ExternalAccountIds = externalAccountIds
            };

            try
            {
                var userId = await _userRepository.Create(user, password, cancellationToken);

                if (userId > 0)
                {
                    user.Id = userId;

                    await SetUserRole(userId, roleName);

                    var token = await GenerateConfirmationToken(userId);

                    string confirmToken = HttpUtility.UrlEncode(token);

                    var confirmUrl        = $"{webUrl}/confirm-email?userId={userId}&token={confirmToken}";
                    var loginUrl          = $"{webUrl}/login";
                    var updatePasswordUrl = $"{webUrl}/user-profile";
                    await _notificationProvider.SendNotification(new SendNotificationRequest
                    {
                        MessageType = NotificationConfig.RegistrationCompleted,
                        Emails      = new List <string>
                        {
                            email
                        }
                    }, new Dictionary <string, string>
                    {
                        { MessageParameter.ConfirmUrl, confirmUrl },
                        { MessageParameter.LoginUrl, loginUrl },
                        { MessageParameter.UpdatePasswordUrl, updatePasswordUrl },
                        { MessageParameter.UserName, email },
                        { MessageParameter.TemporaryPassword, password }
                    });
                }
            }
            catch (Exception ex)
            {
                throw new UserCreationFailedException(user.UserName, ex);
            }

            return(user);
        }
Esempio n. 5
0
 public Task <OperationResult> ApproveMissionNotify(string userId, string missionName)
 {
     return(_notificationsProvider.SendNotification(new[] { userId }, "Ура!", "Миссия \"" + missionName + "\" засчитана!"));
 }