Skip to content

Commit

Permalink
Revert to using Meteor for collection access
Browse files Browse the repository at this point in the history
  • Loading branch information
StorytellerCZ committed Aug 29, 2024
1 parent e355dd0 commit 995467b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 86 deletions.
76 changes: 37 additions & 39 deletions roles/roles_client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* global Roles */
import { Meteor } from 'meteor/meteor'

import { RolesCollection, RoleAssignmentCollection } from './roles_common_async'

/**
* Provides functions related to user authorization. Compatible with built-in Meteor accounts packages.
*
Expand Down Expand Up @@ -65,7 +63,7 @@ Object.assign(Roles, {
unlessExists: false
}, options)

const result = RolesCollection.upsert({ _id: roleName }, { $setOnInsert: { children: [] } })
const result = Meteor.roles.upsert({ _id: roleName }, { $setOnInsert: { children: [] } })

if (!result.insertedId) {
if (options.unlessExists) return null
Expand All @@ -91,16 +89,16 @@ Object.assign(Roles, {
Roles._checkRoleName(roleName)

// Remove all assignments
RoleAssignmentCollection.remove({
Meteor.roleAssignment.remove({
'role._id': roleName
})

do {
// For all roles who have it as a dependency ...
roles = Roles._getParentRoleNames(RolesCollection.findOne({ _id: roleName }))
roles = Roles._getParentRoleNames(Meteor.roles.findOne({ _id: roleName }))

RolesCollection.find({ _id: { $in: roles } }).fetch().forEach(r => {
RolesCollection.update({
Meteor.roles.find({ _id: { $in: roles } }).fetch().forEach(r => {
Meteor.roles.update({
_id: r._id
}, {
$pull: {
Expand All @@ -110,8 +108,8 @@ Object.assign(Roles, {
}
})

inheritedRoles = Roles._getInheritedRoleNames(RolesCollection.findOne({ _id: r._id }))
RoleAssignmentCollection.update({
inheritedRoles = Roles._getInheritedRoleNames(Meteor.roles.findOne({ _id: r._id }))
Meteor.roleAssignment.update({
'role._id': r._id
}, {
$set: {
Expand All @@ -122,7 +120,7 @@ Object.assign(Roles, {
} while (roles.length > 0)

// And finally remove the role itself
RolesCollection.remove({ _id: roleName })
Meteor.roles.remove({ _id: roleName })
},

/**
Expand All @@ -141,18 +139,18 @@ Object.assign(Roles, {

if (oldName === newName) return

const role = RolesCollection.findOne({ _id: oldName })
const role = Meteor.roles.findOne({ _id: oldName })

if (!role) {
throw new Error('Role \'' + oldName + '\' does not exist.')
}

role._id = newName

RolesCollection.insert(role)
Meteor.roles.insert(role)

do {
count = RoleAssignmentCollection.update({
count = Meteor.roleAssignment.update({
'role._id': oldName
}, {
$set: {
Expand All @@ -162,7 +160,7 @@ Object.assign(Roles, {
} while (count > 0)

do {
count = RoleAssignmentCollection.update({
count = Meteor.roleAssignment.update({
'inheritedRoles._id': oldName
}, {
$set: {
Expand All @@ -172,7 +170,7 @@ Object.assign(Roles, {
} while (count > 0)

do {
count = RolesCollection.update({
count = Meteor.roles.update({
'children._id': oldName
}, {
$set: {
Expand All @@ -181,7 +179,7 @@ Object.assign(Roles, {
}, { multi: true })
} while (count > 0)

RolesCollection.remove({ _id: oldName })
Meteor.roles.remove({ _id: oldName })
},

/**
Expand Down Expand Up @@ -216,7 +214,7 @@ Object.assign(Roles, {
Roles._checkRoleName(parentName)

// query to get role's children
const role = RolesCollection.findOne({ _id: roleName })
const role = Meteor.roles.findOne({ _id: roleName })

if (!role) {
throw new Error('Role \'' + roleName + '\' does not exist.')
Expand All @@ -227,7 +225,7 @@ Object.assign(Roles, {
throw new Error('Roles \'' + roleName + '\' and \'' + parentName + '\' would form a cycle.')
}

const count = RolesCollection.update({
const count = Meteor.roles.update({
_id: parentName,
'children._id': {
$ne: role._id
Expand All @@ -244,7 +242,7 @@ Object.assign(Roles, {
// already a subrole; in any case we do not have anything more to do
if (!count) return

RoleAssignmentCollection.update({
Meteor.roleAssignment.update({
'inheritedRoles._id': parentName
}, {
$push: {
Expand Down Expand Up @@ -286,13 +284,13 @@ Object.assign(Roles, {

// check for role existence
// this would not really be needed, but we are trying to match addRolesToParent
const role = RolesCollection.findOne({ _id: roleName }, { fields: { _id: 1 } })
const role = Meteor.roles.findOne({ _id: roleName }, { fields: { _id: 1 } })

if (!role) {
throw new Error('Role \'' + roleName + '\' does not exist.')
}

const count = RolesCollection.update({
const count = Meteor.roles.update({
_id: parentName
}, {
$pull: {
Expand All @@ -307,11 +305,11 @@ Object.assign(Roles, {
if (!count) return

// For all roles who have had it as a dependency ...
const roles = [...Roles._getParentRoleNames(RolesCollection.findOne({ _id: parentName })), parentName]
const roles = [...Roles._getParentRoleNames(Meteor.roles.findOne({ _id: parentName })), parentName]

RolesCollection.find({ _id: { $in: roles } }).fetch().forEach(r => {
const inheritedRoles = Roles._getInheritedRoleNames(RolesCollection.findOne({ _id: r._id }))
RoleAssignmentCollection.update({
Meteor.roles.find({ _id: { $in: roles } }).fetch().forEach(r => {
const inheritedRoles = Roles._getInheritedRoleNames(Meteor.roles.findOne({ _id: r._id }))
Meteor.roleAssignment.update({
'role._id': r._id,
'inheritedRoles._id': role._id
}, {
Expand Down Expand Up @@ -427,7 +425,7 @@ Object.assign(Roles, {
selector.scope = options.scope
}

RoleAssignmentCollection.remove(selector)
Meteor.roleAssignment.remove(selector)

// and then add all
roles.forEach(function (role) {
Expand Down Expand Up @@ -456,7 +454,7 @@ Object.assign(Roles, {
return
}

const role = RolesCollection.findOne({ _id: roleName }, { fields: { children: 1 } })
const role = Meteor.roles.findOne({ _id: roleName }, { fields: { children: 1 } })

if (!role) {
if (options.ifExists) {
Expand All @@ -467,7 +465,7 @@ Object.assign(Roles, {
}

// This might create duplicates, because we don't have a unique index, but that's all right. In case there are two, withdrawing the role will effectively kill them both.
const res = RoleAssignmentCollection.upsert({
const res = Meteor.roleAssignment.upsert({
'user._id': userId,
'role._id': roleName,
scope: options.scope
Expand All @@ -480,7 +478,7 @@ Object.assign(Roles, {
})

if (res.insertedId) {
RoleAssignmentCollection.update({ _id: res.insertedId }, {
Meteor.roleAssignment.update({ _id: res.insertedId }, {
$set: {
inheritedRoles: [roleName, ...Roles._getInheritedRoleNames(role)].map(r => ({ _id: r }))
}
Expand Down Expand Up @@ -509,7 +507,7 @@ Object.assign(Roles, {
const parentRoles = new Set([role._id])

parentRoles.forEach(roleName => {
RolesCollection.find({ 'children._id': roleName }).fetch().forEach(parentRole => {
Meteor.roles.find({ 'children._id': roleName }).fetch().forEach(parentRole => {
parentRoles.add(parentRole._id)
})
})
Expand All @@ -535,7 +533,7 @@ Object.assign(Roles, {
const nestedRoles = new Set([role])

nestedRoles.forEach(r => {
const roles = RolesCollection.find({ _id: { $in: r.children.map(r => r._id) } }, { fields: { children: 1 } }).fetch()
const roles = Meteor.roles.find({ _id: { $in: r.children.map(r => r._id) } }, { fields: { children: 1 } }).fetch()

roles.forEach(r2 => {
inheritedRoles.add(r2._id)
Expand Down Expand Up @@ -619,7 +617,7 @@ Object.assign(Roles, {
selector.scope = options.scope
}

RoleAssignmentCollection.remove(selector)
Meteor.roleAssignment.remove(selector)
},

/**
Expand Down Expand Up @@ -686,7 +684,7 @@ Object.assign(Roles, {
return roles.some((roleName) => {
selector['inheritedRoles._id'] = roleName

return RoleAssignmentCollection.find(selector, { limit: 1 }).count() > 0
return Meteor.roleAssignment.find(selector, { limit: 1 }).count() > 0
})
},

Expand Down Expand Up @@ -750,7 +748,7 @@ Object.assign(Roles, {
delete filter.fields
}

const roles = RoleAssignmentCollection.find(selector, filter).fetch()
const roles = Meteor.roleAssignment.find(selector, filter).fetch()

if (options.fullObjects) {
return roles
Expand Down Expand Up @@ -778,7 +776,7 @@ Object.assign(Roles, {
getAllRoles: function (queryOptions) {
queryOptions = queryOptions || { sort: { _id: 1 } }

return RolesCollection.find({}, queryOptions)
return Meteor.roles.find({}, queryOptions)
},

/**
Expand Down Expand Up @@ -888,7 +886,7 @@ Object.assign(Roles, {
}
}

return RoleAssignmentCollection.find(selector, filter)
return Meteor.roleAssignment.find(selector, filter)
},

/**
Expand Down Expand Up @@ -939,7 +937,7 @@ Object.assign(Roles, {
selector['inheritedRoles._id'] = { $in: roles }
}

const scopes = RoleAssignmentCollection.find(selector, { fields: { scope: 1 } }).fetch().map(obi => obi.scope)
const scopes = Meteor.roleAssignment.find(selector, { fields: { scope: 1 } }).fetch().map(obi => obi.scope)

return [...new Set(scopes)]
},
Expand All @@ -963,7 +961,7 @@ Object.assign(Roles, {
if (oldName === newName) return

do {
count = RoleAssignmentCollection.update({
count = Meteor.roleAssignment.update({
scope: oldName
}, {
$set: {
Expand All @@ -985,7 +983,7 @@ Object.assign(Roles, {
removeScope: function (name) {
Roles._checkScopeName(name)

RoleAssignmentCollection.remove({ scope: name })
Meteor.roleAssignment.remove({ scope: name })
},

/**
Expand Down
Loading

0 comments on commit 995467b

Please sign in to comment.