Project

copper

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Roles, Permissions, User switching
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.5
>= 0
>= 0
 Project Readme

Copper

User Permissions and Policies. Named after the aussie word for police officer (https://www.youtube.com/watch?v=tKNOgX-u8ao)

Installation

Add this line to your application's Gemfile:

gem 'copper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install copper

Permission module and Policies

Cancancan provides an Ability class to control permissions but it is limited in its ability to define more specific controls around certain permissions. So we introduce the Permission module and Policies.

You can create your own Permission class (say an ActiveRecord model) which stores permissions (specifically the object_type and action_name.

For example:

class Permission < ActiveRecord::Base
  include Copper::Permission
end

Permission.create(
  object_type: 'User',
  action_name: 'manage',
  description: 'Allow management of users'
)

Modify your cancancan Ability class as follows (or similar, the key is applying the permissions to the ability):

def initialize(user)
  if user.is_admin?
    can :manage, :all
  else
    user.permissions.each do |permission|
      permission.apply_to(self)
    end
  end
end

From here everything will work as normal, BUT lets say you want to limit the managing of users to a certain group. You could create a policy:

class UserPolicy
  def initialize(ability)
    @ability = ability
  end

  def apply!
    @ability.can(:manage, User, group_id: groups.pluck(:id))
  end

  def groups
    @ability.user.groups
  end
end

This policy is called a Type Policy as it applies to any action taken on that Type. You can also define Action Policies which will overide the type policy for the given action.

class DestroyUserPolicy
  # ...destroy specific logic
end

Contributing

  1. Fork it ( http://github.com/jobready/copper/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request