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 filters to define multiple LDAP synchronized groups

Oliver Günther812cbf183c22
diff --git a/app/cells/table_cell.rb b/app/cells/table_cell.rb
index 18c163bd030..ec3629056dd 100644
--- a/app/cells/table_cell.rb
+++ b/app/cells/table_cell.rb
@@ -7,6 +7,7 @@ class TableCell < RailsCell
include WillPaginate::ActionView
options :groups, :roles, :status, :project
+ options show_inline_create: true
class << self
##
@@ -168,4 +169,8 @@ class TableCell < RailsCell
def action_name
controller.action_name
end
+
+ def options
+ super
+ end
end
diff --git a/app/cells/views/table/show.erb b/app/cells/views/table/show.erb
index ef6b0eaae3b..f2402d921dd 100644
--- a/app/cells/views/table/show.erb
+++ b/app/cells/views/table/show.erb
@@ -70,7 +70,7 @@ See docs/COPYRIGHT.rdoc for more details.
<% end -%>
</tbody>
</table>
- <% if inline_create_link %>
+ <% if inline_create_link && show_inline_create %>
<div class="wp-inline-create-button">
<%= inline_create_link %>
</div>
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rb
index 5dd8bfa787d..05ba3a63853 100644
--- a/app/models/ldap_auth_source.rb
+++ b/app/models/ldap_auth_source.rb
@@ -66,6 +66,11 @@ class LdapAuthSource < AuthSource
raise 'LdapError: ' + error.message
end
+ # Open and return a system connection
+ def with_connection
+ yield initialize_ldap_con(account, account_password)
+ end
+
# test the connection to the LDAP
def test_connection
unless authenticate_dn(account, account_password)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index db3c0210f5f..673e0ec499f 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1360,6 +1360,7 @@ en:
label_duplicate: "duplicate"
label_duplicates: "duplicates"
label_edit: "Edit"
+ label_edit_x: "Edit: %{x}"
label_enable_multi_select: "Toggle multiselect"
label_enabled_project_custom_fields: 'Enabled custom fields'
label_enabled_project_modules: 'Enabled modules'
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.rb
new file mode 100644
index 00000000000..ebe28514d13
--- /dev/null
+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/row_cell.rb
@@ -0,0 +1,45 @@
+module LdapGroups
+ module SynchronizedFilters
+ class RowCell < ::RowCell
+ include ::IconsHelper
+ include ::PasswordHelper
+
+ def synchronized_filter
+ model
+ end
+
+ def name
+ link_to synchronized_filter.name, ldap_groups_synchronized_filter_path(synchronized_filter)
+ end
+
+ def auth_source
+ link_to synchronized_filter.auth_source.name, edit_auth_source_path(synchronized_filter.auth_source)
+ end
+
+ def groups
+ synchronized_filter.groups.count
+ end
+
+ def button_links
+ [
+ edit_link,
+ delete_link
+ ]
+ end
+
+ def edit_link
+ link_to I18n.t(:button_edit),
+ { controller: table.target_controller, ldap_filter_id: model.id, action: :edit },
+ class: 'icon icon-edit',
+ title: t(:button_edit)
+ end
+
+ def delete_link
+ link_to I18n.t(:button_delete),
+ { controller: table.target_controller, ldap_filter_id: model.id, action: :destroy_info },
+ class: 'icon icon-delete',
+ title: t(:button_delete)
+ end
+ end
+ end
+end
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.rb
new file mode 100644
index 00000000000..7d797433f98
--- /dev/null
+++ b/modules/ldap_groups/app/cells/ldap_groups/synchronized_filters/table_cell.rb
@@ -0,0 +1,36 @@
+
+module LdapGroups
+ module SynchronizedFilters
+ class TableCell < ::TableCell
+ columns :name, :auth_source, :groups
+
+ def initial_sort
+ %i[id asc]
+ end
+
+ def target_controller
+ 'ldap_groups/synchronized_filters'
+ end
+
+ def sortable?
+ false
+ end
+
+ def inline_create_link
+ link_to({ controller: target_controller, action: :new },
+ class: 'budget-add-row wp-inline-create--add-link',
+ title: I18n.t('ldap_groups.synchronized_filters.add_new')) do
+ op_icon('icon icon-add')
+ end
+ end
+
+ def headers
+ [
+ ['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')]
+ ]
+ end
+ end
+ end
+end
\ No newline at end of file
diff --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.rb
index 485944a28d3..703b90bfe37 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
@@ -8,8 +8,8 @@ module LdapGroups
model
end
- def entry
- link_to synchronized_group.entry, ldap_groups_synchronized_group_path(synchronized_group)
+ def dn
+ link_to synchronized_group.dn, ldap_groups_synchronized_group_path(synchronized_group)
end
def auth_source
@@ -21,14 +21,16 @@ module LdapGroups
end
def users
- synchronized_group.users.count
+ synchronized_group.users.size
end
def button_links
- [delete_link]
+ [delete_link].compact
end
def delete_link
+ return if table.options[:deletable] == false
+
link_to I18n.t(:button_delete),
{ controller: table.target_controller, ldap_group_id: model.id, action: :destroy_info },
class: 'icon icon-delete',
diff --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.rb
index 0c2ba0086a4..5f195843ee8 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
@@ -2,7 +2,7 @@
module LdapGroups
module SynchronizedGroups
class TableCell < ::TableCell
- columns :entry, :auth_source, :group, :users
+ columns :dn, :auth_source, :group, :users
def initial_sort
%i[id asc]
@@ -30,7 +30,7 @@ module LdapGroups
def headers
[
- ['entry', caption: ::LdapGroups::SynchronizedGroup.human_attribute_name('entry')],
+ ['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)],
diff --git a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb
new file mode 100644
index 00000000000..0ff761842a4
--- /dev/null
+++ b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_filters_controller.rb
@@ -0,0 +1,89 @@
+module LdapGroups
+ class SynchronizedFiltersController < ::ApplicationController
+ before_action :require_admin
+ before_action :check_ee
+ before_action :find_filter, except: %i[new create]
+
+ layout 'admin'
+ menu_item :plugin_ldap_groups
+
+ def new
+ @filter = SynchronizedFilter.new
+ end
+
+ def show; end
+
+ def end; end
+
+ def destroy_info; end
+
+ def create
+ @filter = SynchronizedFilter.new permitted_params
+
+ if @filter.save
+ flash[:notice] = I18n.t(:notice_successful_create)
+ redirect_to ldap_groups_synchronized_groups_path
+ else
+ render action: :new
+ end
+ rescue ActionController::ParameterMissing
+ render_400
+ end
+
+ def update
+ if @filter.update permitted_params
+ flash[:notice] = I18n.t(:notice_successful_update)
+ redirect_to action: :show
+ else
+ render action: :edit
+ end
+ rescue ActionController::ParameterMissing
+ render_400
+ end
+
+ def destroy
+ if @filter.destroy
+ flash[:notice] = I18n.t(:notice_successful_delete)
+ else
+ flash[:error] = I18n.t(:error_can_not_delete_entry)
+ end
+
+ redirect_to ldap_groups_synchronized_groups_path
+ end
+
+ def synchronize
+ ::OpenProject::LdapGroups::SynchronizeFilter.new(@filter)
+ flash[:notice] = 'Done.'
+ redirect_to ldap_groups_synchronized_groups_path
+ end
+
+ private
+
+ def find_filter
+ @filter = SynchronizedFilter.find(params[:ldap_filter_id])
+ rescue ActiveRecord::RecordNotFound
+ render_404
+ end
+
+ def check_ee
+ unless EnterpriseToken.allows_to?(:ldap_groups)
+ render template: 'ldap_groups/synchronized_groups/upsale'
+ return false
+ end
+ end
+
+ def permitted_params
+ params
+ .require(:synchronized_filter)
+ .permit(:filter_string, :name, :auth_source_id, :group_name_attribute)
+ end
+
+ def default_breadcrumb
+ ActionController::Base.helpers.link_to(t('ldap_groups.synchronized_groups.plural'), ldap_groups_synchronized_groups_path)
+ end
+
+ def show_local_breadcrumb
+ true
+ end
+ end
+end
diff --git a/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rb b/modules/ldap_groups/app/controllers/ldap_groups/synchronized_groups_controller.rb
index 398de5791f2..cc613815301 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
@@ -9,6 +9,7 @@ module LdapGroups
def index
@groups = SynchronizedGroup.includes(:auth_source, :group)
+ @filters = SynchronizedFilter.includes(:auth_source, :groups)
end
def new
@@ -46,15 +47,6 @@ module LdapGroups
redirect_to action: :index
end
- def update_settings
- Setting.plugin_openproject_ldap_groups = {
- group_key: params[:group_key],
- group_base: params[:group_base]
- }
- flash[:notice] = I18n.t(:notice_successful_update)
- redirect_to action: :index
- end
-
private
def find_group
@@ -73,7 +65,7 @@ module LdapGroups
def permitted_params
params
.require(:synchronized_group)
- .permit(:entry, :group_id, :auth_source_id)
+ .permit(:dn, :group_id, :auth_source_id)
end
def default_breadcrumb
diff --git a/modules/ldap_groups/app/models/ldap_groups/membership.rb b/modules/ldap_groups/app/models/ldap_groups/membership.rb
index 6a9f6c78af4..4a4793d8747 100644
--- a/modules/ldap_groups/app/models/ldap_groups/membership.rb
+++ b/modules/ldap_groups/app/models/ldap_groups/membership.rb
@@ -3,6 +3,7 @@ module LdapGroups
belongs_to :user
belongs_to :group,
class_name: '::LdapGroups::SynchronizedGroup',
- foreign_key: 'group_id'
+ foreign_key: 'group_id',
+ counter_cache: :users_count
end
end
diff --git a/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb b/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb
new file mode 100644
index 00000000000..8f74790db01
--- /dev/null
+++ b/modules/ldap_groups/app/models/ldap_groups/synchronized_filter.rb
@@ -0,0 +1,29 @@
+require 'net/ldap'
+require 'net/ldap/dn'
+
+module LdapGroups
+ class SynchronizedFilter < ApplicationRecord
+ belongs_to :auth_source
+
+ has_many :groups,
+ class_name: '::LdapGroups::SynchronizedGroup',
+ foreign_key: 'filter_id',
+ dependent: :destroy
+
+ validates_presence_of :filter_string
+ validates_presence_of :auth_source
+ validate :validate_filter_syntax
+
+ def parsed_filter_string
+ Net::LDAP::Filter.from_rfc2254 filter_string
+ end
+
+ private
+
+ def validate_filter_syntax
+ parsed_filter_string
+ rescue Net::LDAP::FilterSyntaxInvalidError
+ errors.add :filter_string, :invalid
+ end
+ end
+end
diff --git a/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb b/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb
index b77d1352cb0..4b066614c4e 100644
--- a/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb
+++ b/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb
@@ -5,20 +5,20 @@ module LdapGroups
class SynchronizedGroup < ApplicationRecord
belongs_to :group
belongs_to :auth_source
+ belongs_to :filter,
+ class_name: '::LdapGroups::SynchronizedFilter',
+ foreign_key: :filter_id
+
has_many :users,
class_name: '::LdapGroups::Membership',
foreign_key: 'group_id'
- validates_presence_of :entry
+ validates_presence_of :dn
validates_presence_of :group
validates_presence_of :auth_source
before_destroy :remove_all_members
- def dn
- ::OpenProject::LdapGroups.group_dn(escaped_entry)
- end
-
##
# Add a set of new members to the internal group
def add_members!(new_users)
@@ -42,10 +42,6 @@ module LdapGroups
end
end
- def escaped_entry
- Net::LDAP::DN.escape(entry)
- end
-
private
def remove_from_actual_group(id)
diff --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.erb
new file mode 100644
index 00000000000..dc21711690a
--- /dev/null
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/_form.html.erb
@@ -0,0 +1,49 @@
+<%= error_messages_for @filter %>
+
+<fieldset class="form--fieldset">
+ <legend class="form--fieldset-legend"><%= @filter.class.human_attribute_name :name %></legend>
+
+ <div class="form--field -required">
+ <%= f.text_field :name,
+ required: true,
+ container_class: '-middle' %>
+ </div>
+
+ <div class="form--field -required">
+ <%= f.text_field :group_name_attribute,
+ required: true,
+ placeholder: 'cn',
+ container_class: '-middle' %>
+ <div class="form--field-instructions">
+ <%= t('ldap_groups.synchronized_filters.form.group_name_attribute_text') %>
+ </div>
+ </div>
+</fieldset>
+
+<fieldset class="form--fieldset">
+ <legend class="form--fieldset-legend"><%= @filter.class.human_attribute_name :auth_source %></legend>
+
+ <div class="form--field -required">
+ <%= f.select :auth_source_id,
+ LdapAuthSource.pluck(:name, :id),
+ required: true,
+ include_blank: "--- #{t(:actionview_instancetag_blank_option)} ---",
+ container_class: '-middle' %>
+ <div class="form--field-instructions">
+ <%= t('ldap_groups.synchronized_groups.form.auth_source_text') %>
+ </div>
+ </div>
+</fieldset>
+<fieldset class="form--fieldset">
+ <legend class="form--fieldset-legend"><%= @filter.class.human_attribute_name :filter_string %></legend>
+ <div class="form--field -required">
+ <%= f.text_area :filter_string,
+ required: true,
+ cols: 200,
+ rows: 10,
+ container_class: '-large' %>
+ <div class="form--field-instructions">
+ <%= t('ldap_groups.synchronized_filters.form.filter_string_text') %>
+ </div>
+ </div>
+</fieldset>
diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/destroy_info.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/destroy_info.html.erb
new file mode 100644
index 00000000000..2690d747eed
--- /dev/null
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/destroy_info.html.erb
@@ -0,0 +1,68 @@
+<% html_title(t(:label_administration), t('ldap_groups.synchronized_filters.destroy.title')) -%>
+
+<%= styled_form_tag({ action: :destroy, ldap_group_id: @filter.id },
+ class: 'danger-zone',
+ method: :delete) do %>
+ <section class="form--section">
+ <h3 class="form--section-title">
+ <%= t('ldap_groups.synchronized_filters.destroy.title', name: @filter.name) %>
+ </h3>
+ <p>
+ </p>
+ <p class="danger-zone--warning">
+ <span class="icon icon-info2"></span>
+ <%= t('ldap_groups.synchronized_filters.destroy.confirmation', name: @filter.name, groups_count: @filter.groups.count) %>
+ </p>
+
+ <% if @filter.groups.count > 0 %>
+ <p><%= t('ldap_groups.synchronized_filters.destroy.removed_groups') %></p>
+ <div class="generic-table--container">
+ <div class="generic-table--results-container">
+ <table class="generic-table">
+ <thead>
+ <th>
+ <div class="generic-table--sort-header-outer">
+ <span class="generic-table--sort-header"><%= t('attributes.name') %>
+ </span>
+ </div>
+ </th>
+ <th>
+ <div class="generic-table--sort-header-outer">
+ <span class="generic-table--sort-header"><%= ::LdapGroups::SynchronizedGroup.human_attribute_name :dn %>
+ </span>
+ </div>
+ </th>
+ </thead>
+ <% @filter.groups.find_each do |synced_group| %>
+ <tr>
+ <td><%= link_to synced_group.group.name,
+ edit_group_path(synced_group.group),
+ target: '_blank' %>
+ </td>
+ <td><%= synced_group.dn %></td>
+ </tr>
+ <% end %>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ <% end %>
+ <p>
+ <%= t('ldap_groups.synchronized_filters.destroy.verification',
+ name: "<em class=\"danger-zone--expected-value\">#{h(@filter.name)}</em>").html_safe %>
+ </p>
+
+ <div class="danger-zone--verification">
+ <input type="text"></input>
+ <%= styled_button_tag title: t(:button_delete), class: '-highlight', disabled: true do
+ concat content_tag :i, '', class: 'button--icon icon-delete'
+ concat content_tag :span, t(:button_delete), class: 'button--text'
+ end %>
+ <%= link_to t(:button_cancel),
+ ldap_groups_synchronized_groups_path,
+ title: t(:button_cancel),
+ class: 'button -with-icon icon-cancel' %>
+ </div>
+ </section>
+<% end %>
+
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.erb
new file mode 100644
index 00000000000..9f51e6d04fe
--- /dev/null
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/edit.html.erb
@@ -0,0 +1,19 @@
+<% 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,
+ as: :synchronized_filter,
+ url: { action: :update } do |f| %>
+
+ <%= render partial: 'form', locals: { f: f } %>
+
+ <p>
+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
+ <%= link_to t(:button_cancel), { action: :show }, class: 'button -with-icon icon-cancel' %>
+ </p>
+<% end %>
+
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.erb
new file mode 100644
index 00000000000..a9bdc228123
--- /dev/null
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/new.html.erb
@@ -0,0 +1,19 @@
+<% 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,
+ as: :synchronized_filter,
+ url: { action: :create } do |f| %>
+
+ <%= render partial: 'form', locals: { f: f } %>
+
+ <p>
+ <%= styled_button_tag t(:button_create), class: '-highlight -with-icon icon-checkmark' %>
+ <%= link_to t(:button_cancel), ldap_groups_synchronized_groups_path, class: 'button -with-icon icon-cancel' %>
+ </p>
+<% end %>
+
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.erb
new file mode 100644
index 00000000000..0a3a27aebcc
--- /dev/null
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_filters/show.html.erb
@@ -0,0 +1,62 @@
+<% html_title(t(:label_administration), t('ldap_groups.synchronized_filters.plural'), h(@filter.name)) -%>
+
+<%= error_messages_for @filter %>
+
+<%= breadcrumb_toolbar(h(@filter.name)) do %>
+ <li class="toolbar-item">
+ <%= link_to({ action: :edit },
+ class: 'button') do %>
+ <%= op_icon('button--icon icon-edit') %>
+ <span class="button--text"><%= t(:button_edit) %></span>
+ <% end %>
+ </li>
+ <li class="toolbar-item">
+ <%= link_to({ action: :destroy_info },
+ class: 'button -danger') do %>
+ <%= op_icon('button--icon icon-delete') %>
+ <span class="button--text"><%= t(:button_delete) %></span>
+ <% end %>
+ </li>
+<% end %>
+
+<section class="admin--edit-section">
+ <div class="attributes-key-value">
+ <div class="attributes-key-value--key"><%= @filter.class.human_attribute_name(:name) %></div>
+ <div class="attributes-key-value--value-container">
+ <div class="attributes-key-value--value">
+ <span><%= @filter.name %></span>
+ </div>
+ </div>
+ <div class="attributes-key-value--key"><%= @filter.class.human_attribute_name(:group_name_attribute) %></div>
+ <div class="attributes-key-value--value-container">
+ <div class="attributes-key-value--value">
+ <span><%= @filter.group_name_attribute %></span>
+ </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">
+ <code><%= @filter.filter_string %></code>
+ </div>
+ </div>
+ <div class="attributes-key-value--key"><%= t('ldap_groups.synchronized_groups.plural') %></div>
+ <div class="attributes-key-value--value-container">
+ <div class="attributes-key-value--value">
+ <%= @filter.groups.count %>
+ </div>
+ </div>
+ </div>
+</section>
+
+<section class="admin--edit-section">
+<%= toolbar(title: t('ldap_groups.synchronized_groups.plural')) do %>
+ <li class="toolbar-item">
+ <%= link_to({ action: :synchronize },
+ class: 'button') do %>
+ <span class="button--text"><%= t('ldap_groups.label_synchronize') %></span>
+ <% end %>
+ </li>
+<% end %>
+ <%= cell ::LdapGroups::SynchronizedGroups::TableCell, @filter.groups, show_inline_create: false, deletable: false %>
+</section>
+
diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/destroy_info.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/destroy_info.html.erb
index d085af297aa..a397518d987 100644
--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/destroy_info.html.erb
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/destroy_info.html.erb
@@ -5,19 +5,19 @@
method: :delete) do %>
<section class="form--section">
<h3 class="form--section-title">
- <%= t('ldap_groups.synchronized_groups.destroy.title', name: @group.entry) %>
+ <%= t('ldap_groups.synchronized_groups.destroy.title', name: @group.dn) %>
</h3>
<p>
</p>
<p class="danger-zone--warning">
<span class="icon icon-info2"></span>
- <%= l('ldap_groups.synchronized_groups.destroy.confirmation', name: @group.entry, users_count: @group.users.count)%>
+ <%= l('ldap_groups.synchronized_groups.destroy.confirmation', name: @group.dn, users_count: @group.users.count)%>
</p>
<p>
<span><%= l('ldap_groups.synchronized_groups.destroy.info')%></span>
<br/>
<%= t('ldap_groups.synchronized_groups.destroy.verification',
- name: "<em class=\"danger-zone--expected-value\">#{h(@group.entry)}</em>").html_safe %>
+ name: "<em class=\"danger-zone--expected-value\">#{h(@group.dn)}</em>").html_safe %>
</p>
<div class="danger-zone--verification">
<input type="text"></input>
diff --git a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/index.html.erb b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/index.html.erb
index 10ac1e9296e..e8d10b37492 100644
--- a/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/index.html.erb
+++ b/modules/ldap_groups/app/views/ldap_groups/synchronized_groups/index.html.erb
@@ -1,9 +1,19 @@
<% html_title(t(:label_administration), t('ldap_groups.synchronized_groups.plural')) -%>
<%= breadcrumb_toolbar t('ldap_groups.synchronized_groups.plural') do %>
+ <li class="toolbar-item">
+ <%= link_to new_ldap_groups_synchronized_filter_path,
+ { class: 'button',
+ aria: {label: t('ldap_groups.synchronized_filters.singular')},
+ title: t('ldap_groups.synchronized_filters.add_new')} do %>
+ <%= op_icon('button--icon icon-add') %>
+ <span class="button--text"><%= t('ldap_groups.synchronized_filters.singular') %></span>
+ <% end %>
+ </li>
+
<li class="toolbar-item">
<%= link_to new_ldap_groups_synchronized_group_path,
- { class: 'button -alt-highlight',
+ { class: 'button',
aria: {label: t('ldap_groups.synchronized_groups.singular')},
title: t('ldap_groups.synchronized_groups.add_new')} do %>
<%= op_icon('button--icon icon-add') %>
@@ -19,44 +29,13 @@
</div>
</div>
-
<section class="admin--edit-section">
- <%= styled_form_tag(update_settings_ldap_groups_synchronized_groups_path,
- method: :post,
- id: 'update-ldap-group-settings-form') do %>
- <fieldset class="form--fieldset">
- <legend class="form--fieldset-legend"><%= t(:label_settings) %></legend>
- <div class="form--field -required">
- <label class="form--label" for='group_key'><%= t('ldap_groups.settings.group_key') %></label>
- <div class="form--field-container">
- <%= styled_text_field_tag 'group_key',
- ::OpenProject::LdapGroups.group_key,
- container_class: '-middle',
- required: true,
- placeholder: 'e.g., uid' %>
- </div>
- <div class="form--field-instructions">
- <%= I18n.t('ldap_groups.settings.group_key_text') %>
- </div>
- </div>
- <div class="form--field">
- <label class="form--label" for='group_base'><%= t('ldap_groups.settings.group_base') %></label>
- <div class="form--field-container">
- <%= styled_text_field_tag 'group_base',
- ::OpenProject::LdapGroups.group_base,
- container_class: '-middle',
- placeholder: 'ou=people,dc=example,dc=com' %>
- </div>
- <div class="form--field-instructions">
- <%= I18n.t('ldap_groups.settings.group_base_text') %>
- </div>
- </div>
- </fieldset>
- <%= styled_submit_tag l(:button_apply), class: '-highlight' %>
- <% end %>
+ <h3 class="form--section-title"><%= t('ldap_groups.synchronized_filters.plural') %></h3>
+ <%= cell ::LdapGroups::SynchronizedFilters::TableCell, @filters %>
</section>
<section class="admin--edit-section">
+ <h3 class="form--section-title"><%= t('ldap_groups.synchronized_groups.plural') %></h3>
<%= cell ::LdapGroups::SynchronizedGroups::TableCell, @groups %>
</section>
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.erb
index b83c9d67bd6..15b3e828eb5 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
@@ -27,13 +27,11 @@
<legend class="form--fieldset-legend"><%= @group.class.model_name.human %></legend>
<div class="form--field -required">
- <%= f.text_field :entry,
+ <%= f.text_field :dn,
required: true,
- container_class: '-middle',
- prefix: "#{::OpenProject::LdapGroups.group_key || '[group key]'}=",
- suffix: ",#{::OpenProject::LdapGroups.group_base || '[group base]'}" %>
+ container_class: '-middle' %>
<div class="form--field-instructions">
- <%= t('ldap_groups.synchronized_groups.form.entry_text') %>
+ <%= t('ldap_groups.synchronized_groups.form.dn_text') %>
</div>
</div>
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.erb
index cfeb92cd115..cd732bb7a74 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
@@ -1,14 +1,14 @@
-<% html_title(t(:label_administration), t('ldap_groups.synchronized_groups.plural'), h(@group.entry)) -%>
+<% html_title(t(:label_administration), t('ldap_groups.synchronized_groups.plural'), h(@group.dn)) -%>
<%= error_messages_for @group %>
-<%= breadcrumb_toolbar(link_to(t('ldap_groups.synchronized_groups.plural'), { action: :index}), h(@group.entry)) %>
+<%= 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('entry') %></div>
+ <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.entry %></span>
+ <span><%= @group.dn %></span>
</div>
</div>
<div class="attributes-key-value--key"><%= @group.class.human_attribute_name('auth_source') %></div>
diff --git a/modules/costs/app/workers/ldap_groups/synchronization_job.rb b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
similarity index 81%
rename from modules/costs/app/workers/ldap_groups/synchronization_job.rb
rename to modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
index aa82a4951a7..20719e6a9c7 100644
--- a/modules/costs/app/workers/ldap_groups/synchronization_job.rb
+++ b/modules/ldap_groups/app/workers/ldap_groups/synchronization_job.rb
@@ -38,7 +38,12 @@ module LdapGroups
begin
LdapAuthSource.find_each do |ldap|
- Rails.logger.info { "[LDAP groups] Start synchronization for ldap auth source #{ldap.name}" }
+ Rails.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) }
+
+ Rails.logger.info { "[LDAP groups] Start group synchronization for ldap auth source #{ldap.name}" }
OpenProject::LdapGroups::Synchronization.new(ldap)
end
rescue StandardError => e
diff --git a/modules/ldap_groups/config/locales/en.yml b/modules/ldap_groups/config/locales/en.yml
index d9d05f9ff0c..8f7b37d717f 100644
--- a/modules/ldap_groups/config/locales/en.yml
+++ b/modules/ldap_groups/config/locales/en.yml
@@ -2,19 +2,35 @@ en:
activerecord:
attributes:
ldap_groups/synchronized_group:
- entry: 'Entry identifier'
+ dn: 'DN'
auth_source: 'LDAP connection'
+ ldap_groups/synchronized_filter:
+ filter_string: 'LDAP filter'
+ auth_source: 'LDAP connection'
+ group_name_attribute: "Group name attribute"
models:
ldap_groups/synchronized_group: 'Synchronized LDAP group'
+ ldap_groups/synchronized_filter: 'LDAP Group synchronization filter'
ldap_groups:
label_menu_item: 'LDAP group synchronization'
label_group_key: 'LDAP group filter key'
- label_group_key: 'LDAP group filter key'
+ label_synchronize: 'Synchronize'
settings:
- group_key: 'LDAP group attribute'
- group_key_text: 'The LDAP attribute name used to identify the groups.'
- group_base: 'LDAP group base'
- group_base_text: 'LDAP group base used to search for group entries.'
+ name_attribute: 'LDAP groups name attribute'
+ name_attribute_text: 'The LDAP attribute used for naming the OpenProject group when created by a filter'
+
+ synchronized_filters:
+ add_new: 'Add synchronized LDAP filter'
+ singular: 'LDAP Group synchronization filter'
+ plural: 'LDAP Group synchronization filters'
+ 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."
+ removed_groups: "Warning: This will remove the following groups from OpenProject and remove it from all projects!"
+ verification: "Enter the filter name %{name} to verify the deletion."
+ 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.'
synchronized_groups:
add_new: 'Add synchronized LDAP group'
@@ -35,7 +51,7 @@ en:
plural: 'Synchronized LDAP groups'
singular: 'Synchronized LDAP group'
form:
- auth_source_text: 'Select which LDAP authentication mode is used.'
- entry_text: 'Define the LDAP group identifier.'
+ auth_source_text: 'Select which LDAP connection should be used.'
+ 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/config/routes.rb b/modules/ldap_groups/config/routes.rb
index fde285ef33b..dff7c9976c8 100644
--- a/modules/ldap_groups/config/routes.rb
+++ b/modules/ldap_groups/config/routes.rb
@@ -1,5 +1,19 @@
OpenProject::Application.routes.draw do
namespace 'ldap_groups' do
+
+ resources :synchronized_filters,
+ param: :ldap_filter_id,
+ except: %i(index) do
+
+ member do
+ # Extract groups from filter
+ get 'synchronize'
+
+ # Destroy warning
+ get 'destroy_info'
+ end
+ end
+
resources :synchronized_groups,
param: :ldap_group_id,
only: %i(new index create show destroy) do
@@ -8,11 +22,6 @@ OpenProject::Application.routes.draw do
# Destroy warning
get 'destroy_info'
end
-
- collection do
- # Plugin settings update
- post 'update_settings', action: 'update_settings'
- end
end
end
end
\ No newline at end of file
diff --git a/modules/ldap_groups/db/migrate/20200420122713_make_synchronized_group_dn.rb b/modules/ldap_groups/db/migrate/20200420122713_make_synchronized_group_dn.rb
new file mode 100644
index 00000000000..9de78708ca1
--- /dev/null
+++ b/modules/ldap_groups/db/migrate/20200420122713_make_synchronized_group_dn.rb
@@ -0,0 +1,27 @@
+class MakeSynchronizedGroupDn < ActiveRecord::Migration[6.0]
+ def up
+ add_column :ldap_groups_synchronized_groups, :dn, :text, index: true
+ add_column :ldap_groups_synchronized_groups,
+ :users_count,
+ :integer,
+ default: 0,
+ null: false
+
+ LdapGroups::SynchronizedGroup.find_each do |group|
+ dn = ::OpenProject::LdapGroups.group_dn(Net::LDAP::DN.escape(group.entry))
+ group.update_column(:dn, dn)
+ end
+
+ remove_column :ldap_groups_synchronized_groups, :entry, :text
+ end
+
+ def down
+ add_column :ldap_groups_synchronized_groups, :entry, :text
+
+ warn "Removing synchronized groups"
+ LdapGroups::SynchronizedGroup.destroy_all
+
+ remove_column :ldap_groups_synchronized_groups, :dn
+ remove_column :ldap_groups_synchronized_groups, :users_count
+ end
+end
diff --git a/modules/ldap_groups/db/migrate/20200420133116_add_synchronized_filters.rb b/modules/ldap_groups/db/migrate/20200420133116_add_synchronized_filters.rb
new file mode 100644
index 00000000000..ef3149d34ac
--- /dev/null
+++ b/modules/ldap_groups/db/migrate/20200420133116_add_synchronized_filters.rb
@@ -0,0 +1,16 @@
+class AddSynchronizedFilters < ActiveRecord::Migration[6.0]
+ def change
+ create_table :ldap_groups_synchronized_filters do |t|
+ t.string :name
+ t.string :group_name_attribute
+ t.string :filter_string
+ t.references :auth_source
+
+ t.timestamps
+ end
+
+ change_table :ldap_groups_synchronized_groups do |t|
+ t.belongs_to :filter, foreign_key: { to_table: :ldap_groups_synchronized_filters }
+ end
+ end
+end
diff --git a/modules/ldap_groups/lib/open_project/ldap_groups.rb b/modules/ldap_groups/lib/open_project/ldap_groups.rb
index 92b4fcaeb6f..a89d19e738e 100644
--- a/modules/ldap_groups/lib/open_project/ldap_groups.rb
+++ b/modules/ldap_groups/lib/open_project/ldap_groups.rb
@@ -8,16 +8,8 @@ module OpenProject
Setting.plugin_openproject_ldap_groups
end
- def group_base
- settings[:group_base]
- end
-
- def group_key
- settings[:group_key]
- end
-
def group_dn(value)
- "#{group_key}=#{value},#{group_base}"
+ "#{settings[:group_key]}=#{value},#{settings[:group_base]}"
end
end
end
diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/engine.rb b/modules/ldap_groups/lib/open_project/ldap_groups/engine.rb
index 80993f2a005..a2fa0b9639b 100644
--- a/modules/ldap_groups/lib/open_project/ldap_groups/engine.rb
+++ b/modules/ldap_groups/lib/open_project/ldap_groups/engine.rb
@@ -9,7 +9,7 @@ module OpenProject::LdapGroups
bundled: true,
settings: {
default: {
- group_base: nil
+ name_attribute: 'cn'
}
} do
menu :admin_menu,
diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
index 918b090cdd6..3383ffb7257 100644
--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
@@ -69,12 +69,12 @@ module OpenProject::LdapGroups
# Add new users to the synced group
def add_memberships!(new_users, sync)
if new_users.empty?
- Rails.logger.info "[LDAP groups] No new users to add for #{sync.entry}"
+ Rails.logger.info "[LDAP groups] No new users to add for #{sync.dn}"
return
end
- Rails.logger.info { "[LDAP groups] Adding users #{new_users.pluck(:login)} to #{sync.entry}" }
- sync.users << new_users.map {|user| ::LdapGroups::Membership.new(group: sync, user: user)}
+ Rails.logger.info { "[LDAP groups] Adding users #{new_users.pluck(:login)} to #{sync.dn}" }
+ sync.users << new_users.map { |user| ::LdapGroups::Membership.new(group: sync, user: user) }
sync.group.users << new_users
end
@@ -82,11 +82,11 @@ module OpenProject::LdapGroups
# Remove a set of memberships
def remove_memberships!(memberships, sync)
if memberships.empty?
- Rails.logger.info "[LDAP groups] No users to remove for #{sync.entry}"
+ Rails.logger.info "[LDAP groups] No users to remove for #{sync.dn}"
return
end
- Rails.logger.info "[LDAP groups] Removing users #{memberships.pluck(:user_id)} from #{sync.entry}"
+ Rails.logger.info "[LDAP groups] Removing users #{memberships.pluck(:user_id)} from #{sync.dn}"
sync.remove_members!(memberships)
end
end
diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb
new file mode 100644
index 00000000000..cb3bd2348ae
--- /dev/null
+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb
@@ -0,0 +1,69 @@
+module OpenProject::LdapGroups
+ class SynchronizeFilter
+ attr_reader :filter, :raise_errors
+
+ def initialize(filter, raise_errors: false)
+ @filter = filter
+ @raise_errors = raise_errors
+ synchronize!
+ end
+
+ private
+
+ ##
+ # Perform the synchronization for the given filter
+ def 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
+ end
+
+ ##
+ # Get the current members from the ldap group
+ def upstream_groups
+ groups = []
+
+ each_group do |dn, name|
+ entry = LdapGroups::SynchronizedGroup.find_or_initialize_by(dn: dn)
+ # Always set the filter and auth source, in case multiple filters match the same group
+ # they are simply being re-assigned to the latest one
+ entry.filter_id = filter.id
+ entry.auth_source_id = filter.auth_source_id
+
+ # Create an OpenProject group
+ unless entry.group_id
+ entry.group = Group.find_or_initialize_by(groupname: name)
+ end
+
+ groups << entry
+ end
+
+ groups
+ end
+
+ ##
+ # Find groups by the filter string
+ # and yield them one by one
+ def each_group
+ ldap = 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)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/ldap_groups/lib/tasks/ldap_groups.rake b/modules/ldap_groups/lib/tasks/ldap_groups.rake
index fab024a3914..cb7ca014031 100644
--- a/modules/ldap_groups/lib/tasks/ldap_groups.rake
+++ b/modules/ldap_groups/lib/tasks/ldap_groups.rake
@@ -38,7 +38,7 @@ namespace :ldap_groups do
desc 'Create a development LDAP server from the fixtures LDIF'
task :ldap_server do
require 'ladle'
- ldif = File.expand_path('../../../spec/fixtures/users.ldif', __FILE__)
+ ldif = ENV['LDIF_FILE'] || File.expand_path('../../../spec/fixtures/users.ldif', __FILE__)
ldap_server = Ladle::Server.new(quiet: false, port: '12389', domain: 'dc=example,dc=com', ldif: ldif).start
puts <<~EOS
@@ -69,7 +69,6 @@ namespace :ldap_groups do
Groups:
cn=foo,ou=groups,dc=example,dc=com (Members: aa729)
cn=bar,ou=groups,dc=example,dc=com (Members: aa729, bb459, cc414)
-
EOS
diff --git a/modules/ldap_groups/spec/controllers/synchronized_groups_controller_spec.rb b/modules/ldap_groups/spec/controllers/synchronized_groups_controller_spec.rb
index 8ce6d1685a0..f9d8acaf9af 100644
--- a/modules/ldap_groups/spec/controllers/synchronized_groups_controller_spec.rb
+++ b/modules/ldap_groups/spec/controllers/synchronized_groups_controller_spec.rb
@@ -121,7 +121,7 @@ describe ::LdapGroups::SynchronizedGroupsController, with_ee: %i[ldap_groups], t
end
context 'with valid params' do
- let(:params) { { auth_source_id: 1, group_id: 1, entry: 'foo' } }
+ let(:params) { { auth_source_id: 1, group_id: 1, dn: 'cn=foo,ou=groups,dc=example,dc=com' } }
context 'and saving succeeds' do
let(:save_result) { true }
diff --git a/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb b/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb
new file mode 100644
index 00000000000..a668127c70f
--- /dev/null
+++ b/modules/ldap_groups/spec/factories/synchronized_filter_factory.rb
@@ -0,0 +1,9 @@
+FactoryBot.define do
+ factory :ldap_synchronized_filter, class: ::LdapGroups::SynchronizedFilter do
+ name { 'foo and bar' }
+ filter_string { '(|(cn=foo)(cn=bar))' }
+ group_name_attribute { 'cn' }
+ auth_source factory: :ldap_auth_source
+ end
+end
+
diff --git a/modules/ldap_groups/spec/factories/synchronized_group_factory.rb b/modules/ldap_groups/spec/factories/synchronized_group_factory.rb
index 4e724a0e46a..16d8de87936 100644
--- a/modules/ldap_groups/spec/factories/synchronized_group_factory.rb
+++ b/modules/ldap_groups/spec/factories/synchronized_group_factory.rb
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :ldap_synchronized_group, class: ::LdapGroups::SynchronizedGroup do
- entry { 'uid' }
+ dn { 'cn=foo,ou=groups,dc=example,dc=com' }
group factory: :group
auth_source factory: :ldap_auth_source
end
diff --git a/modules/ldap_groups/spec/features/administration_spec.rb b/modules/ldap_groups/spec/features/administration_spec.rb
index c6e7a481366..87ec9436af9 100644
--- a/modules/ldap_groups/spec/features/administration_spec.rb
+++ b/modules/ldap_groups/spec/features/administration_spec.rb
@@ -22,28 +22,28 @@ describe 'LDAP group sync administration spec', type: :feature, js: true do
expect(page).to have_no_selector('.upsale-notification')
# Create group
- find('.button.-alt-highlight', text: I18n.t('ldap_groups.synchronized_groups.singular')).click
+ find('.button', text: I18n.t('ldap_groups.synchronized_groups.singular')).click
select 'ldap', from: 'synchronized_group_auth_source_id'
select 'foo', from: 'synchronized_group_group_id'
- fill_in 'synchronized_group_entry', with: 'attr'
+ fill_in 'synchronized_group_dn', with: 'cn=foo,ou=groups,dc=example,dc=com'
click_on 'Create'
expect(page).to have_selector('.flash.notice', text: I18n.t(:notice_successful_create))
- expect(page).to have_selector('td.entry', text: 'attr')
+ expect(page).to have_selector('td.dn', text: 'cn=foo,ou=groups,dc=example,dc=com')
expect(page).to have_selector('td.auth_source', text: 'ldap')
expect(page).to have_selector('td.group', text: 'foo')
expect(page).to have_selector('td.users', text: '0')
# Show entry
- find('td.entry a').click
+ find('td.dn a').click
expect(page).to have_selector '.generic-table--empty-row'
# Check created group
sync = ::LdapGroups::SynchronizedGroup.last
expect(sync.group_id).to eq(group.id)
expect(sync.auth_source_id).to eq(auth_source.id)
- expect(sync.entry).to eq 'attr'
+ expect(sync.dn).to eq 'cn=foo,ou=groups,dc=example,dc=com'
# Assume we have a membership
sync.users.create user_id: admin.id
@@ -56,7 +56,7 @@ describe 'LDAP group sync administration spec', type: :feature, js: true do
expect_angular_frontend_initialized
find('.buttons a', text: 'Delete').click
- find('.danger-zone--verification input').set 'attr'
+ find('.danger-zone--verification input').set 'cn=foo,ou=groups,dc=example,dc=com'
sleep 2
click_on 'Delete'
diff --git a/modules/ldap_groups/spec/lib/synchronization_spec.rb b/modules/ldap_groups/spec/lib/synchronization_spec.rb
index 88ddaa805dc..db0d34f6591 100644
--- a/modules/ldap_groups/spec/lib/synchronization_spec.rb
+++ b/modules/ldap_groups/spec/lib/synchronization_spec.rb
@@ -15,11 +15,6 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
@ldap_server.stop
end
- before do
- # cn=<groupname>,ou=groups,...
- allow(Setting).to receive(:plugin_openproject_ldap_groups).and_return(plugin_settings)
- end
-
# Ldap has:
# three users aa729, bb459, cc414
# two groups foo (aa729), bar(aa729, bb459, cc414)
@@ -39,8 +34,8 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
let(:group_foo) { FactoryBot.create :group, lastname: 'foo_internal' }
let(:group_bar) { FactoryBot.create :group, lastname: 'bar' }
- let(:synced_foo) { FactoryBot.create :ldap_synchronized_group, entry: 'foo', group: group_foo, auth_source: auth_source }
- let(:synced_bar) { FactoryBot.create :ldap_synchronized_group, entry: 'bar', group: group_bar, auth_source: auth_source }
+ let(:synced_foo) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=groups,dc=example,dc=com', group: group_foo, auth_source: auth_source }
+ let(:synced_bar) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=groups,dc=example,dc=com', group: group_bar, auth_source: auth_source }
subject { described_class.new auth_source }
@@ -176,8 +171,8 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
group_foo.users << user_aa729
# Outputs that nothing was added to sync group
- expect(Rails.logger).to receive(:info).with("[LDAP groups] No users to remove for foo")
- expect(Rails.logger).to receive(:info).with("[LDAP groups] No new users to add for foo")
+ expect(Rails.logger).to receive(:info).with("[LDAP groups] No users to remove for cn=foo,ou=groups,dc=example,dc=com")
+ expect(Rails.logger).to receive(:info).with("[LDAP groups] No new users to add for cn=foo,ou=groups,dc=example,dc=com")
subject
@@ -220,10 +215,9 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
end
end
- context 'with invalid settings' do
- let(:plugin_settings) do
- { group_base: 'ou=invalid,dc=example,dc=com', group_key: 'cn' }
- end
+ context 'with invalid base' do
+ let(:synced_foo) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=invalid,dc=example,dc=com', group: group_foo, auth_source: auth_source }
+ let(:synced_bar) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=invalid,dc=example,dc=com', group: group_bar, auth_source: auth_source }
context 'when one synced group exists' do
before do
diff --git a/modules/ldap_groups/spec/lib/synchronize_filter_spec.rb b/modules/ldap_groups/spec/lib/synchronize_filter_spec.rb
new file mode 100644
index 00000000000..920d52e9c16
--- /dev/null
+++ b/modules/ldap_groups/spec/lib/synchronize_filter_spec.rb
@@ -0,0 +1,97 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+require 'ladle'
+
+describe OpenProject::LdapGroups::SynchronizeFilter, with_ee: %i[ldap_groups] do
+ before(:all) do
+ ldif = File.expand_path('../../fixtures/users.ldif', __FILE__)
+ @ldap_server = Ladle::Server.new(quiet: false, port: '12389', domain: 'dc=example,dc=com', ldif: ldif).start
+ end
+
+ after(:all) do
+ @ldap_server.stop
+ end
+
+ # Ldap has:
+ # three users aa729, bb459, cc414
+ # two groups foo (aa729), bar(aa729, bb459, cc414)
+ let(:auth_source) do
+ FactoryBot.create :ldap_auth_source,
+ port: '12389',
+ account: 'uid=admin,ou=system',
+ account_password: 'secret',
+ base_dn: 'dc=example,dc=com',
+ attr_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' }
+
+ let(:synced_foo) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=foo,ou=groups,dc=example,dc=com', group: group_foo, auth_source: auth_source }
+ let(:synced_bar) { FactoryBot.create :ldap_synchronized_group, dn: 'cn=bar,ou=groups,dc=example,dc=com', group: group_bar, auth_source: auth_source }
+
+ let(:filter_foo_bar) { FactoryBot.create :ldap_synchronized_filter, auth_source: auth_source }
+
+ subject { described_class.new filter_foo_bar }
+
+ shared_examples 'has foo and bar synced groups' do
+ it 'creates the two groups' 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
+ expect(filter_foo_bar.groups.map(&:dn)).to match_array ['cn=foo,ou=groups,dc=example,dc=com', 'cn=bar,ou=groups,dc=example,dc=com']
+
+ # Expect two actual groups added
+ op_foo_group = Group.find_by(lastname: 'foo')
+ op_bar_group = Group.find_by(lastname: 'bar')
+ expect(op_foo_group).to be_present
+ expect(op_bar_group).to be_present
+
+ 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.group).to eq op_foo_group
+ expect(sync_bar_group.group).to eq op_bar_group
+ end
+ end
+
+ describe 'when filter is new and nothing exists' do
+ it_behaves_like 'has foo and bar synced groups'
+ end
+
+ describe 'when one group already exists' do
+ before do
+ synced_foo
+ end
+
+ it_behaves_like 'has foo and bar synced groups'
+
+ it 'the group is taken over by the filter' do
+ expect { subject }.not_to raise_error
+
+ synced_foo.reload
+ expect(synced_foo.filter).to eq filter_foo_bar
+ end
+ end
+
+ describe 'when it has a group that no longer exists in ldap' do
+ let!(:group_doesnotexist) { FactoryBot.create :group, lastname: 'doesnotexist' }
+ let!(:synced_doesnotexist) do
+ FactoryBot.create :ldap_synchronized_group,
+ dn: 'cn=doesnotexist,ou=groups,dc=example,dc=com',
+ group: group_doesnotexist,
+ filter: filter_foo_bar,
+ auth_source: auth_source
+ end
+
+ it 'removes that group' do
+ expect { subject }.not_to raise_error
+ expect { synced_doesnotexist.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+end
diff --git a/modules/ldap_groups/spec/models/synchronized_group_spec.rb b/modules/ldap_groups/spec/models/synchronized_group_spec.rb
index 4fd4a7dbf3c..49d7602b4ae 100644
--- a/modules/ldap_groups/spec/models/synchronized_group_spec.rb
+++ b/modules/ldap_groups/spec/models/synchronized_group_spec.rb
@@ -3,30 +3,6 @@ require 'spec_helper'
describe LdapGroups::SynchronizedGroup, type: :model do
subject { FactoryBot.build :ldap_synchronized_group }
- describe '#escaped_entry' do
- it 'escapes the entry for ldap' do
- subject.entry = '<script>alert("1");</script>'
- expect(subject.escaped_entry).to eq("\\<script\\>alert(\\\"1\\\")\\;\\</script\\>")
- end
- end
-
- describe '#dn' do
- before do
- allow(Setting).to receive(:plugin_openproject_ldap_groups)
- .and_return(group_base: 'ou=example,ou=org', group_key: 'uid')
- end
-
- it 'uses the plugin settings' do
- subject.entry = 'myattr'
- expect(subject.dn).to eq 'uid=myattr,ou=example,ou=org'
- end
-
- it 'escapes the value in the dn' do
- subject.entry = '<script>alert("1");</script>'
- expect(subject.dn).to eq "uid=\\<script\\>alert(\\\"1\\\")\\;\\</script\\>,ou=example,ou=org"
- end
- end
-
describe 'validations' do
context 'correct attributes' do
it 'saves the record' do
@@ -38,7 +14,7 @@ describe LdapGroups::SynchronizedGroup, type: :model do
subject { described_class.new }
it 'validates missing attributes' do
expect(subject.save).to eq false
- expect(subject.errors[:entry]).to include "can't be blank."
+ expect(subject.errors[:dn]).to include "can't be blank."
expect(subject.errors[:auth_source]).to include "can't be blank."
expect(subject.errors[:group]).to include "can't be blank."
end