1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

(quality) add unique constraints in db

Also: code lint in SSO models
This commit is contained in:
Sylvain 2023-03-02 13:26:05 +01:00
parent 66a55b62d9
commit 6ae8c965f3
5 changed files with 32 additions and 16 deletions

View File

@ -41,10 +41,10 @@ class AuthProvider < ApplicationRecord
provider = find_by(status: 'active')
return local if provider.nil?
return provider
provider
rescue ActiveRecord::StatementInvalid
# we fall here on database creation because the table "active_providers" still does not exists at the moment
return local
local
end
end
@ -59,7 +59,7 @@ class AuthProvider < ApplicationRecord
parsed = /^([^-]+)-(.+)$/.match(strategy_name)
ret = nil
all.each do |strategy|
all.find_each do |strategy|
if strategy.provider_type == parsed[1] && strategy.name.downcase.parameterize == parsed[2]
ret = strategy
break
@ -70,13 +70,13 @@ class AuthProvider < ApplicationRecord
## Return the name that should be registered in OmniAuth for the corresponding strategy
def strategy_name
provider_type + '-' + name.downcase.parameterize
"#{provider_type}-#{name.downcase.parameterize}"
end
## Return the provider type name without the "Provider" part.
## eg. DatabaseProvider will return 'database'
def provider_type
providable_type[0..-9].downcase
providable_type[0..-9]&.downcase
end
## Return the user's profile fields that are currently managed from the SSO
@ -84,7 +84,7 @@ class AuthProvider < ApplicationRecord
def sso_fields
fields = []
auth_provider_mappings.each do |mapping|
fields.push(mapping.local_model + '.' + mapping.local_field)
fields.push("#{mapping.local_model}.#{mapping.local_field}")
end
fields
end
@ -96,10 +96,10 @@ class AuthProvider < ApplicationRecord
end
def safe_destroy
if status != 'active'
destroy
else
if status == 'active'
false
else
destroy
end
end

View File

@ -3,6 +3,5 @@
# OAuth2Provider is a special type of AuthProvider which provides authentication through an external SSO server using
# the oAuth 2.0 protocol.
class OAuth2Provider < ApplicationRecord
has_one :auth_provider, as: :providable
has_one :auth_provider, as: :providable, dependent: :destroy
end

View File

@ -3,7 +3,7 @@
# OpenIdConnectProvider is a special type of AuthProvider which provides authentication through an external SSO server using
# the OpenID Connect protocol.
class OpenIdConnectProvider < ApplicationRecord
has_one :auth_provider, as: :providable
has_one :auth_provider, as: :providable, dependent: :destroy
validates :issuer, presence: true
validates :client__identifier, presence: true
@ -28,8 +28,8 @@ class OpenIdConnectProvider < ApplicationRecord
end
def client_config
OpenIdConnectProvider.columns.map(&:name).filter { |n| n.start_with?('client__') }.map do |n|
OpenIdConnectProvider.columns.map(&:name).filter { |n| n.start_with?('client__') }.to_h do |n|
[n.sub('client__', ''), send(n)]
end.to_h
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
# Add uniqueness constraint at database level
class AddUniquenessConstraints < ActiveRecord::Migration[5.2]
def change
add_index :credits, %i[plan_id creditable_id creditable_type], unique: true
add_index :prices, %i[plan_id priceable_id priceable_type group_id duration], unique: true,
name: 'index_prices_on_plan_priceable_group_and_duration'
add_index :price_categories, :name, unique: true
add_index :auth_providers, :name, unique: true
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_02_13_134954) do
ActiveRecord::Schema.define(version: 2023_03_02_120458) do
# These are extensions that must be enabled in order to support this database
enable_extension "fuzzystrmatch"
@ -122,6 +122,7 @@ ActiveRecord::Schema.define(version: 2023_02_13_134954) do
t.datetime "updated_at", null: false
t.string "providable_type"
t.integer "providable_id"
t.index ["name"], name: "index_auth_providers_on_name", unique: true
end
create_table "availabilities", id: :serial, force: :cascade do |t|
@ -163,10 +164,10 @@ ActiveRecord::Schema.define(version: 2023_02_13_134954) do
create_table "cart_item_event_reservation_tickets", force: :cascade do |t|
t.integer "booked"
t.bigint "event_price_category_id"
t.bigint "cart_item_event_reservation_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "event_price_category_id"
t.index ["cart_item_event_reservation_id"], name: "index_cart_item_tickets_on_cart_item_event_reservation"
t.index ["event_price_category_id"], name: "index_cart_item_tickets_on_event_price_category"
end
@ -287,6 +288,7 @@ ActiveRecord::Schema.define(version: 2023_02_13_134954) do
t.integer "hours"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["plan_id", "creditable_id", "creditable_type"], name: "index_credits_on_plan_id_and_creditable_id_and_creditable_type", unique: true
t.index ["plan_id"], name: "index_credits_on_plan_id"
end
@ -787,6 +789,8 @@ ActiveRecord::Schema.define(version: 2023_02_13_134954) do
t.text "conditions"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index "btrim(lower((name)::text))", name: "index_price_categories_on_TRIM_BOTH_FROM_LOWER_name", unique: true
t.index ["name"], name: "index_price_categories_on_name", unique: true
end
create_table "prices", id: :serial, force: :cascade do |t|
@ -799,6 +803,7 @@ ActiveRecord::Schema.define(version: 2023_02_13_134954) do
t.datetime "updated_at", null: false
t.integer "duration", default: 60
t.index ["group_id"], name: "index_prices_on_group_id"
t.index ["plan_id", "priceable_id", "priceable_type", "group_id", "duration"], name: "index_prices_on_plan_priceable_group_and_duration", unique: true
t.index ["plan_id"], name: "index_prices_on_plan_id"
t.index ["priceable_type", "priceable_id"], name: "index_prices_on_priceable_type_and_priceable_id"
end