-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPermissionRoleMatrix.js
More file actions
147 lines (131 loc) · 5.32 KB
/
PermissionRoleMatrix.js
File metadata and controls
147 lines (131 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
$(document).ready(function () {
const urlParams = new URLSearchParams(window.location.search);
const isExpanded = urlParams.get('Render') === 'Expanded';
const exportTitle = `${abp.currentTenant.name}_${(new Date()).toISOString().slice(0, 10)}_Permission-Role Matrix`;
const userRoles = abp.currentUser.roles;
const userPolicies = abp.auth.grantedPolicies;
let _permissionsModal = new abp.ModalManager(
abp.appPath + 'AbpPermissionManagement/PermissionManagementModal'
);
_permissionsModal.onClose(function () {
// Refresh the page to show updated permissions
window.location.reload();
});
const roleColumnIndexes = [];
$('#permissionTable th[data-role-header]').each(function (index) {
roleColumnIndexes.push($(this).index());
});
const columnDefs = [
{
targets: 0,
orderable: true,
className: 'notexport'
}
];
if (roleColumnIndexes.length > 0) {
columnDefs.push({
targets: roleColumnIndexes,
orderable: false
});
}
$.fn.dataTable.Buttons.defaults.dom.button.className = 'btn flex-none';
let localTable = $('#permissionTable').DataTable({
paging: false,
searching: true,
ordering: true,
fixedHeader: {
header: true,
footer: false,
headerOffset: 0
},
scrollX: true,
scrollCollapse: true,
dom: 'Blfrtip',
columnDefs: columnDefs,
buttons: [
{
text: isExpanded
? '<i class="fl fl-back-to-window align-middle"></i> <span>View Simple</span>'
: '<i class="fl fl-fullscreen align-middle"></i> <span>View Expanded</span>',
className: 'btn-light rounded-1',
action: function (e, dt, button, config) {
window.location = isExpanded
? '/Identity/Roles/PermissionRoleMatrix'
: '/Identity/Roles/PermissionRoleMatrix?Render=Expanded';
}
},
{
extend: 'copy',
text: 'Copy',
title: exportTitle,
className: 'custom-table-btn flex-none btn btn-secondary',
exportOptions: {
columns: ':visible:not(.notexport)'
}
},
{
extend: 'csv',
text: 'Export',
title: exportTitle,
className: 'custom-table-btn flex-none btn btn-secondary',
exportOptions: {
columns: ':visible:not(.notexport)'
}
}
],
createdRow: function (row, data, dataIndex) {
$('td', row).each(function () {
let cellText = $(this).text().trim();
// Check if the cell text matches a key in userPolicies
if (userPolicies[cellText]) {
$(this).addClass('text-decoration-underline');
}
if (cellText === "TRUE") {
$(this).addClass('bg-success text-dark bg-opacity-25 fw-bold border border-success dt-center');
}
});
},
initComplete: function () {
const table = this.api();
// Required for DataTable 1.x - Disable sorting for role name columns after table initialization
// Explicitly disable ordering for role columns using the API
$('th[data-role-header]').removeClass('sorting').addClass('sorting_disabled');
const headers = table.columns().header().toArray().map(header => $(header).text().trim());
// Highlight columns where the header matches a user role
headers.forEach((header, index) => {
if (userRoles.includes(header.toLowerCase())) {
$(table.column(index).header()).addClass('text-decoration-underline');
table.column(index).nodes().to$().filter(function () {
return !$(this).text().trim();
}).addClass('bg-light');
}
});
}
});
localTable.buttons().container().prependTo('#dynamicButtonContainerId');
$("#search").on('input', function () {
localTable.search($(this).val()).draw();
});
// Hide spinner and show table after initialization
$('.loading-spinner').hide();
$('#permissionTable').show();
// Add click handlers to role column headers using data-role-header attribute
$(document).on('click', 'th[data-role-header]', function () {
const roleName = $(this).attr('data-role-header');
_permissionsModal.open({
providerName: 'R',
providerKey: roleName,
providerKeyDisplayName: roleName
});
});
// Add styles to all headers, including dynamically created ones
$(document).on('mouseover', 'th[data-role-header]', function () {
$(this).css('cursor', 'pointer');
if (!$(this).attr('title')) {
$(this).attr('title', 'Click to manage permissions for this role');
}
});
init(localTable);
// Workaround - required until Datatables.net 2.x
$('th[data-role-header]').removeClass('sorting').addClass('sorting_disabled');
});