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
921 stars 166 forks source link

Problem adding user in Roles.GLOBAL_GROUP #281

Closed allenfuller closed 5 years ago

allenfuller commented 5 years ago

I'm using Meteor 1.8.1 and trying to create a default user on startup with a clean install, using this code:

const createAdmin = () => {
  const adminEmail  = 'admin@admin.com';
  const adminExists = Meteor.users.find({ 'emails.address': adminEmail }).count();

  if ( adminExists === 0 ) {
    const adminDoc  = {
                        email: adminEmail,
                        password: 'password',
                      },
          userId    = Accounts.createUser( adminDoc );

    Roles.addUsersToRoles( userId, 'admin', Roles.GLOBAL_GROUP );

    Meteor.users.update( userId, {
      $set: {
        name: {
          first: 'Admin',
          last: 'User',
        }
      }
    });

    return userId;
  } else {
    return false;
  }
};

The user creation works just fine, but when it gets to the Roles part, I get the following error:

MongoError: Cannot create field '__global_roles__' in element {roles: [ "user" ]}
    at Function.create (/Users/allen/.meteor/packages/npm-mongo/.3.1.2.1j1pkfe.pd2v++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/error.js:43:12)
    at toError (/Users/allen/.meteor/packages/npm-mongo/.3.1.2.1j1pkfe.pd2v++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/utils.js:149:22)
    at coll.s.topology.update (/Users/allen/.meteor/packages/npm-mongo/.3.1.2.1j1pkfe.pd2v++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/operations/collection_ops.js:1414:39)
    at /Users/allen/.meteor/packages/npm-mongo/.3.1.2.1j1pkfe.pd2v++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb-core/lib/connection/pool.js:532:18
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

Anyone else run into this issue? Any idea why I can't create a __global_roles__ field? I ran this immediately after running meteor reset, so there should not be any existing database to create problems.

Thanks for any advice or feedback you may have!

allenfuller commented 5 years ago

As is so often the case, nevermind! The application I was updating had a default Accounts.onCreateUser() function that set a user's role to 'user' indiscriminately. This, of course, made it impossible to go back and add group-based roles.