Ruby cancan: Difference between revisions

From wikinotes
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Cancan is a framework for defining/granting/checking permissions for an ability.
Cancan is a framework for defining/granting/checking permissions for an ability.
{{ WARNING |
cancan has been superceeded by cancancan
}}


= Documentation =
= Documentation =
Line 49: Line 53:
:manage  # all permissions
:manage  # all permissions
</source>
</source>
cancan defines these group permissions (ex. manage, read, ..) as [https://github.com/CanCanCommunity/cancancan/blob/develop/docs/define_check_abilities.md#can-actions can actions] in their docs.


Object Types
Object Types

Latest revision as of 19:30, 26 May 2023

Cancan is a framework for defining/granting/checking permissions for an ability.


WARNING:

cancan has been superceeded by cancancan

Documentation

official docs https://github.com/ryanb/cancan/wiki
github https://github.com/ryanb/cancan

Overview

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end

Permissions

Permissions are defined by pairing a permission, with a class.

can(:create, User)  # has permission to create User classes
can [:update, :destroy], [Article, Comment]

Permission Types

# single permissions
:create
:read
:update
:destroy

# group permissions
:manage  # all permissions

cancan defines these group permissions (ex. manage, read, ..) as can actions in their docs.

Object Types

can(:create, :all)  # can create any managed object
can(:create, User)  # can create instances of a class