Skip to content

Commit

Permalink
Lock/Unlock button added for users
Browse files Browse the repository at this point in the history
  • Loading branch information
aliarmaganuygun committed Oct 28, 2024
1 parent 4d48d49 commit 59bdb91
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
23 changes: 23 additions & 0 deletions BulkyWeb/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ public IActionResult Index()
return View();
}

[HttpPost]
public IActionResult LockUnlock([FromBody] string id)
{
var objFromDb = _db.ApplicationUsers.FirstOrDefault(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, we will unlock them
objFromDb.LockoutEnd = DateTime.Now;
}
else
{
objFromDb.LockoutEnd = DateTime.Now.AddYears(1000);
}

_db.SaveChanges();
return Json(new { success = true, message = "Operation Successful" });
}

#region API CALLS
[HttpGet]
public IActionResult GetAll()
Expand Down
13 changes: 9 additions & 4 deletions BulkyWeb/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
<li class="nav-item">
<a class="nav-link" asp-area="Customer" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" asp-area="Admin" asp-controller="Order" asp-action="Index">Order Management</a>
</li>
@if (User.IsInRole(SD.Role_Admin))
{
<li class="nav-item dropdown">
Expand All @@ -47,8 +44,12 @@
<a class="dropdown-item" asp-area="Admin" asp-controller="Category" asp-action="Index">Category</a>
</li>
<li class="nav-item">
<a class="dropdown-item" asp-area="Admin" asp-controller="Product" asp-action="Index">Book</a>
<a class="dropdown-item" asp-area="Admin" asp-controller="Category" asp-action="Index">Cover Type</a>
</li>
<li class="nav-item">
<a class="dropdown-item" asp-area="Admin" asp-controller="Product" asp-action="Index">Product</a>
</li>
<li><hr class="dropdown-divider"></li>
<li class="nav-item">
<a class="dropdown-item" asp-area="Admin" asp-controller="Company" asp-action="Index">Company</a>
</li>
Expand All @@ -62,6 +63,9 @@
</ul>
</li>
}
<li class="nav-item">
<a class="nav-link" asp-area="Admin" asp-controller="Order" asp-action="Index">Order Management</a>
</li>
<li class="nav-item">
<a class="nav-link" asp-area="Customer" asp-controller="Cart" asp-action="Index">
@await Component.InvokeAsync("ShoppingCart")
Expand Down Expand Up @@ -90,6 +94,7 @@
<script src="https://cdn.tiny.cloud/1/idefu98uibhy9ivtvjypr8q08egl0jnbpqgo7ov2upvvqit8/tinymce/7/tinymce.min.js" referrerpolicy="origin"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="//cdn.datatables.net/2.1.8/js/dataTables.min.js" asp-append-version="true"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
Expand Down
59 changes: 50 additions & 9 deletions BulkyWeb/wwwroot/js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,60 @@ 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": "name", "width": "15%" },
{ "data": "email", "width": "15%" },
{ "data": "phoneNumber", "width": "15%" },
{ "data": "company.name", "width": "15%" },
{ "data": "role", "width": "15%" },
{
data: 'id',
data: { id: "id", lockoutEnd: "lockoutEnd" },
"render": function (data) {
return `<div class="w-75 btn-group" role="group">
<a href="/admin/user/upsert?id=${data}" class="btn btn-primary mx-2"><i class="bi bi-pencil-square"></i> Edit </a>`
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 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 class="btn btn-danger text-white" style="cursor:pointer; width:150px;">
<i class="bi bi-pencil-square"></i> Permission
</a>
</div>
`
}
},
width: '25%'
"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();
}
}
});
}

0 comments on commit 59bdb91

Please sign in to comment.