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
148
149
150
151
152
'use strict';
const API = '/pim-api';
let me = null;
let orgs = [];
let attrs = {};
async function boot() {
try {
console.log('Fetching /pim-api/me...');
const res = await fetch('/pim-api/me');
console.log('Response status:', res.status);
if (res.status === 401) {
document.getElementById('auth').innerHTML = '<h1>Authentication Required</h1><p>Start Authelia:</p><pre>docker run -d --name authelia -p 9091:9091 -v $(pwd)/etc/authelia.yml:/etc/authelia/authelia.yml -v $(pwd)/etc/authelia-users.yml:/etc/authelia/users.yml authelia/authelia:latest</pre><p>Then uncomment proxy line in test/lighttpd-dev.conf and restart.</p>';
return;
}
if (!res.ok) throw new Error('HTTP ' + res.status);
me = await res.json();
console.log('Authenticated as:', me.user);
if (!me.roles || !me.roles.pim_admin) {
document.getElementById('auth').innerHTML = '<h1>Access Denied</h1><p>User ' + (me.user || 'unknown') + ' does not have pim_admin role. Login as admin/admin.</p>';
return;
}
document.getElementById('auth').style.display = 'none';
document.getElementById('attrs').style.display = 'block';
document.getElementById('whoami').textContent = me.user + ' ยท pim_admin';
await loadOrgs();
} catch (e) {
console.error(e);
document.getElementById('auth').innerHTML = '<h1>Error</h1><p>' + e.message + '</p><p>Check console (F12) for details.</p>';
}
}
async function loadOrgs() {
try {
const res = await fetch(API + '/attrs');
if (!res.ok) throw new Error('Failed to load attributes: ' + res.status);
const data = await res.json();
attrs = {};
orgs = [...new Set(data.map(a => a.org_code))].sort();
data.forEach(a => {
if (!attrs[a.org_code]) attrs[a.org_code] = {};
attrs[a.org_code][a.attr_key] = { type: a.attr_type, value: a.attr_value };
});
const sel = document.getElementById('org');
sel.innerHTML = '<option value="">(select org)</option>';
orgs.forEach(org => {
const opt = document.createElement('option');
opt.value = org;
opt.textContent = org;
sel.appendChild(opt);
});
} catch (e) {
showError(e.message);
}
}
function loadAttrs() {
const org = document.getElementById('org').value;
const div = document.getElementById('org-attrs');
const list = document.getElementById('attrs-list');
if (!org) {
div.style.display = 'none';
return;
}
div.style.display = 'block';
list.innerHTML = '';
const orgAttrs = attrs[org] || {};
Object.entries(orgAttrs).forEach(([key, data]) => {
const row = document.createElement('div');
row.className = 'attr-row';
row.innerHTML = `
<span><strong>${key}</strong> (${data.type})</span>
<span style="font-size: 12px; color: var(--muted);">${data.value || '(empty)'}</span>
<button class=danger onclick="delAttr('${org}', '${key}')">Delete</button>
`;
list.appendChild(row);
});
document.getElementById('key').value = '';
document.getElementById('type').value = 'text';
}
async function addAttr() {
const org = document.getElementById('org').value;
const key = document.getElementById('key').value.trim();
const type = document.getElementById('type').value;
if (!org || !key) {
showError('Select organization and enter attribute key');
return;
}
const params = new URLSearchParams();
params.append('org', org);
params.append('key', key);
params.append('type', type);
params.append('value', '');
try {
const res = await fetch(API + '/attrs', {
method: 'POST',
body: params
});
if (!res.ok) throw new Error(await res.text());
await loadOrgs();
loadAttrs();
} catch (e) {
showError(e.message);
}
}
async function delAttr(org, key) {
if (!confirm(`Delete ${key}?`)) return;
const params = new URLSearchParams();
params.append('org', org);
params.append('key', key);
params.append('type', 'text');
params.append('value', '');
try {
const res = await fetch(API + '/attrs', {
method: 'POST',
body: params
});
if (!res.ok) throw new Error(await res.text());
await loadOrgs();
loadAttrs();
} catch (e) {
showError(e.message);
}
}
function showError(msg) {
const el = document.getElementById('error');
el.textContent = msg;
el.style.display = 'block';
setTimeout(() => el.style.display = 'none', 5000);
}
boot();