text: 'use strict';
  
  /**
   * Module dependencies
   */
  var acl = require('acl');
  
  // Using the memory backend
  acl = new acl(new acl.memoryBackend());
  
  /**
   * Invoke Items Permissions
   */
  exports.invokeRolesPolicies = function () {
          acl.allow([{
                  roles: ['admin'],
                  allows: [{
                          resources: '/api/items',
                          permissions: '*'
                  }, {
                          resources: '/api/items/:itemId',
                          permissions: '*'
                  }]
          }, {
                  roles: ['user'],
                  allows: [{
                          resources: '/api/items',
                          permissions: ['get', 'post']
                  }, {
                          resources: '/api/items/:itemId',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/upload',
                          permissions: ['post']
                  }, {
                          resources: '/api/item/captcha',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/send_code',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/:itemId/register',
                          permissions: ['post']
                  }, {
                          resources: '/api/customer',
                          permissions: ['get']
                  }, {
                          resources: '/api/items/statistics',
                          permissions: ['get']
                  }, {
                          resources: '/api/items/:itemId/statistics',
                          permissions: ['get']
                  }]
          }, {
                  roles: ['guest'],
                  allows: [{
                          resources: '/api/items',
                          permissions: ['get']
                  }, {
                          resources: '/api/items/:itemId',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/captcha',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/send_code',
                          permissions: ['get']
                  }, {
                          resources: '/api/item/:itemId/register',
                          permissions: ['post']
                  }, {
                          resources: '/api/customer',
                          permissions: ['get']
                  }]
          }]);
  };
  
  /**
   * Check If Items Policy Allows
   */
  exports.isAllowed = function (req, res, next) {
          var roles = (req.user) ? req.user.roles : ['guest'];
  
          // If an Item is being processed and the current user created it then allow any manipulation
          if (req.item && req.user && req.item.user && req.item.user.id === req.user.id) {
                  return next();
          }
  
          // Check for user roles
          acl.areAnyRolesAllowed(roles, req.route.path, req.method.toLowerCase(), function (err, isAllowed) {
                  if (err) {
                          // An authorization error occurred
                          return res.status(500).send('Unexpected authorization error');
                  } else {
                          if (isAllowed) {
                                  // Access granted! Invoke next middleware
                                  return next();
                          } else {
                                  return res.status(403).json({
                                          message: 'User is not authorized'
                                  });
                          }
                  }
          });
  };
  