Skip to content

Instantly share code, notes, and snippets.

@simonlehmann
Forked from vmarcetic/ability.rb
Last active February 23, 2018 15:43
Show Gist options
  • Save simonlehmann/a4f07b9e80eb42ce05da4157552f99b0 to your computer and use it in GitHub Desktop.
Save simonlehmann/a4f07b9e80eb42ce05da4157552f99b0 to your computer and use it in GitHub Desktop.

Revisions

  1. simonlehmann revised this gist Mar 3, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ def initialize_group
    Group.current = Group.find(params[:group_id]) if params.has_key?(:group_id)
    end

    # we find user role for companie that he is managing and we store data in class ( cattr_accessor :current_role )
    # we find user role for group that he is managing and we store data in class ( cattr_accessor :current_role )
    def initialize_user_role
    User.current_role = Unity.where(group_id: Group.current.id, user_id: current_user.id).first.role.name unless Group.current.nil?
    end
  2. simonlehmann revised this gist Mar 3, 2017. 8 changed files with 24 additions and 20 deletions.
    8 changes: 6 additions & 2 deletions ability.rb
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,19 @@ class Ability
    def initialize(user)
    user ||= User.new # This is used for not logged user if you have a need for it
    case user.current_role

    when 'admin'
    can :manage, :all
    can :manage, :all

    when 'moderator'
    can :read, Products
    can :update, Products
    cannot :destroy, Products
    cannot :create, Products
    cannot :manage, Client
    # ... other roles and abilities ...

    # ... other roles and abilities ...

    end
    end
    end
    10 changes: 5 additions & 5 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@
    class ApplicationController < ActionController::Base
    before_filter :initialize_company, :initialize_user_role
    before_filter :initialize_group, :initialize_user_role

    # we find company that user currently uses and we store data in class ( cattr_accessor :current)
    def initialize_company
    Company.current = Company.find(params[:company_id]) if params.has_key?(:company_id)
    # we find group that user currently uses and we store data in class ( cattr_accessor :current)
    def initialize_group
    Group.current = Group.find(params[:group_id]) if params.has_key?(:group_id)
    end

    # we find user role for companie that he is managing and we store data in class ( cattr_accessor :current_role )
    def initialize_user_role
    User.current_role = Unity.where(company_id: Company.current.id, user_id: current_user.id).first.role.name unless Company.current.nil?
    User.current_role = Unity.where(group_id: Group.current.id, user_id: current_user.id).first.role.name unless Group.current.nil?
    end
    end
    7 changes: 0 additions & 7 deletions company.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    class Company < ActiveRecord::Base
    has_many :unities
    has_many :users, through: :unities
    has_many :roles, through: :unities

    cattr_accessor :current # here I added a current company, so I can check wich company is active I will use that later.
    end
    7 changes: 7 additions & 0 deletions group.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class Group < ActiveRecord::Base
    has_many :unities
    has_many :users, through: :unities
    has_many :roles, through: :unities

    cattr_accessor :current # here I added a current group, so I can check wich group is active I will use that later.
    end
    2 changes: 1 addition & 1 deletion migration file
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    create_table :unities do |t|
    t.integer :role_id
    t.integer :user_id
    t.integer :company_id
    t.integer :group_id

    t.timestamps
    end
    2 changes: 1 addition & 1 deletion role.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    class Role < ActiveRecord::Base
    has_many :unities
    has_many :users, through: :unities
    has_many :companies, through: :unities
    has_many :groups, through: :unities
    end
    4 changes: 2 additions & 2 deletions unity.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    class Unity < ActiveRecord::Base
    belongs_to :role
    belongs_to :company
    belongs_to :group
    belongs_to :user

    attr_accessible :role_id, :user_id, :company_id
    attr_accessible :role_id, :user_id, :group_id
    end
    4 changes: 2 additions & 2 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ class User < ActiveRecord::Base

    has_many :unities
    has_many :roles, through: :unities
    has_many :companies, through: :unities
    has_many :groups, through: :unities

    cattr_accessor :current_role # I added current_role so I can check which role does user haves on a company that he is on but I will get to that later
    cattr_accessor :current_role # I added current_role so I can check which role does user haves on a group that he is on but I will get to that later
    end
  3. simonlehmann revised this gist Mar 3, 2017. 3 changed files with 6 additions and 7 deletions.
    5 changes: 2 additions & 3 deletions company.rb
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,7 @@
    class Company < ActiveRecord::Base

    cattr_accessor :current # here I added a current company, so I can check wich company is active I will use that later.

    has_many :unities
    has_many :users, through: :unities
    has_many :roles, through: :unities

    cattr_accessor :current # here I added a current company, so I can check wich company is active I will use that later.
    end
    4 changes: 2 additions & 2 deletions unity.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    class Unity < ActiveRecord::Base
    attr_accessible :role_id, :user_id, :company_id

    belongs_to :role
    belongs_to :company
    belongs_to :user

    attr_accessible :role_id, :user_id, :company_id
    end
    4 changes: 2 additions & 2 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    class User < ActiveRecord::Base
    rolify

    cattr_accessor :current_role # I added current_role so I can check which role does user haves on a company that he is on but I will get to that later

    has_many :unities
    has_many :roles, through: :unities
    has_many :companies, through: :unities

    cattr_accessor :current_role # I added current_role so I can check which role does user haves on a company that he is on but I will get to that later
    end
  4. simonlehmann revised this gist Mar 3, 2017. 6 changed files with 26 additions and 40 deletions.
    25 changes: 10 additions & 15 deletions ability.rb
    Original file line number Diff line number Diff line change
    @@ -2,22 +2,17 @@ class Ability
    include CanCan::Ability

    def initialize(user)

    user ||= User.new # This is used for not logged user if you have a need for it

    if User.current_role == 'admin' # From ApplicationController we can get current_role and check it up against the role we want.
    can :manage, :all
    else
    if User.current_role == 'moderator'
    can :read, Products
    can :update, Products
    cannot :destroy, Products
    cannot :create, Products

    cannot :manage, Client
    end

    can :read, :all
    case user.current_role
    when 'admin'
    can :manage, :all
    when 'moderator'
    can :read, Products
    can :update, Products
    cannot :destroy, Products
    cannot :create, Products
    cannot :manage, Client
    # ... other roles and abilities ...
    end
    end
    end
    26 changes: 11 additions & 15 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,13 @@
    class ApplicationController < ActionController::Base

    before_filter :initialize_company, :initialize_user_role

    # we find company that user currently uses and we store data in class ( cattr_accessor :current)


    def initialize_company
    Company.current = Company.find(params[:company_id]) if params.has_key?(:company_id)
    end

    # we find user role for companie that he is managing and we store data in class ( cattr_accessor :current_role )

    def initialize_user_role
    User.current_role = Unity.where(company_id: Company.current.id, user_id: current_user.id).first.role.name unless Company.current.nil?
    end
    before_filter :initialize_company, :initialize_user_role

    # we find company that user currently uses and we store data in class ( cattr_accessor :current)
    def initialize_company
    Company.current = Company.find(params[:company_id]) if params.has_key?(:company_id)
    end

    # we find user role for companie that he is managing and we store data in class ( cattr_accessor :current_role )
    def initialize_user_role
    User.current_role = Unity.where(company_id: Company.current.id, user_id: current_user.id).first.role.name unless Company.current.nil?
    end
    end
    2 changes: 1 addition & 1 deletion migration file
    Original file line number Diff line number Diff line change
    @@ -3,5 +3,5 @@ create_table :unities do |t|
    t.integer :user_id
    t.integer :company_id

    t.timestamps
    t.timestamps
    end
    2 changes: 0 additions & 2 deletions role.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    class Role < ActiveRecord::Base

    has_many :unities
    has_many :users, through: :unities
    has_many :companies, through: :unities

    end
    10 changes: 4 additions & 6 deletions unity.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    class Unity < ActiveRecord::Base

    attr_accessible :role_id, :user_id, :company_id
    attr_accessible :role_id, :user_id, :company_id

    belongs_to :role
    belongs_to :company
    belongs_to :user

    belongs_to :role
    belongs_to :company
    belongs_to :user
    end
    1 change: 0 additions & 1 deletion user.rb
    Original file line number Diff line number Diff line change
    @@ -6,5 +6,4 @@ class User < ActiveRecord::Base
    has_many :unities
    has_many :roles, through: :unities
    has_many :companies, through: :unities

    end
  5. @vmarcetic vmarcetic created this gist Jul 21, 2013.
    23 changes: 23 additions & 0 deletions ability.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    class Ability
    include CanCan::Ability

    def initialize(user)

    user ||= User.new # This is used for not logged user if you have a need for it

    if User.current_role == 'admin' # From ApplicationController we can get current_role and check it up against the role we want.
    can :manage, :all
    else
    if User.current_role == 'moderator'
    can :read, Products
    can :update, Products
    cannot :destroy, Products
    cannot :create, Products

    cannot :manage, Client
    end

    can :read, :all
    end
    end
    end
    17 changes: 17 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    class ApplicationController < ActionController::Base

    before_filter :initialize_company, :initialize_user_role

    # we find company that user currently uses and we store data in class ( cattr_accessor :current)


    def initialize_company
    Company.current = Company.find(params[:company_id]) if params.has_key?(:company_id)
    end

    # we find user role for companie that he is managing and we store data in class ( cattr_accessor :current_role )

    def initialize_user_role
    User.current_role = Unity.where(company_id: Company.current.id, user_id: current_user.id).first.role.name unless Company.current.nil?
    end
    end
    8 changes: 8 additions & 0 deletions company.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class Company < ActiveRecord::Base

    cattr_accessor :current # here I added a current company, so I can check wich company is active I will use that later.

    has_many :unities
    has_many :users, through: :unities
    has_many :roles, through: :unities
    end
    7 changes: 7 additions & 0 deletions migration file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    create_table :unities do |t|
    t.integer :role_id
    t.integer :user_id
    t.integer :company_id

    t.timestamps
    end
    7 changes: 7 additions & 0 deletions role.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    class Role < ActiveRecord::Base

    has_many :unities
    has_many :users, through: :unities
    has_many :companies, through: :unities

    end
    9 changes: 9 additions & 0 deletions unity.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class Unity < ActiveRecord::Base

    attr_accessible :role_id, :user_id, :company_id

    belongs_to :role
    belongs_to :company
    belongs_to :user

    end
    10 changes: 10 additions & 0 deletions user.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class User < ActiveRecord::Base
    rolify

    cattr_accessor :current_role # I added current_role so I can check which role does user haves on a company that he is on but I will get to that later

    has_many :unities
    has_many :roles, through: :unities
    has_many :companies, through: :unities

    end