Commit 4b662d49 authored by Toon Claes's avatar Toon Claes

Merge branch 'if-233139-scim_finder_to_support_email_username' into 'master'

ScimFinder to support email fragment as username

See merge request gitlab-org/gitlab!38807
parents cc9e0d9a a745349f
......@@ -73,10 +73,22 @@ class ScimFinder
end
def by_username(username)
user = User.find_by_username(username) || User.find_by_any_email(username)
user = User.find_by_username(username)
if !user && email?(username)
user ||= User.find_by_any_email(username) || User.find_by_username(email_local_part(username))
end
return group.scim_identities.for_user(user) if scim_identities_enabled?
saml_provider.identities.for_user(user)
end
def email?(email)
::ValidateEmail.valid?(email)
end
def email_local_part(email)
::Mail::Address.new(email).local
end
end
---
title: SCIM get users API to search for email fragment as username
merge_request: 38807
author:
type: changed
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe ScimFinder do
let(:group) { create(:group) }
let_it_be(:group) { create(:group) }
let(:unused_params) { double }
subject(:finder) { described_class.new(group) }
......@@ -42,7 +42,7 @@ RSpec.describe ScimFinder do
end
context 'with SCIM enabled' do
let!(:saml_provider) { create(:saml_provider, group: group) }
let_it_be(:saml_provider) { create(:saml_provider, group: group) }
context 'with an eq filter' do
shared_examples 'valid lookups' do
......@@ -57,10 +57,15 @@ RSpec.describe ScimFinder do
end
context 'allows lookup by userName' do
it 'finds user when userName is an email address' do
it 'finds user by an email address' do
expect(finder.search(filter: "userName eq #{id.user.email}").first).to eq id
end
it 'finds user by using local part of email address as username' do
email = "#{id.user.username}@example.com"
expect(finder.search(filter: "userName eq #{email}").first).to eq id
end
it 'finds user by username' do
expect(finder.search(filter: "userName eq \"#{id.user.username}\"").first).to eq id
end
......@@ -75,7 +80,7 @@ RSpec.describe ScimFinder do
before do
stub_feature_flags(scim_identities: false)
end
let(:id) { create(:group_saml_identity, saml_provider: saml_provider) }
let_it_be(:id) { create(:group_saml_identity, saml_provider: saml_provider) }
it_behaves_like 'valid lookups'
end
......@@ -84,7 +89,8 @@ RSpec.describe ScimFinder do
before do
stub_feature_flags(scim_identities: true)
end
let(:id) { create(:scim_identity, group: group) }
let_it_be(:user) { create(:user, username: 'foo', email: 'bar@example.com') }
let_it_be(:id) { create(:scim_identity, group: group, user: user) }
it_behaves_like 'valid lookups'
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