Temp

How to create a new role in Solidus or Spree

Jul 19, 2018 by ohiodn8




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=> 

Spree::Role.all

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  Spree::Role.all


CREATING A NEW ROLE

Still in the rails console, run =>

Spree::Role.create(name: 'vendor')

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 =>

http://localhost:3000/admin

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 => 

http://localhost:3000/admin

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!)



About ohiodn8

Ruby on Rails developer, AWS Engineer, anything fun, music, a little bit of mobile game. . .

Comments (14)

Source:
https://subscribe.ru/author/26101922

about 3 hours ago by FloydAmopy

Source:
http://www.realto.ru/journal/articles/poteryali-dokumenty-ne-beda-vam-pomogut-ih-vosstanovit-v-diplomyshop/

about 5 hours ago by FloydAmopy

Source:
https://stolovoye-serebro.ru/product-category/stolovoe-serebro/grafiny/
Серебряные графины

1 day ago by FloydAmopy

Source:
https://stolovoye-serebro.ru/product-category/stolovoe-serebro/chajnaja-posuda/podstakanniki/
[url=https://stolovoye-serebro.ru/product-category/stolovoe-serebro/chajnaja-posuda/podstakanniki/ ]Серебряные подстаканники[/url]

1 day ago by FloydAmopy

Source:
https://xn----7sbqkfd0alodr9b.xn--p1ai/lechenie-narkomanii-v-nalchike-kabardino-balkariya/
Лечение наркомании в Нальчике

2 days ago by FloydAmopy

Source:
https://xn----7sbqkfd0alodr9b.xn--p1ai/lechenie-narkomanii-vo-vladikavkaze-severnaya-osetiya/
Лечение наркозависимых во Владикавказе

3 days ago by FloydAmopy

Source:
https://xn----7sbqkfd0alodr9b.xn--p1ai/lechenie-narkomanii-v-nalchike-kabardino-balkariya/
Реабилитационный центр для зависимых в КБР

3 days ago by FloydAmopy

Decoding:
IonCube Decoder / Source Guardian / Zend / TrueBug

DECODING:
PHP 5.6 / 7.0 / 7.1 / 7.2 / 7.3 / 7.4 ALL PHP verions we can DECODE


Decompile prices:
1-20 files = 10$ / file
21-50 files = 9$ / file
51-100 files = 8$ / file
101 and more = 7$ / file

Forum verified: https://zerocode.su/index.php?threads/ioncube-decoding-and-deobfuscation-php.9/

Contacts:
telegram: MR_Fredo
Jabber : mr_fredo@thesecure.biz

5 days ago by Michaellache

Sell casino script Goldsvet 8.X NULLED. SOURCE CODE

A new version of GOLDSVET / mrs system. In this issue you will find new features for managing games and the system, the unification of
game banks, a significant increase in performance and a decrease in the load on the server and database. Several new unique interfaces,
as well as updating the Laravel Framework to version 8.14.0.

Our casino script:
1) has no domain restrictions.
2) all games are open source.
3) 960+ games in total.
4) all games work on a computer and on a phone, and have html5 format.

Casino price: 2500 EUR
Goldsvet MRS Core 8.0 DECODED (NULLED) + GAMES
Video review: https://youtu.be/-ZcD8Jf3YN4
Contacts:
telegram: https://t.me/kseGB
mail: saportcasino@protonmail.com
site: http://win-game.net/

7 days ago by Jasonmeada

Great Content. Works well

3 months ago by CharlesOwept

Good Read

9 months ago by Alvaroknogs

Thank you

9 months ago by ZuvilsRog

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

about 3 years ago by Hugens Louis

Great Tutorial! It works!! Thanks for sharing!!!

about 3 years ago by fhms info