Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Unipisa/dm-manager into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
robol committed Nov 21, 2023
2 parents e66c5cc + 090c29f commit 0f78ecb
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const LocalStrategy = require('passport-local')
const fs = require('fs')

const User = require('./models/User')
const Person = require('./models/Person')
const Token = require('./models/Token')
const config = require('./config')
const UnipiAuthStrategy = require('./unipiAuth')
Expand Down Expand Up @@ -83,8 +84,49 @@ function setup_routes(app) {
})
})

app.post('/login', function(req, res) {
const user = req.user || null
app.post('/login', async function(req, res) {
const req_user = req.user || null
let user = null
if (req_user) {
user = await User.findById(req_user._id)
if (user && user.email) {
/* attach roles to user */
const persons = await Person.aggregate([
{ $match: {$or: [
{email: user.email },
{alternativeEmails: user.email}] }
},
{
$lookup: {
from: 'staffs',
let: { person_id: '$_id'},
pipeline: [
{ $match: {
$expr: {
$and: [
{ $eq: [ '$person', '$$person_id' ]},
{ $or: [ {$eq: ['$startDate', null]}, {$lte: ['$startDate', '$$NOW']}]},
{ $or: [ {$eq: ['$endDate', null]}, {$gte: ['$endDate', '$$NOW']}]},
]
}
}
}
],
as: 'staff',
}
},
{
$unwind: {
path: '$staff',
preserveNullAndEmptyArrays: false,
}
}
])
const isInternal = persons.reduce((acc, person) => acc || person.staff.isInternal, false)
if (isInternal) user.roles.push('/process/seminars')
console.log(`sending user ${JSON.stringify(user)}`)
}
}
res.send({ user })
})

Expand Down

0 comments on commit 0f78ecb

Please sign in to comment.