Esempio n. 1
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            _logger.LogTrace(LogEvents.AccountController, AwesomeMethods.ConvertDataForLog(model, _converter));

            // If user not found act like everything was ok. Do not reveal critical info like that
            // no user with that email exists
            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                return(View("ForgotPasswordCheckEmail"));
            }

            var code = await _userManager.GeneratePasswordResetTokenAsync(user);

            var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);

            await _emailSender.SendEmailAsync(
                user.Email,
                $"{_serverOptions.Value.ProjectName} - {_localizer["ResetPassword"]}",
                $"{_localizer["ResetPasswordDetail"]} <a href='{callbackUrl}'>{callbackUrl}</a>");

            _logger.LogInformation(LogEvents.AccountController, $"RESET PASSWORD EMAIL SENT FOR USER {user.Email}");

            return(View("ForgotPasswordCheckEmail"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Patch(int id, [FromBody] TViewModel viewModel)
        {
            _logger.LogTrace(LogEventId, AwesomeMethods.ConvertDataForLog(viewModel, _converter));

            if (viewModel.Id != id)
            {
                _logger.LogError(LogEventId, $"INVALID ID : ({id})");
                return(BadRequest(_response
                                  .AddError(_localizer["InvalidId"])
                                  .Get()));
            }

            var model = await _repository.GetById(viewModel.Id);

            if (model == null)
            {
                _logger.LogWarning(LogEventId, $"NOT FOUND : ID ({id})");
                return(NotFound(_response
                                .AddWarning(_localizer["IdNotFound"])
                                .Get()));
            }

            Mapper.Map <TViewModel, TModel>(viewModel, model);

            return(await ApplyChanges(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> Get()
        {
            var items = await _repository.GetAll();

            _logger.LogTrace(LogEventId, AwesomeMethods.ConvertDataForLog(items, _converter));

            return(Ok(_response
                      .AddData(Mapper.Map <IEnumerable <TViewModel> >(items))
                      .Get()));
        }
Esempio n. 4
0
            public void OnActionExecuting(ActionExecutingContext context)
            {
                if (context.ActionArguments.ContainsKey("viewModel") &&
                    context.ActionArguments["viewModel"] != null)
                {
                    _logger.LogTrace(LogEvents.ValidateViewModel, AwesomeMethods.ConvertDataForLog(context.ActionArguments["viewModel"], _converter));
                }

                if (!context.ModelState.IsValid)
                {
                    _logger.LogError(LogEvents.ValidateViewModel, "INVALID MODEL");
                    _logger.LogError(LogEvents.ValidateViewModel, AwesomeMethods.ConvertDataForLog(context.ModelState, _converter));
                    context.Result = new BadRequestObjectResult(_response.AddModelState(context.ModelState)
                                                                .Get());
                }
            }
Esempio n. 5
0
        public async Task <IActionResult> Get(int id)
        {
            var model = await _repository.GetById(id);

            if (model == null)
            {
                _logger.LogWarning(LogEventId, $"NOT FOUND : ID ({id})");
                return(NotFound(_response
                                .AddWarning(_localizer["IdNotFound"])
                                .Get()));
            }

            _logger.LogTrace(LogEventId, AwesomeMethods.ConvertDataForLog(model, _converter));

            return(Ok(_response
                      .AddData(Mapper.Map <TViewModel>(model))
                      .Get()));
        }
Esempio n. 6
0
        public async Task <IActionResult> SignIn(SignInViewModel model, string returnUrl)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            _logger.LogTrace(LogEvents.AccountController, AwesomeMethods.ConvertDataForLog(model, _converter));

            // If user is already authenticated redirect
            if (User.Identity.IsAuthenticated)
            {
                return(ResolveRedirect(returnUrl));
            }

            var user = await _userManager.FindByEmailAsync(model.Email);

            if (user == null)
            {
                _logger.LogWarning(LogEvents.AccountController, $"USER WITH EMAIL {model.Email} NOT FOUND");
                ModelState.AddModelError("", _localizer["InvalidUsernameOrPassword"]);
                return(View());
            }

            // Check if user is confirmed to send confirm email again
            if (!user.EmailConfirmed)
            {
                return(await SendRegisterConfirmationEmail(user));
            }

            // Sign in
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                return(ResolveRedirect(returnUrl));
            }

            return(View());
        }
Esempio n. 7
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            _logger.LogTrace(LogEvents.AccountController, AwesomeMethods.ConvertDataForLog(model, _converter));

            // If user is already authenticated redirect
            if (User.Identity.IsAuthenticated)
            {
                return(ResolveRedirect(returnUrl));
            }

            var user = new IdentityUser
            {
                UserName = model.Email,
                Email    = model.Email
            };

            // Create new user
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                _logger.LogInformation(LogEvents.AccountController, $"USER {user.Email} CREATED");
                return(await SendRegisterConfirmationEmail(user));
            }
            else
            {
                _logger.LogWarning(LogEvents.AccountController, $"USER {user.Email} CREATION FAILED");
            }

            return(View());
        }
Esempio n. 8
0
        public async Task <IActionResult> Post([FromBody] TViewModel viewModel)
        {
            _logger.LogTrace(LogEventId, AwesomeMethods.ConvertDataForLog(viewModel, _converter));

            var model = Mapper.Map <TModel>(viewModel);

            _repository.Add(model);

            if (await _repository.Save())
            {
                _logger.LogInformation(LogEventId, $"SAVED : ID ({model.Id})");
                return(CreatedAtAction("Get", new { id = model.Id },
                                       _response
                                       .AddInfo(_localizer["Saved"])
                                       .AddData(Mapper.Map <TViewModel>(model))
                                       .Get()));
            }
            else
            {
                _logger.LogWarning(LogEventId, "NOT SAVED");
                return(NoContent());
            }
        }