48hr.email/infrastructure/web/middleware/lock.js
ClaraCrazy 8ed7ccade8
[Chore]: Misc changes around user merge
- Update lock removal timer and behaviour
- Redirect to previous path on sign-in and out
- Fix dashbaord UI and other UX elemets
- Lose sanity threlf times
2026-01-02 20:56:14 +01:00

44 lines
1.7 KiB
JavaScript

function checkLockAccess(req, res, next) {
const inboxLock = req.app.get('inboxLock')
const address = req.params.address
const userId = req.session && req.session.userId
const isAuthenticated = req.session && req.session.isAuthenticated
if (!address || !inboxLock) {
return next()
}
const isLocked = inboxLock.isLocked(address)
// For authenticated users, check database ownership
// Also allow session-based access for immediate unlock after locking
const hasAccess = isAuthenticated && userId ?
(inboxLock.isLockedByUser(address, userId) || req.session.lockedInbox === address.toLowerCase()) :
(req.session && req.session.lockedInbox === address.toLowerCase())
// Block access to locked inbox without proper authentication
if (isLocked && !hasAccess) {
const count = req.app.get('mailProcessingService').getCount()
const unlockError = req.session ? req.session.unlockError : undefined
if (req.session) delete req.session.unlockError
return res.render('error', {
purgeTime: require('../../../application/helper').prototype.purgeTimeElemetBuilder(),
address: address,
count: count,
message: 'This inbox is locked by another user. Only the owner can access it.',
branding: req.app.get('config').http.branding,
currentUser: req.session && req.session.username,
authEnabled: req.app.get('config').user.authEnabled
})
}
// Update last access if they have access and are authenticated
if (isLocked && hasAccess && isAuthenticated && userId) {
inboxLock.updateAccess(userId, address)
}
next()
}
module.exports = { checkLockAccess }