Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
920 stars 167 forks source link

Version 4 #379

Closed StorytellerCZ closed 4 weeks ago

StorytellerCZ commented 1 year ago
jankapunkt commented 9 months ago

@StorytellerCZ is there anything that isn't already implemented by the latest release that contains the async counterparts?

StorytellerCZ commented 9 months ago

Not 100% sure, but I think we have everything. I will remove the sync MongoDB calls in this branch. Or maybe for the client we can leave the sync methods? Thoughts?

tchax commented 9 months ago

@StorytellerCZ I think it would make sense to leave sync methods for the client. This is consistent with other packages like Accounts isn't it ? Thanks

dallman2 commented 7 months ago

Synchronous Mongo calls on the client side are alright in Meteor 3.0, IIRC. When is this PR expected to be merged in and published? I would love to test this in my application.

StorytellerCZ commented 7 months ago

Once this is merged: https://github.com/Meteor-Community-Packages/meteor-roles/pull/344

I will release a beta.

dallman2 commented 7 months ago

I would suggest updating the userIsInRole function on like 658 of roles_common.js to:

  userIsInRole: async function (user, roles, options) {
    let id
    options = Roles._normalizeOptions(options)

    // ensure array to simplify code
    if (!Array.isArray(roles)) roles = [roles]

    roles = roles.filter(r => r != null)

    if (!roles.length) return false

    Roles._checkScopeName(options.scope)

    options = Object.assign({
      anyScope: false
    }, options)

    if (user && typeof user === 'object') {
      id = user._id
    } else {
      id = user
    }

    if (!id) return false
    if (typeof id !== 'string') return false

    const selector = { 'user._id': id }

    if (!options.anyScope) {
      selector.scope = { $in: [options.scope, null] }
    }

    const promises = roles.map((roleName) => {
      selector['inheritedRoles._id'] = roleName

      return Meteor.roleAssignment.find(selector, { limit: 1 }).countAsync() > 0
    })
    await Promise.all(promises)
    return promises.some(p => p)
  },

For obvious reasons, the method needs to become async to be able to count how many records are in the collection.

StorytellerCZ commented 7 months ago

@dallman2 PRs welcome.

StorytellerCZ commented 5 months ago

Published alanning:roles@4.0.0-alpha.1, please take things out for a spin. It is Meteor 3.0-beta.7 compatible!

StorytellerCZ commented 2 months ago

Tests fixed, just trying one little thing for better hopefully better performance.

StorytellerCZ commented 1 month ago

@jankapunkt @harryadel can I get a check on this from you, so that we can do the release? Many thanks!

StorytellerCZ commented 1 month ago

Anything else for release? @jankapunkt @harryadel