Role Permission

This commit is contained in:
Mohaimen Hasan 2026-11-16 17:55:51 +06:00
parent 77321b60ff
commit 83b5935846
5 changed files with 321 additions and 40 deletions

View File

@ -334,6 +334,11 @@ namespace HRM.DA
try
{
tc = TransactionContext.Begin(true);
// Audit of the permission rows, by PermissionCode, read before they are deleted and
// re-inserted below, so each kept menu keeps who/when it was created and approved.
Dictionary<string, Role.RolePermission> existingPermissions = new Dictionary<string, Role.RolePermission>();
if (oRole.IsNew)
{
int id = tc.GenerateID("Role", "RoleID");
@ -344,6 +349,15 @@ namespace HRM.DA
}
else
{
DataReader existingReader = new DataReader(RoleDA.GetRolePermissionByRoleID(tc, oRole.ID));
while (existingReader.Read())
{
Role.RolePermission existingPermission = this.CreateRolePermissionObject(existingReader);
if (existingPermission.PermissionCode != null && !existingPermissions.ContainsKey(existingPermission.PermissionCode))
existingPermissions.Add(existingPermission.PermissionCode, existingPermission);
}
existingReader.Close();
RoleDA.Update(tc, oRole);
#region Delete Children
RoleDA.Delete(tc, "RolePermission", "RoleID", oRole.ID);
@ -372,11 +386,34 @@ namespace HRM.DA
newid = newid + 1;
}
// Whoever made this change: the editor, or the creator for a new role. On approval
// the controller keeps the maker in ModifiedBy, so this is still the maker.
int makerID = (oRole.ModifiedBy ?? 0) > 0 ? oRole.ModifiedBy.Value : oRole.CreatedBy;
foreach (Role.RolePermission rolePermission in oRole.RolePermissions)
{
rolePermission.RoleID = oRole.ID;
rolePermission.CreatedBy = oRole.CreatedBy;
rolePermission.CreatedDate = oRole.CreatedDate;
Role.RolePermission existingPermission = null;
if (rolePermission.PermissionCode != null)
existingPermissions.TryGetValue(rolePermission.PermissionCode, out existingPermission);
if (existingPermission != null)
{
// Menu the role already had: keep its original audit.
rolePermission.CreatedBy = existingPermission.CreatedBy;
rolePermission.CreatedDate = existingPermission.CreatedDate;
rolePermission.ApproveBy = existingPermission.ApproveBy;
rolePermission.ApproveDate = existingPermission.ApproveDate;
}
else
{
// Menu added by this change.
rolePermission.CreatedBy = makerID;
rolePermission.CreatedDate = DateTime.Now;
rolePermission.ApproveBy = null;
rolePermission.ApproveDate = null;
}
//if (oRole.RoleStatus == EnumAuthStatus.Approved )
//{
// if (rolePermission.Status == EnumAuthStatus.DeletedNotYetApprove)
@ -391,6 +428,17 @@ namespace HRM.DA
rolePermission.Status = EnumAuthStatus.Approved;
else
rolePermission.Status = EnumAuthStatus.None;
// A menu approved by this approval (new, or still pending before it) gets this
// approver; a menu that was already approved keeps its earlier approver.
bool approvedNow = existingPermission == null
|| existingPermission.Status == EnumAuthStatus.NewNotYetApprove
|| existingPermission.Status == EnumAuthStatus.EditedNotYetApprove;
if (rolePermission.Status == EnumAuthStatus.Approved && approvedNow)
{
rolePermission.ApproveBy = oRole.ApproveBy;
rolePermission.ApproveDate = oRole.ApproveDate;
}
}
else
{

View File

@ -1 +1,45 @@

/* Table design matching the User page grid (users-list.component.scss).
This component uses ViewEncapsulation.None, so every rule is scoped under .rl-grid. */
/* Uniform, compact rows */
.rl-grid .k-grid-table td {
padding-top: 4px;
padding-bottom: 4px;
vertical-align: middle;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.rl-grid .k-grid-toolbar {
padding: 8px 12px;
}
/* Status badge */
.rl-grid .rl-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 2px 10px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
line-height: 20px;
white-space: nowrap;
}
.rl-grid .rl-badge__dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.rl-grid .rl-badge--success { background: #E6F4EC; color: #137A4B; }
.rl-grid .rl-badge--neutral { background: #EEEEF2; color: #5B5B6B; }
.rl-grid .rl-badge--warning { background: #FFF2DB; color: #8A5300; }
/* Role Type badge, same shape as the Status badge */
.rl-grid .rl-badge--admin { background: #E8EEFC; color: #2F4FB5; }
.rl-grid .rl-badge--ess { background: #F1EAFB; color: #6B3FA0; }
.rl-grid .rl-badge--mobile { background: #E4F5F7; color: #0F6E7A; }

View File

@ -1,8 +1,11 @@
<kendo-grid [data]="roles"
<kendo-grid [kendoGridBinding]="gridView"
class="rl-grid"
kendoGridSelectBy="id"
[pageSize]="20"
[pageable]="true"
[sortable]="true"
[filterable]="true"
[reorderable]="true"
[resizable]="true"
[scrollable]="true"
@ -20,21 +23,56 @@
</kendo-grid-column>
<kendo-grid-column field="description" title="Description">
</kendo-grid-column>
<kendo-grid-column field="roleType" title="Role Type">
<ng-template kendoGridCellTemplate let-dataItem>
<span class="rl-badge"
[class.rl-badge--admin]="dataItem.roleType == 1"
[class.rl-badge--ess]="dataItem.roleType == 2"
[class.rl-badge--mobile]="dataItem.roleType == 3"
[class.rl-badge--neutral]="dataItem.roleType != 1 && dataItem.roleType != 2 && dataItem.roleType != 3">
<span class="rl-badge__dot"></span>{{ roleTypeString(dataItem.roleType) }}
</span>
</ng-template>
</kendo-grid-column>
<kendo-grid-column field="roleStatus" title="Status">
<ng-template kendoGridCellTemplate let-dataItem let-rowIndex="rowIndex">
{{ statusString(dataItem.roleStatus)}}
<span class="rl-badge"
[class.rl-badge--success]="statusString(dataItem.roleStatus) == 'Active'"
[class.rl-badge--warning]="dataItem.roleStatus == 2 || dataItem.roleStatus == 3 || dataItem.roleStatus == 4"
[class.rl-badge--neutral]="statusString(dataItem.roleStatus) == 'In-Active'">
<span class="rl-badge__dot"></span>{{ statusString(dataItem.roleStatus)}}
</span>
</ng-template>
</kendo-grid-column>
<kendo-grid-command-column title="Edit/Approve" width="220">
<ng-template kendoGridCellTemplate let-dataItem let-rowIndex="rowIndex">
@if (dataItem.roleStatus ==1 || dataItem.roleStatus ==5) {
<button class="k-button k-primary" kendoButton [svgIcon]="apiService.KendoSVGIcon['edit']" (click)="ViewPermission(rowIndex);">Edit</button>
<button class="k-button k-primary" kendoButton [svgIcon]="apiService.KendoSVGIcon['edit']" (click)="ViewPermission(roles.indexOf(dataItem));">Edit</button>
}
@if (dataItem.roleStatus !=1 && dataItem.roleStatus !=5) {
<button class="k-button k-primary" kendoButton [svgIcon]="apiService.KendoSVGIcon['edit']" (click)="Approve(rowIndex);">Approve</button>
<button class="k-button k-primary" kendoButton [svgIcon]="apiService.KendoSVGIcon['edit']" (click)="Approve(roles.indexOf(dataItem));">Approve</button>
}
</ng-template>
</kendo-grid-command-column>
<kendo-grid-excel fileName="Roles.xlsx" [fetchData]="allData">
<kendo-excelexport-column field="code" title="Code" [width]="120"></kendo-excelexport-column>
<kendo-excelexport-column field="name" title="Name" [width]="200"></kendo-excelexport-column>
<kendo-excelexport-column field="description" title="Description" [width]="260"></kendo-excelexport-column>
<kendo-excelexport-column field="roleTypeText" title="Role Type" [width]="130"></kendo-excelexport-column>
<kendo-excelexport-column field="statusText" title="Status" [width]="110"></kendo-excelexport-column>
</kendo-grid-excel>
<kendo-grid-pdf fileName="Roles.pdf" [allPages]="true" [repeatHeaders]="true" paperSize="A4" [landscape]="true" [scale]="0.7">
<kendo-grid-column field="code" title="Code"></kendo-grid-column>
<kendo-grid-column field="name" title="Name"></kendo-grid-column>
<kendo-grid-column field="description" title="Description"></kendo-grid-column>
<kendo-grid-column field="roleType" title="Role Type">
<ng-template kendoGridCellTemplate let-dataItem>{{ roleTypeString(dataItem.roleType) }}</ng-template>
</kendo-grid-column>
<kendo-grid-column field="roleStatus" title="Status">
<ng-template kendoGridCellTemplate let-dataItem>{{ statusString(dataItem.roleStatus) }}</ng-template>
</kendo-grid-column>
</kendo-grid-pdf>
</kendo-grid>
@ -42,14 +80,15 @@
@if (showModal) {
<kendo-dialog (close)="onPopUpClose()" [minWidth]="650"
>
<kendo-dialog-titlebar>
Role Permission
</kendo-dialog-titlebar>
<app-role-permission-entry [setviewMode]="viewMode" [setRole]="selectedRole" (saveCompleted)="saveCompleted($event)">
</app-role-permission-entry>
</kendo-dialog>
<div class="blur-background">
<kendo-dialog (close)="onPopUpClose()" [minWidth]="650">
<kendo-dialog-titlebar>
Role Permission
</kendo-dialog-titlebar>
<app-role-permission-entry [setviewMode]="viewMode" [setRole]="selectedRole" (saveCompleted)="saveCompleted($event)">
</app-role-permission-entry>
</kendo-dialog>
</div>
}
<!--<p-dialog [(visible)]="showModal" [style]="{width: '700px',height:'800px', position: 'absolute'}" header="Role Information"

View File

@ -1,12 +1,14 @@
import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {State} from '@progress/kendo-data-query';
import {Component, OnInit, ViewChild, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {process, State} from '@progress/kendo-data-query';
import {DataBindingDirective, GridComponent} from '@progress/kendo-angular-grid';
import {ExcelExportData} from '@progress/kendo-angular-excel-export';
import {Router} from '@angular/router';
import {AuthService} from '../../../_services/auth/auth.service';
import {Role} from '../../../_models/Authentication/role';
import { HRMNotificationService } from '../../../app.notification.service';
import { UntilityHandlerService } from '../../../utility.hanldler.service';
import { ApiService } from '../../../app.api.service';
import { EnumAuthStatus } from '../../../_models/enums';
import { EnumAuthStatus, EnumExtension, EnumRoleType } from '../../../_models/enums';
import { ButtonModule } from '@progress/kendo-angular-buttons';
import { DropDownListModule } from '@progress/kendo-angular-dropdowns';
@ -26,6 +28,11 @@ import { DatePickerModule } from '@progress/kendo-angular-dateinputs';
// This blocks global css to this component
})
export class RolePermissionListComponent implements OnInit {
@ViewChild(DataBindingDirective) dataBinding: DataBindingDirective;
@ViewChild(GridComponent) grid: GridComponent;
/** Rows the grid binds to: `roles` narrowed by the toolbar search box. Column filters, sort and paging are done by kendoGridBinding. */
gridView: Role[] = [];
searchText = '';
showModal: boolean;
IsNew = true;
roles: Role[];
@ -36,6 +43,7 @@ export class RolePermissionListComponent implements OnInit {
this.setSelectableSettings();
this.utilityService.ShowLoadingPanel = true;
this.apiservice.selectedMenuName="Role Entry"
this.allData = this.allData.bind(this);
}
public ngOnInit(): void {
@ -49,7 +57,9 @@ export class RolePermissionListComponent implements OnInit {
this.useService.getAllRoles()
.subscribe(
(resp: any) => {
debugger;
this.roles = resp;
this.refreshGridView();
},
(err: any) => {
//ON ERROR
@ -77,6 +87,9 @@ export class RolePermissionListComponent implements OnInit {
switch (status) {
case EnumAuthStatus.Active: { str = "Active"; break };
case EnumAuthStatus.Approved: { str = "Active"; break };
case EnumAuthStatus.New_Not_Yet_Approve: { str = "New - Not Yet Approved"; break };
case EnumAuthStatus.Edited_Not_Yet_Approve: { str = "Edited - Not Yet Approved"; break };
case EnumAuthStatus.Deleted_Not_Yet_Approve: { str = "Deleted - Not Yet Approved"; break };
default: str="In-Active"
}
@ -102,14 +115,67 @@ export class RolePermissionListComponent implements OnInit {
var index = this.roles.findIndex(x => x.id == role.id);
this.roles[index] = role;
}
this.refreshGridView();
}
this.showModal = false;
}
/**
* Excel export: every row the grid currently shows across all pages (not just the visible page),
* with Role Type and Status as text.
*/
public allData(): ExcelExportData {
const rows = process(this.gridView ?? [], {
filter: this.grid?.filter,
sort: this.grid?.sort,
}).data;
return {
data: rows.map((r: Role) => ({
code: r.code,
name: r.name,
description: r.description,
roleTypeText: this.roleTypeString(r.roleType),
statusText: this.statusString(r.roleStatus),
})),
};
}
/** Role Type column text, the same names the entry dialog's Role Type dropdown shows. */
roleTypeString(roleType: number): string {
return EnumExtension.getName(EnumRoleType, roleType);
}
/** Toolbar search box: keeps the roles whose code, name or description contain the text. */
public onFilter(value: string): void {
this.searchText = value ?? '';
this.refreshGridView();
if (this.dataBinding) {
this.dataBinding.skip = 0;
}
}
private refreshGridView(): void {
const term = this.searchText.trim();
if (!term) {
this.gridView = (this.roles ?? []).slice();
return;
}
this.gridView = process(this.roles ?? [], {
filter: {
logic: 'or',
filters: [
{field: 'code', operator: 'contains', value: term},
{field: 'name', operator: 'contains', value: term},
{field: 'description', operator: 'contains', value: term},
],
},
}).data;
}
public editHandler(rowIndex: any) {
this.viewMode = 2;
this.selectedRole = this.roles[rowIndex];

View File

@ -1263,30 +1263,77 @@ namespace HRM.UI.Controllers
public ActionResult SaveRole(Role role)
{
int id = 0;
CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
if (role.IsNew == true)
{
role.CreatedBy = currentUser.UserID;
role.CreatedDate = DateTime.Now;
role.RoleStatus = EnumAuthStatus.NewNotYetApprove;
}
else
{
role.ApproveDate = null;
role.ApproveBy = null;
role.ModifiedBy = currentUser.UserID;
role.ModifiedDate = DateTime.Now;
}
if (role.RoleStatus == EnumAuthStatus.Approved)
{
role.ApproveDate = DateTime.Today;
role.ApproveBy = currentUser.UserID;
}
try
{
User actingUser = this._userService.Get(currentUser.UserID);
if (actingUser == null || (actingUser.UserType != EnumUserType.SuperUser && actingUser.SISU != true))
{
return StatusCode(StatusCodes.Status403Forbidden, "You are not allowed to create, edit or approve roles.");
}
if (role.IsNew == true)
{
role.CreatedBy = currentUser.UserID;
role.CreatedDate = DateTime.Now;
role.RoleStatus = EnumAuthStatus.NewNotYetApprove;
}
else
{
Role existingRole = this._roleService.Get(role.ID);
if (existingRole == null)
{
return BadRequest("Role not found.");
}
bool isPending = existingRole.RoleStatus == EnumAuthStatus.NewNotYetApprove
|| existingRole.RoleStatus == EnumAuthStatus.EditedNotYetApprove
|| existingRole.RoleStatus == EnumAuthStatus.DeletedNotYetApprove;
if (role.RoleStatus == EnumAuthStatus.Approved)
{
if (!isPending)
{
return BadRequest("This role has no pending change to approve.");
}
int makerID = (existingRole.ModifiedBy ?? 0) > 0 ? existingRole.ModifiedBy.Value : existingRole.CreatedBy;
if (makerID == currentUser.UserID)
{
return BadRequest("You cannot approve a change you made yourself. Another user must approve it.");
}
}
else
{
if (isPending)
{
return BadRequest("This role is waiting for approval and cannot be edited.");
}
role.RoleStatus = EnumAuthStatus.EditedNotYetApprove;
}
// The browser sends createdBy 0 on edit/approve; use the real creator (RoleDA.Update
// does not write it, it is only used for the permission rows' audit).
role.CreatedBy = existingRole.CreatedBy;
role.CreatedDate = existingRole.CreatedDate;
role.ApproveDate = null;
role.ApproveBy = null;
if (role.RoleStatus == EnumAuthStatus.Approved)
{
// Approval keeps the maker (who made the change) on the role; the approver is
// recorded separately in ApproveBy / ApproveDate below.
role.ModifiedBy = existingRole.ModifiedBy;
role.ModifiedDate = existingRole.ModifiedDate;
}
else
{
role.ModifiedBy = currentUser.UserID;
role.ModifiedDate = DateTime.Now;
}
}
if (role.RoleStatus == EnumAuthStatus.Approved)
{
role.ApproveDate = DateTime.Today;
role.ApproveBy = currentUser.UserID;
}
id = this._roleService.Save(role);
}
catch (Exception e)
@ -1296,6 +1343,43 @@ namespace HRM.UI.Controllers
return Ok(id);
}
//[HttpPost("saveRole")]
//public ActionResult SaveRole(Role role)
//{
// int id = 0;
// CurrentUser currentUser = CurrentUser.GetCurrentUser(HttpContext.User);
// if (role.IsNew == true)
// {
// role.CreatedBy = currentUser.UserID;
// role.CreatedDate = DateTime.Now;
// role.RoleStatus = EnumAuthStatus.NewNotYetApprove;
// }
// else
// {
// role.ApproveDate = null;
// role.ApproveBy = null;
// role.ModifiedBy = currentUser.UserID;
// role.ModifiedDate = DateTime.Now;
// }
// if (role.RoleStatus == EnumAuthStatus.Approved)
// {
// role.ApproveDate = DateTime.Today;
// role.ApproveBy = currentUser.UserID;
// }
// try
// {
// id = this._roleService.Save(role);
// }
// catch (Exception e)
// {
// return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
// }
// return Ok(id);
//}
[HttpPost("GetEmpUserRole")]
public ActionResult GetEmpUserRole(List<SearchEmployee> searchEmps)