CJVT updates August and September 2026

This commit is contained in:
Luka Romih
2026-10-01 00:49:26 +02:00
parent fd02e25719
commit 83f73aeb58
135 changed files with 6146 additions and 1970 deletions
+170 -3
View File
@@ -26,6 +26,135 @@ User.isEmailAlreadyTaken = async email => {
return isTaken
}
// Check if a user with provided username already exists, optionally excluding one user id (used
// when re-checking a username against its own current owner, e.g. during account conversion).
User.isUsernameAlreadyTaken = async (username, excludeUserId) => {
const { rows } = await db.query(
`SELECT 1 FROM "user" WHERE username = $1 ${
excludeUserId ? 'AND id <> $2' : ''
}`,
excludeUserId ? [username, excludeUserId] : [username]
)
return rows.length > 0
}
// Fetch the active user linked to a given external identity (auth_provider, subject), if any.
User.fetchByExternalIdentity = async (authProvider, subject) => {
const {
rows: [user]
} = await db.query(
`SELECT u.id, u.status
FROM user_external_identity uei
JOIN "user" u ON u.id = uei.user_id
WHERE uei.auth_provider = $1 AND uei.subject = $2`,
[authProvider, subject]
)
return user
}
// Fetch an active local user by email. Used to detect whether an SSO login's email claim
// matches an existing local account, for the account-linking flow.
User.fetchActiveByEmail = async email => {
const {
rows: [user]
} = await db.query('SELECT id FROM "user" WHERE email = $1 AND status = $2', [
email,
'active'
])
return user
}
// Atomically create a brand-new SSO-only user (no bcrypt_hash) and link it to the given external
// identity. Used the first time someone signs in via an external IdP with no matching account.
User.createFromExternalIdentity = async (authProvider, subject, profile, t) => {
let userId
await db.transaction(async dbClient => {
if (await User.isEmailAlreadyTaken(profile.email)) {
const err = Error(t('Elektronski naslov uporablja že drug uporabnik.'))
err.status = 400
err.displayInProd = true
throw err
}
if (await User.isUsernameAlreadyTaken(profile.username)) {
const err = Error(
t('Izbrano uporabniško ime uporablja že drug uporabnik.')
)
err.status = 400
err.displayInProd = true
throw err
}
const values = [
profile.username,
profile.firstName,
profile.lastName,
profile.email
]
if (profile.language) values.push(profile.language)
// bcrypt_hash/status/time_activated are appended as fixed literals (not bound params) after
// the dynamic column list, on both the column-list and VALUES-tuple side, so the two stay
// positionally aligned regardless of whether the optional `language` column is included.
const {
rows: [user]
} = await dbClient.query(
`INSERT INTO "user" (
username, first_name, last_name, email ${
profile.language ? ', language' : ''
},
bcrypt_hash, status, time_activated
)
VALUES (${db.genParamStr(values)}, NULL, 'active', NOW())
RETURNING id`,
values
)
userId = user.id
await dbClient.query(
'INSERT INTO user_external_identity (auth_provider, subject, user_id) VALUES ($1, $2, $3)',
[authProvider, subject, userId]
)
})
return userId
}
// Permanently convert an existing local user into an SSO-only user: overwrite its
// username/first/last name with the IdP-derived profile, remove its local password (from then on
// it can only sign in via this external identity, exactly like a user created straight from the
// IdP via createFromExternalIdentity), and link the external identity. Used once the user has
// confirmed ownership of the matched local account (its password).
User.convertToExternalIdentity = async (authProvider, subject, userId, profile, t) => {
await db.transaction(async dbClient => {
if (await User.isUsernameAlreadyTaken(profile.username, userId)) {
const err = Error(
t('Izbrano uporabniško ime uporablja že drug uporabnik.')
)
err.status = 400
err.displayInProd = true
throw err
}
await dbClient.query(
`UPDATE "user"
SET username = $1, first_name = $2, last_name = $3, bcrypt_hash = NULL
WHERE id = $4`,
[profile.username, profile.firstName, profile.lastName, userId]
)
await dbClient.query(
'INSERT INTO user_external_identity (auth_provider, subject, user_id) VALUES ($1, $2, $3)',
[authProvider, subject, userId]
)
})
}
// Create new user in DB.
User.create = async (user, t) => {
const {
@@ -208,10 +337,25 @@ User.resetPasswordWithToken = async (token, password, t) => {
;({
rows: [user]
} = await dbClient.query(
'UPDATE "user" SET bcrypt_hash = $1 WHERE id = $2 RETURNING id, username, email',
// Only ever overwrites an EXISTING local password - never sets an initial one for an
// SSO-only account (bcrypt_hash IS NULL). Also guarded earlier, at token-issuance time
// (generateResetPasswordToken); this is defense in depth for any already-issued token.
`UPDATE "user" SET bcrypt_hash = $1 WHERE id = $2 AND bcrypt_hash IS NOT NULL RETURNING id, username, email`,
[bcryptHash, userId]
))
if (!user) {
const err = Error(
t(
'Ta uporabniški račun je bil ustvarjen prek prijave z Univerzo v Ljubljani in nima gesla. Prijavite se prek gumba za prijavo z Univerzo v Ljubljani.'
)
)
err.status = 403
err.displayInProd = true
throw err
}
await dbClient.query(
'DELETE FROM user_token_reset_password WHERE token = $1',
[token]
@@ -298,6 +442,7 @@ User.fetchDeserializedDataById = async userId => {
u.status,
u.hits_per_page,
u.language,
u.bcrypt_hash IS NULL AS is_sso,
ARRAY(
SELECT jsonb_build_object(
'roleName', r.role_name,
@@ -540,7 +685,8 @@ User.fetchUserRoles = async userId => {
User.fetchAllWithDictionaryRights = async dictionaryId => {
const text = `
SELECT u.username,u.email, u.id, r.administration, r.editing, r.terminology_review, r.language_review
SELECT u.username, u.email, u.id, u.first_name, u.last_name, u.language,
r.administration, r.editing, r.terminology_review, r.language_review
FROM user_role r
INNER JOIN "user" u ON u.id = r.user_id
WHERE dictionary_id = $1
@@ -711,6 +857,18 @@ User.changePassword = async (userId, passwordOld, passwordNew, t) => {
userId
])
if (!bcryptHashOld) {
const err = Error(
t(
'Ta uporabniški račun je bil ustvarjen prek prijave z Univerzo v Ljubljani in nima gesla. Prijavite se prek gumba za prijavo z Univerzo v Ljubljani.'
)
)
err.status = 403
err.displayInProd = true
throw err
}
const isOldPasswordCorrect = await bcrypt.compare(
passwordOld,
bcryptHashOld
@@ -753,6 +911,7 @@ User.closeAccount = async userId => {
first_name = $2,
last_name = $3,
email = $4,
bcrypt_hash = NULL,
status = 'closed',
time_closed = NOW()
WHERE id = $5`,
@@ -776,7 +935,15 @@ User.closeAccount = async userId => {
),
dbClient.query('DELETE FROM user_token_change_email WHERE user_id = $1', [
userId
])
]),
dbClient.query('DELETE FROM user_external_identity WHERE user_id = $1', [
userId
]),
dbClient.query('DELETE FROM user_role WHERE user_id = $1', [userId]),
dbClient.query(
'DELETE FROM consultancy_entry_consultant WHERE user_id = $1',
[userId]
)
])
})
}