Spree customize/extend user roles and permissions

Posted by swapnil on Stack Overflow See other posts from Stack Overflow or by swapnil
Published on 2012-06-18T09:13:42Z Indexed on 2012/06/18 9:15 UTC
Read the original article Hit count: 275

Filed under:
|

I am trying to specify some custom roles in Spree for example role 'client' and extend the permissions to access the admin section for this role.

This user will be able to access only those Product created by that user. Concept is letting a user with role 'client' manage only products and other certain Models.

To start with I added CanCan plugin and defined a RoleAbility Class in role_ability.rb

Just following this post : Spree Custom Roles Permissions

class RoleAbility
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.has_role? 'admin'
        can :manage, :all
    elsif user.has_role? 'client_admin'
      can :read, Product
      can :admin, Product
    end
  end
end

Added this to an initializer : config/initializers/spree.rb

Ability.register_ability(RetailerAbility)

Also extended admin_products_controller_decorator.rb :app/controllersadmin_products_controller_decorator.rb

Admin::ProductsController.class_eval do
    def authorize_admin
        authorize! :admin, Product
        authorize! params[:action].to_sym, Product
    end
end

But I am getting flash message 'Authorisation Failure'

Trying to find some luck, I referred following links

A github gist for Customizing Spree Roles : https://gist.github.com/1277326

Here's a similar issue what I am facing : http://groups.google.com/group/spree-user/browse_thread/thread/1e819e10410d03c5/23b269e09c7ed47e

All efforts in vain...

Any pointers of what is going on here highly appreciated ?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about spree