-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #690 from ShelterTechSF/687-authorization-schema
687 authorization schema
- Loading branch information
Showing
21 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: groups | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_groups_on_name (name) UNIQUE | ||
# | ||
|
||
# Groups provide a way to categorize a given group of users and to assign | ||
# specific permissions to said group | ||
|
||
class Group < ApplicationRecord | ||
has_and_belongs_to_many(:users, | ||
join_table: "user_groups") | ||
|
||
has_and_belongs_to_many(:permissions, | ||
join_table: "group_permissions") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: group_permissions | ||
# | ||
# group_id :bigint not null | ||
# permission_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_group_permissions_on_group_id_and_permission_id (group_id,permission_id) | ||
# index_group_permissions_on_permission_id_and_group_id (permission_id,group_id) | ||
# | ||
class GroupPermission < ApplicationRecord | ||
belongs_to :group | ||
belongs_to :permission | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: permissions | ||
# | ||
# id :bigint not null, primary key | ||
# action :integer | ||
# resource_id :bigint | ||
# service_id :bigint | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_permissions_on_resource_id (resource_id) | ||
# index_permissions_on_resource_id_and_action (resource_id,action) UNIQUE | ||
# index_permissions_on_service_id (service_id) | ||
# index_permissions_on_service_id_and_action (service_id,action) UNIQUE | ||
# | ||
|
||
# The permissions system provides a way to assign specific permissions | ||
# to a group to view/alter specific objects. | ||
|
||
class Permission < ApplicationRecord | ||
enum action: { add: 0, view: 1, edit: 2, remove: 3 } | ||
|
||
has_and_belongs_to_many(:groups, join_table: "group_permissions") | ||
belongs_to :resource, optional: true | ||
belongs_to :service, optional: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: users | ||
# | ||
# id :integer not null, primary key | ||
# name :string | ||
# | ||
class User < ActiveRecord::Base | ||
has_and_belongs_to_many(:groups, | ||
join_table: "user_groups") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: user_groups | ||
# | ||
# user_id :bigint not null | ||
# group_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_user_groups_on_group_id (group_id) | ||
# index_user_groups_on_user_id (user_id) | ||
# | ||
class UserGroup < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :groups | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateGroups < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :groups do |t| | ||
t.string :name | ||
t.index :name, unique: true | ||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateJoinTableUsersGroups < ActiveRecord::Migration[6.1] | ||
def change | ||
create_join_table :users, :groups, table_name: :user_groups do |t| | ||
t.index :user_id | ||
t.index :group_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class CreatePermissions < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :permissions do |t| | ||
t.integer :action | ||
t.references :resource, foreign_key: true | ||
t.references :service, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :permissions, [:service_id, :action], unique: true | ||
add_index :permissions, [:resource_id, :action], unique: true | ||
|
||
reversible do |dir| | ||
dir.up do | ||
execute <<-SQL | ||
ALTER TABLE permissions | ||
ADD CONSTRAINT resource_xor_service | ||
CHECK ( | ||
(resource_id IS NOT NULL AND service_id IS NULL) OR | ||
(resource_id IS NULL AND service_id IS NOT NULL) | ||
) | ||
SQL | ||
end | ||
|
||
dir.down do | ||
execute <<-SQL | ||
ALTER TABLE permissions | ||
DROP CONSTRAINT resource_xor_service | ||
SQL | ||
end | ||
end | ||
|
||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
db/migrate/20230531183601_create_join_table_group_permissions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateJoinTableGroupPermissions < ActiveRecord::Migration[6.1] | ||
def change | ||
create_join_table :groups, :permissions, table_name: :group_permissions do |t| | ||
t.index [:group_id, :permission_id] | ||
t.index [:permission_id, :group_id] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: groups | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_groups_on_name (name) UNIQUE | ||
# | ||
FactoryBot.define do | ||
factory :group do | ||
name { "MyString" } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: permissions | ||
# | ||
# id :bigint not null, primary key | ||
# action :integer | ||
# resource_id :bigint | ||
# service_id :bigint | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_permissions_on_resource_id (resource_id) | ||
# index_permissions_on_resource_id_and_action (resource_id,action) UNIQUE | ||
# index_permissions_on_service_id (service_id) | ||
# index_permissions_on_service_id_and_action (service_id,action) UNIQUE | ||
# | ||
FactoryBot.define do | ||
factory :permission do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: user_groups | ||
# | ||
# user_id :bigint not null | ||
# group_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_user_groups_on_group_id (group_id) | ||
# index_user_groups_on_user_id (user_id) | ||
# | ||
FactoryBot.define do | ||
factory :user_group do | ||
user { nil } | ||
groups { nil } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: groups | ||
# | ||
# id :bigint not null, primary key | ||
# name :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_groups_on_name (name) UNIQUE | ||
# | ||
require 'rails_helper' | ||
|
||
RSpec.describe Group, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Oops, something went wrong.