-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mohammed
committed
Sep 27, 2024
1 parent
e33f70c
commit f780ef4
Showing
11 changed files
with
334 additions
and
8 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
|
||
namespace BookStore.Models.ViewModels; | ||
|
||
public class RoleManagmentVM | ||
{ | ||
public ApplicationUser ApplicationUser { get; set; } | ||
public IEnumerable<SelectListItem> RoleList { get; set; } | ||
public IEnumerable<SelectListItem> CompanyList { get; set; } | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
BookStore.Web/Areas/Admin/Controllers/UserController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
| ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.EntityFrameworkCore; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using BookStore.DataAccess.Repository.IRepository; | ||
using BookStore.Models; | ||
using BookStore.Models.ViewModels; | ||
using BookStore.Utility; | ||
|
||
namespace BookStore.Web.Areas.Admin.Controllers | ||
{ | ||
[Area("Admin")] | ||
[Authorize(Roles = SD.Role_Admin)] | ||
public class UserController : Controller | ||
{ | ||
private readonly UserManager<IdentityUser> _userManager; | ||
private readonly RoleManager<IdentityRole> _roleManager; | ||
private readonly IUnitOfWork _unitOfWork; | ||
public UserController(UserManager<IdentityUser> userManager, IUnitOfWork unitOfWork, RoleManager<IdentityRole> roleManager) { | ||
_unitOfWork = unitOfWork; | ||
_roleManager = roleManager; | ||
_userManager = userManager; | ||
} | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult RoleManagment(string userId) { | ||
|
||
RoleManagmentVM RoleVM = new RoleManagmentVM() { | ||
ApplicationUser = _unitOfWork.ApplicationUser.Get(u => u.Id == userId, includeProperties:"Company"), | ||
RoleList = _roleManager.Roles.Select(i => new SelectListItem { | ||
Text = i.Name, | ||
Value = i.Name | ||
}), | ||
CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem { | ||
Text = i.Name, | ||
Value = i.Id.ToString() | ||
}), | ||
}; | ||
|
||
RoleVM.ApplicationUser.Role = _userManager.GetRolesAsync(_unitOfWork.ApplicationUser.Get(u=>u.Id==userId)) | ||
.GetAwaiter().GetResult().FirstOrDefault(); | ||
return View(RoleVM); | ||
} | ||
|
||
[HttpPost] | ||
public IActionResult RoleManagment(RoleManagmentVM roleManagmentVM) { | ||
|
||
string oldRole = _userManager.GetRolesAsync(_unitOfWork.ApplicationUser.Get(u => u.Id == roleManagmentVM.ApplicationUser.Id)) | ||
.GetAwaiter().GetResult().FirstOrDefault(); | ||
|
||
ApplicationUser applicationUser = _unitOfWork.ApplicationUser.Get(u => u.Id == roleManagmentVM.ApplicationUser.Id); | ||
|
||
|
||
if (!(roleManagmentVM.ApplicationUser.Role == oldRole)) { | ||
//a role was updated | ||
if (roleManagmentVM.ApplicationUser.Role == SD.Role_Company) { | ||
applicationUser.CompanyId = roleManagmentVM.ApplicationUser.CompanyId; | ||
} | ||
if (oldRole == SD.Role_Company) { | ||
applicationUser.CompanyId = null; | ||
} | ||
_unitOfWork.ApplicationUser.Update(applicationUser); | ||
_unitOfWork.Save(); | ||
|
||
_userManager.RemoveFromRoleAsync(applicationUser, oldRole).GetAwaiter().GetResult(); | ||
_userManager.AddToRoleAsync(applicationUser, roleManagmentVM.ApplicationUser.Role).GetAwaiter().GetResult(); | ||
|
||
} | ||
else { | ||
if(oldRole==SD.Role_Company && applicationUser.CompanyId != roleManagmentVM.ApplicationUser.CompanyId) { | ||
applicationUser.CompanyId = roleManagmentVM.ApplicationUser.CompanyId; | ||
_unitOfWork.ApplicationUser.Update(applicationUser); | ||
_unitOfWork.Save(); | ||
} | ||
} | ||
|
||
return RedirectToAction("Index"); | ||
} | ||
|
||
|
||
#region API CALLS | ||
|
||
[HttpGet] | ||
public IActionResult GetAll() | ||
{ | ||
List<ApplicationUser> objUserList = _unitOfWork.ApplicationUser.GetAll(includeProperties: "Company").ToList(); | ||
|
||
foreach(var user in objUserList) { | ||
|
||
user.Role= _userManager.GetRolesAsync(user).GetAwaiter().GetResult().FirstOrDefault(); | ||
|
||
if (user.Company == null) { | ||
user.Company = new Company() { | ||
Name = "" | ||
}; | ||
} | ||
} | ||
|
||
return Json(new { data = objUserList }); | ||
} | ||
|
||
|
||
[HttpPost] | ||
public IActionResult LockUnlock([FromBody]string id) | ||
{ | ||
|
||
var objFromDb = _unitOfWork.ApplicationUser.Get(u => u.Id == id); | ||
if (objFromDb == null) | ||
{ | ||
return Json(new { success = false, message = "Error while Locking/Unlocking" }); | ||
} | ||
|
||
if(objFromDb.LockoutEnd!=null && objFromDb.LockoutEnd > DateTime.Now) { | ||
//user is currently locked and we need to unlock them | ||
objFromDb.LockoutEnd = DateTime.Now; | ||
} | ||
else { | ||
objFromDb.LockoutEnd = DateTime.Now.AddYears(1000); | ||
} | ||
_unitOfWork.ApplicationUser.Update(objFromDb); | ||
_unitOfWork.Save(); | ||
return Json(new { success = true, message = "Operation Successful" }); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
<div class="card shadow border-0 my-4"> | ||
<div class="card-header bg-secondary bg-gradient ml-0 py-3"> | ||
<div class="row"> | ||
<div class="col-12 text-center"> | ||
<h2 class="text-white py-2">User List</h2> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="card-body p-4"> | ||
|
||
<table id="tblData" class="table table-bordered table-striped" style="width:100%"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Email</th> | ||
<th>Phone</th> | ||
<th>Company</th> | ||
<th>Role</th> | ||
<th></th> | ||
</tr> | ||
</thead> | ||
</table> | ||
|
||
</div> | ||
</div> | ||
|
||
@section Scripts{ | ||
<script src="~/js/user.js"></script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
@using BookStore.Utility | ||
@model RoleManagmentVM | ||
|
||
<div class="card shadow border-0 mt-4"> | ||
<div class="card-header bg-secondary bg-gradient ml-0 py-3"> | ||
<div class="row"> | ||
<div class="col-12 text-center"> | ||
<h2 class="text-white py-2">Manage User Role</h2> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="card-body p-4"> | ||
<form method="post" class="row"> | ||
<input asp-for="ApplicationUser.Id" hidden /> | ||
<div class="border p-3"> | ||
@*<div asp-validation-summary="ModelOnly"></div>*@ | ||
<div class="form-floating py-2 col-12"> | ||
<input asp-for="ApplicationUser.Name" readonly class="form-control border-0 shadow" /> | ||
<label asp-for="ApplicationUser.Name" class="ms-2"></label> | ||
</div> | ||
<div class="form-floating py-2 col-12"> | ||
<select asp-for="ApplicationUser.Role" asp-items="@Model.RoleList" class="form-select"></select> | ||
</div> | ||
@{ | ||
var companyVisible = "display:none;"; | ||
} | ||
@if (Model.ApplicationUser.Role == SD.Role_Company) { | ||
companyVisible = "display:block;"; | ||
} | ||
<div class="form-floating py-2 col-12"> | ||
<select asp-for="ApplicationUser.CompanyId" style="@companyVisible" asp-items="@Model.CompanyList" class="form-select"> | ||
</select> | ||
</div> | ||
|
||
<div class="row pt-2"> | ||
<div class="col-6 col-md-3"> | ||
<button type="submit" class="btn btn-primary form-control">Update Role</button> | ||
</div> | ||
<div class="col-6 col-md-3"> | ||
<a asp-action="Index" class="btn btn-outline-primary border form-control"> | ||
Back to List | ||
</a> | ||
</div> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
|
||
|
||
|
||
@section Scripts{ | ||
@{ | ||
<partial name="_ValidationScriptsPartial"/> | ||
} | ||
<script> | ||
$(document).ready(function () { | ||
$('#ApplicationUser_Role').change(function () { | ||
var selection = $('#ApplicationUser_Role Option:Selected').text(); | ||
if (selection == 'Company') { | ||
$('#ApplicationUser_CompanyId').show(); | ||
} | ||
else { | ||
$('#ApplicationUser_CompanyId').hide(); | ||
} | ||
}) | ||
}) | ||
</script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var dataTable; | ||
|
||
$(document).ready(function () { | ||
loadDataTable(); | ||
}); | ||
|
||
function loadDataTable() { | ||
dataTable = $('#tblData').DataTable({ | ||
"ajax": { url:'/admin/user/getall'}, | ||
"columns": [ | ||
{ "data": "name", "width": "15%" }, | ||
{ "data": "email", "width": "15%" }, | ||
{ "data": "phoneNumber", "width": "15%" }, | ||
{ "data": "company.name", "width": "15%" }, | ||
{ "data": "role", "width": "15%" }, | ||
{ | ||
data: { id:"id", lockoutEnd:"lockoutEnd"}, | ||
"render": function (data) { | ||
var today = new Date().getTime(); | ||
var lockout = new Date(data.lockoutEnd).getTime(); | ||
|
||
if (lockout > today) { | ||
return ` | ||
<div class="text-center"> | ||
<a onclick=LockUnlock('${data.id}') class="btn btn-danger text-white" style="cursor:pointer; width:100px;"> | ||
<i class="bi bi-lock-fill"></i> Lock | ||
</a> | ||
<a href="/admin/user/RoleManagment?userId=${data.id}" class="btn btn-danger text-white" style="cursor:pointer; width:150px;"> | ||
<i class="bi bi-pencil-square"></i> Permission | ||
</a> | ||
</div> | ||
` | ||
} | ||
else { | ||
return ` | ||
<div class="text-center"> | ||
<a onclick=LockUnlock('${data.id}') class="btn btn-success text-white" style="cursor:pointer; width:100px;"> | ||
<i class="bi bi-unlock-fill"></i> UnLock | ||
</a> | ||
<a href="/admin/user/RoleManagment?userId=${data.id}" class="btn btn-danger text-white" style="cursor:pointer; width:150px;"> | ||
<i class="bi bi-pencil-square"></i> Permission | ||
</a> | ||
</div> | ||
` | ||
} | ||
|
||
|
||
}, | ||
"width": "25%" | ||
} | ||
] | ||
}); | ||
} | ||
|
||
|
||
function LockUnlock(id) { | ||
$.ajax({ | ||
type: "POST", | ||
url: '/Admin/User/LockUnlock', | ||
data: JSON.stringify(id), | ||
contentType: "application/json", | ||
success: function (data) { | ||
if (data.success) { | ||
toastr.success(data.message); | ||
dataTable.ajax.reload(); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters