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

[40433] Create users through create service in ldap group sync

Oliver Günther8d16758e43ac
diff --git a/app/models/user.rb b/app/models/user.rb
index 55bd055c8d0..d55895b716d 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -237,25 +237,6 @@ class User < Principal
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) 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
- 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
- end
-
# Returns the user who matches the given autologin +key+ or nil
def self.try_to_autologin(key)
token = Token::AutoLogin.find_by_plaintext_value(key)
diff --git a/db/migrate/20220106145037_fix_missing_notification_settings.rb b/db/migrate/20220106145037_fix_missing_notification_settings.rb
new file mode 100644
index 00000000000..ff4e864c792
--- /dev/null
+++ b/db/migrate/20220106145037_fix_missing_notification_settings.rb
@@ -0,0 +1,19 @@
+class FixMissingNotificationSettings < ActiveRecord::Migration[6.1]
+ def up
+ execute <<~SQL.squish
+ INSERT INTO
+ notification_settings
+ (user_id, watched, involved, mentioned)
+ SELECT
+ u.id, true, true, true
+ FROM
+ users u
+ WHERE type = 'User'
+ AND NOT EXISTS (SELECT * FROM notification_settings ns WHERE ns.user_id = u.id)
+ SQL
+ end
+
+ def down
+ # No data to revert
+ end
+end
diff --git a/modules/ldap_groups/app/services/ldap_groups/synchronize_groups_service.rb b/modules/ldap_groups/app/services/ldap_groups/synchronize_groups_service.rb
index 8ae6642932d..d18f1ead64b 100644
--- a/modules/ldap_groups/app/services/ldap_groups/synchronize_groups_service.rb
+++ b/modules/ldap_groups/app/services/ldap_groups/synchronize_groups_service.rb
@@ -54,7 +54,25 @@ module LdapGroups
entries.each do |login, data|
next if existing[login]
- User.try_to_create(data)
+ if OpenProject::Enterprise.user_limit_reached?
+ Rails.logger.error("[LDAP groups] User '#{user.login}' could not be created as user limit exceeded.")
+ break
+ end
+
+ try_to_create(data)
+ end
+ end
+
+ # Try to create the user from attributes
+ def try_to_create(attrs)
+ call = Users::CreateService
+ .new(user: User.system)
+ .call(attrs)
+
+ if call.success?
+ Rails.logger.info("[LDAP groups] User '#{call.result.login}' created")
+ else
+ Rails.logger.error("[LDAP groups] User '#{user}' could not be created: #{call.message}")
end
end