Configure Rolify
This commit is contained in:
parent
afaf1e6f97
commit
51f5f29404
5
Gemfile
5
Gemfile
|
@ -80,6 +80,11 @@ gem 'devise', '~> 4.5'
|
|||
# Translations for the devise gem.
|
||||
gem 'devise-i18n', '~> 1.7'
|
||||
|
||||
# Very simple Roles library without any authorization enforcement
|
||||
# supporting scope on resource objects (instance or class).
|
||||
# Supports ActiveRecord and Mongoid ORMs.
|
||||
gem 'rolify', '~> 5.2'
|
||||
|
||||
group :development, :test do
|
||||
# factory_bot provides a framework and DSL for defining and using factories.
|
||||
gem 'factory_bot_rails', '~> 4.10'
|
||||
|
|
|
@ -204,6 +204,7 @@ GEM
|
|||
http-cookie (>= 1.0.2, < 2.0)
|
||||
mime-types (>= 1.16, < 4.0)
|
||||
netrc (~> 0.8)
|
||||
rolify (5.2.0)
|
||||
rspec-core (3.8.0)
|
||||
rspec-support (~> 3.8.0)
|
||||
rspec-expectations (3.8.2)
|
||||
|
@ -326,6 +327,7 @@ DEPENDENCIES
|
|||
rails (~> 5.2.1)
|
||||
rails-i18n (~> 5.1)
|
||||
rest-client (~> 2.0)
|
||||
rolify (~> 5.2)
|
||||
rspec-rails (~> 3.8)
|
||||
rubocop (~> 0.60.0)
|
||||
sass-rails (~> 5.0)
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Role < ApplicationRecord
|
||||
has_many :user_roles, dependent: :destroy
|
||||
|
||||
has_many :users, through: :user_roles, source: :user
|
||||
|
||||
belongs_to :resource, polymorphic: true, optional: true
|
||||
|
||||
validates :resource_type,
|
||||
allow_nil: true,
|
||||
inclusion: { in: Rolify.resource_types }
|
||||
|
||||
scopify
|
||||
end
|
|
@ -13,4 +13,6 @@ class User < ApplicationRecord
|
|||
:trackable,
|
||||
:validatable,
|
||||
)
|
||||
|
||||
rolify role_join_table_name: 'user_roles'
|
||||
end
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class UserRole < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :role
|
||||
end
|
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
Rolify.configure do |config|
|
||||
# Dynamic shortcuts for User class (user.is_admin? like methods).
|
||||
# Default is: false.
|
||||
config.use_dynamic_shortcuts
|
||||
|
||||
# Configuration to remove roles from database
|
||||
# once the last resource is removed. Default is: true.
|
||||
config.remove_role_if_empty = false
|
||||
end
|
|
@ -0,0 +1,24 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RolifyCreateRoles < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :roles do |t|
|
||||
t.timestamps null: false
|
||||
t.string :name
|
||||
t.references :resource, polymorphic: true
|
||||
|
||||
t.index %i[name resource_type resource_id], unique: true
|
||||
end
|
||||
|
||||
create_table :user_roles do |t|
|
||||
t.timestamps null: false
|
||||
t.references :user, null: false
|
||||
t.references :role, null: false
|
||||
|
||||
t.index %i[user_id role_id], unique: true
|
||||
end
|
||||
|
||||
add_foreign_key :user_roles, :users
|
||||
add_foreign_key :user_roles, :roles
|
||||
end
|
||||
end
|
24
db/schema.rb
24
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2018_11_29_203927) do
|
||||
ActiveRecord::Schema.define(version: 2018_11_29_231814) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -30,6 +30,16 @@ ActiveRecord::Schema.define(version: 2018_11_29_203927) do
|
|||
t.text "comment"
|
||||
end
|
||||
|
||||
create_table "roles", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.string "name"
|
||||
t.string "resource_type"
|
||||
t.bigint "resource_id"
|
||||
t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id", unique: true
|
||||
t.index ["resource_type", "resource_id"], name: "index_roles_on_resource_type_and_resource_id"
|
||||
end
|
||||
|
||||
create_table "telegram_bots", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
@ -37,6 +47,16 @@ ActiveRecord::Schema.define(version: 2018_11_29_203927) do
|
|||
t.string "api_token", null: false
|
||||
end
|
||||
|
||||
create_table "user_roles", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.bigint "user_id", null: false
|
||||
t.bigint "role_id", null: false
|
||||
t.index ["role_id"], name: "index_user_roles_on_role_id"
|
||||
t.index ["user_id", "role_id"], name: "index_user_roles_on_user_id_and_role_id", unique: true
|
||||
t.index ["user_id"], name: "index_user_roles_on_user_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
@ -63,4 +83,6 @@ ActiveRecord::Schema.define(version: 2018_11_29_203927) do
|
|||
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
|
||||
end
|
||||
|
||||
add_foreign_key "user_roles", "roles"
|
||||
add_foreign_key "user_roles", "users"
|
||||
end
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :role do
|
||||
end
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Role, type: :model do
|
||||
subject { create :role }
|
||||
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
|
@ -4,4 +4,6 @@ require 'rails_helper'
|
|||
|
||||
RSpec.describe User, type: :model do
|
||||
subject { create :user }
|
||||
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
|
|
Reference in New Issue