LDAP/SAML auto-create of new users fails with "Not authorized" (fix in PR #1036)

Hi everyone,

since 8.10, SuiteCRM no longer creates new users on their first sign-in through LDAP or SAML. Existing users still sign in. Every new user gets “login credentials incorrect” (LDAP) or ends up without a session (SAML), and no row is written to users. The bug is tracked in #1020. I opened a pull request with a fix and would like to ask the maintainers to review it and ship it in the next 8.10 patch release.

Symptom

request.CRITICAL: Uncaught PHP Exception RuntimeException: "Not authorized" at User.php line 626

Cause

In 8.10, User::hasSaveAccess() returns is_admin($current_user) for a record that has no id yet, and User::save() throws Not authorized when it returns false. In 8.9.3 it returned true for that case. Nobody is signed in while LDAP or SAML auto-creates an account, so the check always fails. Both providers reach save() through the same method:

  • SAML: AppSamlUserFactory::createUser() → UserHandler::createExternalAuthUser() → CreateUser::createExternalAuthUser() → User::save()
  • LDAP: AppLdapUserProvider::createUser() → UserHandler::createExternalAuthUser() → same path

CreateUser.php is identical in 8.9.3 and 8.10.2, so nothing on this path gives the creation the rights it now needs.

Fix

PR: Fix #1020 - Create external auth users in the system user's context by kevinveenbirkenbach · Pull Request #1036 · SuiteCRM/SuiteCRM-Core · GitHub

The fix creates the account in CreateUser::createExternalAuthUser() as the system user and restores the previous user afterwards:

-        return $this->create($name, $userInfo);
+        global $current_user;
+        $previousUser = $current_user;
+        $current_user = BeanFactory::newBean('Users')->getSystemUser();
+
+        try {
+            return $this->create($name, $userInfo);
+        } finally {
+            $current_user = $previousUser;
+        }

The system-user context covers only this one creation. createExternalAuthUser() already forces is_admin = 0, status = Active and external_auth_only = 1, so the new account is the same as in 8.9 and gets no admin rights.

Tested

On 8.10.2 with SAML against Keycloak, the first sign-in of an unknown user creates the account and opens a session, and existing users still sign in. LDAP is not tested yet, but it uses the same method.

Reproduce without an identity provider

From public/legacy on the command line:

define('sugarEntry', true);
require_once 'include/entryPoint.php';
require_once 'include/portability/User/CreateUser.php';
$user = (new CreateUser())->createExternalAuthUser('samlprobe', ['user_name' => 'samlprobe', 'email1' => 'samlprobe@example.org']);
echo $user->id;

Before the fix: RuntimeException: Not authorized at modules/Users/User.php:626. With the fix: the id of the new user.

Workaround until a release

Apply the diff above to public/legacy/include/portability/User/CreateUser.php. Keep in mind that the next upgrade overwrites the file.

What would help

  • Maintainers: could you review #1036 and include it in the next 8.10 patch release? I am happy to change the PR if you prefer another approach.
  • LDAP users: if you can test the patch with LDAP_AUTO_CREATE=enabled, please post your result here or on the PR.

For context, this bug blocks our SuiteCRM upgrade from 8.9.1 to 8.10.2 in Infinito.Nexus, which provisions SuiteCRM users through Keycloak SSO.

Thanks!

LDAP: autocreation of users not working :partying_face: