-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds new organisation settings API. (#1398 - LL-77)
BREAKING CHANGE: Adds new organisation settings API and removes organisation settings from user API.
- Loading branch information
1 parent
1bdafbd
commit b4c1030
Showing
24 changed files
with
1,158 additions
and
79 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
api/src/controllers/UserOrganisationSettingsController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import User from 'lib/models/user'; | ||
import { SITE_ADMIN } from 'lib/constants/scopes'; | ||
import { MANAGE_ALL_USERS } from 'lib/constants/orgScopes'; | ||
import NotFoundError from 'lib/errors/NotFoundError'; | ||
import ClientError from 'lib/errors/ClientError'; | ||
import catchErrors from 'api/controllers/utils/catchErrors'; | ||
|
||
const createOrganisationSetting = catchErrors(async (req, res) => { | ||
const user = await User.findOne({ _id: req.params.userId }); | ||
if (!user) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
const alreadyExists = user.organisationSettings.some(s => s.organisation.toString() === req.params.organisationId); | ||
if (alreadyExists) { | ||
throw new ClientError(`Duplicated: The user already has the organisationSettings for the organisation (${req.params.organisationId})`); | ||
} | ||
|
||
// Should we set organisation required parameter? | ||
if (req.body.organisation === undefined) { | ||
req.body.organisation = req.params.organisationId; | ||
} | ||
|
||
if (req.params.organisationId !== req.body.organisation) { | ||
throw new ClientError(`Invalid: organisationId in URL path (${req.params.organisationId}) and organisation in body (${req.body.organisation}) are not matched.`); | ||
} | ||
|
||
user.organisationSettings.push(req.body); | ||
const updatedUser = await user.save(); | ||
const insertedOrganisationSetting = updatedUser.organisationSettings.find(s => s.organisation.toString() === req.params.organisationId); | ||
res.status(200).send(insertedOrganisationSetting); | ||
}); | ||
|
||
/** | ||
* @param {object} body | ||
* @param {string[]} scopes | ||
* @returns {boolean} | ||
*/ | ||
const validateUpdatableKeys = (body, scopes) => { | ||
if (scopes.includes(SITE_ADMIN)) { | ||
return true; | ||
} | ||
|
||
const updatingKeys = Object.keys(body); | ||
|
||
if (scopes.includes(MANAGE_ALL_USERS)) { | ||
return updatingKeys.every(key => ['filter', 'roles', 'scopes'].includes(key)); | ||
} | ||
|
||
return updatingKeys.every(key => ['samlEnabled'].includes(key)); | ||
}; | ||
|
||
const updateOrganisationSetting = catchErrors(async (req, res) => { | ||
const user = await User.findOne({ _id: req.params.userId }); | ||
if (!user) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
const scopes = req.user.authInfo.token.scopes; | ||
const isValid = validateUpdatableKeys(req.body, scopes); | ||
if (!isValid) { | ||
throw new ClientError('Can not update some fields you are trying to update'); | ||
} | ||
|
||
if (req.body.organisation && req.params.organisationId !== req.body.organisation) { | ||
throw new ClientError(`Invalid: organisationId in URL path (${req.params.organisationId}) and organisation in body (${req.body.organisation}) are not matched.`); | ||
} | ||
|
||
const i = user.organisationSettings.findIndex(s => s.organisation.toString() === req.params.organisationId); | ||
if (i < 0) { | ||
user.organisationSettings.push({ | ||
organisation: req.params.organisationId, | ||
...req.body, | ||
}); | ||
} else { | ||
user.organisationSettings[i] = { | ||
...user.organisationSettings[i].toObject(), | ||
...req.body, | ||
}; | ||
} | ||
|
||
const updatedUser = await user.save(); | ||
const insertedOrganisationSetting = updatedUser.organisationSettings[i]; | ||
|
||
res.status(200).send(insertedOrganisationSetting); | ||
}); | ||
|
||
|
||
const deleteOrganisationSetting = catchErrors(async (req, res) => { | ||
const user = await User.findOne({ _id: req.params.userId }); | ||
if (!user) { | ||
throw new NotFoundError(); | ||
} | ||
|
||
user.organisationSettings = user.organisationSettings.filter(s => s.organisation.toString() !== req.params.organisationId); | ||
await user.save(); | ||
res.status(200).send(); | ||
}); | ||
|
||
const UserOrganisationSettingsController = { | ||
create: createOrganisationSetting, | ||
update: updateOrganisationSetting, | ||
delete: deleteOrganisationSetting, | ||
}; | ||
|
||
export default UserOrganisationSettingsController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import User from 'lib/models/user'; | ||
import NotFoundError from 'lib/errors/NotFoundError'; | ||
import catchErrors from 'api/controllers/utils/catchErrors'; | ||
|
||
const removeOrganisationFromUser = catchErrors(async (req, res) => { | ||
const user = await User.findOne({ _id: req.params.userId }); | ||
if (!user) { | ||
throw new NotFoundError(`Not found user (_id: ${req.params.userId})`); | ||
} | ||
|
||
const userOrganisationSet = new Set(user.organisations.map(o => o.toString())); | ||
userOrganisationSet.delete(req.params.organisationId); | ||
user.organisations = Array.from(userOrganisationSet); | ||
|
||
await user.save(); | ||
res.status(200).send(); | ||
}); | ||
|
||
const UserOrganisationsController = { | ||
delete: removeOrganisationFromUser, | ||
}; | ||
|
||
export default UserOrganisationsController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.