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 ...@@ -73,10 +73,22 @@ class ScimFinder
end end
def by_username(username) 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? return group.scim_identities.for_user(user) if scim_identities_enabled?
saml_provider.identities.for_user(user) saml_provider.identities.for_user(user)
end end
def email?(email)
::ValidateEmail.valid?(email)
end
def email_local_part(email)
::Mail::Address.new(email).local
end
end end
---
title: SCIM get users API to search for email fragment as username
merge_request: 38807
author:
type: changed
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ScimFinder do RSpec.describe ScimFinder do
let(:group) { create(:group) } let_it_be(:group) { create(:group) }
let(:unused_params) { double } let(:unused_params) { double }
subject(:finder) { described_class.new(group) } subject(:finder) { described_class.new(group) }
...@@ -42,7 +42,7 @@ RSpec.describe ScimFinder do ...@@ -42,7 +42,7 @@ RSpec.describe ScimFinder do
end end
context 'with SCIM enabled' do 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 context 'with an eq filter' do
shared_examples 'valid lookups' do shared_examples 'valid lookups' do
...@@ -57,10 +57,15 @@ RSpec.describe ScimFinder do ...@@ -57,10 +57,15 @@ RSpec.describe ScimFinder do
end end
context 'allows lookup by userName' do 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 expect(finder.search(filter: "userName eq #{id.user.email}").first).to eq id
end 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 it 'finds user by username' do
expect(finder.search(filter: "userName eq \"#{id.user.username}\"").first).to eq id expect(finder.search(filter: "userName eq \"#{id.user.username}\"").first).to eq id
end end
...@@ -75,7 +80,7 @@ RSpec.describe ScimFinder do ...@@ -75,7 +80,7 @@ RSpec.describe ScimFinder do
before do before do
stub_feature_flags(scim_identities: false) stub_feature_flags(scim_identities: false)
end 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' it_behaves_like 'valid lookups'
end end
...@@ -84,7 +89,8 @@ RSpec.describe ScimFinder do ...@@ -84,7 +89,8 @@ RSpec.describe ScimFinder do
before do before do
stub_feature_flags(scim_identities: true) stub_feature_flags(scim_identities: true)
end 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' it_behaves_like 'valid lookups'
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