From 82d270a66b8ffca28e321f29b2eb90b2412ac9a7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 8 May 2021 18:10:08 +0300 Subject: Implemented authorization; Replaced Role with IdentityRole; Renamed UserController to AccountController, updated links --- ExamTemplate/Web/Controllers/AccountController.cs | 140 ++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 ExamTemplate/Web/Controllers/AccountController.cs (limited to 'ExamTemplate/Web/Controllers/AccountController.cs') diff --git a/ExamTemplate/Web/Controllers/AccountController.cs b/ExamTemplate/Web/Controllers/AccountController.cs new file mode 100644 index 0000000..7fb7ab3 --- /dev/null +++ b/ExamTemplate/Web/Controllers/AccountController.cs @@ -0,0 +1,140 @@ +using ExamTemplate.Services; +using Microsoft.AspNetCore.Mvc; +using ExamTemplate.Web.Models.User; +using AutoMapper; +using ExamTemplate.Services.Models; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; + +namespace ExamTemplate.Web.Controllers +{ + [Authorize] + public class AccountController : Controller + { + private readonly IMapper _autoMapper; + private readonly UserService _userService; + + public AccountController(IMapper autoMapper, UserService userService) + { + this._autoMapper = autoMapper; + this._userService = userService; + } + + [HttpGet] + [AllowAnonymous] + public IActionResult Register() + { + return View(); + } + + [HttpPost] + [AllowAnonymous] + public async Task Register(RegisterUserViewModel registerUserViewModel) + { + if (!ModelState.IsValid) + return View(registerUserViewModel); + + RegisterUserServiceModel registerUserServiceModel = this._autoMapper.Map(registerUserViewModel); + + bool result = await this._userService.RegisterUserAsync(registerUserServiceModel); + + if (result) + return RedirectToAction("Index", "Home"); + else + return View(); + } + + [HttpGet] + [AllowAnonymous] + public IActionResult Login() + { + return View(); + } + + [HttpPost] + [AllowAnonymous] + public async Task Login(LoginUserViewModel loginUserViewModel) + { + if (!ModelState.IsValid) + return View(loginUserViewModel); + + LoginUserServiceModel loginUserServiceModel = this._autoMapper.Map(loginUserViewModel); + + bool result = await this._userService.LoginUserAsync(loginUserServiceModel); + + if (result) + return RedirectToAction("Index", "Home"); + else + return View(); + } + + [HttpPost] + public async Task Logout() + { + await this._userService.LogoutAsync(); + + return RedirectToAction("Login"); + } + + [HttpGet] + [AllowAnonymous] + public async Task Profile(string username) + { + UserServiceModel userServiceModel = await this._userService.GetUserByUsernameAsync(username); + + if (userServiceModel == default(UserServiceModel)) + return RedirectToAction("Login"); + + UserViewModel userViewModel = this._autoMapper.Map(userServiceModel); + + return View(userViewModel); + } + + [HttpGet] + public async Task Edit() + { + UserServiceModel userServiceModel = await this._userService.GetUserByClaimsAsync(this.HttpContext.User); + + if (userServiceModel == default(UserServiceModel)) + return RedirectToAction("Login"); + + EditUserViewModel editUserViewModel = this._autoMapper.Map(userServiceModel); + + return View(editUserViewModel); + } + + [HttpPost] + public async Task Edit(EditUserViewModel editUserViewModel) + { + if (!await this._userService.IsAuthorizedToModify(HttpContext.User, editUserViewModel.OriginalUsername)) + return new UnauthorizedResult(); + + if (!ModelState.IsValid) + return View(editUserViewModel); + + if (!this._userService.IsSignedIn(HttpContext.User)) + return RedirectToAction("Login"); + + UserServiceModel loggedInUser = await this._userService.GetUserByClaimsAsync(HttpContext.User); + + EditUserServiceModel editUserServiceModel = this._autoMapper.Map(editUserViewModel); + bool result = await this._userService.EditUserAsync(HttpContext.User, editUserServiceModel); + + if (result) + { + if (loggedInUser.Username != editUserViewModel.Username) + await this._userService.LogoutAsync(); + + return RedirectToAction("Profile", new { username = editUserViewModel.Username }); + } + else + return RedirectToAction("Profile", new { username = loggedInUser.Username }); + } + + // [HttpPost] + // public async Task DeleteProfile(string username) + // { + // throw new System.NotImplementedException(); + // } + } +} -- cgit v1.2.3