At matestack, we have had good experiences using matestack with Pundit. CanCanCan, another very popular authorization library in Rails, is also supported, as shown below!
Both Pundit and CanCanCan use pure Ruby and focus on the model and controller layer, so they are compatible to matestack's UI library.
Here we see how Pundit defines policies and we can check for them in the controller action, just before matestack's responder_for
!
A Pundit example in app/policies/user_policy.rb
:
class UserPolicyattr_reader :user​def initialize(user)@user = userend​def show?user.is_visible?end​end
Matestack's app/controllers/user_controller.rb
:
class UserController < ApplicationController​matestack_app UserApp​def show@user = User.find_by(id: params[:id])authorize @user # checking Pundit policyrender UserApp::Pages::Show # matestack page responderend​end
Here we see how CanCanCan defines abilities and we can check for them in the controller action, just before matestack's responder_for
!
CanCanCan's app/models/ability.rb
example, borrowed from their guides:
class Abilityinclude CanCan::Ability​def initialize(user)can :read, :all # permissions for every user, even if not logged in# [...]end​end
Matestack's app/controllers/user_controller.rb
:
class UserController < ApplicationController​matestack_app UserApp​def show@user = User.find_by(id: params[:id])authorize! :read, @user # checking for CanCanCan abilityrender UserApp::Pages::Show # matestack page responderend​end