feat: oauth2 add groups mapping (#6053)

Co-authored-by: Nicolas Giard <github@ngpixel.com>
This commit is contained in:
Aurélien Lajoie
2023-01-30 00:08:13 +01:00
committed by GitHub
parent 43a797d322
commit 1da80eaab8
2 changed files with 30 additions and 4 deletions

View File

@@ -31,6 +31,19 @@ module.exports = {
email: _.get(profile, conf.emailClaim)
}
})
if (conf.mapGroups) {
const groups = _.get(profile, conf.groupsClaim)
if (groups && _.isArray(groups)) {
const currentGroups = (await user.$relatedQuery('groups').select('groups.id')).map(g => g.id)
const expectedGroups = Object.values(WIKI.auth.groups).filter(g => groups.includes(g.name)).map(g => g.id)
for (const groupId of _.difference(expectedGroups, currentGroups)) {
await user.$relatedQuery('groups').relate(groupId)
}
for (const groupId of _.difference(currentGroups, expectedGroups)) {
await user.$relatedQuery('groups').unrelate().where('groupId', groupId)
}
}
}
cb(null, user)
} catch (err) {
cb(err, null)