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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Allow LDAP filters and groups to individually sync users (#9031)
Oliver Günther4980b4fb4a14
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rbindex 1d8c1321af2..33062dcdd66 100644--- a/app/models/ldap_auth_source.rb+++ b/app/models/ldap_auth_source.rb@@ -118,6 +118,10 @@ class LdapAuthSource < AuthSourceparsed_filter_string || object_filterend+ def parsed_filter_string+ Net::LDAP::Filter.from_rfc2254(filter_string) if filter_string.present?+ end+privatedef strip_ldap_attributes@@ -189,10 +193,6 @@ class LdapAuthSource < AuthSourceself.port = 389 if port.to_i == 0end- def parsed_filter_string- Net::LDAP::Filter.from_rfc2254(filter_string) if filter_string.present?- end-def validate_filter_stringparsed_filter_stringrescue Net::LDAP::FilterSyntaxInvalidErrordiff --git a/app/views/ldap_auth_sources/_form.html.erb b/app/views/ldap_auth_sources/_form.html.erbindex 4253a09c182..211f5ea5cb7 100644--- a/app/views/ldap_auth_sources/_form.html.erb+++ b/app/views/ldap_auth_sources/_form.html.erb@@ -137,9 +137,6 @@ See docs/COPYRIGHT.rdoc for more details.<%= f.check_box 'onthefly_register', label: AuthSource.human_attribute_name(:onthefly) %><div class="form--field-instructions"><p><%= t('ldap_auth_sources.attribute_texts.onthefly_register') %></p>- <% if EnterpriseToken.allows_to?(:ldap_groups) %>- <p><%= t('ldap_auth_sources.attribute_texts.onthefly_group_sync') %></p>- <% end %></div></div></fieldset>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 765c2909ebe..f6c318d04be 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -136,9 +136,6 @@ en:If you check this box, OpenProject will automatically create new users from their LDAP entrieswhen they first authenticate with OpenProject.Leave this unchecked to only allow existing accounts in OpenProject to authenticate through LDAP!- onthefly_group_sync: |- If you enable group synchronization for this LDAP connection, users will also be automatically created- in OpenProject for synchronized groups. Without it, only existing accounts in OpenProject will be added to groups.connection_encryption: 'Connection encryption'system_account: 'System account'diff --git a/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/row_cell.rb b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/row_cell.rbindex ebe28514d13..3c9654d2f90 100644--- a/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/row_cell.rb+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/row_cell.rb@@ -13,13 +13,21 @@ module LdapGroupsenddef auth_source- link_to synchronized_filter.auth_source.name, edit_auth_source_path(synchronized_filter.auth_source)+ link_to synchronized_filter.auth_source.name, edit_ldap_auth_source_path(synchronized_filter.auth_source)+ end++ def base_dn+ synchronized_filter.base_dnenddef groupssynchronized_filter.groups.countend+ def sync_users+ checked_image synchronized_filter.sync_users+ end+def button_links[edit_link,diff --git a/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/table_cell.rb b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/table_cell.rbindex 6b459d9cf94..a014d20babf 100644--- a/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/table_cell.rb+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/table_cell.rb@@ -1,7 +1,7 @@module LdapGroupsmodule SynchronizedFiltersclass TableCell < ::TableCell- columns :name, :auth_source, :groups+ columns :name, :auth_source, :groups, :sync_usersdef initial_sort%i[id asc]@@ -27,7 +27,8 @@ module LdapGroups[['name', { caption: ::LdapGroups::SynchronizedFilter.human_attribute_name('name') }],['auth_source', { caption: ::LdapGroups::SynchronizedFilter.human_attribute_name('auth_source') }],- ['groups', { caption: I18n.t('ldap_groups.synchronized_filters.plural') }]+ ['groups', { caption: I18n.t('ldap_groups.synchronized_groups.plural') }],+ ['sync_users', { caption: ::LdapGroups::SynchronizedFilter.human_attribute_name('sync_users') }]]endenddiff --git a/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/row_cell.rb b/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/row_cell.rbindex 703b90bfe37..6880c539625 100644--- a/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/row_cell.rb+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/row_cell.rb@@ -13,13 +13,17 @@ module LdapGroupsenddef auth_source- link_to synchronized_group.auth_source.name, edit_auth_source_path(synchronized_group.auth_source)+ link_to synchronized_group.auth_source.name, edit_ldap_auth_source_path(synchronized_group.auth_source)enddef grouplink_to synchronized_group.group.name, edit_group_path(synchronized_group.group)end+ def sync_users+ checked_image synchronized_group.sync_users+ end+def userssynchronized_group.users.sizeenddiff --git a/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/table_cell.rb b/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/table_cell.rbindex c4133bd7a4b..75cfe3847d9 100644--- a/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/table_cell.rb+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_groups/table_cell.rb@@ -1,7 +1,7 @@module LdapGroupsmodule SynchronizedGroupsclass TableCell < ::TableCell- columns :dn, :auth_source, :group, :users+ columns :dn, :auth_source, :group, :users, :sync_usersdef initial_sort%i[id asc]@@ -36,7 +36,8 @@ module LdapGroups['dn', { caption: ::LdapGroups::SynchronizedGroup.human_attribute_name('dn') }],['auth_source', { caption: ::LdapGroups::SynchronizedGroup.human_attribute_name('auth_source') }],['group', { caption: I18n.t(:label_group) }],- ['users', { caption: I18n.t(:label_user_plural) }]+ ['users', { caption: I18n.t(:label_user_plural) }],+ ['sync_users', { caption: ::LdapGroups::SynchronizedFilter.human_attribute_name('sync_users') }]]endenddiff --git a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rbindex d4d8ee1d1d4..f3750bd70e0 100644--- a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb+++ b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb@@ -52,8 +52,20 @@ module LdapGroupsenddef synchronize- ::OpenProject::LdapGroups::SynchronizeFilter.new(@filter)- flash[:notice] = 'Done.'+ call = ::LdapGroups::SynchronizeFilterService+ .new(@filter)+ .call++ call.on_success do+ count = call.result+ symbol = count > 0 ? :notice : :info+ flash[symbol] = I18n.t('ldap_groups.synchronized_filters.label_n_groups_found', count: count)+ end++ call.on_failure do+ flash[:error] = call.message+ end+redirect_to ldap_groups_synchronized_groups_pathend@@ -75,7 +87,7 @@ module LdapGroupsdef permitted_paramsparams.require(:synchronized_filter)- .permit(:filter_string, :name, :auth_source_id, :group_name_attribute)+ .permit(:filter_string, :name, :auth_source_id, :group_name_attribute, :sync_users, :base_dn)enddef default_breadcrumbdiff --git a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rb b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rbindex 620a9e1ccaf..d68ea70eaa9 100644--- a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rb+++ b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rb@@ -66,7 +66,7 @@ module LdapGroupsdef permitted_paramsparams.require(:synchronized_group)- .permit(:dn, :group_id, :auth_source_id)+ .permit(:dn, :group_id, :auth_source_id, :sync_users)enddef default_breadcrumbdiff --git a/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb b/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rbindex 8f74790db01..c91e3db6197 100644--- a/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb+++ b/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb@@ -13,11 +13,16 @@ module LdapGroupsvalidates_presence_of :filter_stringvalidates_presence_of :auth_sourcevalidate :validate_filter_syntax+ validate :validate_base_dndef parsed_filter_stringNet::LDAP::Filter.from_rfc2254 filter_stringend+ def used_base_dn+ base_dn.presence || auth_source.base_dn+ end+privatedef validate_filter_syntax@@ -25,5 +30,13 @@ module LdapGroupsrescue Net::LDAP::FilterSyntaxInvalidErrorerrors.add :filter_string, :invalidend++ def validate_base_dn+ return unless base_dn.present? && auth_source.present?++ unless base_dn.end_with?(auth_source.base_dn)+ errors.add :base_dn, :must_contain_base_dn+ end+ endendenddiff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb b/modules/ldap_groups/app/services/ldap_groups/synchronization_service.rbsimilarity index 75%rename from modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rbrename to modules/ldap_groups/app/services/ldap_groups/synchronization_service.rbindex 74a8d9596f6..68cbb6d17fd 100644--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb+++ b/modules/ldap_groups/app/services/ldap_groups/synchronization_service.rb@@ -1,5 +1,5 @@-module OpenProject::LdapGroups- class Synchronization+module LdapGroups+ class SynchronizationServiceattr_reader :ldap, :synced_groupsdef initialize(ldap)@@ -7,32 +7,41 @@ module OpenProject::LdapGroups# Get current synced groups in OP@synced_groups = ::LdapGroups::SynchronizedGroup.where(auth_source: ldap)+ end- synchronize!+ def call+ count = synchronize!+ ServiceResult.new(result: count, success: true)rescue StandardError => eerror = "[LDAP groups] Failed to perform LDAP group synchronization: #{e.class}: #{e.message}"Rails.logger.error(error)+ ServiceResult.new(message: error, success: false)enddef synchronize!ldap_con = ldap.instance_eval { initialize_ldap_con(account, account_password) }+ count = 0::LdapGroups::Membership.transaction do- @synced_groups.find_each do |group|- user_data = get_members(ldap_con, group)+ @synced_groups.find_each do |sync_group|+ user_data = get_members(ldap_con, sync_group)# Create users that are not existing- users = map_to_users(user_data)+ users = map_to_users(sync_group, user_data)++ update_memberships!(sync_group, users)- update_memberships!(group, users)+ count += users.countendend++ countend### Map LDAP entries to user accounts, creating them if necessary- def map_to_users(entries)- create_missing!(entries) if ldap.onthefly_register?+ def map_to_users(sync_group, entries)+ create_missing!(entries) if sync_group.sync_usersUser.where(login: entries.keys)end@@ -75,12 +84,9 @@ module OpenProject::LdapGroups# Get user login attribute and base dn which are privatebase_dn = ldap.base_dn- # memberOf filter to identifiy member entries of the group- memberof_filter = Net::LDAP::Filter.eq('memberOf', group.dn)-users = {}ldap_con.search(base: base_dn,- filter: ldap.default_filter & memberof_filter,+ filter: memberof_filter(group),attributes: ldap.search_attributes) do |entry|data = ldap.get_user_attributes_from_ldap_entry(entry)users[data[:login]] = data.except(:dn)@@ -102,6 +108,20 @@ module OpenProject::LdapGroupssync.add_members! new_member_idsend+ ##+ # Get the memberof filter to use for querying members+ def memberof_filter(group)+ # memberOf filter to identify member entries of the group+ filter = Net::LDAP::Filter.eq('memberOf', group.dn)++ # Add the LDAP auth source own filter if present+ if ldap.filter_string.present?+ filter = filter & ldap.parsed_filter_string+ end++ filter+ end+### Remove a set of membershipsdef remove_memberships!(memberships, sync)diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb b/modules/ldap_groups/app/services/ldap_groups/synchronize_filter_service.rbsimilarity index 65%rename from modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rbrename to modules/ldap_groups/app/services/ldap_groups/synchronize_filter_service.rbindex db47797f036..5432e360acb 100644--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb+++ b/modules/ldap_groups/app/services/ldap_groups/synchronize_filter_service.rb@@ -1,27 +1,33 @@-module OpenProject::LdapGroups- class SynchronizeFilter- attr_reader :filter, :raise_errors+module LdapGroups+ class SynchronizeFilterService+ attr_reader :filter- def initialize(filter, raise_errors: false)+ def initialize(filter)@filter = filter- @raise_errors = raise_errors- synchronize!end- private+ def call+ count = synchronize!+ ServiceResult.new(success: true, result: count)+ rescue StandardError => e+ error = "[LDAP groups] Failed to extract LDAP groups from filter #{filter.name}: #{e.class}: #{e.message}"+ Rails.logger.error(error)+ ServiceResult.new(success: false, message: error)+ end### Perform the synchronization for the given filterdef synchronize!# Replace the filter groups, this will remove any groups# no longer found in ldap- filter.groups.replace upstream_groups- rescue StandardError => e- error = "[LDAP groups] Failed to extract LDAP groups from filter #{filter.name}: #{e.class}: #{e.message}"- Rails.logger.error(error)- raise(e) if raise_errors+ groups = upstream_groups+ filter.groups.replace groups++ groups.countend+ private+### Get the current members from the ldap groupdef upstream_groups@@ -42,21 +48,24 @@ module OpenProject::LdapGroups# and yield them one by onedef each_groupldap = filter.auth_source- base_dn = ldap.base_dn- group_name = filter.group_name_attribute-ldap.with_connection do |ldap_con|- ldap_con- .search(- base: base_dn,- filter: filter.parsed_filter_string,- attributes: ['dn', group_name]- ).each do |entry|- yield entry.dn, LdapAuthSource.get_attr(entry, group_name)+ search(filter, ldap_con) do |entry|+ yield entry.dn, LdapAuthSource.get_attr(entry, filter.group_name_attribute)endendend+ ##+ # Perform the LDAP search for the groups+ def search(filter, ldap_con, &block)+ ldap_con.search(+ base: filter.used_base_dn,+ filter: filter.parsed_filter_string,+ attributes: ['dn', filter.group_name_attribute],+ &block+ )+ end+### Create or update the synchronized group itemdef create_or_update_sync_group(dn)@@ -65,6 +74,9 @@ module OpenProject::LdapGroups# they are simply being re-assigned to the latest onesync.filter_id = filter.idsync.auth_source_id = filter.auth_source_id++ # Tell the group to synchronize users if the filter has requested it to+ sync.sync_users = filter.sync_usersendenddiff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/_form.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/_form.html.erbindex dc21711690a..b8c905f56c6 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/_form.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/_form.html.erb@@ -18,6 +18,14 @@<%= t('ldap_groups.synchronized_filters.form.group_name_attribute_text') %></div></div>++ <div class="form--field -required">+ <%= f.check_box :sync_users,+ container_class: '-middle' %>+ <div class="form--field-instructions">+ <%= t('ldap_groups.synchronized_groups.form.sync_users_text') %>+ </div>+ </div></fieldset><fieldset class="form--fieldset">@@ -33,6 +41,15 @@<%= t('ldap_groups.synchronized_groups.form.auth_source_text') %></div></div>++ <div class="form--field">+ <%= f.text_field :base_dn,+ required: false,+ container_class: '-middle' %>+ <div class="form--field-instructions">+ <%= t('ldap_groups.synchronized_filters.form.base_dn_text') %>+ </div>+ </div></fieldset><fieldset class="form--fieldset"><legend class="form--fieldset-legend"><%= @filter.class.human_attribute_name :filter_string %></legend>diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/edit.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/edit.html.erbindex 9f51e6d04fe..566e8af702c 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/edit.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/edit.html.erb@@ -1,8 +1,6 @@<% local_assigns[:additional_breadcrumb] = h(@filter.name) %><% html_title(t(:label_administration), t(:label_edit_x, x: @filter.name)) -%>-<%= error_messages_for @filter %>-<%= toolbar title: t(:label_edit_x, x: @filter.name) %><%= labelled_tabular_form_for @filter,diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/new.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/new.html.erbindex a9bdc228123..4111fbb90e1 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/new.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/new.html.erb@@ -1,8 +1,6 @@<% local_assigns[:additional_breadcrumb] = t('ldap_groups.synchronized_filters.add_new') %><% html_title(t(:label_administration), t('ldap_groups.synchronized_filters.add_new')) -%>-<%= error_messages_for @filter %>-<%= toolbar title: t('ldap_groups.synchronized_filters.add_new') %><%= labelled_tabular_form_for @filter,diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/show.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/show.html.erbindex 0a3a27aebcc..f0c5a0a2c70 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/show.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/show.html.erb@@ -33,6 +33,22 @@<span><%= @filter.group_name_attribute %></span></div></div>+ <div class="attributes-key-value--key"><%= @filter.class.human_attribute_name(:base_dn) %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <%= @filter.used_base_dn %>+ </div>+ </div>+ <div class="attributes-key-value--key"><%= @filter.class.human_attribute_name(:sync_users) %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <% if @filter.sync_users %>+ <%= checked_image @filter.sync_users %>+ <% else %>+ <%= t(:general_text_no) %>+ <% end %>+ </div>+ </div><div class="attributes-key-value--key"><%= @filter.class.human_attribute_name(:filter_string) %></div><div class="attributes-key-value--value-container"><div class="attributes-key-value--value">diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/new.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/new.html.erbindex 7054934f4dd..1ccba454302 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/new.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/new.html.erb@@ -35,6 +35,14 @@</div></div>+ <div class="form--field -required">+ <%= f.check_box :sync_users,+ container_class: '-middle' %>+ <div class="form--field-instructions">+ <%= t('ldap_groups.synchronized_groups.form.sync_users_text') %>+ </div>+ </div>+<div class="form--field -required"><%= f.select :group_id,Group.pluck(:lastname, :id),diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/show.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/show.html.erbindex cd732bb7a74..a5c7251488e 100644--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/show.html.erb+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/show.html.erb@@ -2,32 +2,42 @@<%= error_messages_for @group %>-<%= breadcrumb_toolbar(link_to(t('ldap_groups.synchronized_groups.plural'), { action: :index}), h(@group.dn)) %>+<%= breadcrumb_toolbar(link_to(t('ldap_groups.synchronized_groups.plural'), { action: :index }), h(@group.dn)) %><section class="admin--edit-section">- <div class="attributes-key-value">- <div class="attributes-key-value--key"><%= @group.class.human_attribute_name('dn') %></div>- <div class="attributes-key-value--value-container">- <div class="attributes-key-value--value">- <span><%= @group.dn %></span>- </div>+ <div class="attributes-key-value">+ <div class="attributes-key-value--key"><%= @group.class.human_attribute_name('dn') %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <span><%= @group.dn %></span></div>- <div class="attributes-key-value--key"><%= @group.class.human_attribute_name('auth_source') %></div>- <div class="attributes-key-value--value-container">- <div class="attributes-key-value--value">- <%= link_to @group.auth_source.name, edit_auth_source_path(@group.auth_source) %>- </div>+ </div>+ <div class="attributes-key-value--key"><%= @group.class.human_attribute_name(:sync_users) %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <% if @group.sync_users %>+ <%= checked_image @group.sync_users %>+ <% else %>+ <%= t(:general_text_no) %>+ <% end %>+ </div>+ </div>+ <div class="attributes-key-value--key"><%= @group.class.human_attribute_name('auth_source') %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <%= link_to @group.auth_source.name, edit_ldap_auth_source_path(@group.auth_source) %></div>- <div class="attributes-key-value--key"><%= t(:label_group) %></div>- <div class="attributes-key-value--value-container">- <div class="attributes-key-value--value">- <%= link_to @group.group, edit_group_path(@group.group) %>- </div>+ </div>+ <div class="attributes-key-value--key"><%= t(:label_group) %></div>+ <div class="attributes-key-value--value-container">+ <div class="attributes-key-value--value">+ <%= link_to @group.group, edit_group_path(@group.group) %></div></div>+ </div></section><section class="admin--edit-section">- <h2><%= t :label_user_plural %></h2>+ <h2><%= t :label_user_plural %></h2><%= cell ::LdapGroups::Memberships::TableCell, @group.users %></section>diff --git a/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rbindex 60de3a12830..edb7b4d4021 100644--- a/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb+++ b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb@@ -48,10 +48,15 @@ module LdapGroupsRails.logger.info { "[LDAP groups] Retrieving groups from filters for ldap auth source #{ldap.name}" }LdapGroups::SynchronizedFilter.where(auth_source_id: ldap.id)- .find_each { |filter| OpenProject::LdapGroups::SynchronizeFilter.new(filter) }+ .find_each do |filter|++ LdapGroups::SynchronizeFilterService+ .new(filter)+ .call+ endRails.logger.info { "[LDAP groups] Start group synchronization for ldap auth source #{ldap.name}" }- OpenProject::LdapGroups::Synchronization.new(ldap)+ LdapGroups::SynchronizationService.new(ldap).callendrescue StandardError => emsg = "[LDAP groups] Failed to run LDAP group synchronization. #{e.class.name}: #{e.message}"diff --git a/modules/ldap_groups/config/locales/en.yml b/modules/ldap_groups/config/locales/en.ymlindex 8f7b37d717f..261c76671e3 100644--- a/modules/ldap_groups/config/locales/en.yml+++ b/modules/ldap_groups/config/locales/en.yml@@ -4,13 +4,20 @@ en:ldap_groups/synchronized_group:dn: 'DN'auth_source: 'LDAP connection'+ sync_users: 'Sync users'ldap_groups/synchronized_filter:filter_string: 'LDAP filter'auth_source: 'LDAP connection'group_name_attribute: "Group name attribute"+ sync_users: 'Sync users'+ base_dn: "Search base DN"models:ldap_groups/synchronized_group: 'Synchronized LDAP group'ldap_groups/synchronized_filter: 'LDAP Group synchronization filter'+ errors:+ models:+ ldap_groups/synchronized_filter:+ must_contain_base_dn: "Filter base DN must be contained within the LDAP connection's base DN"ldap_groups:label_menu_item: 'LDAP group synchronization'label_group_key: 'LDAP group filter key'@@ -23,6 +30,10 @@ en:add_new: 'Add synchronized LDAP filter'singular: 'LDAP Group synchronization filter'plural: 'LDAP Group synchronization filters'+ label_n_groups_found:+ one: "1 group found by the filter"+ other: "%{count} groups found by the filter"+ zero: "No groups were found by the filter"destroy:title: 'Remove synchronized filter %{name}'confirmation: "If you continue, the synchronized filter %{name} and all groups %{groups_count} created through it will be removed."@@ -31,6 +42,10 @@ en:form:group_name_attribute_text: 'Enter the attribute of the LDAP group used for setting the OpenProject group name.'filter_string_text: 'Enter the RFC4515 LDAP filter that returns groups in your LDAP to synchronize with OpenProject.'+ base_dn_text: >+ Enter the search base DN to use for this filter. It needs to be below the base DN of the selected LDAP connection.++ Leave this option empty to reuse the base DN of the connectionsynchronized_groups:add_new: 'Add synchronized LDAP group'@@ -52,6 +67,9 @@ en:singular: 'Synchronized LDAP group'form:auth_source_text: 'Select which LDAP connection should be used.'+ sync_users_text: >+ If you enable this option, found users will also be automatically created in OpenProject.+ Without it, only existing accounts in OpenProject will be added to groups.dn_text: 'Enter the full DN of the group in LDAP'group_text: 'Select an existing OpenProject group that members of the LDAP group shall be synchronized with'diff --git a/modules/ldap_groups/db/migrate/20210219092709_move_base_dn_into_filters.rb b/modules/ldap_groups/db/migrate/20210219092709_move_base_dn_into_filters.rbnew file mode 100644index 00000000000..fa1c1ba66de--- /dev/null+++ b/modules/ldap_groups/db/migrate/20210219092709_move_base_dn_into_filters.rb@@ -0,0 +1,37 @@+class MoveBaseDnIntoFilters < ActiveRecord::Migration[6.1]+ def change+ add_column :ldap_groups_synchronized_filters, :base_dn, :text, null: true++ # Add sync_users option to filters+ add_column :ldap_groups_synchronized_filters,+ :sync_users,+ :boolean,+ null: false,+ default: false++ # Add sync_users option to groups+ add_column :ldap_groups_synchronized_groups,+ :sync_users,+ :boolean,+ null: false,+ default: false+++ LdapGroups::SynchronizedFilter.reset_column_information+ LdapGroups::SynchronizedGroup.reset_column_information++ # Take over the connection's onthefly setting+ # for whether to sync users for filters and groups+ LdapAuthSource+ .pluck(:id, :onthefly_register)+ .each do |id, onthefly|+ LdapGroups::SynchronizedFilter+ .where(auth_source_id: id)+ .update_all(sync_users: onthefly)++ LdapGroups::SynchronizedGroup+ .where(auth_source_id: id)+ .update_all(sync_users: onthefly)+ end+ end+enddiff --git a/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb b/modules/ldap_groups/spec/factories/synchronized_filter_factory.rbindex 495775856aa..ac0cdcaeff7 100644--- a/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb+++ b/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb@@ -3,6 +3,7 @@ FactoryBot.define doname { 'foo and bar' }filter_string { '(|(cn=foo)(cn=bar))' }group_name_attribute { 'cn' }+ base_dn { 'dc=example,dc=com' }auth_source factory: :ldap_auth_sourceendenddiff --git a/modules/ldap_groups/spec/features/administration_spec.rb b/modules/ldap_groups/spec/features/administration_spec.rbindex c986331de5c..ea745cf5981 100644--- a/modules/ldap_groups/spec/features/administration_spec.rb+++ b/modules/ldap_groups/spec/features/administration_spec.rb@@ -28,6 +28,7 @@ describe 'LDAP group sync administration spec', type: :feature, js: true doselect 'ldap', from: 'synchronized_group_auth_source_id'select 'foo', from: 'synchronized_group_group_id'fill_in 'synchronized_group_dn', with: 'cn=foo,ou=groups,dc=example,dc=com'+ check 'synchronized_group_sync_users'click_on 'Create'expect(page).to have_selector('.flash.notice', text: I18n.t(:notice_successful_create))diff --git a/modules/ldap_groups/spec/models/synchronized_filter_spec.rb b/modules/ldap_groups/spec/models/synchronized_filter_spec.rbnew file mode 100644index 00000000000..2563b7dfe99--- /dev/null+++ b/modules/ldap_groups/spec/models/synchronized_filter_spec.rb@@ -0,0 +1,28 @@+require 'spec_helper'++describe LdapGroups::SynchronizedFilter, type: :model do+ describe '#used_base_dn' do+ let(:auth_source) { FactoryBot.build :ldap_auth_source, base_dn: 'dc=example,dc=com' }+ let(:filter) { FactoryBot.build :ldap_synchronized_filter, auth_source: auth_source }++ it 'validates the end of the base dn matches the auth_source' do+ filter.base_dn = nil+ expect(filter.base_dn).to eq(nil)+ expect(filter.used_base_dn).to eq(auth_source.base_dn)+ end+ end++ describe '#base_dn' do+ let(:auth_source) { FactoryBot.build :ldap_auth_source, base_dn: 'dc=example,dc=com' }+ let(:filter) { FactoryBot.build :ldap_synchronized_filter, auth_source: auth_source }++ it 'validates the end of the base dn matches the auth_source' do+ filter.base_dn = nil+ expect(filter).to be_valid++ filter.base_dn = 'dc=something,dc=else'+ expect(filter).not_to be_valid+ expect(filter.errors.details[:base_dn]).to contain_exactly(error: :must_contain_base_dn)+ end+ end+enddiff --git a/modules/ldap_groups/spec/lib/synchronization_spec.rb b/modules/ldap_groups/spec/services/synchronization_spec.rbsimilarity index 75%rename from modules/ldap_groups/spec/lib/synchronization_spec.rbrename to modules/ldap_groups/spec/services/synchronization_spec.rbindex 47f1aca982a..7e92cbdec8c 100644--- a/modules/ldap_groups/spec/lib/synchronization_spec.rb+++ b/modules/ldap_groups/spec/services/synchronization_spec.rb@@ -1,7 +1,7 @@require File.dirname(__FILE__) + '/../spec_helper'require 'ladle'-describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do+describe LdapGroups::SynchronizationService, with_ee: %i[ldap_groups] dolet(:plugin_settings) do{ group_base: 'ou=groups,dc=example,dc=com', group_key: 'cn' }end@@ -34,6 +34,7 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] doendlet(:onthefly_register) { false }+ let(:sync_users) { false }let(:ldap_filter) { nil }let(:user_aa729) { FactoryBot.create :user, login: 'aa729', auth_source: auth_source }@@ -44,18 +45,24 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] dolet(:group_bar) { FactoryBot.create :group, lastname: 'bar' }let(:synced_foo) do- FactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=groups,dc=example,dc=com', group: group_foo,- auth_source: auth_source+ FactoryBot.create :ldap_synchronized_group,+ dn: 'cn=foo,ou=groups,dc=example,dc=com',+ group: group_foo,+ sync_users: sync_users,+ auth_source: auth_sourceendlet(:synced_bar) do- FactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=groups,dc=example,dc=com', group: group_bar,- auth_source: auth_source+ FactoryBot.create :ldap_synchronized_group,+ dn: 'cn=bar,ou=groups,dc=example,dc=com',+ group: group_bar,+ sync_users: sync_users,+ auth_source: auth_sourceendsubject do# Need the system user for admin permissionUser.system.run_given do- described_class.new auth_source+ described_class.new(auth_source).callendend@@ -181,29 +188,65 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] dolet(:user_aa729) { User.find_by login: 'aa729' }let(:user_bb459) { User.find_by login: 'bb459' }- it 'creates the remaining users' do- subject- expect(synced_foo.users.count).to eq(1)- expect(synced_bar.users.count).to eq(3)- expect(group_foo.users).to contain_exactly(user_aa729)- expect(group_bar.users).to contain_exactly(user_aa729, user_bb459, user_cc414)+ context 'and users sync in the groups enabled' do+ let(:sync_users) { true }++ it 'creates the remaining users' do+ subject+ expect(synced_foo.users.count).to eq(1)+ expect(synced_bar.users.count).to eq(3)++ expect(group_foo.users).to contain_exactly(user_aa729)+ expect(group_bar.users).to contain_exactly(user_aa729, user_bb459, user_cc414)+ end+ end++ context 'and users sync not enabled' do+ let(:sync_users) { false }++ it 'does not create the users' do+ subject+ expect(synced_foo.users.count).to eq(0)+ expect(synced_bar.users.count).to eq(1)++ expect(group_foo.users).to be_empty+ expect(group_bar.users).to contain_exactly(user_cc414)+ endendendcontext 'with an LDAP filter for users starting with b and on-the-fly enabled' dolet(:onthefly_register) { true }let(:ldap_filter) { '(uid=b*)' }+ let(:user_aa729) { User.find_by login: 'aa729' }let(:user_bb459) { User.find_by login: 'bb459' }- it 'creates the remaining users' do- subject- expect(synced_foo.users.count).to eq(0)- expect(synced_bar.users.count).to eq(1)+ context 'and users sync in the groups enabled' do+ let(:sync_users) { true }++ it 'creates the remaining users' do+ subject+ expect(synced_foo.users.count).to eq(0)+ expect(synced_bar.users.count).to eq(1)++ expect(user_aa729).to eq nil+ # Only matched users are added to the group, meaning cc414 is not added+ expect(group_bar.users).to contain_exactly(user_bb459)+ end+ end++ context 'and users sync not enabled' do+ let(:sync_users) { false }++ it 'does not create the users' do+ subject+ expect(synced_foo.users.count).to eq(0)+ expect(synced_bar.users.count).to eq(0)- expect(User.find_by(login: 'aa729')).to eq nil- # Only matched users are added to the group, meaning cc414 is not added- expect(group_bar.users).to contain_exactly(user_bb459)+ expect(user_aa729).to eq nil+ expect(user_bb459).to eq nil+ endendendend@@ -267,11 +310,11 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] docontext 'with invalid base' dolet(:synced_foo) doFactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=invalid,dc=example,dc=com', group: group_foo,- auth_source: auth_source+ auth_source: auth_sourceendlet(:synced_bar) doFactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=invalid,dc=example,dc=com', group: group_bar,- auth_source: auth_source+ auth_source: auth_sourceendcontext 'when one synced group exists' dodiff --git a/modules/ldap_groups/spec/lib/synchronize_filter_spec.rb b/modules/ldap_groups/spec/services/synchronize_filter_spec.rbsimilarity index 73%rename from modules/ldap_groups/spec/lib/synchronize_filter_spec.rbrename to modules/ldap_groups/spec/services/synchronize_filter_spec.rbindex 1a8f58f8a01..4185bcc43c2 100644--- a/modules/ldap_groups/spec/lib/synchronize_filter_spec.rb+++ b/modules/ldap_groups/spec/services/synchronize_filter_spec.rb@@ -1,7 +1,7 @@require File.dirname(__FILE__) + '/../spec_helper'require 'ladle'-describe OpenProject::LdapGroups::SynchronizeFilter, with_ee: %i[ldap_groups] do+describe LdapGroups::SynchronizeFilterService, with_ee: %i[ldap_groups] dobefore(:all) doldif = Rails.root.join('spec/fixtures/ldap/users.ldif')@ldap_server = Ladle::Server.new(quiet: false, port: ParallelHelper.port_for_ldap.to_s, domain: 'dc=example,dc=com',@@ -24,10 +24,6 @@ describe OpenProject::LdapGroups::SynchronizeFilter, with_ee: %i[ldap_groups] doattr_login: 'uid'end- let(:user_aa729) { FactoryBot.create :user, login: 'aa729', auth_source: auth_source }- let(:user_bb459) { FactoryBot.create :user, login: 'bb459', auth_source: auth_source }- let(:user_cc414) { FactoryBot.create :user, login: 'cc414', auth_source: auth_source }-let(:group_foo) { FactoryBot.create :group, lastname: 'foo' }let(:group_bar) { FactoryBot.create :group, lastname: 'bar' }@@ -42,7 +38,7 @@ describe OpenProject::LdapGroups::SynchronizeFilter, with_ee: %i[ldap_groups] dolet(:filter_foo_bar) { FactoryBot.create :ldap_synchronized_filter, auth_source: auth_source }- subject { described_class.new filter_foo_bar }+ subject { described_class.new(filter_foo_bar).call }shared_examples 'has foo and bar synced groups' doit 'creates the two groups' do@@ -102,4 +98,37 @@ describe OpenProject::LdapGroups::SynchronizeFilter, with_ee: %i[ldap_groups] doexpect { synced_doesnotexist.reload }.to raise_error(ActiveRecord::RecordNotFound)endend++ describe 'when filter has sync_users selected' do+ let(:filter_foo_bar) { FactoryBot.create :ldap_synchronized_filter, auth_source: auth_source, sync_users: true }++ it 'creates the groups with sync_users flag set' do+ expect { subject }.not_to raise_error++ filter_foo_bar.reload++ # Expect two synchronized groups added+ expect(filter_foo_bar.groups.count).to eq 2+ sync_foo_group = LdapGroups::SynchronizedGroup.find_by(dn: 'cn=foo,ou=groups,dc=example,dc=com')+ sync_bar_group = LdapGroups::SynchronizedGroup.find_by(dn: 'cn=bar,ou=groups,dc=example,dc=com')+ expect(sync_foo_group.sync_users).to be_truthy+ expect(sync_bar_group.sync_users).to be_truthy+ end+ end++ describe 'when filter has its own base dn' do+ let(:filter_foo_bar) do+ FactoryBot.create :ldap_synchronized_filter,+ auth_source: auth_source,+ base_dn: 'ou=users,dc=example,dc=com'+ end++ it 'users that base for searching and doesnt find any groups' do+ expect { subject }.not_to raise_error++ filter_foo_bar.reload++ expect(filter_foo_bar.groups.count).to eq 0+ end+ endend