Skip to content

Commit

Permalink
Merge pull request #7 from GerardGargan/dev
Browse files Browse the repository at this point in the history
Merge dev to master
  • Loading branch information
GerardGargan authored Aug 24, 2024
2 parents 765b930 + d7c6707 commit 00311be
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
61 changes: 61 additions & 0 deletions Bookstore.Web/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using Bookstore.DataAccess.Repository.IRepository;
using Bookstore.Models;
using Bookstore.Models.ViewModels;
using Bookstore.Utility;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;

namespace BookstoreWeb.Areas.Admin.Controllers
{
Expand Down Expand Up @@ -58,6 +61,64 @@ public IActionResult RoleManagement(string userId)
return View(userRoleVM);
}

[HttpPost]
[ActionName("RoleManagement")]
public IActionResult RoleManagementPOST(UserRoleVM userRoleVM)
{
ApplicationUser user = _db.ApplicationUsers.Where(x => x.Id == userRoleVM.User.Id).FirstOrDefault();

if(user == null)
{
TempData["error"] = "Cannot find user";
RedirectToAction(nameof(Index));
}
var userRoleCurrent = _db.UserRoles.Where(x => x.UserId == user.Id).FirstOrDefault();

// check if the user is a company user and they have selected a company
var newRoleName = _db.Roles.Where(x => x.Id == userRoleVM.RoleId).FirstOrDefault().Name;
if (newRoleName == SD.Role_Company && userRoleVM.User.CompanyId == null)
{
userRoleVM.CompanyList = _db.Companies.Select(x => new SelectListItem()
{
Text = x.Name,
Value = x.Id.ToString()
});
userRoleVM.RoleList = _db.Roles.Select(x => new SelectListItem()
{
Text = x.Name,
Value = x.Id.ToString()
}).ToList();
userRoleVM.RoleId = userRoleCurrent.RoleId;
TempData["error"] = "Please select a company";
return View(userRoleVM);
}

// remove current userRole record

_db.UserRoles.Remove(userRoleCurrent);

// create a new user role record
var newUserRole = new IdentityUserRole<string>()
{
RoleId = userRoleVM.RoleId,
UserId = user.Id
};

// update company
if(newRoleName == SD.Role_Company)
{
user.CompanyId = userRoleVM.User.CompanyId;
} else
{
user.CompanyId = null;
}

_db.UserRoles.Add(newUserRole);
_db.SaveChanges();
TempData["success"] = "User updated successfully";
return RedirectToAction(nameof(Index));
}

#region API

[HttpGet]
Expand Down
14 changes: 11 additions & 3 deletions Bookstore.Web/Areas/Admin/Views/User/RoleManagement.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@model UserRoleVM

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

<div class="card shadow border-0 my-4">
Expand All @@ -22,16 +22,24 @@
<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">
<select asp-for="@Model.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">
<select asp-for="@Model.User.CompanyId" asp-items="@Model.CompanyList" class="form-select border-0 shadow" style="@isCompanyUser">
<option disabled selected>--Select Company--</option>
</select>
</div>
<div class="row pt-2">
<div class="col-6 col-md-3">
<button type="Submit" class="btn btn-primary form-control">Update Permissions</button>
</div>
<div class="col-6 col-md-3">
<a asp-area="Admin" asp-controller="User" asp-action="Index" class="btn btn-outline-primary form-control">Back to list</a>
</div>
</div>
</div>
</form>
</div>
Expand Down
6 changes: 3 additions & 3 deletions Bookstore.Web/wwwroot/js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ function loadDataTable() {
if (lockout > today) {
return `
<div class="text-center">
<a onClick=LockUnlock('${data.id}') class="btn btn-success text-white" style="cursor:pointer; width:100px;">
<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;">
<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;">
<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;">
Expand Down

0 comments on commit 00311be

Please sign in to comment.