Files
wmantly 3790e8001a Add Unix/POSIX service accounts, distinct from LDAP bind-only ones
The Integrations page's Service Accounts (bind-only, organizationalRole)
don't cover the other real use case: an account something actually runs
as on a Linux host -- a media manager, a torrent client, Emby -- with a
real uidNumber/gidNumber that owns files, and a group other accounts
join for write access (e.g. a `stuff_manager` group granting write
rights to a media library). That needs a real posixAccount, which the
bind-only model can't be.

- New well-known group `app_sso_service_account`, seeded the same way as
  app_sso_admin/app_sso_invite/app_sso_oauth_admin (docker-entrypoint.sh,
  ops/ldap-setup.sh). Not a permission gate -- a marker.
- "Add new user" form gets a "This is a service account" checkbox: swaps
  the person-shaped fields (first/last name, birthday, ToS agreement)
  for a single account-name field, since none of those make sense for a
  non-person account. On create, the route adds the user to
  app_sso_service_account.
- User.listDetail() annotates each user with isServiceAccount (checked
  against the marker group's member list once per call, not the memberof
  overlay's reverse attribute -- not reliably returned by every LDAP
  server this app might point at, confirmed against a real external
  directory during testing). Users page shows a "service" badge.
- Notification broadcasts (filter_type=all/all_active) exclude service
  accounts by default -- nobody reads mail as `stuff_manager`.
- Fixed a real, previously-unrelated bug this surfaced: addPosixAccount
  unconditionally set `mail: data.mail` in the LDAP entry even when
  undefined, and ldapts/slapd reject an attribute given an explicit
  undefined value ("no values for attribute type") rather than treating
  it as absent. This meant creating ANY user without an email already
  failed outright -- not something a service account (which commonly has
  no real mailbox) could route around. Made mail conditional, matching
  how mobile/sshPublicKey/dob already work.
- docs/ldap.md now explains both kinds of service account side by side
  and when to use which.

Verified end-to-end against a real external LDAP server (not a local
sandbox): created a service account with no email, confirmed it's
correctly flagged and excluded from broadcast recipient resolution,
confirmed a normal user is unaffected, confirmed the code degrades
gracefully if the marker group doesn't exist yet (pre-upgrade
deployments).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-15 20:52:50 -04:00

110 lines
3.5 KiB
JavaScript

'use strict';
const router = require('express').Router();
const {User} = require('../models/user_ldap');
const {Group} = require('../models/group_ldap');
const {Notification} = require('../models/notification');
const {Mail} = require('../models/email');
const permission = require('../utils/permission');
async function resolveRecipients(filter_type, filter_value, active_only) {
if (filter_type === 'all' || filter_type === 'all_active') {
// Service accounts (media managers, app service users, ...) aren't
// read by anyone -- a broadcast to "all users" shouldn't include them.
// Target one explicitly via filter_type=users/group if it ever needs
// its own notification.
const users = (await User.listDetail()).filter(u => !u.isServiceAccount);
const active = filter_type === 'all_active' || active_only;
return active ? users.filter(u => !u.pwdAccountLockedTime) : users;
}
if (filter_type === 'group') {
const groupNames = filter_value.split(',').map(s => s.trim()).filter(Boolean);
const groups = await Promise.all(groupNames.map(name => Group.get(name).catch(() => null)));
const dnSet = new Set();
groups.filter(Boolean).forEach(group => {
[].concat(group.member || []).forEach(dn => dnSet.add(dn));
});
const uids = [...dnSet].map(dn => {
const m = dn.match(/^uid=([^,]+)/i);
return m ? m[1] : null;
}).filter(Boolean);
const users = (await Promise.all(uids.map(uid => User.get(uid).catch(() => null)))).filter(Boolean);
return active_only ? users.filter(u => !u.pwdAccountLockedTime) : users;
}
if (filter_type === 'users') {
const uids = JSON.parse(filter_value);
const users = await Promise.all(uids.map(uid => User.get(uid).catch(() => null)));
return users.filter(Boolean);
}
throw Object.assign(new Error('Invalid filter_type'), { status: 400 });
}
router.post('/', async function(req, res, next) {
try {
await permission.byGroup(req.user, ['app_sso_admin']);
const { subject, message, filter_type, filter_value = '', active_only = false } = req.body;
if (!subject || !message || !filter_type) {
return res.status(400).json({ name: 'ValidationError', message: 'subject, message, and filter_type are required' });
}
const recipients = await resolveRecipients(filter_type, filter_value, active_only);
const record = await Notification.create({
created_by: req.user.uid,
subject,
message,
filter_type,
filter_value: String(filter_value),
active_only: Boolean(active_only),
});
let sent = 0, failed = 0;
for (const user of recipients) {
if (!user.mail) { failed++; continue; }
try {
await Mail.sendTemplate(user.mail, 'notification', {
givenName: user.givenName || user.uid,
subject,
message,
});
sent++;
} catch(e) {
console.error(`Notification send failed for ${user.uid}:`, e.message);
failed++;
}
}
await record.update({ status: 'sent', sent_count: sent, failed_count: failed, sent_at: Date.now() });
return res.json({ results: record });
} catch(e) {
next(e);
}
});
router.get('/', async function(req, res, next) {
try {
await permission.byGroup(req.user, ['app_sso_admin']);
const list = await Notification.listDetail();
list.sort((a, b) => (b.created_on || 0) - (a.created_on || 0));
return res.json({ results: list });
} catch(e) {
next(e);
}
});
router.get('/:id', async function(req, res, next) {
try {
await permission.byGroup(req.user, ['app_sso_admin']);
return res.json({ results: await Notification.get(req.params.id) });
} catch(e) {
next(e);
}
});
module.exports = router;