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

Add creation of users based on on-the-fly register

Oliver Günther273ae7e443f5
diff --git a/app/models/ldap_auth_source.rb b/app/models/ldap_auth_source.rb
index 05ba3a63853..c03c7933852 100644
--- a/app/models/ldap_auth_source.rb
+++ b/app/models/ldap_auth_source.rb
@@ -84,6 +84,28 @@ class LdapAuthSource < AuthSource
'LDAP'
end
+ def get_user_attributes_from_ldap_entry(entry)
+ {
+ dn: entry.dn,
+ login: LdapAuthSource.get_attr(entry, attr_login),
+ firstname: LdapAuthSource.get_attr(entry, attr_firstname),
+ lastname: LdapAuthSource.get_attr(entry, attr_lastname),
+ mail: LdapAuthSource.get_attr(entry, attr_mail),
+ admin: !!LdapAuthSource.get_attr(entry, attr_admin),
+ auth_source_id: id
+ }
+ end
+
+ # Return the attributes needed for the LDAP search. It will only
+ # include the user attributes if on-the-fly registration is enabled
+ def search_attributes
+ if onthefly_register?
+ ['dn', attr_login, attr_firstname, attr_lastname, attr_mail, attr_admin].compact
+ else
+ ['dn', attr_login]
+ end
+ end
+
private
def strip_ldap_attributes
@@ -110,27 +132,6 @@ class LdapAuthSource < AuthSource
end
end
- def get_user_attributes_from_ldap_entry(entry)
- {
- dn: entry.dn,
- firstname: LdapAuthSource.get_attr(entry, attr_firstname),
- lastname: LdapAuthSource.get_attr(entry, attr_lastname),
- mail: LdapAuthSource.get_attr(entry, attr_mail),
- admin: !!LdapAuthSource.get_attr(entry, attr_admin),
- auth_source_id: id
- }
- end
-
- # Return the attributes needed for the LDAP search. It will only
- # include the user attributes if on-the-fly registration is enabled
- def search_attributes
- if onthefly_register?
- ['dn', attr_firstname, attr_lastname, attr_mail, attr_admin]
- else
- ['dn']
- end
- end
-
# Check if a DN (user record) authenticates with the password
def authenticate_dn(dn, password)
if dn.present? && password.present?
diff --git a/app/models/user.rb b/app/models/user.rb
index 9eb47d5e5bc..f003f61e4ce 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -269,25 +269,27 @@ class User < Principal
def self.try_authentication_and_create_user(login, password)
return nil if OpenProject::Configuration.disable_password_login?
- user = nil
attrs = AuthSource.authenticate(login, password)
- if attrs
- # login is both safe and protected in chilis core code
- # in case it's intentional we keep it that way
- user = new(attrs.except(:login))
- user.login = login
+ try_to_create(attrs) if attrs
+ end
+
+ # Try to create the user from attributes
+ def self.try_to_create(attrs, notify: false)
+ new(attrs).tap do |user|
user.language = Setting.default_language
if OpenProject::Enterprise.user_limit_reached?
- OpenProject::Enterprise.send_activation_limit_notification_about user
+ OpenProject::Enterprise.send_activation_limit_notification_about(user) if notify
+ Rails.logger.error("User '#{user.login}' could not be created as user limit exceeded.")
user.errors.add :base, I18n.t(:error_enterprise_activation_user_limit)
elsif user.save
user.reload
- logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
+ Rails.logger.info("User '#{user.login}' created from external auth source: #{user.auth_source&.type} - #{user.auth_source&.name}")
+ else
+ Rails.logger.error("User '#{user.login}' could not be created: #{user.errors.full_messages.join(". ")}")
end
end
- user
end
# Returns the user who matches the given autologin +key+ or nil
diff --git a/app/views/ldap_auth_sources/_form.html.erb b/app/views/ldap_auth_sources/_form.html.erb
index 45e27126afc..1f9a47ef3bb 100644
--- a/app/views/ldap_auth_sources/_form.html.erb
+++ b/app/views/ldap_auth_sources/_form.html.erb
@@ -125,9 +125,12 @@ See docs/COPYRIGHT.rdoc for more details.
</div>
<div class="form--field">
<%= f.check_box 'onthefly_register', label: AuthSource.human_attribute_name(:onthefly) %>
- <span class="form--field-instructions">
- <%= t('ldap_auth_sources.attribute_texts.onthefly_register') %>
- </span>
+ <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.yml
index 673e0ec499f..35266aecf16 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -124,6 +124,9 @@ en:
If you check this box, OpenProject will automatically create new users from their LDAP entries
when 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/lib/open_project/ldap_groups/synchronization.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
index 3383ffb7257..47eef61cdc8 100644
--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb
@@ -19,13 +19,36 @@ module OpenProject::LdapGroups
::LdapGroups::Membership.transaction do
@synced_groups.find_each do |group|
- members = get_members(ldap_con, group)
- users = User.where(login: members)
+ user_data = get_members(ldap_con, group)
+
+ # Create users that are not existing
+ users = map_to_users(user_data)
+
update_memberships!(group, users)
end
end
end
+ ##
+ # Map LDAP entries to user accounts, creating them if necessary
+ def map_to_users(entries)
+ create_missing!(entries) if ldap.onthefly_register?
+
+ User.where(login: entries.keys)
+ end
+
+ ##
+ # Create missing users from ldap data
+ def create_missing!(entries)
+ existing = User.where(login: entries.keys).pluck(:login, :id).to_h
+
+ entries.each do |login, data|
+ next if existing[login]
+
+ User.try_to_create(data)
+ end
+ end
+
##
# Apply memberships from the ldap group and remove outdated
def update_memberships!(sync, users)
@@ -49,20 +72,22 @@ module OpenProject::LdapGroups
def get_members(ldap_con, group)
# Get user login attribute and base dn which are private
- attr_login = ldap.send :attr_login
- base_dn = ldap.send :base_dn
+ base_dn = ldap.base_dn
# memberOf filter to identifiy member entries of the group
memberof_filter = Net::LDAP::Filter.eq('memberOf', group.dn)
- logins = []
+ # TODO add ldap filter
+
+ users = {}
ldap_con.search(base: base_dn,
filter: memberof_filter,
- attributes: [attr_login]) do |entry|
- logins << ::LdapAuthSource.get_attr(entry, attr_login)
+ attributes: ldap.search_attributes) do |entry|
+ data = ldap.get_user_attributes_from_ldap_entry(entry)
+ users[data[:login]] = data.except(:dn)
end
- logins
+ users
end
##
diff --git a/modules/ldap_groups/spec/lib/synchronization_spec.rb b/modules/ldap_groups/spec/lib/synchronization_spec.rb
index db0d34f6591..540267bbf32 100644
--- a/modules/ldap_groups/spec/lib/synchronization_spec.rb
+++ b/modules/ldap_groups/spec/lib/synchronization_spec.rb
@@ -3,7 +3,7 @@ require 'ladle'
describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
let(:plugin_settings) do
- { group_base: 'ou=groups,dc=example,dc=com', group_key: 'cn' }
+ {group_base: 'ou=groups,dc=example,dc=com', group_key: 'cn'}
end
before(:all) do
@@ -20,13 +20,19 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
# 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: 'ou=people,dc=example,dc=com',
- attr_login: 'uid'
+ port: '12389',
+ account: 'uid=admin,ou=system',
+ account_password: 'secret',
+ base_dn: 'ou=people,dc=example,dc=com',
+ onthefly_register: onthefly_register,
+ attr_login: 'uid',
+ attr_firstname: 'givenName',
+ attr_lastname: 'sn',
+ attr_mail: 'mail'
end
+ let(:onthefly_register) { false }
+
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 }
@@ -123,8 +129,8 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
end
it 'removes all memberships and groups after removing auth source' do
- expect{ auth_source.destroy }
- .to change{::LdapGroups::Membership.count}.from(4).to(0)
+ expect { auth_source.destroy }
+ .to change { ::LdapGroups::Membership.count }.from(4).to(0)
auth_source.destroy
@@ -158,6 +164,21 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] do
expect(group_foo.users).to be_empty
expect(group_bar.users).to eq([user_cc414])
end
+
+ context 'with LDAP on-the-fly enabled' do
+ let(:onthefly_register) { true }
+ 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(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
end
end