From 51f5f294048e50a469380ee31745589da4347889 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 30 Nov 2018 04:26:48 +0500 Subject: [PATCH] Configure Rolify --- Gemfile | 5 ++++ Gemfile.lock | 2 ++ app/models/role.rb | 15 ++++++++++++ app/models/user.rb | 2 ++ app/models/user_role.rb | 6 +++++ config/initializers/rolify.rb | 11 +++++++++ .../20181129231814_rolify_create_roles.rb | 24 +++++++++++++++++++ db/schema.rb | 24 ++++++++++++++++++- factories/roles.rb | 6 +++++ spec/models/role_spec.rb | 9 +++++++ spec/models/user_spec.rb | 2 ++ 11 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/models/role.rb create mode 100644 app/models/user_role.rb create mode 100644 config/initializers/rolify.rb create mode 100644 db/migrate/20181129231814_rolify_create_roles.rb create mode 100644 factories/roles.rb create mode 100644 spec/models/role_spec.rb diff --git a/Gemfile b/Gemfile index 663f198..96d4c21 100644 --- a/Gemfile +++ b/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' diff --git a/Gemfile.lock b/Gemfile.lock index 20db720..240a59b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/app/models/role.rb b/app/models/role.rb new file mode 100644 index 0000000..58b0f00 --- /dev/null +++ b/app/models/role.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 8b8bd9f..1484dd3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,4 +13,6 @@ class User < ApplicationRecord :trackable, :validatable, ) + + rolify role_join_table_name: 'user_roles' end diff --git a/app/models/user_role.rb b/app/models/user_role.rb new file mode 100644 index 0000000..b79aefa --- /dev/null +++ b/app/models/user_role.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +class UserRole < ApplicationRecord + belongs_to :user + belongs_to :role +end diff --git a/config/initializers/rolify.rb b/config/initializers/rolify.rb new file mode 100644 index 0000000..bd0377f --- /dev/null +++ b/config/initializers/rolify.rb @@ -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 diff --git a/db/migrate/20181129231814_rolify_create_roles.rb b/db/migrate/20181129231814_rolify_create_roles.rb new file mode 100644 index 0000000..b1246a5 --- /dev/null +++ b/db/migrate/20181129231814_rolify_create_roles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f98b7ef..40eb54b 100644 --- a/db/schema.rb +++ b/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 diff --git a/factories/roles.rb b/factories/roles.rb new file mode 100644 index 0000000..130ad8c --- /dev/null +++ b/factories/roles.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :role do + end +end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb new file mode 100644 index 0000000..2b63797 --- /dev/null +++ b/spec/models/role_spec.rb @@ -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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4023235..14f98b4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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