Commit 675b67b1 authored by Drew Blessing's avatar Drew Blessing Committed by Adam Hegyi

Create SamlGroupLink model

Create the model associated with the new `saml_group_links`
database table.
parent f8aa9be0
......@@ -8,20 +8,18 @@ class CreateSamlGroupLinks < ActiveRecord::Migration[6.0]
disable_ddl_transaction!
def up
unless table_exists?(:saml_group_links)
with_lock_retries do
create_table :saml_group_links do |t|
t.references :group, foreign_key: { to_table: :namespaces, on_delete: :cascade }, null: false
t.timestamps_with_timezone
t.integer :access_level, null: false
t.text :group_name, null: false
with_lock_retries do
create_table :saml_group_links, if_not_exists: true do |t|
t.integer :access_level, null: false, limit: 2
t.references :group, index: false, foreign_key: { to_table: :namespaces, on_delete: :cascade }, null: false
t.timestamps_with_timezone
t.text :saml_group_name, null: false
t.index [:group_id, :group_name], unique: true
end
t.index [:group_id, :saml_group_name], unique: true
end
end
add_text_limit :saml_group_links, :group_name, 255
add_text_limit :saml_group_links, :saml_group_name, 255
end
def down
......
......@@ -15754,12 +15754,12 @@ ALTER SEQUENCE routes_id_seq OWNED BY routes.id;
CREATE TABLE saml_group_links (
id bigint NOT NULL,
access_level smallint NOT NULL,
group_id bigint NOT NULL,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
access_level integer NOT NULL,
group_name text NOT NULL,
CONSTRAINT check_1a5ae2ac07 CHECK ((char_length(group_name) <= 255))
saml_group_name text NOT NULL,
CONSTRAINT check_1b3fc49d1e CHECK ((char_length(saml_group_name) <= 255))
);
CREATE SEQUENCE saml_group_links_id_seq
......@@ -21478,9 +21478,7 @@ CREATE INDEX index_routes_on_path_trigram ON routes USING gin (path gin_trgm_ops
CREATE UNIQUE INDEX index_routes_on_source_type_and_source_id ON routes USING btree (source_type, source_id);
CREATE INDEX index_saml_group_links_on_group_id ON saml_group_links USING btree (group_id);
CREATE UNIQUE INDEX index_saml_group_links_on_group_id_and_group_name ON saml_group_links USING btree (group_id, group_name);
CREATE UNIQUE INDEX index_saml_group_links_on_group_id_and_saml_group_name ON saml_group_links USING btree (group_id, saml_group_name);
CREATE INDEX index_saml_providers_on_group_id ON saml_providers USING btree (group_id);
......
......@@ -27,6 +27,7 @@ module EE
has_one :scim_oauth_access_token
has_many :ldap_group_links, foreign_key: 'group_id', dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :saml_group_links, foreign_key: 'group_id'
has_many :hooks, dependent: :destroy, class_name: 'GroupHook' # rubocop:disable Cop/ActiveRecordDependent
has_one :dependency_proxy_setting, class_name: 'DependencyProxy::GroupSetting'
......@@ -222,6 +223,12 @@ module EE
ensure_saml_discovery_token!
end
def saml_enabled?
return false unless saml_provider
saml_provider.persisted? && saml_provider.enabled?
end
override :multiple_issue_boards_available?
def multiple_issue_boards_available?
feature_available?(:multiple_group_issue_boards)
......
# frozen_string_literal: true
class SamlGroupLink < ApplicationRecord
belongs_to :group
enum access_level: ::Gitlab::Access.options_with_owner
validates :group, :access_level, presence: true
validates :saml_group_name, presence: true, uniqueness: { scope: [:group_id] }, length: { maximum: 255 }
end
---
title: Create SamlGroupLink table and model
merge_request: 45061
author:
type: added
# frozen_string_literal: true
FactoryBot.define do
factory :saml_group_link do
sequence(:saml_group_name) { |n| "saml-group#{n}" }
access_level { Gitlab::Access::GUEST }
group
end
end
......@@ -23,6 +23,7 @@ RSpec.describe Group do
it { is_expected.to have_one(:deletion_schedule) }
it { is_expected.to have_one(:group_wiki_repository) }
it { is_expected.to belong_to(:push_rule) }
it { is_expected.to have_many(:saml_group_links) }
it_behaves_like 'model with wiki' do
let(:container) { create(:group, :nested, :wiki_repo) }
......@@ -767,6 +768,30 @@ RSpec.describe Group do
end
end
describe '#saml_enabled?' do
subject { group.saml_enabled? }
context 'when a SAML provider does not exist' do
it { is_expected.to eq(false) }
end
context 'when a SAML provider exists and is persisted' do
before do
create(:saml_provider, group: group)
end
it { is_expected.to eq(true) }
end
context 'when a SAML provider is not persisted' do
before do
build(:saml_provider, group: group)
end
it { is_expected.to eq(false) }
end
end
describe '#alpha/beta_feature_available?' do
it_behaves_like 'an entity with alpha/beta feature support' do
let(:entity) { group }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe SamlGroupLink do
describe 'associations' do
it { is_expected.to belong_to(:group) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:group) }
it { is_expected.to validate_presence_of(:access_level) }
it { is_expected.to validate_presence_of(:saml_group_name) }
it { is_expected.to validate_length_of(:saml_group_name).is_at_most(255) }
it { is_expected.to define_enum_for(:access_level).with_values(Gitlab::Access.options_with_owner) }
context 'group name uniqueness' do
before do
create(:saml_group_link, group: create(:group))
end
it { is_expected.to validate_uniqueness_of(:saml_group_name).scoped_to([:group_id]) }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment