Skip to content
kristianmandrup edited this page Nov 11, 2011 · 9 revisions

The CanTango Permit engine operates like this:

  • Receive an ability candidate as input
  • Determine which execution modes are enabled for the Permit engine, :cache and :no_cache

For each enabled execution mode, starting with cache mode:

  • Execute the SystemPermit before any other Permit (if special permits enabled)
  • Determine which Permits are enabled for use (see Configuration
  • Iterate through each permit type and for each permit type executes a Permits executor for that type, passing in the candidate
  • Execute the AnyPermit after all other Permits (if special permits are enabled)
  • Cache the rule set if in caching mode

The SystemPermit has the chance to "break out" of the execution loop given some global or candidate condition, thus only adding the SystemPermit rules and disregarding any other Permit rules.

Each Permit is executed in the context of the execution mode in operation. For :cache mode, the module Cached is included into the Permit. For the :no_cache mode, the same goes for the NonCached module (see Permits).

Permits executor

  • Each enabled permit of the given type that is registered is executed, using the candidate as the context
  • If any permit returns the special symbol :break, further Permit evaluation of that type is aborted
  • Currently, the :break does not bubble up to break off Permit engine execution. Should it?
  • The result of each Permit execution is a rule set, these rule sets are merged together into one rule set in the end
  • The permit executor will attempt to cache the rule set under a permit type key, fx :role for role permits

Configuration

The Permit engine has the general engine config API as demonstrated here:

CanTango.config.engine(:permit) do |engine|
  puts engine.modes
  engine.mode = :cache
  engine.set :on
  engine.reset! # state to default value
  puts "ON" if engine.on?
  puts "OFF" if engine.off?

  # specific to permit engine
  puts engine.types
  puts engine.special_permits
end

The engine mode is used to turn caching on or off for that engine.