{"record":{"id":"c52528b5ace3bd71","repo":"moodle/moodle","slug":"auth-ldap-ad-create-req","errorCode":"auth_ldap_ad_create_req","errorMessage":"auth_ldap_ad_create_req","messagePattern":"auth_ldap_ad_create_req","errorType":"exception","errorClass":"moodle_exception","httpStatus":null,"severity":"error","filePath":"public/auth/ldap/auth.php","lineNumber":469,"sourceCode":"                // strings (UCS-2 Little Endian format) and surrounded with\n                // double quotes. See http://support.microsoft.com/?kbid=269190\n                if (!function_exists('mb_convert_encoding')) {\n                    throw new \\moodle_exception('auth_ldap_no_mbstring', 'auth_ldap');\n                }\n\n                // Check for invalid sAMAccountName characters.\n                if (preg_match('#[/\\\\[\\]:;|=,+*?<>@\"]#', $extusername)) {\n                    throw new \\moodle_exception ('auth_ldap_ad_invalidchars', 'auth_ldap');\n                }\n\n                // First create the user account, and mark it as disabled.\n                $newuser['objectClass'] = array('top', 'person', 'user', 'organizationalPerson');\n                $newuser['sAMAccountName'] = $extusername;\n                $newuser['userAccountControl'] = AUTH_AD_NORMAL_ACCOUNT |\n                                                 AUTH_AD_ACCOUNTDISABLE;\n                $userdn = 'cn='.ldap_addslashes($extusername).','.$this->config->create_context;\n                if (!ldap_add($ldapconnection, $userdn, $newuser)) {\n                    throw new \\moodle_exception('auth_ldap_ad_create_req', 'auth_ldap');\n                }\n\n                // Now set the password\n                unset($newuser);\n                $newuser['unicodePwd'] = mb_convert_encoding('\"' . $extpassword . '\"',\n                                                             'UCS-2LE', 'UTF-8');\n                if(!ldap_modify($ldapconnection, $userdn, $newuser)) {\n                    // Something went wrong: delete the user account and error out\n                    ldap_delete ($ldapconnection, $userdn);\n                    throw new \\moodle_exception('auth_ldap_ad_create_req', 'auth_ldap');\n                }\n                $uadd = true;\n                break;\n            default:\n               throw new \\moodle_exception('auth_ldap_unsupportedusertype', 'auth_ldap', '', $this->config->user_type_name);\n        }\n        $this->ldap_close();\n        return $uadd;","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/auth/ldap/auth.php#L451-L487","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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.","Check the bind user's permissions — it needs rights to create user objects under create_context.","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.","Confirm the username has no invalid sAMAccountName characters and does not collide with an existing AD object.","Ensure LDAP referrals/SSL (ldaps) configuration allows writes; AD typically refuses writes over plain unauthenticated or unsigned channels."],"exampleFix":"// before (nonexistent OU)\n$this->config->create_context = 'ou=People,dc=example,dc=com';\n// after (verified OU)\n$this->config->create_context = 'ou=MoodleUsers,dc=example,dc=com';","handlingStrategy":"try-catch","validationCode":"// pre-flight checks before AD provisioning\n$ctx = get_config('auth/ldap', 'create_context');\nif (empty($ctx)) { /* fail early */ }\n// verify bind user can add under $ctx:\nldap_search($conn, $ctx, '(objectClass=organizationalUnit)');","typeGuard":null,"tryCatchPattern":"try {\n    $auth->user_signup($user, $notify);\n} catch (\\moodle_exception $e) {\n    if ($e->errorcode === 'auth_ldap_ad_create_req') {\n        // Check create_context DN and bind-user create rights before retrying.\n    }\n}","preventionTips":["Validate create_context DN exists in AD before enabling account creation","Grant the bind account 'Create User objects' rights on the OU","Test with ldapadd CLI against the same OU first","Watch AD event logs for LDAP provision failures"],"tags":["ldap","active-directory","provisioning","permissions","moodle"],"backgroundTag":"database-write-failed","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}