Skip to content

Commit

Permalink
Merge pull request #6 from GerardGargan/dev
Browse files Browse the repository at this point in the history
marge dev to master
  • Loading branch information
GerardGargan authored Aug 21, 2024
2 parents 4b61085 + b4ba421 commit 765b930
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 5 deletions.
17 changes: 17 additions & 0 deletions Bookstore.Models/ViewModels/UserRoleVM.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bookstore.Models.ViewModels
{
public class UserRoleVM
{
public ApplicationUser User { get; set; }

Check warning on line 12 in Bookstore.Models/ViewModels/UserRoleVM.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public IEnumerable<SelectListItem> RoleList { get; set; }

Check warning on line 13 in Bookstore.Models/ViewModels/UserRoleVM.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'RoleList' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public IEnumerable<SelectListItem> CompanyList { get; set; }

Check warning on line 14 in Bookstore.Models/ViewModels/UserRoleVM.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'CompanyList' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string RoleId { get; set; }

Check warning on line 15 in Bookstore.Models/ViewModels/UserRoleVM.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'RoleId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
}
63 changes: 62 additions & 1 deletion Bookstore.Web/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Bookstore.DataAccess.Data;
using Bookstore.DataAccess.Repository.IRepository;
using Bookstore.Models;
using Bookstore.Models.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace BookstoreWeb.Areas.Admin.Controllers
{
Expand All @@ -22,7 +24,41 @@ public IActionResult Index()
return View();
}

#region
public IActionResult RoleManagement(string userId)
{
ApplicationUser user = _unitOfWork.User.Get(x => x.Id == userId);

if(user == null || userId == null)
{
return NotFound();
}
var roleId = _db.UserRoles.FirstOrDefault(x => x.UserId == userId).RoleId;
var roleName = _db.Roles.FirstOrDefault(x => x.Id == roleId).Name;

user.Role = roleName;



UserRoleVM userRoleVM = new UserRoleVM()
{
User = user,
RoleList = _db.Roles.Select(x => new SelectListItem()
{
Text = x.Name,
Value = x.Id.ToString()
}).ToList(),
CompanyList = _db.Companies.Select(x => new SelectListItem()
{
Text = x.Name,
Value = x.Id.ToString()
}),
RoleId = roleId
};

return View(userRoleVM);
}

#region API

[HttpGet]
public IActionResult GetAll()
Expand All @@ -46,6 +82,31 @@ public IActionResult GetAll()
return Json(new { data = allUsers });
}

[HttpPost]
public IActionResult LockUnlock([FromBody] string id)
{
ApplicationUser user = _db.ApplicationUsers.FirstOrDefault(x => x.Id == id);

if(user == null)
{
return Json(new { success = false, message = "Error while Locking/Unlocking" });
}

if (user.LockoutEnd != null && user.LockoutEnd > DateTime.Now)
{
// user is currently locked, we will unlock them
user.LockoutEnd = DateTime.Now;
} else
{
// lock the user
user.LockoutEnd = DateTime.Now.AddYears(1000);
}

_db.SaveChanges();

return Json(new { success = true, message = "User updated" });
}

#endregion

}
Expand Down
61 changes: 61 additions & 0 deletions Bookstore.Web/Areas/Admin/Views/User/RoleManagement.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@model UserRoleVM

@{
var isCompanyUser = Model.User.CompanyId == null ? "display: none" : "";
}

<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">Manage User</h2>
</div>
</div>
</div>
<div class="card-body p-4">
<form method="POST" class="row" enctype="multipart/form-data">
<input asp-for="@Model.User.Id" hidden />
<div class="border p-3">
<div class="form-floating py-2 col-12">
<input asp-for="User.Name" class="form-control border-0 shadow" />
<label asp-for="User.Name" class="ms-2"></label>
<span asp-validation-for="User.Name" class="text-danger p-0"></span>
</div>
<div class="form-floating py-2 col-12">
<select asp-for="RoleId" asp-items="@Model.RoleList" class="form-select border-0 shadow">
<option disabled selected>--Select Role--</option>
</select>
<label asp-for="@Model.User.Role" class="ms-2"></label>
</div>
<div class="form-floating py-2 col-12">
<select asp-for="User.CompanyId" asp-items="@Model.CompanyList" class="form-select border-0 shadow" style="@isCompanyUser">
<option disabled selected>--Select Company--</option>
</select>
</div>
</div>
</form>
</div>
</div>



@section Scripts {
<partial name="_ValidationScriptsPartial" />
<script>
$(document).ready(function () {
$('#RoleId').change(function () {
console.log("Change event");
let roleSelected = $('#RoleId Option:Selected').text();
if (roleSelected == 'Company') {
console.log("Show");
$('#User_CompanyId').show();
} else {
console.log("Hide");
$('#User_CompanyId').hide();
}
});
});
</script>
}
1 change: 1 addition & 0 deletions Bookstore.Web/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
Expand Down
48 changes: 44 additions & 4 deletions Bookstore.Web/wwwroot/js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,54 @@ function loadDataTable() {
{ data: 'company.name', "width": "10%" },
{ data: 'role', "width": "10%" },
{
data: 'id',
data: { id: 'id', lockoutEnd: 'lockoutEnd' },
"render": function (data) {
return `<div class="w-75 btn-group" role="group">
<a href="/Admin/User/Upsert/${data}" class="btn btn-primary mx-1"><i class="bi bi-pencil-square"></i> Edit</a>
</div>`
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-success text-white" style="cursor:pointer; width:100px;">
<i class="bi bi-unlock-fill"></i> Unlock
</a>
<a href="/admin/user/rolemanagement?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-danger text-white" style="cursor:pointer; width:100px;">
<i class="bi bi-lock-fill"></i> Lock
</a>
<a href="/admin/user/rolemanagement?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();
}
}
});
}

0 comments on commit 765b930

Please sign in to comment.