mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +00:00
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).
This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.
Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:
* 7 `src:` values built from a role variable holding a bare filename
(svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
The variable doubles as the destination name inside the container, so the
language segment belongs at the call site, not in the variable.
* `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
18 roles ship exactly that and have no template fallback: the play would
have died on each.
* `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
interface now reads `files/javascript/javascript.js`.
* `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
path through `path_absolute(... 'files', ...)`.
* 4 `ansible.builtin.script:` invocations naming a bare command
(pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
* `dev-python/files/shell/install.sh` pointed at a sibling role's script.
Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
188 lines
6.0 KiB
JavaScript
188 lines
6.0 KiB
JavaScript
/* logoutPatch.js */
|
||
(function(global) {
|
||
|
||
/**
|
||
* Initialize the logout patch script.
|
||
* @param {string} logoutUrl - Logout URL (e.g., from your OIDC client).
|
||
* @param {boolean} debugMode - If true, debug logging is enabled.
|
||
*/
|
||
function initLogoutPatch(logoutUrl, debugMode) {
|
||
const DEBUG = !!debugMode;
|
||
|
||
function log(reason, el, extra) {
|
||
if (!DEBUG) return;
|
||
console.debug('[logoutPatch]', reason, extra || {}, el || null);
|
||
}
|
||
|
||
function matchesLogout(str) {
|
||
const matched = str && /(?:^|\W)log\s*out(?:\W|$)|logout/i.test(str);
|
||
if (matched) log('matchesLogout', null, { value: str });
|
||
return matched;
|
||
}
|
||
|
||
/**
|
||
* Returns true if any attribute name or value on the given element
|
||
* contains the substring "logout" (case-insensitive).
|
||
*
|
||
* @param {Element} element – The DOM element to inspect.
|
||
* @returns {boolean} – True if "logout" appears in any relevant attribute name or value.
|
||
*/
|
||
function containsLogoutAttribute(element) {
|
||
for (const attribute of element.attributes) {
|
||
const name = attribute.name || '';
|
||
const value = attribute.value || '';
|
||
|
||
// Strong indicator: attribute *name* contains "logout"
|
||
if (/logout/i.test(name)) {
|
||
log('containsLogoutAttribute (name match)', element, {
|
||
attrName: name,
|
||
attrValue: value
|
||
});
|
||
return true;
|
||
}
|
||
|
||
// Only consider values of semantic attributes (NOT href/action)
|
||
if ((name.startsWith('data-') || name.startsWith('aria-')) && /logout/i.test(value)) {
|
||
log('containsLogoutAttribute (data/aria value match)', element, {
|
||
attrName: name,
|
||
attrValue: value
|
||
});
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
function matchesTechnicalIndicators(el) {
|
||
const title = el.getAttribute('title');
|
||
const ariaLabel = el.getAttribute('aria-label');
|
||
const onclick = el.getAttribute('onclick');
|
||
|
||
if (matchesLogout(title)) return true;
|
||
if (matchesLogout(ariaLabel)) return true;
|
||
if (matchesLogout(onclick)) return true;
|
||
|
||
for (const attr of el.attributes) {
|
||
if (attr.name.startsWith('data-') &&
|
||
matchesLogout(attr.name + attr.value)) {
|
||
log('matchesTechnicalIndicators (data-* match)', el, {
|
||
attrName: attr.name,
|
||
attrValue: attr.value
|
||
});
|
||
return true;
|
||
}
|
||
}
|
||
|
||
if (typeof el.onclick === 'function' &&
|
||
matchesLogout(el.onclick.toString())) {
|
||
log('matchesTechnicalIndicators (onclick function match)', el);
|
||
return true;
|
||
}
|
||
|
||
if (el.tagName.toLowerCase() === 'use') {
|
||
const href = el.getAttribute('xlink:href') || el.getAttribute('href');
|
||
if (matchesLogout(href)) {
|
||
log('matchesTechnicalIndicators (<use> href match)', el, { href });
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* Apply logout redirect behavior to a matching element:
|
||
* – Installs a capturing click‐handler to force navigation to logoutUrl
|
||
* – Always sets href/formaction/action to logoutUrl
|
||
* – Marks the element as patched to avoid double‐binding
|
||
*
|
||
* @param {Element} el – The element to override (e.g. <a>, <button>, <form>, <input>)
|
||
* @param {string} logoutUrl – The logout URL to navigate to
|
||
*/
|
||
function overrideLogout(el, logoutUrl) {
|
||
// avoid patching the same element twice
|
||
if (el.dataset._logoutHandled) {
|
||
log('overrideLogout skipped (already handled)', el);
|
||
return;
|
||
}
|
||
el.dataset._logoutHandled = "true";
|
||
|
||
log('overrideLogout applied', el, { logoutUrl });
|
||
|
||
// show pointer cursor
|
||
el.style.cursor = 'pointer';
|
||
|
||
// capture‐phase listener so it fires before any framework handlers
|
||
el.addEventListener('click', function(e) {
|
||
log('click intercepted, redirecting to logoutUrl', el);
|
||
e.preventDefault();
|
||
window.location.href = logoutUrl;
|
||
}, { capture: true });
|
||
|
||
const tag = el.tagName.toLowerCase();
|
||
|
||
// always set the link target on <a>
|
||
if (tag === 'a') {
|
||
el.setAttribute('href', logoutUrl);
|
||
}
|
||
// always set the formaction on <button> or <input>
|
||
else if ((tag === 'button' || tag === 'input') && el.hasAttribute('formaction')) {
|
||
el.setAttribute('formaction', logoutUrl);
|
||
}
|
||
// always set the form action on <form>
|
||
else if (tag === 'form') {
|
||
el.setAttribute('action', logoutUrl);
|
||
}
|
||
}
|
||
|
||
function scanAndPatch(elements) {
|
||
elements.forEach(el => {
|
||
const tagName = el.tagName.toLowerCase();
|
||
const isPotential = ['a','button','input','use'].includes(tagName);
|
||
if (!isPotential) return;
|
||
|
||
// Prevent massive matches on huge containers
|
||
let text = el.innerText;
|
||
if (text && text.length > 1000) {
|
||
text = ''; // ignore very large blocks
|
||
}
|
||
|
||
const match =
|
||
matchesLogout(el.getAttribute('name')) ||
|
||
matchesLogout(el.id) ||
|
||
matchesLogout(el.className) ||
|
||
matchesLogout(text) ||
|
||
containsLogoutAttribute(el) ||
|
||
matchesTechnicalIndicators(el);
|
||
|
||
if (match) {
|
||
log('scanAndPatch match', el, { tagName });
|
||
overrideLogout(el, logoutUrl);
|
||
}
|
||
});
|
||
}
|
||
|
||
// Initial scan
|
||
log('initial scan start');
|
||
scanAndPatch(Array.from(document.querySelectorAll('*')));
|
||
log('initial scan end');
|
||
|
||
// Watch for dynamic content
|
||
const observer = new MutationObserver(mutations => {
|
||
mutations.forEach(mutation => {
|
||
mutation.addedNodes.forEach(node => {
|
||
if (!(node instanceof Element)) return;
|
||
log('MutationObserver (new element)', node);
|
||
scanAndPatch([node, ...node.querySelectorAll('*')]);
|
||
});
|
||
});
|
||
});
|
||
observer.observe(document.body, { childList: true, subtree: true });
|
||
|
||
log('MutationObserver attached');
|
||
}
|
||
|
||
// Expose to global scope
|
||
global.initLogoutPatch = initLogoutPatch;
|
||
})(window);
|