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 164 forks source link

Problem with Meteor.roleAssignment and typescript #338

Closed FoudreNoire26i closed 5 months ago

FoudreNoire26i commented 3 years ago

Hi, I'm trying to use meteor-roles package, but i faced a problem : Roles.userIsInRole(myId, 'admin')) always return false in client side. I read that I had to publish Meteor.roleAssignement, but I can't because I use typescript and roleAssignment is undefined. Can someone help me ?

Capture d’écran 2021-06-29 à 17 28 25

Thanks

FoudreNoire26i commented 3 years ago

Hi, I finally find a hack by myself. I keep this issue open, if anyone has a better idea (or if the maintainer want to close it ;)), but this is the best solution I found today. Meteor.publish(null, function (){ var myId = Meteor.userId() if (myId && Roles.userIsInRole(myId, 'admin')) { return [Meteor.roles.find(), (Meteor as any).roleAssignment.find()] } else { return this.ready() } });

StorytellerCZ commented 2 years ago

We will have to figure out how to update the Typescript typings for this. For right now you should be able to supress the warning with // @ts-ignore

StorytellerCZ commented 2 years ago

Here is the types package, but we'll need to udpate it: https://www.npmjs.com/package/@types/meteor-roles

danyhiol commented 2 years ago

You can just add it to your meteor namespace: const roleAssignment: Mongo.Collection<Roles.Role>;. This is if the type package has not yet been updated.

StorytellerCZ commented 9 months ago

@FoudreNoire26i @danyhiol please check the latest version. Types have been added recently.

bruceborrett commented 7 months ago

I have the latest version installed and I can see the types being declared if I look at the definitions file in the installed package source, but for some reason I am still getting the same error. Are they perhaps being declared incorrectly? Or am I doing something wrong?

StorytellerCZ commented 7 months ago

This is the current declaration in the types for the problematic variables:

declare namespace Meteor {
  var roles: Mongo.Collection<Roles.Role>
  var roleAssignment: Mongo.Collection<Roles.RoleAssignment>
}
bruceborrett commented 5 months ago

If I do:

import { Meteor } from 'meteor/alanning:roles;

Then I get the correct types, but I lose all of the other Meteor types. Perhaps this is an issue with the way zodern:types handles these d.ts files because it seems the Meteor namespace is not being extended correctly.

bruceborrett commented 5 months ago

Looks like this is the correct definition:

declare module 'meteor/meteor' {
  namespace Meteor {
    const roles: Mongo.Collection<Roles.Role>
    const roleAssignment: Mongo.Collection<Roles.RoleAssignment>
  }
}

With the above everything works as expected.