Coder Social home page Coder Social logo

Comments (2)

aron avatar aron commented on May 28, 2024

Suggested API.

Plugin Options

user:        A string or object to represent the current user. This will be
             attached to the `annotation.user` property.

permissions: An object of permissions, each permission can have an array
             of tokens that will be validated against the user object.
             By default the permission will be an empty array meaning
             anyone can edit it.

             Available permissions are as follows:

               read:    Can the user view the annotation.
               update:  Can the user edit the annotation.
               destroy: Can the user delete the annotation.
               admin:   Can the user edit the annotation.permissions object.

userId:      A callback function that receives the `user` object and returns
             a unique id for that user. By default it just returns `user`

userDisplay: A callback function that returns a display name for the user.
             By default it just returns `user`

authorize:   A callback function that receives the `user` object and a
             permissions `token`. The function should return true if the
             `user` can perform the action. By default the function will
             check to see if `user` === `token`.

Example Usage (Basic)

// All annotations will be world editable but will have a user.
annotator.addPlugin('Permissions', {
  user: 'Alice'
});

Example Usage (With Permissions)

A simple permissions model. By default the Permissions plugin will check
the contents of the permissions array for each action to see if it contains
the current user as it does now.

// Anyone can read the annotations but only "Alice" can modify them.
annotator.addPlugin('Permissions', {
  user: 'Alice',
  permissions: {
    'read':    [],
    'update':  ['Alice'],
    'destroy': ['Alice'],
    'admin':   ['Alice'] 
  }
});

Example Usage (Complex With Permissions)

Here we extend the current idea of providing callbacks via the options to allow
complex permission logic such as groups without having to complicate the
Annotator itself.

annotator.addPlugin('Permissions', {

  // A user object for the current user.
  user: { id: 'alice', name: 'Alice' },

  // Object of permissions for the annotation. By default these will be set to
  // an empty array. Meaning anyone can edit.
  permissions: {
    'read':    [],
    'update':  ['alice', 'bob', 'group:logged-in', 'group:admin'],
    'destroy': ['alice', 'group:admin'],
    'admin':   ['alice', 'group:admin']
  },

  // Function to return the id for a user.
  // Allows custom user objects to be used.
  userId: function (user) {
    return user.id;
  },

  // Function to return the display name for the user.
  userDisplay: function (user) {
    return user.name;
  },

  // Callback function returns true if user matches the token. This allows
  // complex logic such as groups to be implemented without complicating
  // the annotator.
  authorize: function (user, token) {
    var group;

    if (token.indexOf('group:') === 0) {
       group = token.replace('group:', '');

       // Local function to check whether this user is in the group. Not
       // part of the Annotator.
       if (userInGroup(user, group)) {
         return true;
       }
    }
    else if (user.id === permission) {
      return true;
    }

    return false;
  }
});

from annotator.

aron avatar aron commented on May 28, 2024

The above has been implemented as of 544f9f2, by default Permissions#authorize() will match simple permission tokens as described in the "simple example" above.

from annotator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.