|
8 | 8 | hasServiceAbilities, hasResourceAbilities, getQueryForAbilities,
|
9 | 9 | Roles, RoleNames, countSubjectsForResource
|
10 | 10 | } from '../../common/permissions.js'
|
11 |
| -import { isTagEqual } from '../utils.js' |
12 | 11 |
|
13 | 12 | const { getItems, replaceItems } = common
|
14 | 13 | const { Forbidden } = errors
|
@@ -289,98 +288,3 @@ export function updateAbilities (options = {}) {
|
289 | 288 | }
|
290 | 289 | }
|
291 | 290 |
|
292 |
| -export function preventRemovingLastOwner (resourceScope) { |
293 |
| - return async function (hook) { |
294 |
| - // By pass check ? |
295 |
| - if (hook.params.force) return hook |
296 |
| - const params = hook.params |
297 |
| - const data = hook.data || {} |
298 |
| - const query = params.query || {} |
299 |
| - const scope = data.scope || query.scope |
300 |
| - const grantedPermissions = data.permissions || query.permissions |
301 |
| - const grantedRole = (grantedPermissions ? Roles[grantedPermissions] : undefined) |
302 |
| - const resource = hook.params.resource |
303 |
| - const subjects = hook.params.subjects |
304 |
| - const subjectService = hook.params.subjectsService |
305 |
| - // On create check if we try to downgrade permissions otherwise let pass through |
306 |
| - if (!_.isUndefined(grantedRole) && (grantedRole === Roles.owner)) return hook |
307 |
| - |
308 |
| - if ((scope === resourceScope) && resource && resource._id) { |
309 |
| - // Count existing owners |
310 |
| - const owners = await countSubjectsForResource(subjectService, resourceScope, resource._id, Roles.owner) |
311 |
| - // Now count owners we change/remove permissions on |
312 |
| - const removedOwners = subjects.reduce((count, subject) => { |
313 |
| - const resources = _.get(subject, resourceScope, []) |
314 |
| - const ownedResource = _.find(resources, { _id: resource._id, permissions: RoleNames[Roles.owner] }) |
315 |
| - return (ownedResource ? count + 1 : count) |
316 |
| - }, 0) |
317 |
| - // If none remains stop |
318 |
| - if (removedOwners >= owners.total) { |
319 |
| - debug('Cannot remove the last owner of resource ', resource) |
320 |
| - const resourceName = resource.name ? resource.name : resource._id.toString() |
321 |
| - throw new Forbidden('You are not allowed to remove the last owner of resource ' + resourceName, { |
322 |
| - translation: { |
323 |
| - key: 'CANNOT_REMOVE_LAST_OWNER', |
324 |
| - params: { resource: resourceName } |
325 |
| - } |
326 |
| - }) |
327 |
| - } |
328 |
| - } |
329 |
| - return hook |
330 |
| - } |
331 |
| -} |
332 |
| - |
333 |
| -export async function removeOrganisationGroupsAuthorisations (hook) { |
334 |
| - const app = hook.app |
335 |
| - const authorisationService = app.getService('authorisations') |
336 |
| - const org = hook.params.resource |
337 |
| - const user = hook.params.user |
338 |
| - // Unset membership for the all org groups |
339 |
| - const orgGroupService = app.getService('groups', org) |
340 |
| - const groups = await orgGroupService.find({ paginate: false }) |
341 |
| - await Promise.all(groups.map(group => { |
342 |
| - // Unset membership on group for the all org users |
343 |
| - return authorisationService.remove(group._id.toString(), { |
344 |
| - query: { |
345 |
| - scope: 'groups' |
346 |
| - }, |
347 |
| - user, |
348 |
| - force: hook.params.force, |
349 |
| - // Because we already have resource set it as objects to avoid populating |
350 |
| - // Moreover used as an after hook the resource might not already exist anymore |
351 |
| - subjects: hook.params.subjects, |
352 |
| - subjectsService: hook.params.subjectsService, |
353 |
| - resource: group, |
354 |
| - resourcesService: orgGroupService |
355 |
| - }) |
356 |
| - })) |
357 |
| - debug('Authorisations unset on groups for organisation ' + org._id) |
358 |
| - return hook |
359 |
| -} |
360 |
| - |
361 |
| -export async function removeOrganisationTagsAuthorisations (hook) { |
362 |
| - const app = hook.app |
363 |
| - const org = hook.params.resource |
364 |
| - const subjectService = hook.params.subjectsService |
365 |
| - const orgTagsService = app.getService('tags', org) |
366 |
| - const subjects = hook.params.subjects || [] |
367 |
| - if (subjects.length === 0) return hook |
368 |
| - // Retrieve org tags |
369 |
| - const orgTags = await orgTagsService.find({ paginate: false }) |
370 |
| - const promises = [] |
371 |
| - subjects.forEach(subject => { |
372 |
| - const tags = subject.tags || [] |
373 |
| - // Find tags from org |
374 |
| - const fromOrg = _.intersectionWith(tags, orgTags, isTagEqual) |
375 |
| - // Clear removed tags |
376 |
| - const notFromOrg = _.differenceWith(tags, orgTags, isTagEqual) |
377 |
| - // Update subject if required |
378 |
| - if (fromOrg.length > 0) { |
379 |
| - promises.push(subjectService.patch(subject._id.toString(), { tags: notFromOrg })) |
380 |
| - } |
381 |
| - }) |
382 |
| - // Perform subject updates in parallel |
383 |
| - await Promise.all(promises) |
384 |
| - debug(`Tags unset on ${promises.length} subjects for organisation ` + org._id) |
385 |
| - return hook |
386 |
| -} |
0 commit comments