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

Allow editing auth_source as a global user (#8954)

Oliver Günther109ade62da1b
diff --git a/app/contracts/users/base_contract.rb b/app/contracts/users/base_contract.rb
index 5ca26378ee4..35d2065c68b 100644
--- a/app/contracts/users/base_contract.rb
+++ b/app/contracts/users/base_contract.rb
@@ -43,7 +43,7 @@ module Users
attribute :language
attribute :auth_source_id,
- writeable: ->(*) { user.admin? }
+ writeable: ->(*) { user.allowed_to_globally?(:add_user) }
attribute :identity_url,
writeable: ->(*) { user.admin? }
diff --git a/app/models/permitted_params.rb b/app/models/permitted_params.rb
index 9af78cf8402..c571e474a54 100644
--- a/app/models/permitted_params.rb
+++ b/app/models/permitted_params.rb
@@ -181,8 +181,8 @@ class PermittedParams
permitted_params.merge(params[:settings].to_unsafe_hash.slice(*all_valid_keys))
end
- def user
- permitted_params = params.require(:user).permit(*self.class.permitted_attributes[:user])
+ def user(additional_params = [])
+ permitted_params = params.require(:user).permit(*self.class.permitted_attributes[:user] + additional_params)
permitted_params = permitted_params.merge(custom_field_values(:user))
permitted_params
@@ -204,21 +204,16 @@ class PermittedParams
def user_create_as_admin(external_authentication,
change_password_allowed,
additional_params = [])
- if current_user.admin?
- additional_params << :auth_source_id unless external_authentication
- additional_params << :force_password_change if change_password_allowed
- allowed_params = self.class.permitted_attributes[:user] + \
- additional_params + \
- %i[admin login]
+ additional_params << :auth_source_id unless external_authentication
- permitted_params = params.require(:user).permit(*allowed_params)
- permitted_params = permitted_params.merge(custom_field_values(:user))
-
- permitted_params
- else
- user
+ if current_user.admin?
+ additional_params << :force_password_change if change_password_allowed
+ additional_params << :admin
+ additional_params << :login
end
+
+ user additional_params
end
def type(args = {})
diff --git a/app/views/users/_form.html.erb b/app/views/users/_form.html.erb
index 22692354b1e..d271c86625a 100644
--- a/app/views/users/_form.html.erb
+++ b/app/views/users/_form.html.erb
@@ -44,12 +44,13 @@ See docs/COPYRIGHT.rdoc for more details.
<%= call_hook(:view_users_form, user: @user, form: f) %>
</section>
-<% if current_user.admin? %>
- <% if Setting.consent_required? %>
- <%= render partial: "users/consent" %>
- <% end %>
+<% if Setting.consent_required? %>
+ <%= render partial: "users/consent" %>
+<% end %>
- <%= render partial: 'users/form/authentication', locals: { f: f } %>
+<%= render partial: 'users/form/authentication/form', locals: { f: f } %>
+
+<% if current_user.admin? %>
<%= render partial: 'users/form/mail_notifications', locals: { f: f } %>
<%= render partial: 'users/form/preferences', locals: { f: f } %>
<% end %>
diff --git a/app/views/users/_simple_form.html.erb b/app/views/users/_simple_form.html.erb
index 4c7b5bec3b6..48445b18f63 100644
--- a/app/views/users/_simple_form.html.erb
+++ b/app/views/users/_simple_form.html.erb
@@ -37,7 +37,7 @@ See docs/COPYRIGHT.rdoc for more details.
<%= render partial: 'users/form/basic_attributes', locals: { f: f } %>
<% if current_user.admin? %>
- <%= render partial: 'users/form/auth_source', locals: { f: f } %>
+ <%= render partial: 'users/form/authentication/auth_source', locals: { f: f } %>
<%= render partial: 'users/form/admin_flag', locals: { f: f } %>
<% end %>
diff --git a/app/views/users/form/_authentication.html.erb b/app/views/users/form/_authentication.html.erb
deleted file mode 100644
index 28b11dc738a..00000000000
--- a/app/views/users/form/_authentication.html.erb
+++ /dev/null
@@ -1,82 +0,0 @@
-<section class="form--section">
- <h3 class="form--section-title"><%=t(:label_authentication)%></h3>
- <% if @user.uses_external_authentication? %>
- <div class="form--field">
- <%= styled_label_tag nil, I18n.t('user.authentication_provider') %>
- <div class="form--field-container">
- <%= @user.authentication_provider %>
- </div>
- </div>
- <div class="form--field-instructions">
- <%= I18n.t('user.authentication_settings_disabled_due_to_external_authentication') %>
- </div>
- <% else %>
- <%= render partial: 'users/form/auth_source', locals: { f: f } %>
- <% if !OpenProject::Configuration.disable_password_login? %>
- <%
- pw_style =
- if @user.change_password_allowed?
- ''
- else
- ' style="display: none;"'
- end
- %>
- <div id="password_fields"<%= pw_style.html_safe %>>
- <% assign_random_password_enabled = params[:user] &&
- params[:user][:assign_random_password] %>
- <div class="form--field">
- <%= styled_label_tag 'user_assign_random_password',
- I18n.t(:assign_random_password, scope: :user) %>
- <div class="form--field-container">
- <%= styled_check_box_tag("user[assign_random_password]",
- "1",
- assign_random_password_enabled) %>
- </div>
- </div>
-
- <% unless OpenProject::Configuration.disable_password_choice? %>
- <div class="form--field">
- <%= f.password_field :password,
- required: @user.new_record?,
- disabled: assign_random_password_enabled,
- container_class: '-middle' %>
- <div class="form--field-instructions">
- <%= password_complexity_requirements %>
- </div>
- </div>
- <div class="form--field">
- <%= f.password_field :password_confirmation,
- required: @user.new_record?,
- disabled: assign_random_password_enabled,
- container_class: '-middle' %>
- </div>
- <% end %>
-
- <% if @user.active? -%>
- <div class="form--field send-information -hidden">
- <%= styled_label_tag 'send_information', t(:label_send_information) %>
- <div class="form--field-container">
- <%= styled_check_box_tag("send_information",
- "1",
- true) %>
- </div>
- </div>
- <% end -%>
-
- <div class="form--field">
- <%= f.check_box :force_password_change,
- disabled: assign_random_password_enabled %>
- </div>
- </div>
- <% else %>
- <div id="no_password_info">
- <div class="form--field">
- <%= styled_label_tag nil, I18n.t(:warning) %>
- <div class="form--field-container">
- <%= I18n.t 'user.no_login' %>
- </div>
- </div>
- </div>
- <% end %>
- <% end %>
-</section>
\ No newline at end of file
diff --git a/app/views/users/form/_basic_attributes.html.erb b/app/views/users/form/_basic_attributes.html.erb
index ee79e244de9..51e9dfe266b 100644
--- a/app/views/users/form/_basic_attributes.html.erb
+++ b/app/views/users/form/_basic_attributes.html.erb
@@ -29,5 +29,5 @@
<%= f.select :language,
lang_options_for_select,
prompt: "--- #{t(:actionview_instancetag_blank_option)} ---",
- container_class: '-slim' %>
+ container_class: '-middle' %>
</div>
\ No newline at end of file
diff --git a/app/views/users/form/_auth_source.html.erb b/app/views/users/form/authentication/_auth_source.html.erb
similarity index 86%
rename from app/views/users/form/_auth_source.html.erb
rename to app/views/users/form/authentication/_auth_source.html.erb
index 8953c83c04e..e75d986cb21 100644
--- a/app/views/users/form/_auth_source.html.erb
+++ b/app/views/users/form/authentication/_auth_source.html.erb
@@ -1,7 +1,7 @@
<% unless @auth_sources.empty? || OpenProject::Configuration.disable_password_login? %>
<div class="form--field">
<% sources = ([[t(:label_internal), ""]] + @auth_sources.collect { |a| [a.name, a.id] }) %>
- <%= f.select :auth_source_id, sources, label: :'activerecord.attributes.user.auth_source' %>
+ <%= f.select :auth_source_id, sources, label: :'activerecord.attributes.user.auth_source', container_class: '-middle' %>
</div>
<div class="form--field" id="new_user_login" style="display: none;">
diff --git a/app/views/users/form/authentication/_external.html.erb b/app/views/users/form/authentication/_external.html.erb
new file mode 100644
index 00000000000..d901a77ba6e
--- /dev/null
+++ b/app/views/users/form/authentication/_external.html.erb
@@ -0,0 +1,9 @@
+<div class="form--field">
+ <%= styled_label_tag nil, I18n.t('user.authentication_provider') %>
+ <div class="form--field-container">
+ <%= @user.authentication_provider %>
+ </div>
+</div>
+<div class="form--field-instructions">
+ <%= I18n.t('user.authentication_settings_disabled_due_to_external_authentication') %>
+</div>
\ No newline at end of file
diff --git a/app/views/users/form/authentication/_form.html.erb b/app/views/users/form/authentication/_form.html.erb
new file mode 100644
index 00000000000..4b1586f83ba
--- /dev/null
+++ b/app/views/users/form/authentication/_form.html.erb
@@ -0,0 +1,12 @@
+<section class="form--section">
+ <h3 class="form--section-title"><%=t(:label_authentication)%></h3>
+ <% if @user.uses_external_authentication? %>
+ <%= render partial: 'users/form/authentication/external', locals: { f: f } %>
+ <% else %>
+ <%= render partial: 'users/form/authentication/auth_source', locals: { f: f } %>
+
+ <% if current_user.admin? %>
+ <%= render partial: 'users/form/authentication/internal', locals: { f: f } %>
+ <% end %>
+ <% end %>
+</section>
\ No newline at end of file
diff --git a/app/views/users/form/authentication/_internal.html.erb b/app/views/users/form/authentication/_internal.html.erb
new file mode 100644
index 00000000000..e65ba9f8cb9
--- /dev/null
+++ b/app/views/users/form/authentication/_internal.html.erb
@@ -0,0 +1,12 @@
+<% if OpenProject::Configuration.disable_password_login? %>
+ <div id="no_password_info">
+ <div class="form--field">
+ <%= styled_label_tag nil, I18n.t(:warning) %>
+ <div class="form--field-container">
+ <%= I18n.t 'user.no_login' %>
+ </div>
+ </div>
+ </div>
+<% else %>
+ <%= render partial: 'users/form/authentication/internal_password', locals: { f: f } %>
+<% end %>
\ No newline at end of file
diff --git a/app/views/users/form/authentication/_internal_password.html.erb b/app/views/users/form/authentication/_internal_password.html.erb
new file mode 100644
index 00000000000..e1eba37ab39
--- /dev/null
+++ b/app/views/users/form/authentication/_internal_password.html.erb
@@ -0,0 +1,55 @@
+<%
+ pw_style =
+ if @user.change_password_allowed?
+ ''
+ else
+ ' style="display: none;"'
+ end
+%>
+<div id="password_fields"<%= pw_style.html_safe %>>
+ <% assign_random_password_enabled = params[:user] &&
+ params[:user][:assign_random_password] %>
+ <div class="form--field">
+ <%= styled_label_tag 'user_assign_random_password',
+ I18n.t(:assign_random_password, scope: :user) %>
+ <div class="form--field-container">
+ <%= styled_check_box_tag("user[assign_random_password]",
+ "1",
+ assign_random_password_enabled) %>
+ </div>
+ </div>
+
+ <% unless OpenProject::Configuration.disable_password_choice? %>
+ <div class="form--field">
+ <%= f.password_field :password,
+ required: @user.new_record?,
+ disabled: assign_random_password_enabled,
+ container_class: '-middle' %>
+ <div class="form--field-instructions">
+ <%= password_complexity_requirements %>
+ </div>
+ </div>
+ <div class="form--field">
+ <%= f.password_field :password_confirmation,
+ required: @user.new_record?,
+ disabled: assign_random_password_enabled,
+ container_class: '-middle' %>
+ </div>
+ <% end %>
+
+ <% if @user.active? -%>
+ <div class="form--field send-information -hidden">
+ <%= styled_label_tag 'send_information', t(:label_send_information) %>
+ <div class="form--field-container">
+ <%= styled_check_box_tag("send_information",
+ "1",
+ true) %>
+ </div>
+ </div>
+ <% end -%>
+
+ <div class="form--field">
+ <%= f.check_box :force_password_change,
+ disabled: assign_random_password_enabled %>
+ </div>
+</div>
\ No newline at end of file
diff --git a/spec/contracts/users/create_contract_spec.rb b/spec/contracts/users/create_contract_spec.rb
index f3e86117efd..baa8464b85d 100644
--- a/spec/contracts/users/create_contract_spec.rb
+++ b/spec/contracts/users/create_contract_spec.rb
@@ -96,15 +96,16 @@ describe Users::CreateContract do
end
end
- describe 'cannot set the auth_source' do
+ describe 'can set the auth_source' do
let!(:auth_source) { FactoryBot.create :auth_source }
before do
+ user.password = user.password_confirmation = nil
user.auth_source = auth_source
end
- it 'is invalid' do
- expect_valid(false, auth_source_id: %i(error_readonly))
+ it 'is valid' do
+ expect_valid(true)
end
end
diff --git a/spec/features/users/edit_users_spec.rb b/spec/features/users/edit_users_spec.rb
index 1f80a181859..706aa7dd59b 100644
--- a/spec/features/users/edit_users_spec.rb
+++ b/spec/features/users/edit_users_spec.rb
@@ -95,7 +95,7 @@ describe 'edit users', type: :feature, js: true do
expect(page).to have_no_selector('.users-and-permissions-menu-item', text: 'Users & Permissions')
expect(page).to have_selector('.users-menu-item.selected', text: 'Users')
- expect(page).to have_no_selector 'select#user_auth_source_id'
+ expect(page).to have_selector 'select#user_auth_source_id'
expect(page).to have_no_selector 'input#user_password'
expect(page).to have_selector '#user_login'
@@ -104,6 +104,7 @@ describe 'edit users', type: :feature, js: true do
expect(page).to have_selector '#user_mail'
fill_in 'user[firstname]', with: 'NewName', fill_options: { clear: :backspace }
+ select auth_source.name, from: 'user[auth_source_id]'
click_on 'Save'
@@ -112,6 +113,7 @@ describe 'edit users', type: :feature, js: true do
user.reload
expect(user.firstname).to eq 'NewName'
+ expect(user.auth_source).to eq auth_source
end
it 'can reinvite the user' do
diff --git a/spec/models/permitted_params_spec.rb b/spec/models/permitted_params_spec.rb
index b3d5f7aea4c..f991ea0081c 100644
--- a/spec/models/permitted_params_spec.rb
+++ b/spec/models/permitted_params_spec.rb
@@ -506,7 +506,7 @@ describe PermittedParams, type: :model do
subject { PermittedParams.new(params, user).send(attribute, external_authentication, change_password_allowed).to_h }
- admin_permissions = ['admin',
+ all_permissions = ['admin',
'login',
'firstname',
'lastname',
@@ -521,19 +521,18 @@ describe PermittedParams, type: :model do
let(:attribute) { :user_create_as_admin }
context 'non-admin' do
- let(:hash) { Hash[admin_permissions.zip(admin_permissions)] }
+ let(:hash) { Hash[all_permissions.zip(all_permissions)] }
it 'permits default permissions' do
- expect(subject.keys).to match_array(
- %w[custom_fields firstname lastname language mail mail_notification]
- )
+ expect(subject.keys)
+ .to match_array(%w[custom_fields firstname lastname language mail mail_notification auth_source_id])
end
end
context 'admin' do
let(:user) { admin }
- admin_permissions.each do |field|
+ all_permissions.each do |field|
context field do
let(:hash) { { field => 'test' } }
@@ -600,7 +599,7 @@ describe PermittedParams, type: :model do
end
end
- (admin_permissions - user_permissions).each do |field|
+ (all_permissions - user_permissions).each do |field|
context "#{field} (admin-only)" do
let(:hash) { { field => 'test' } }
diff --git a/spec/requests/api/v3/membership_resources_spec.rb b/spec/requests/api/v3/membership_resources_spec.rb
index a4a5e9cad4c..68032773fbe 100644
--- a/spec/requests/api/v3/membership_resources_spec.rb
+++ b/spec/requests/api/v3/membership_resources_spec.rb
@@ -109,11 +109,9 @@ describe 'API v3 memberships resource', type: :request, content_type: :json do
context 'as an admin' do
let(:current_user) { admin }
- it 'responds 200 OK' do
+ it 'returns a collection of memberships containing only the visible ones', :aggregate_failures do
expect(subject.status).to eq(200)
- end
- it 'returns a collection of memberships containing only the visible ones' do
expect(subject.body)
.to be_json_eql('Collection'.to_json)
.at_path('_type')
diff --git a/spec/requests/api/v3/user/create_user_resource_spec.rb b/spec/requests/api/v3/user/create_user_resource_spec.rb
index cbd59496300..1e80be778f2 100644
--- a/spec/requests/api/v3/user/create_user_resource_spec.rb
+++ b/spec/requests/api/v3/user/create_user_resource_spec.rb
@@ -232,7 +232,13 @@ describe ::API::V3::Users::UsersAPI, type: :request do
}
end
- it_behaves_like 'property is not writable', 'authSource'
+ it 'creates the user with the given auth source id' do
+ send_request
+
+ user = User.find_by(login: parameters[:login])
+
+ expect(user.auth_source).to eq auth_source
+ end
end
end