cynx-lib 1.3.99 → 1.3.101
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gen.js +77 -77
- package/package.json +5 -1
- package/ui/components/ActionsMenu.jsx +357 -357
- package/ui/components/Drawer.jsx +208 -208
- package/ui/components/HoverCard.jsx +85 -85
- package/ui/components/Pagination.jsx +145 -145
- package/ui/components/PrivacyBlur.jsx +603 -603
- package/ui/components/XTable.jsx +112 -112
- package/utils/index.cjs +5 -0
- package/utils/index.js +23 -21
- package/utils/utils/duration.js +139 -139
- package/utils/utils/oidc.js +214 -214
- package/utils/utils/realtime.js +1634 -1634
- package/utils/utils/server.js +371 -371
- package/utils/utils/session.js +131 -131
- package/utils/utils/session_manager.cjs +17 -11
- package/utils/utils/session_manager.js +35 -29
- package/utils/utils/sso_manager.cjs +621 -0
- package/utils/utils/sso_manager.js +702 -0
- package/utils/utils/storage.js +393 -393
- package/utils/utils/ui.js +111 -111
- package/vue/components/Badge.vue +79 -79
- package/vue/components/Button.vue +209 -209
- package/vue/components/Checkbox.vue +103 -103
- package/vue/components/CircularProgress.vue +383 -383
- package/vue/components/Divider.vue +94 -94
- package/vue/components/Drawer.vue +243 -243
- package/vue/components/InfoBadge.vue +223 -223
- package/vue/components/Input.vue +341 -341
- package/vue/components/LinearProgress.vue +268 -268
- package/vue/components/Modal.vue +226 -226
- package/vue/components/Pagination.vue +199 -199
- package/vue/components/Select.vue +713 -713
- package/vue/components/Sidebar.vue +955 -955
- package/vue/components/Skeleton.vue +91 -91
- package/vue/components/Spinner.vue +263 -263
- package/vue/components/StatusTag.vue +89 -89
- package/vue/components/TabBar.vue +176 -176
- package/vue/components/Table.vue +388 -388
- package/vue/components/TimePicker.vue +399 -399
- package/vue/components/Toggle.vue +115 -115
- package/vue/components/XTable.vue +140 -140
- package/vue/composables/useAutoSkeletonRows.ts +71 -71
- package/vue/index.d.ts +67 -67
- package/vue/index.js +86 -86
package/utils/utils/session.js
CHANGED
|
@@ -1,131 +1,131 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* cynx/utils — Session and Device Resolution Utilities (session.js)
|
|
3
|
-
*
|
|
4
|
-
* Helpers for generating secure session tokens and extracting human-friendly device names
|
|
5
|
-
* from request headers and user-agent strings.
|
|
6
|
-
*
|
|
7
|
-
* STRICT NO EMOJIS!
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { getRandomBytes } from './uid.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Generate a cryptographically secure random session token.
|
|
14
|
-
* Example: "st_9b1deb4d3b7d4bad9bdd2b0d7b3dcb6d..."
|
|
15
|
-
*
|
|
16
|
-
* @param {number} [byteLength=32]
|
|
17
|
-
* @returns {string}
|
|
18
|
-
*/
|
|
19
|
-
export function genSessionToken(byteLength = 32) {
|
|
20
|
-
const bytes = getRandomBytes(byteLength);
|
|
21
|
-
let hex = '';
|
|
22
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
-
hex += bytes[i].toString(16).padStart(2, '0');
|
|
24
|
-
}
|
|
25
|
-
return `st_${hex}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Extract human-readable device label from User-Agent string.
|
|
30
|
-
*
|
|
31
|
-
* @param {string} ua User-Agent string
|
|
32
|
-
* @returns {string} e.g. "Windows 10/11 PC", "MacBook / Mac", "iPhone", "Android Phone", "Linux PC"
|
|
33
|
-
*/
|
|
34
|
-
export function getDeviceLabel(ua) {
|
|
35
|
-
if (!ua) return 'PC';
|
|
36
|
-
if (/windows phone/i.test(ua)) return 'Windows Phone';
|
|
37
|
-
if (/windows nt 10\.0/i.test(ua)) return 'Windows 10/11 PC';
|
|
38
|
-
if (/windows nt 6\.3/i.test(ua)) return 'Windows 8.1 PC';
|
|
39
|
-
if (/windows nt 6\.1/i.test(ua)) return 'Windows 7 PC';
|
|
40
|
-
if (/win64|win32|windows/i.test(ua)) return 'Windows PC';
|
|
41
|
-
if (/ipad/i.test(ua)) return 'iPad';
|
|
42
|
-
if (/iphone/i.test(ua)) return 'iPhone';
|
|
43
|
-
if (/macintosh|mac os x/i.test(ua)) return 'MacBook / Mac';
|
|
44
|
-
if (/android/i.test(ua)) return /mobile/i.test(ua) ? 'Android Phone' : 'Android Tablet';
|
|
45
|
-
if (/cros/i.test(ua)) return 'Chromebook';
|
|
46
|
-
if (/linux/i.test(ua)) return 'Linux PC';
|
|
47
|
-
return 'Desktop PC';
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Parse User-Agent string into browser, OS, and device type.
|
|
52
|
-
*
|
|
53
|
-
* @param {string} ua User-Agent string
|
|
54
|
-
* @returns {{ browser: string, os: string, deviceType: 'desktop' | 'mobile' | 'tablet' }}
|
|
55
|
-
*/
|
|
56
|
-
export function parseUserAgent(ua) {
|
|
57
|
-
if (!ua) return { browser: 'Unknown', os: 'Unknown', deviceType: 'desktop' };
|
|
58
|
-
|
|
59
|
-
let os = 'Unknown';
|
|
60
|
-
let browser = 'Unknown';
|
|
61
|
-
let deviceType = 'desktop';
|
|
62
|
-
|
|
63
|
-
if (/windows phone/i.test(ua)) { os = 'Windows Phone'; deviceType = 'mobile'; }
|
|
64
|
-
else if (/win64|win32|windows/i.test(ua)) { os = 'Windows'; deviceType = 'desktop'; }
|
|
65
|
-
else if (/ipad/i.test(ua)) { os = 'iPadOS'; deviceType = 'tablet'; }
|
|
66
|
-
else if (/iphone|ipod/i.test(ua)) { os = 'iOS'; deviceType = 'mobile'; }
|
|
67
|
-
else if (/android/i.test(ua)) { os = 'Android'; deviceType = /mobile/i.test(ua) ? 'mobile' : 'tablet'; }
|
|
68
|
-
else if (/macintosh|mac os x/i.test(ua)) { os = 'macOS'; deviceType = 'desktop'; }
|
|
69
|
-
else if (/cros/i.test(ua)) { os = 'Chrome OS'; deviceType = 'desktop'; }
|
|
70
|
-
else if (/linux/i.test(ua)) { os = 'Linux'; deviceType = 'desktop'; }
|
|
71
|
-
|
|
72
|
-
if (/edg\//i.test(ua)) {
|
|
73
|
-
const m = ua.match(/edg\/(\d+)/i);
|
|
74
|
-
browser = m ? `Edge ${m[1]}` : 'Edge';
|
|
75
|
-
} else if (/opr\/|opera/i.test(ua)) {
|
|
76
|
-
const m = ua.match(/(?:opr|opera)\/(\d+)/i);
|
|
77
|
-
browser = m ? `Opera ${m[1]}` : 'Opera';
|
|
78
|
-
} else if (/chrome|crios/i.test(ua)) {
|
|
79
|
-
const m = ua.match(/(?:chrome|crios)\/(\d+)/i);
|
|
80
|
-
browser = m ? `Chrome ${m[1]}` : 'Chrome';
|
|
81
|
-
} else if (/firefox|fxios/i.test(ua)) {
|
|
82
|
-
const m = ua.match(/(?:firefox|fxios)\/(\d+)/i);
|
|
83
|
-
browser = m ? `Firefox ${m[1]}` : 'Firefox';
|
|
84
|
-
} else if (/safari/i.test(ua) && !/chrome|crios/i.test(ua)) {
|
|
85
|
-
const m = ua.match(/version\/(\d+)/i);
|
|
86
|
-
browser = m ? `Safari ${m[1]}` : 'Safari';
|
|
87
|
-
} else if (/postman/i.test(ua)) {
|
|
88
|
-
browser = 'Postman API';
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return { browser, os, deviceType };
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Derive human-readable device name from Express/Fastify request or User-Agent string.
|
|
96
|
-
*
|
|
97
|
-
* @param {object} [req] HTTP request object
|
|
98
|
-
* @param {string} [userAgent=''] User-Agent string
|
|
99
|
-
* @returns {string} e.g. "Windows 10/11 PC", "MacBook / Mac", "iPhone", "Android Phone", "Desktop PC"
|
|
100
|
-
*/
|
|
101
|
-
export function deriveDeviceName(req, userAgent = '') {
|
|
102
|
-
const headers = req?.headers || {};
|
|
103
|
-
const fromHeader = headers['x-device-name'] || headers['x-pc-name'] || headers['x-computer-name'] || '';
|
|
104
|
-
if (typeof fromHeader === 'string' && fromHeader.trim()) {
|
|
105
|
-
return fromHeader.trim().slice(0, 120);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const fromBody = req?.body?.deviceName || req?.body?.device_name || req?.body?.pcName || req?.body?.pc_name;
|
|
109
|
-
if (typeof fromBody === 'string' && fromBody.trim()) {
|
|
110
|
-
return fromBody.trim().slice(0, 120);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const ua = userAgent || headers['user-agent'] || '';
|
|
114
|
-
return getDeviceLabel(ua);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export {
|
|
118
|
-
formatToken,
|
|
119
|
-
parseToken,
|
|
120
|
-
getSessionId,
|
|
121
|
-
extractToken,
|
|
122
|
-
SessionManager,
|
|
123
|
-
createSessionManager,
|
|
124
|
-
} from './session_manager.js';
|
|
125
|
-
|
|
126
|
-
export default {
|
|
127
|
-
genSessionToken,
|
|
128
|
-
getDeviceLabel,
|
|
129
|
-
parseUserAgent,
|
|
130
|
-
deriveDeviceName,
|
|
131
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* cynx/utils — Session and Device Resolution Utilities (session.js)
|
|
3
|
+
*
|
|
4
|
+
* Helpers for generating secure session tokens and extracting human-friendly device names
|
|
5
|
+
* from request headers and user-agent strings.
|
|
6
|
+
*
|
|
7
|
+
* STRICT NO EMOJIS!
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { getRandomBytes } from './uid.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Generate a cryptographically secure random session token.
|
|
14
|
+
* Example: "st_9b1deb4d3b7d4bad9bdd2b0d7b3dcb6d..."
|
|
15
|
+
*
|
|
16
|
+
* @param {number} [byteLength=32]
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
export function genSessionToken(byteLength = 32) {
|
|
20
|
+
const bytes = getRandomBytes(byteLength);
|
|
21
|
+
let hex = '';
|
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
+
hex += bytes[i].toString(16).padStart(2, '0');
|
|
24
|
+
}
|
|
25
|
+
return `st_${hex}`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extract human-readable device label from User-Agent string.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} ua User-Agent string
|
|
32
|
+
* @returns {string} e.g. "Windows 10/11 PC", "MacBook / Mac", "iPhone", "Android Phone", "Linux PC"
|
|
33
|
+
*/
|
|
34
|
+
export function getDeviceLabel(ua) {
|
|
35
|
+
if (!ua) return 'PC';
|
|
36
|
+
if (/windows phone/i.test(ua)) return 'Windows Phone';
|
|
37
|
+
if (/windows nt 10\.0/i.test(ua)) return 'Windows 10/11 PC';
|
|
38
|
+
if (/windows nt 6\.3/i.test(ua)) return 'Windows 8.1 PC';
|
|
39
|
+
if (/windows nt 6\.1/i.test(ua)) return 'Windows 7 PC';
|
|
40
|
+
if (/win64|win32|windows/i.test(ua)) return 'Windows PC';
|
|
41
|
+
if (/ipad/i.test(ua)) return 'iPad';
|
|
42
|
+
if (/iphone/i.test(ua)) return 'iPhone';
|
|
43
|
+
if (/macintosh|mac os x/i.test(ua)) return 'MacBook / Mac';
|
|
44
|
+
if (/android/i.test(ua)) return /mobile/i.test(ua) ? 'Android Phone' : 'Android Tablet';
|
|
45
|
+
if (/cros/i.test(ua)) return 'Chromebook';
|
|
46
|
+
if (/linux/i.test(ua)) return 'Linux PC';
|
|
47
|
+
return 'Desktop PC';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Parse User-Agent string into browser, OS, and device type.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} ua User-Agent string
|
|
54
|
+
* @returns {{ browser: string, os: string, deviceType: 'desktop' | 'mobile' | 'tablet' }}
|
|
55
|
+
*/
|
|
56
|
+
export function parseUserAgent(ua) {
|
|
57
|
+
if (!ua) return { browser: 'Unknown', os: 'Unknown', deviceType: 'desktop' };
|
|
58
|
+
|
|
59
|
+
let os = 'Unknown';
|
|
60
|
+
let browser = 'Unknown';
|
|
61
|
+
let deviceType = 'desktop';
|
|
62
|
+
|
|
63
|
+
if (/windows phone/i.test(ua)) { os = 'Windows Phone'; deviceType = 'mobile'; }
|
|
64
|
+
else if (/win64|win32|windows/i.test(ua)) { os = 'Windows'; deviceType = 'desktop'; }
|
|
65
|
+
else if (/ipad/i.test(ua)) { os = 'iPadOS'; deviceType = 'tablet'; }
|
|
66
|
+
else if (/iphone|ipod/i.test(ua)) { os = 'iOS'; deviceType = 'mobile'; }
|
|
67
|
+
else if (/android/i.test(ua)) { os = 'Android'; deviceType = /mobile/i.test(ua) ? 'mobile' : 'tablet'; }
|
|
68
|
+
else if (/macintosh|mac os x/i.test(ua)) { os = 'macOS'; deviceType = 'desktop'; }
|
|
69
|
+
else if (/cros/i.test(ua)) { os = 'Chrome OS'; deviceType = 'desktop'; }
|
|
70
|
+
else if (/linux/i.test(ua)) { os = 'Linux'; deviceType = 'desktop'; }
|
|
71
|
+
|
|
72
|
+
if (/edg\//i.test(ua)) {
|
|
73
|
+
const m = ua.match(/edg\/(\d+)/i);
|
|
74
|
+
browser = m ? `Edge ${m[1]}` : 'Edge';
|
|
75
|
+
} else if (/opr\/|opera/i.test(ua)) {
|
|
76
|
+
const m = ua.match(/(?:opr|opera)\/(\d+)/i);
|
|
77
|
+
browser = m ? `Opera ${m[1]}` : 'Opera';
|
|
78
|
+
} else if (/chrome|crios/i.test(ua)) {
|
|
79
|
+
const m = ua.match(/(?:chrome|crios)\/(\d+)/i);
|
|
80
|
+
browser = m ? `Chrome ${m[1]}` : 'Chrome';
|
|
81
|
+
} else if (/firefox|fxios/i.test(ua)) {
|
|
82
|
+
const m = ua.match(/(?:firefox|fxios)\/(\d+)/i);
|
|
83
|
+
browser = m ? `Firefox ${m[1]}` : 'Firefox';
|
|
84
|
+
} else if (/safari/i.test(ua) && !/chrome|crios/i.test(ua)) {
|
|
85
|
+
const m = ua.match(/version\/(\d+)/i);
|
|
86
|
+
browser = m ? `Safari ${m[1]}` : 'Safari';
|
|
87
|
+
} else if (/postman/i.test(ua)) {
|
|
88
|
+
browser = 'Postman API';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { browser, os, deviceType };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Derive human-readable device name from Express/Fastify request or User-Agent string.
|
|
96
|
+
*
|
|
97
|
+
* @param {object} [req] HTTP request object
|
|
98
|
+
* @param {string} [userAgent=''] User-Agent string
|
|
99
|
+
* @returns {string} e.g. "Windows 10/11 PC", "MacBook / Mac", "iPhone", "Android Phone", "Desktop PC"
|
|
100
|
+
*/
|
|
101
|
+
export function deriveDeviceName(req, userAgent = '') {
|
|
102
|
+
const headers = req?.headers || {};
|
|
103
|
+
const fromHeader = headers['x-device-name'] || headers['x-pc-name'] || headers['x-computer-name'] || '';
|
|
104
|
+
if (typeof fromHeader === 'string' && fromHeader.trim()) {
|
|
105
|
+
return fromHeader.trim().slice(0, 120);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const fromBody = req?.body?.deviceName || req?.body?.device_name || req?.body?.pcName || req?.body?.pc_name;
|
|
109
|
+
if (typeof fromBody === 'string' && fromBody.trim()) {
|
|
110
|
+
return fromBody.trim().slice(0, 120);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const ua = userAgent || headers['user-agent'] || '';
|
|
114
|
+
return getDeviceLabel(ua);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export {
|
|
118
|
+
formatToken,
|
|
119
|
+
parseToken,
|
|
120
|
+
getSessionId,
|
|
121
|
+
extractToken,
|
|
122
|
+
SessionManager,
|
|
123
|
+
createSessionManager,
|
|
124
|
+
} from './session_manager.js';
|
|
125
|
+
|
|
126
|
+
export default {
|
|
127
|
+
genSessionToken,
|
|
128
|
+
getDeviceLabel,
|
|
129
|
+
parseUserAgent,
|
|
130
|
+
deriveDeviceName,
|
|
131
|
+
};
|
|
@@ -113,6 +113,7 @@ class SessionManager {
|
|
|
113
113
|
usersTable: opts.usersTable || "users",
|
|
114
114
|
rolesTable: opts.rolesTable || "roles",
|
|
115
115
|
settingsTable: opts.settingsTable || "settings",
|
|
116
|
+
employeeColumn: opts.employeeColumn || "employee",
|
|
116
117
|
superRole: (opts.superRole || "superadmin").toLowerCase(),
|
|
117
118
|
tokenPrefix: opts.tokenPrefix || "st_",
|
|
118
119
|
tokenVersion: opts.tokenVersion || 2,
|
|
@@ -236,6 +237,7 @@ class SessionManager {
|
|
|
236
237
|
if (!cleanToken) return null;
|
|
237
238
|
try {
|
|
238
239
|
const parsed = parseToken(cleanToken, this.options.tokenPrefix);
|
|
240
|
+
const empCol = this.options.employeeColumn || "employee";
|
|
239
241
|
let sessionRes;
|
|
240
242
|
if (parsed.version === 2 && parsed.sessionId) {
|
|
241
243
|
const sql = `
|
|
@@ -243,7 +245,7 @@ class SessionManager {
|
|
|
243
245
|
s.expires_at, s.last_activity, s.device_name, s.ip_address,
|
|
244
246
|
u.id as user_id, u.username, u.full_name, u.role, u.is_active as user_active,
|
|
245
247
|
COALESCE(u.is_disabled, false) as user_disabled,
|
|
246
|
-
u
|
|
248
|
+
u.${empCol} as employee,
|
|
247
249
|
u.permissions as user_permissions,
|
|
248
250
|
r.permissions as role_permissions
|
|
249
251
|
FROM ${this.options.sessionsTable} s
|
|
@@ -258,7 +260,7 @@ class SessionManager {
|
|
|
258
260
|
s.expires_at, s.last_activity, s.device_name, s.ip_address,
|
|
259
261
|
u.id as user_id, u.username, u.full_name, u.role, u.is_active as user_active,
|
|
260
262
|
COALESCE(u.is_disabled, false) as user_disabled,
|
|
261
|
-
u
|
|
263
|
+
u.${empCol} as employee,
|
|
262
264
|
u.permissions as user_permissions,
|
|
263
265
|
r.permissions as role_permissions
|
|
264
266
|
FROM ${this.options.sessionsTable} s
|
|
@@ -296,8 +298,9 @@ class SessionManager {
|
|
|
296
298
|
sessionId: row.session_id,
|
|
297
299
|
token: cleanToken,
|
|
298
300
|
role: row.role,
|
|
299
|
-
|
|
300
|
-
|
|
301
|
+
employee: row.employee || null,
|
|
302
|
+
employeeId: row.employee || null,
|
|
303
|
+
leaderId: null,
|
|
301
304
|
userPermissions: row.user_permissions || {},
|
|
302
305
|
rolePermissions: row.role_permissions || {},
|
|
303
306
|
user: {
|
|
@@ -305,8 +308,9 @@ class SessionManager {
|
|
|
305
308
|
username: row.username,
|
|
306
309
|
fullName: row.full_name,
|
|
307
310
|
role: row.role,
|
|
308
|
-
|
|
309
|
-
|
|
311
|
+
employee: row.employee || null,
|
|
312
|
+
employeeId: row.employee || null,
|
|
313
|
+
leaderId: null
|
|
310
314
|
}
|
|
311
315
|
};
|
|
312
316
|
}
|
|
@@ -319,7 +323,7 @@ class SessionManager {
|
|
|
319
323
|
const uId = decoded.userId || decoded.id;
|
|
320
324
|
const uRes = await this._query(
|
|
321
325
|
`SELECT u.id, u.username, u.full_name, u.role, u.is_active, COALESCE(u.is_disabled, false) as is_disabled,
|
|
322
|
-
u
|
|
326
|
+
u.${empCol} as employee, u.permissions as user_permissions, r.permissions as role_permissions
|
|
323
327
|
FROM ${this.options.usersTable} u
|
|
324
328
|
LEFT JOIN ${this.options.rolesTable} r ON r.id = u.role OR r.name = u.role OR LOWER(r.name) = LOWER(u.role)
|
|
325
329
|
WHERE u.id = $1`,
|
|
@@ -333,8 +337,9 @@ class SessionManager {
|
|
|
333
337
|
sessionId: null,
|
|
334
338
|
token: cleanToken,
|
|
335
339
|
role: uRow.role || decoded.role,
|
|
336
|
-
|
|
337
|
-
|
|
340
|
+
employee: uRow.employee || null,
|
|
341
|
+
employeeId: uRow.employee || null,
|
|
342
|
+
leaderId: null,
|
|
338
343
|
userPermissions: uRow.user_permissions || {},
|
|
339
344
|
rolePermissions: uRow.role_permissions || {},
|
|
340
345
|
user: {
|
|
@@ -342,8 +347,9 @@ class SessionManager {
|
|
|
342
347
|
username: uRow.username,
|
|
343
348
|
fullName: uRow.full_name,
|
|
344
349
|
role: uRow.role || decoded.role,
|
|
345
|
-
|
|
346
|
-
|
|
350
|
+
employee: uRow.employee || null,
|
|
351
|
+
employeeId: uRow.employee || null,
|
|
352
|
+
leaderId: null
|
|
347
353
|
}
|
|
348
354
|
};
|
|
349
355
|
}
|
|
@@ -111,30 +111,31 @@ export function extractToken(req) {
|
|
|
111
111
|
*/
|
|
112
112
|
export class SessionManager {
|
|
113
113
|
constructor(queryOrOptions = {}, sessionsTable, usersTable, rolesTable, settingsTable) {
|
|
114
|
-
let opts = {};
|
|
115
|
-
if (typeof queryOrOptions === 'function') {
|
|
116
|
-
opts = {
|
|
117
|
-
query: queryOrOptions,
|
|
118
|
-
...(typeof sessionsTable === 'string' ? { sessionsTable } : (typeof sessionsTable === 'object' && sessionsTable !== null ? sessionsTable : {})),
|
|
119
|
-
...(typeof usersTable === 'string' ? { usersTable } : {}),
|
|
120
|
-
...(typeof rolesTable === 'string' ? { rolesTable } : {}),
|
|
121
|
-
...(typeof settingsTable === 'string' ? { settingsTable } : {}),
|
|
122
|
-
};
|
|
123
|
-
} else if (typeof queryOrOptions === 'string') {
|
|
124
|
-
opts = { sessionsTable: queryOrOptions };
|
|
125
|
-
} else if (queryOrOptions && typeof queryOrOptions === 'object') {
|
|
126
|
-
opts = {
|
|
127
|
-
...queryOrOptions,
|
|
128
|
-
...(typeof sessionsTable === 'string' ? { sessionsTable } : {}),
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
114
|
+
let opts = {};
|
|
115
|
+
if (typeof queryOrOptions === 'function') {
|
|
116
|
+
opts = {
|
|
117
|
+
query: queryOrOptions,
|
|
118
|
+
...(typeof sessionsTable === 'string' ? { sessionsTable } : (typeof sessionsTable === 'object' && sessionsTable !== null ? sessionsTable : {})),
|
|
119
|
+
...(typeof usersTable === 'string' ? { usersTable } : {}),
|
|
120
|
+
...(typeof rolesTable === 'string' ? { rolesTable } : {}),
|
|
121
|
+
...(typeof settingsTable === 'string' ? { settingsTable } : {}),
|
|
122
|
+
};
|
|
123
|
+
} else if (typeof queryOrOptions === 'string') {
|
|
124
|
+
opts = { sessionsTable: queryOrOptions };
|
|
125
|
+
} else if (queryOrOptions && typeof queryOrOptions === 'object') {
|
|
126
|
+
opts = {
|
|
127
|
+
...queryOrOptions,
|
|
128
|
+
...(typeof sessionsTable === 'string' ? { sessionsTable } : {}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
132
|
this.options = {
|
|
133
133
|
query: opts.query || null,
|
|
134
134
|
sessionsTable: opts.sessionsTable || 'sessions',
|
|
135
135
|
usersTable: opts.usersTable || 'users',
|
|
136
136
|
rolesTable: opts.rolesTable || 'roles',
|
|
137
137
|
settingsTable: opts.settingsTable || 'settings',
|
|
138
|
+
employeeColumn: opts.employeeColumn || 'employee',
|
|
138
139
|
superRole: (opts.superRole || 'superadmin').toLowerCase(),
|
|
139
140
|
tokenPrefix: opts.tokenPrefix || 'st_',
|
|
140
141
|
tokenVersion: opts.tokenVersion || 2,
|
|
@@ -267,6 +268,7 @@ export class SessionManager {
|
|
|
267
268
|
|
|
268
269
|
try {
|
|
269
270
|
const parsed = parseToken(cleanToken, this.options.tokenPrefix);
|
|
271
|
+
const empCol = this.options.employeeColumn || 'employee';
|
|
270
272
|
let sessionRes;
|
|
271
273
|
|
|
272
274
|
if (parsed.version === 2 && parsed.sessionId) {
|
|
@@ -276,7 +278,7 @@ export class SessionManager {
|
|
|
276
278
|
s.expires_at, s.last_activity, s.device_name, s.ip_address,
|
|
277
279
|
u.id as user_id, u.username, u.full_name, u.role, u.is_active as user_active,
|
|
278
280
|
COALESCE(u.is_disabled, false) as user_disabled,
|
|
279
|
-
u
|
|
281
|
+
u.${empCol} as employee,
|
|
280
282
|
u.permissions as user_permissions,
|
|
281
283
|
r.permissions as role_permissions
|
|
282
284
|
FROM ${this.options.sessionsTable} s
|
|
@@ -292,7 +294,7 @@ export class SessionManager {
|
|
|
292
294
|
s.expires_at, s.last_activity, s.device_name, s.ip_address,
|
|
293
295
|
u.id as user_id, u.username, u.full_name, u.role, u.is_active as user_active,
|
|
294
296
|
COALESCE(u.is_disabled, false) as user_disabled,
|
|
295
|
-
u
|
|
297
|
+
u.${empCol} as employee,
|
|
296
298
|
u.permissions as user_permissions,
|
|
297
299
|
r.permissions as role_permissions
|
|
298
300
|
FROM ${this.options.sessionsTable} s
|
|
@@ -339,8 +341,9 @@ export class SessionManager {
|
|
|
339
341
|
sessionId: row.session_id,
|
|
340
342
|
token: cleanToken,
|
|
341
343
|
role: row.role,
|
|
342
|
-
|
|
343
|
-
|
|
344
|
+
employee: row.employee || null,
|
|
345
|
+
employeeId: row.employee || null,
|
|
346
|
+
leaderId: null,
|
|
344
347
|
userPermissions: row.user_permissions || {},
|
|
345
348
|
rolePermissions: row.role_permissions || {},
|
|
346
349
|
user: {
|
|
@@ -348,8 +351,9 @@ export class SessionManager {
|
|
|
348
351
|
username: row.username,
|
|
349
352
|
fullName: row.full_name,
|
|
350
353
|
role: row.role,
|
|
351
|
-
|
|
352
|
-
|
|
354
|
+
employee: row.employee || null,
|
|
355
|
+
employeeId: row.employee || null,
|
|
356
|
+
leaderId: null,
|
|
353
357
|
},
|
|
354
358
|
};
|
|
355
359
|
}
|
|
@@ -364,7 +368,7 @@ export class SessionManager {
|
|
|
364
368
|
const uId = decoded.userId || decoded.id;
|
|
365
369
|
const uRes = await this._query(
|
|
366
370
|
`SELECT u.id, u.username, u.full_name, u.role, u.is_active, COALESCE(u.is_disabled, false) as is_disabled,
|
|
367
|
-
u
|
|
371
|
+
u.${empCol} as employee, u.permissions as user_permissions, r.permissions as role_permissions
|
|
368
372
|
FROM ${this.options.usersTable} u
|
|
369
373
|
LEFT JOIN ${this.options.rolesTable} r ON r.id = u.role OR r.name = u.role OR LOWER(r.name) = LOWER(u.role)
|
|
370
374
|
WHERE u.id = $1`,
|
|
@@ -379,8 +383,9 @@ export class SessionManager {
|
|
|
379
383
|
sessionId: null,
|
|
380
384
|
token: cleanToken,
|
|
381
385
|
role: uRow.role || decoded.role,
|
|
382
|
-
|
|
383
|
-
|
|
386
|
+
employee: uRow.employee || null,
|
|
387
|
+
employeeId: uRow.employee || null,
|
|
388
|
+
leaderId: null,
|
|
384
389
|
userPermissions: uRow.user_permissions || {},
|
|
385
390
|
rolePermissions: uRow.role_permissions || {},
|
|
386
391
|
user: {
|
|
@@ -388,8 +393,9 @@ export class SessionManager {
|
|
|
388
393
|
username: uRow.username,
|
|
389
394
|
fullName: uRow.full_name,
|
|
390
395
|
role: uRow.role || decoded.role,
|
|
391
|
-
|
|
392
|
-
|
|
396
|
+
employee: uRow.employee || null,
|
|
397
|
+
employeeId: uRow.employee || null,
|
|
398
|
+
leaderId: null,
|
|
393
399
|
},
|
|
394
400
|
};
|
|
395
401
|
}
|