UNPKG

@hmcts/rpx-xui-node-lib

Version:

Common nodejs library components for XUI

144 lines 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getUserSessionTimeout = exports.sortUserRoles = exports.anyRolesMatch = exports.isRoleMatch = exports.DEFAULT_SESSION_TIMEOUT = void 0; const arrayPatternMatch_1 = require("./arrayPatternMatch"); const stringPatternMatch_1 = require("./stringPatternMatch"); /** * Default Session Idle Time * * If the timeout configuration has not been set, or the User has no roles ( although the * User shouldn't reach this point if they have no roles associated with them ) the * default session idle time will be used. */ exports.DEFAULT_SESSION_TIMEOUT = { idleModalDisplayTime: 10, pattern: 'ERROR: NO-SESSION_TIMEOUT_SET. You need to set a DEFAULT Session Timeout for this application through' + 'the configuration file. ie. use the pattern ".", @see unit tests. The totalIdleTime will be set to a low value.', totalIdleTime: 480, }; /** * Is Role Match * * Checks if a User's role, matches a specified Regular Expression. * * We use a Regular Expression so that we can set the default session timeout via configuration ie. '.', hence we used * JS .match over .includes. * * There will be a different default session timeout per application, and different session timeouts per user groups * hence setting it via configuration. * * The following AC apply: * * should return true if there is a match of the User's role to the Session Timeout regex pattern so * that the App knows that we need to have a specified Session Timeout for that user role. * should return true if there is a partial match of the User's role to the Session Timeout regex pattern. * should return false if there is no match of the User's role to the Session Timeout regex pattern. * should return true for a wildcard regex pattern, note that this pattern acts as our configurable DEFAULT. * * @param role - 'pui-case-manager' * @param pattern - 'case-manager' / 'pui-' / '.' * @returns {boolean} */ const isRoleMatch = (role, pattern) => { return (0, stringPatternMatch_1.isStringPatternMatch)(role, pattern); }; exports.isRoleMatch = isRoleMatch; /** * Any Roles Match * * Checks an array of roles for pattern matches. * * The following AC apply: * * should return true if any of a Users roles match a regex pattern. * should return true if any of a Users roles match a Regular Expression wildcard. * should return false if none of a Users roles match the regex pattern. * * @param roles - [ * 'pui-case-manager', * 'pui-finance-manager', * ] * @param pattern - 'case-manager' / 'pui-' / '.' */ const anyRolesMatch = (roles, pattern) => { return (0, arrayPatternMatch_1.arrayPatternMatch)(roles, pattern); }; exports.anyRolesMatch = anyRolesMatch; /** * Sort User Roles * * Should sort the User's Roles alphabetically. Why? So that a priority order can be given to the Session Timeout + * configuration list. * * We clone the original array, so that we avoid mutation. * * Example: If we want a PUI Session Timeout to be given preference over another Session Timeout it would be further * up the Session Timeout Configuration list. * * @param - [ * 'caseworker-divorce-financialremedy', * 'pui-user-manager', * 'caseworker-probate-solicitor', * 'caseworker', * 'caseworker-probate', * 'pui-finance-manager', * 'caseworker-divorce-solicitor', * ] * @return - [ * 'caseworker', * 'caseworker-divorce-financialremedy', * 'caseworker-divorce-solicitor', * 'caseworker-probate', * 'caseworker-probate-solicitor', * 'pui-user-manager', * 'pui-finance-manager', * ] */ const sortUserRoles = (roles) => roles.sort(); exports.sortUserRoles = sortUserRoles; /** * Get User Session Timeout * * We calculate the timeout for this user. * * A user is given a specified timeout based on their User Roles, and a given set of * statically configured Session Timeouts, defined by the XUI team for a User Role Group. * * Example: * * A Department of Work & Pensions User on Manage Cases should have a Total Idle Time of 12 minutes, and * and should show the Session Timeout Modal 3 minutes before the end of their session. * * Whereas a Manage Organisation application user should have an Total Idle Time of 50 minutes, * and should show the Session Timeout Modal 10 minutes before the end of their session. * * Note that the Session Timeout needs to be easily configurable and will change for each XUI * application, and each User role group. * * Important: the Session Timeout configuration should be in PRIORITY ORDER, with the DEFAULT for * this application being the last item in the array. * * Jargon: * * Session Timeout Modal - The modal popup that appears BEFORE the users Total Idle Time is over. * Total Idle Time - The Users total idle time, this includes time in which we show the Session Timeout Modal to the User. * Session Timeout Configuration - An array that contains the Applications and User Groups session timeout times. * Session Timeout - The idle timeout time for that User. * * @param userRoles - [ * 'pui-organisation-manager', * ] * @param sessionTimeouts - @see unit tests * @returns */ const getUserSessionTimeout = (userRoles, sessionTimeouts) => { const sortedUserRoles = (0, exports.sortUserRoles)(userRoles); for (const sessionTimeout of sessionTimeouts) { if ((0, exports.anyRolesMatch)(sortedUserRoles, sessionTimeout.pattern)) { return sessionTimeout; } } return exports.DEFAULT_SESSION_TIMEOUT; }; exports.getUserSessionTimeout = getUserSessionTimeout; //# sourceMappingURL=userTimeout.js.map