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

If users are created without it, they do not get their notification
settings

https://community.openproject.org/wp/40433
pull/10022/head
Oliver Günther 3 years ago
parent 907671de7c
commit 8d16758e43
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 19
      app/models/user.rb
  2. 19
      db/migrate/20220106145037_fix_missing_notification_settings.rb
  3. 20
      modules/ldap_groups/app/services/ldap_groups/synchronize_groups_service.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)

@ -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

@ -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

Loading…
Cancel
Save