moodle/moodle · error · moodle_exception

auth_ldap_ad_create_req

auth_ldap_ad_create_req

Error message

auth_ldap_ad_create_req

What it means

Thrown by auth_ldap's user_create() in AD mode when ldap_add() fails while creating the initial (disabled) AD user object at cn=<username>,<create_context>. The plugin builds the DN and attributes (objectClass user, sAMAccountName, userAccountControl disabled) and, if the directory refuses the add, raises this error. It means the account could not be provisioned in AD at all.

Solutions

  1. Verify create_context in the LDAP plugin settings is a valid, existing OU DN (e.g. OU=Users,DC=example,DC=com) and that the DN built from the username is correct.
  2. Check the bind user's permissions — it needs rights to create user objects under create_context.
  3. Reproduce ldap_add failure details: enable LDAP debug (ldap_set_option LDAP_OPT_DEBUG_LEVEL) or check AD event logs for the specific LDAP error code.
  4. Confirm the username has no invalid sAMAccountName characters and does not collide with an existing AD object.
  5. Ensure LDAP referrals/SSL (ldaps) configuration allows writes; AD typically refuses writes over plain unauthenticated or unsigned channels.

Example fix

// before (nonexistent OU)
$this->config->create_context = 'ou=People,dc=example,dc=com';
// after (verified OU)
$this->config->create_context = 'ou=MoodleUsers,dc=example,dc=com';
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight checks before AD provisioning
$ctx = get_config('auth/ldap', 'create_context');
if (empty($ctx)) { /* fail early */ }
// verify bind user can add under $ctx:
ldap_search($conn, $ctx, '(objectClass=organizationalUnit)');

Try / catch

try {
    $auth->user_signup($user, $notify);
} catch (\moodle_exception $e) {
    if ($e->errorcode === 'auth_ldap_ad_create_req') {
        // Check create_context DN and bind-user create rights before retrying.
    }
}

Prevention

When it happens

Trigger: user_signup/user_create with AD user type where ldap_add returns false: wrong create_context DN, insufficient bind-user privileges to create user objects, objectClass/DN constraint violations, or the entry already exists.

Common situations: create_context DN typo or OU not existing; bind credentials lacking 'Create User objects' rights on the OU; sAMAccountName conflicts with an existing object; AD schema/attribute rules blocking the add; password policy or UAC flags misconfigured.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/c52528b5ace3bd71. Report an issue: GitHub.

Appendix: source

Thrown at public/auth/ldap/auth.php:469

                // strings (UCS-2 Little Endian format) and surrounded with
                // double quotes. See http://support.microsoft.com/?kbid=269190
                if (!function_exists('mb_convert_encoding')) {
                    throw new \moodle_exception('auth_ldap_no_mbstring', 'auth_ldap');
                }

                // Check for invalid sAMAccountName characters.
                if (preg_match('#[/\\[\]:;|=,+*?<>@"]#', $extusername)) {
                    throw new \moodle_exception ('auth_ldap_ad_invalidchars', 'auth_ldap');
                }

                // First create the user account, and mark it as disabled.
                $newuser['objectClass'] = array('top', 'person', 'user', 'organizationalPerson');
                $newuser['sAMAccountName'] = $extusername;
                $newuser['userAccountControl'] = AUTH_AD_NORMAL_ACCOUNT |
                                                 AUTH_AD_ACCOUNTDISABLE;
                $userdn = 'cn='.ldap_addslashes($extusername).','.$this->config->create_context;
                if (!ldap_add($ldapconnection, $userdn, $newuser)) {
                    throw new \moodle_exception('auth_ldap_ad_create_req', 'auth_ldap');
                }

                // Now set the password
                unset($newuser);
                $newuser['unicodePwd'] = mb_convert_encoding('"' . $extpassword . '"',
                                                             'UCS-2LE', 'UTF-8');
                if(!ldap_modify($ldapconnection, $userdn, $newuser)) {
                    // Something went wrong: delete the user account and error out
                    ldap_delete ($ldapconnection, $userdn);
                    throw new \moodle_exception('auth_ldap_ad_create_req', 'auth_ldap');
                }
                $uadd = true;
                break;
            default:
               throw new \moodle_exception('auth_ldap_unsupportedusertype', 'auth_ldap', '', $this->config->user_type_name);
        }
        $this->ldap_close();
        return $uadd;

View on GitHub (pinned to 48ea8c3322)