Skip to content

Commit

Permalink
Day 20 - UserRoles
Browse files Browse the repository at this point in the history
  • Loading branch information
fdeniz07 committed Oct 7, 2021
1 parent d9fb0de commit bbdd066
Show file tree
Hide file tree
Showing 76 changed files with 5,965 additions and 3,331 deletions.
77 changes: 77 additions & 0 deletions BlogWeb/Areas/Admin/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
public class AuthController : Controller
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;

public AuthController(UserManager<User> userManager, SignInManager<User> signInManager)
{
_userManager = userManager;
_signInManager = signInManager;
}

[HttpGet]
public IActionResult Login()
{
return View();
}

[HttpPost]
public async Task<IActionResult> Login(UserLoginDto userLoginDto)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(userLoginDto.Email);
if (user != null)
{
var result = await _signInManager.PasswordSignInAsync(user, userLoginDto.Password,
userLoginDto.RememberMe, false);// bu islem sonucunda bize bir result dönüyor.
if (result.Succeeded) // eger bir islem sonucunda result dönülüyorsa, basarili olup olmadigi her zaman kontrol edilir
{
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", "E-posta adresiniz veya şifreniz yanlıştır.");
return View();
}
}
else
{
ModelState.AddModelError("", "E-posta adresiniz veya şifreniz yanlıştır.");
return View();
}
}
else
{
return View();
}
}



[Authorize]
[HttpGet]
public async Task<IActionResult> Logut()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home", new {Area = ""});
}

[Authorize]
[HttpGet]
public ViewResult AccessDenied()
{
return View();
}
}
}
8 changes: 8 additions & 0 deletions BlogWeb/Areas/Admin/Controllers/BlogController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using EntityLayer.ComplexTypes;
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using NToastNotify;

Expand All @@ -30,6 +31,7 @@ public BlogController(IBlogService blogService, ICategoryService categoryService
_toastNotification = toastNotification;
}

[Authorize(Roles = "SuperAdmin,Blog.Read")]
[HttpGet]
public async Task<IActionResult> Index()
{
Expand All @@ -38,6 +40,7 @@ public async Task<IActionResult> Index()
return NotFound();
}

[Authorize(Roles = "SuperAdmin,Blog.Read")]
[HttpGet]
public async Task<IActionResult> Add()
{
Expand All @@ -53,6 +56,7 @@ public async Task<IActionResult> Add()
return NotFound();
}

[Authorize(Roles = "SuperAdmin,Blog.Create")]
[HttpPost]
public async Task<IActionResult> Add(BlogAddViewModel blogAddViewModel)
{
Expand Down Expand Up @@ -81,6 +85,7 @@ public async Task<IActionResult> Add(BlogAddViewModel blogAddViewModel)
return View(blogAddViewModel);
}

[Authorize(Roles = "SuperAdmin,Blog.Update")]
[HttpGet]
public async Task<IActionResult> Update(int blogId)
{
Expand All @@ -98,6 +103,7 @@ public async Task<IActionResult> Update(int blogId)
}
}

[Authorize(Roles = "SuperAdmin,Blog.Update")]
[HttpPost]
public async Task<IActionResult> Update(BlogUpdateViewModel blogUpdateViewModel)
{
Expand Down Expand Up @@ -143,6 +149,7 @@ public async Task<IActionResult> Update(BlogUpdateViewModel blogUpdateViewModel)
return View(blogUpdateViewModel);
}

[Authorize(Roles = "SuperAdmin,Blog.Delete")]
[HttpPost]
public async Task<JsonResult> Delete(int blogId)
{
Expand All @@ -151,6 +158,7 @@ public async Task<JsonResult> Delete(int blogId)
return Json(blogResult);
}

[Authorize(Roles = "SuperAdmin,Blog.Read")]
[HttpGet]
public async Task<JsonResult> GetAllBlogs()
{
Expand Down
8 changes: 7 additions & 1 deletion BlogWeb/Areas/Admin/Controllers/CategoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize(Roles = "Admin,Editor")]
public class CategoryController : BaseController
{
private readonly ICategoryService _categoryService;
Expand All @@ -26,18 +25,21 @@ public CategoryController(ICategoryService categoryService,UserManager<User> use
_categoryService = categoryService;
}

[Authorize(Roles = "SuperAdmin,Category.Read")]
public async Task<IActionResult> Index()
{
var result = await _categoryService.GetAllByNonDeletedAsync();
return View(result.Data);
}

[Authorize(Roles = "SuperAdmin,Category.Create")]
[HttpGet]
public IActionResult Add()
{
return PartialView("_CategoryAddPartial");
}

[Authorize(Roles = "SuperAdmin,Category.Create")]
[HttpPost]
public async Task<IActionResult> Add(CategoryAddDto categoryAddDto)
{
Expand All @@ -61,6 +63,7 @@ public async Task<IActionResult> Add(CategoryAddDto categoryAddDto)
return Json(categoryAddAjaxErrorModel);
}

[Authorize(Roles = "SuperAdmin,Category.Read")]
public async Task<JsonResult> GetAllCategories()
{
var result = await _categoryService.GetAllByNonDeletedAsync();
Expand All @@ -71,6 +74,7 @@ public async Task<JsonResult> GetAllCategories()
return Json(categories);
}

[Authorize(Roles = "SuperAdmin,Category.Delete")]
[HttpPost]
public async Task<JsonResult> Delete(int categoryId)
{
Expand All @@ -79,6 +83,7 @@ public async Task<JsonResult> Delete(int categoryId)
return Json(deletedCategory);
}

[Authorize(Roles = "SuperAdmin,Category.Update")]
[HttpGet]
public async Task<IActionResult> Update(int categoryId)
{
Expand All @@ -93,6 +98,7 @@ public async Task<IActionResult> Update(int categoryId)
}
}

[Authorize(Roles = "SuperAdmin,Category.Update")]
[HttpPost]
public async Task<IActionResult> Update(CategoryUpdateDto categoryUpdateDto)
{
Expand Down
126 changes: 126 additions & 0 deletions BlogWeb/Areas/Admin/Controllers/CommentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using AutoMapper;
using BlogWeb.Areas.Admin.Models;
using BlogWeb.Helpers.Abstract;
using BusinessLayer.Abstract;
using CoreLayer.Utilities.Extensions;
using CoreLayer.Utilities.Results.ComplexTypes;
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;

namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
public class CommentController : BaseController
{
private readonly ICommentService _commentService;
public CommentController(UserManager<User> userManager, IMapper mapper, IImageHelper imageHelper, ICommentService commentService) : base(userManager, mapper, imageHelper)
{
_commentService = commentService;
}

[Authorize(Roles = "SuperAdmin,Comment.Read")]
[HttpGet]
public async Task<IActionResult> Index()
{
var result = await _commentService.GetAllByNonDeletedAsync();
return View(result.Data);
}

[Authorize(Roles = "SuperAdmin,Comment.Read")]
[HttpGet]
public async Task<IActionResult> GetAllComments()
{
var result = await _commentService.GetAllByNonDeletedAsync();
var commentsResult = JsonSerializer.Serialize(result, new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve,
});
return Json(commentsResult);
}

[Authorize(Roles = "SuperAdmin,Comment.Read")]
[HttpGet]
public async Task<IActionResult> GetDetail(int commentId)
{
var result = await _commentService.GetAsync(commentId);
if (result.ResultStatus==ResultStatus.Success)
{
return PartialView("_CommentDetailPartial", result.Data);
}
else
{
return NotFound();
}
}

[Authorize(Roles = "SuperAdmin,Comment.Delete")]
[HttpPost]
public async Task<IActionResult> Delete(int commentId)
{
var result = await _commentService.DeleteAsync(commentId, LoggedInUser.UserName);
var commentResult = JsonSerializer.Serialize(result);
return Json(commentResult);
}

[Authorize(Roles = "SuperAdmin,Comment.Update")]
[HttpPost]
public async Task<IActionResult> Approve(int commentId)
{
var result = await _commentService.ApproveAsync(commentId, LoggedInUser.UserName);
var commentResult = JsonSerializer.Serialize(result,new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve
});
return Json(commentResult);
}

[Authorize(Roles = "SuperAdmin,Comment.Update")]
[HttpGet]
public async Task<IActionResult> Update(int commentId)
{
var result = await _commentService.GetCommentUpdateDtoAsync(commentId);
if (result.ResultStatus == ResultStatus.Success)
{
return PartialView("_CommentUpdatePartial", result.Data);
}
else
{
return NotFound();
}
}

[Authorize(Roles = "SuperAdmin,Comment.Update")]
[HttpPost]
public async Task<IActionResult> Update(CommentUpdateDto commentUpdateDto)
{
if (ModelState.IsValid)
{
var result = await _commentService.UpdateAsync(commentUpdateDto, LoggedInUser.UserName);
if (result.ResultStatus == ResultStatus.Success)
{
var commentUpdateAjaxModel = JsonSerializer.Serialize(new CommentUpdateAjaxViewModel
{
CommentDto = result.Data,
CommentUpdatePartial = await this.RenderViewToStringAsync("_CommentUpdatePartial", commentUpdateDto)
},new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve
});
return Json(commentUpdateAjaxModel);
}
}
var commentUpdateAjaxErrorModel = JsonSerializer.Serialize(new CommentUpdateAjaxViewModel
{
CommentUpdatePartial = await this.RenderViewToStringAsync("_CommentUpdatePartial", commentUpdateDto)
});
return Json(commentUpdateAjaxErrorModel);
}

}
}
2 changes: 1 addition & 1 deletion BlogWeb/Areas/Admin/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
[Authorize(Roles = "Admin,Editor")]
public class HomeController : Controller
{
private readonly ICategoryService _categoryService;
Expand All @@ -27,6 +26,7 @@ public HomeController(ICategoryService categoryService, IBlogService blogService
_userManager = userManager;
}

[Authorize(Roles = "SuperAdmin,AdminArea.Home.Read")]
public async Task<IActionResult> Index()
{
var categoriesCountResult = await _categoryService.CountByNonDeletedAsync();
Expand Down
46 changes: 46 additions & 0 deletions BlogWeb/Areas/Admin/Controllers/RoleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Text.Json;
using System.Threading.Tasks;
using EntityLayer.Concrete;
using EntityLayer.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace BlogWeb.Areas.Admin.Controllers
{
[Area("Admin")]
public class RoleController : Controller
{
private readonly RoleManager<Role> _roleManager;

public RoleController(RoleManager<Role> roleManager)
{
_roleManager = roleManager;
}


[Authorize(Roles = "SuperAdmin,Role.Read")]
[HttpGet]
public async Task<IActionResult> Index()
{
var roles = await _roleManager.Roles.ToListAsync();
return View(new RoleListDto
{
Roles = roles
});
}

[Authorize(Roles = "SuperAdmin,Role.Read")]
[HttpGet]
public async Task<IActionResult> GetAllRoles()
{
var roles = await _roleManager.Roles.ToListAsync();
var roleListDto = JsonSerializer.Serialize(new RoleListDto
{
Roles = roles
});
return Json(roleListDto);
}
}
}
Loading

0 comments on commit bbdd066

Please sign in to comment.