Israel Z Kollie
Israel Z Kollie

Reputation: 273

Meteor make one user to read and update all docs

I have written a publish function that gets the current userId and finds all docs related to that user. Now all users can only have access to read, update and delete what they created.

I want to add a user that will basically be an admin user that can access to read, update or delete all docs.

Is there a simple way this is achievable? Please see my push function code below, how can I add one admin user to the publish function?

Meteor.publish("docs", function() {
    return Docs.find({ userId: this.userId });
  });

Meteor.methods({
  "docs.insert"(
    name,
    title,
    purpose
  ) {
    if (!this.userId) {
      throw new Meteor.Error("not-authorized");
    }
return Docs.insert({
      name,
      title,
      purpose
      userId: this.userId,
    });
  },

Creating and login users is already working. The only thing I need is for a general user to have access to all docs.

Upvotes: 1

Views: 38

Answers (1)

rooch84
rooch84

Reputation: 654

    Meteor.publish("docs", function() {
      if (this.userId === 'superuser') {
        return Docs.find({});
      } else {
        return Docs.find({ userId: this.userId });
    });

    Meteor.methods({
      "docs.update"(
        docId,
        <props to update>
      ) {
        if (!this.userId ) {
          throw new Meteor.Error("not-authorized");
        }

        let userId = Docs.findOne({_id: docId}).userId;
        if (this.userId === userId || this.userId === 'superuser') {
          // Do the update 
        } else {
          throw new Meteor.Error("not-authorized");
        } 
    });

From https://docs.meteor.com/api/pubsub.html.

Upvotes: 1

Related Questions