Skip to content
Tim Elfelt edited this page Sep 24, 2016 · 2 revisions

How to Use Circles in your custom packages

First, add permissions array to model of package you want to protect. You may optionally set a default

permissions: {
  type: Array,
  default: ['authenticated']
}

Filter calls for all in server controller

// where package is your package name
module.exports = function(package, app, circles, database) {
	var Package = mongoose.model('Package');
	return {
		all: function(req, res) {
				var query;
				if(!req.user || req.acl.user.allowed.indexOf('admin') === -1) {
					query = req.acl.query('Package'); // non admins only see items from their circles
				} else {
					query = Package; // admins see all
				}

				query.find().sort('-created')
				....
				....
		}
	}

this will filter the results to return all items that the user has permission to access.

Circles middleware in the server side routes:

lock down a route for admins only

var requiresAdmin = circles.controller.hasCircle('admin');
 app.route('/api/whatever')
    .get(requiresAdmin , whatever.all)

You can also check roles like this req.user.roles.indexOf('admin') > -1

Circles middleware in the frontend routes:

$stateProvider.state('yourState', {
      url: '/yourState',
      templateUrl: 'yourpackage/views/yourState.html',
      requiredCircles : {
        circles: ['authenticated'],
        denyState: 'auth.login'
      }
}

Add circles as a dependency

You will also want to add circles as a dependency of you mean.json and package.json files, and inject circles into your custom package declaration, passing it along into server routes/controller files module.exports = function(package, app, circles, database) {

mean.json
{
  "name": "package",
  "version": "0.1.2",
  "mean": "0.6.0",
  "dependencies": {
    "circles": "0.1.x"
  }
}

package.json
  "dependencies": {
    "meanio-circles": "0.1.x",

You might find more examples in the gitter.im chatroom

Clone this wiki locally