In this tutorial we will be adding a new role to a solidus project. At the moment my project is built on the solidus gem, but then, the solidus gem is built on spree. So, this tutorial will work for a spree or solidus project.
A solidus/spree user defines only one role with cancan (or cancancan) - admin. You can check this by running
rails console in the root of your project folder (on the terminal/cmd), and inside the console run=>
That should be fine to view the admin role with an id of 1, or it may prompt you to run
Spree::Role.connection, and then
CREATING A NEW ROLE
Still in the rails console, run =>
This will create a new role - vendor, and you can close the console.
Head over to your application folder and create app/models/spree/ability.rb =>
require 'cancan' module Spree class Ability include CanCan::Ability class_attribute :abilities self.abilities = Set.new # Allows us to go beyond the standard cancan initialize method which makes it difficult for engine def self.register_ability(ability) abilities.add(ability) end def self.remove_ability(ability) abilities.delete(ability) end def initialize(user) clear_aliased_actions # override cancan default aliasing (we don't want to differentiate between read and index) alias_action :delete, to: :destroy alias_action :edit, to: :update alias_action :new, to: :create alias_action :new_action, to: :create alias_action :show, to: :read alias_action :index, :read, to: :display alias_action :create, :update, :destroy, to: :modify user ||= Spree.user_class.new if user.respond_to?(:has_spree_role?) && user.has_spree_role?('admin') can :manage, :all elsif user.respond_to?(:has_spree_role?) && user.has_spree_role?('vendor') can :manage, User can :manage, Product else can :display, Country can :display, OptionType can :display, OptionValue can :create, Order can :read, Order do |order, token| order.user == user || order.token && token == order.token end can :update, Order do |order, token| !order.completed? && (order.user == user || order.token && token == order.token) end can :display, CreditCard, user_id: user.id can :display, Product can :display, ProductProperty can :display, Property can :create, Spree.user_class can [:read, :update, :destroy], Spree.user_class, id: user.id can :display, State can :display, Taxon can :display, Taxonomy can :display, Variant can :display, Zone end # Include any abilities registered by extensions, etc. Ability.abilities.merge(abilities_to_register).each do |clazz| merge clazz.new(user) end # Protect admin role cannot [:update, :destroy], Role, name: ['admin'] end private # you can override this method to register your abilities # this method has to return array of classes def abilities_to_register  end end end
In here, you gave the admin role the ability to manage all your resources and gave the vendor role the ability to mange your product and user resources.
Start your server and head over to =>
Login with your admin password and create a new user account. Beneath the email link, you should see admin and vendor. Select vendor (input the new email and password for the vendor account) and create.
Now logout of your admin account, and head back to =>
This time use the vendor account details that you just create and you'll be greeted with image below =>
Ignore the Authorization Failure error, and click on the Products and Users tab on the left side of the screen. To give your vendor role more functionality/ability, edit your the ability.rb file.
(Hope I covered everything!)
Ruby on Rails developer, AWS Engineer, anything fun, music, a little bit of mobile game. . .
Thanks a lot for this tutorial. How can i create a separated route for the vendor, and not accessing it from the admin link? Thanks
almost 2 years ago by Hugens Louis
Great Tutorial! It works!! Thanks for sharing!!!
about 2 years ago by fhms info