Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Expose User#authentication_provider as AR class

Jan Sandbrink3708fa4513a3
diff --git a/app/models/user.rb b/app/models/user.rb
index 4b3d3317572..e5f2d462219 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -304,16 +304,16 @@ class User < Principal
end
end
- # Return user's authentication provider for display
def authentication_provider
- return if identity_url.blank?
+ return nil if identity_url.blank?
- identity_url.split(":", 2).first
+ slug = identity_url.split(":", 2).first
+ AuthProvider.find_by(slug:)
end
# Return user's authentication provider for display
def human_authentication_provider
- authentication_provider&.titleize
+ authentication_provider&.display_name
end
##
diff --git a/app/services/users/register_user_service.rb b/app/services/users/register_user_service.rb
index 3ab6efc7baa..b498044d593 100644
--- a/app/services/users/register_user_service.rb
+++ b/app/services/users/register_user_service.rb
@@ -137,7 +137,7 @@ module Users
end
def provider_name(user)
- user.authentication_provider&.downcase
+ user.authentication_provider&.slug&.downcase
end
def register_by_email_activation
diff --git a/spec/features/auth/lost_password_spec.rb b/spec/features/auth/lost_password_spec.rb
index f7652f67207..0928f5ba77e 100644
--- a/spec/features/auth/lost_password_spec.rb
+++ b/spec/features/auth/lost_password_spec.rb
@@ -90,6 +90,7 @@ RSpec.describe "Lost password" do
end
context "when user has identity_url" do
+ let!(:provider) { create(:saml_provider, slug: "saml", display_name: "The SAML provider") }
let!(:user) { create(:user, identity_url: "saml:foobar") }
it "sends an email with external auth info" do
@@ -105,7 +106,7 @@ RSpec.describe "Lost password" do
expect(ActionMailer::Base.deliveries.size).to be 1
mail = ActionMailer::Base.deliveries.first
expect(mail.subject).to eq I18n.t("mail_password_change_not_possible.title")
- expect(mail.body.parts.first.body.to_s).to include "Saml"
+ expect(mail.body.parts.first.body.to_s).to include "(The SAML provider)"
end
end
end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index c0ac9094fbc..11e084ab73a 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -365,17 +365,44 @@ RSpec.describe User do
end
describe "#authentication_provider" do
+ let!(:provider) { create(:oidc_provider, slug: "test_provider") }
+
+ before do
+ user.identity_url = "test_provider:veryuniqueid"
+ user.save!
+ end
+
+ it "returns the provider" do
+ expect(user.authentication_provider).to eql(provider)
+ end
+
+ context "when no matching provider exists" do
+ let!(:provider) { nil }
+
+ it "returns nil" do
+ expect(user.authentication_provider).to be_nil
+ end
+ end
+ end
+
+ describe "#human_authentication_provider" do
+ let!(:provider) { create(:oidc_provider, slug: "test_provider", display_name: "Karl") }
+
before do
user.identity_url = "test_provider:veryuniqueid"
user.save!
end
- it "returns the internal provider slug" do
- expect(user.authentication_provider).to eql("test_provider")
+ it "returns a human readable name" do
+ expect(user.human_authentication_provider).to eql("Karl")
end
- it "creates a human readable name" do
- expect(user.human_authentication_provider).to eql("Test Provider")
+ context "when no matching provider exists" do
+ let!(:provider) { nil }
+
+ it "returns nil" do
+ expect(user.authentication_provider).to be_nil
+ end
end
end
diff --git a/spec/views/users/edit.html.erb_spec.rb b/spec/views/users/edit.html.erb_spec.rb
index 78db605d151..ef07941e9a4 100644
--- a/spec/views/users/edit.html.erb_spec.rb
+++ b/spec/views/users/edit.html.erb_spec.rb
@@ -43,6 +43,7 @@ RSpec.describe "users/edit" do
build(:user, id: 1, # id is required to create route to edit
identity_url: "test_provider:veryuniqueid")
end
+ let!(:provider) { create(:oidc_provider, slug: "test_provider", display_name: "The Test Provider") }
before do
assign(:user, user)
@@ -56,7 +57,7 @@ RSpec.describe "users/edit" do
it "shows the authentication provider" do
render
- expect(rendered).to include("Test Provider")
+ expect(rendered).to include("The Test Provider")
end
it "does not show a no-login warning when password login is disabled" do