Skip to content

Defining rules for wildcard any subtarget

Adam Pahlevi Baihaqi edited this page Sep 24, 2015 · 2 revisions

Bali also support wildcard subtarget, in addition to definite subtargets.

In this case, Bali provides others block. Rules defined within definite subtargets are given priority to its others counterpart, if any.

Bali.map_rules do
  rules_for My::Transaction do
    describe(:supreme) { can_all }
    describe :admin do
      can_all
      cannot :delete
    end
    describe :finance do
      cannot :view
      can :print
    end
    others do
      can :view, if: proc { |txn| txn.is_settled? }
      can :print, if: proc { |txn| txn.is_settled? }
      can :index
    end
  end
end

Some that we can deduct from the code above:

  1. All users, all, nil, supreme, finance, all; can :index (returns true)
  2. Finance user cannot :view, as rule defined within :finance (which is definite) will nullify its counterpart that is defined in others block
  3. Admin, Supreme, Finance user can :print, regardless if the transaction is settled or not.
  4. Other users, be it nil, :monitoring, or any subtarget that is not definitely defined will pay respect to the others block