Skip to content

Commit 27b2354

Browse files
caseysoftwareclaude
andcommitted
LDAP: always create a contact for a new user
User Management lists users through an inner join on contacts, and the per-user permission check also depends on the contact, so a user without one cannot be found, viewed or given a role. That is the state an LDAP user was left in whenever the contact could not be saved: - the directory had no givenName (CContact requires a first name), - another contact already had the same e-mail address, or - the attributes array was empty, which skipped the contact entirely. The login name is now used as the first name when the directory has none, the contact is saved once more without the e-mail address if the first save fails, and the contact is created even with no attributes. With the contact owner fix in the previous commit, a first LDAP login now produces a user that appears under Users > Inactive Users, where an admin can assign a role. Refs #320 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016CP89WVmGZ1VVCCZMS6ntN
1 parent d2155cc commit 27b2354

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

‎classes/w2p/Authenticators/LDAP.class.php‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,22 +163,37 @@ protected function ldapAttribute(array $ldap_attribs, $name)
163163

164164
public function createsqluser($username, $password, $ldap_attribs = array())
165165
{
166+
// Every user needs a contact: the user lists and the per-user
167+
// permission checks join on it, so a user without one cannot be
168+
// found or given a role.
166169
$c = new CContact();
167-
if (count($ldap_attribs)) {
168-
// Contact information based on the inetOrgPerson class schema
169-
$c->contact_first_name = $this->ldapAttribute($ldap_attribs, 'givenname');
170-
$c->contact_last_name = $this->ldapAttribute($ldap_attribs, 'sn');
171-
$c->contact_city = $this->ldapAttribute($ldap_attribs, 'l');
172-
$c->contact_country = $this->ldapAttribute($ldap_attribs, 'country');
173-
$c->contact_state = $this->ldapAttribute($ldap_attribs, 'st');
174-
$c->contact_zip = $this->ldapAttribute($ldap_attribs, 'postalcode');
175-
$c->contact_job = $this->ldapAttribute($ldap_attribs, 'title');
176-
$c->contact_email = $this->ldapAttribute($ldap_attribs, 'mail');
177-
$c->contact_phone = $this->ldapAttribute($ldap_attribs, 'telephonenumber');
178-
$result = $c->store();
179-
$contactArray = array('phone_mobile' => $this->ldapAttribute($ldap_attribs, 'mobile'));
180-
$c->setContactMethods($contactArray);
181-
}
170+
if (count($ldap_attribs)) {
171+
// Contact information based on the inetOrgPerson class schema
172+
$c->contact_first_name = $this->ldapAttribute($ldap_attribs, 'givenname');
173+
$c->contact_last_name = $this->ldapAttribute($ldap_attribs, 'sn');
174+
$c->contact_city = $this->ldapAttribute($ldap_attribs, 'l');
175+
$c->contact_country = $this->ldapAttribute($ldap_attribs, 'country');
176+
$c->contact_state = $this->ldapAttribute($ldap_attribs, 'st');
177+
$c->contact_zip = $this->ldapAttribute($ldap_attribs, 'postalcode');
178+
$c->contact_job = $this->ldapAttribute($ldap_attribs, 'title');
179+
$c->contact_email = $this->ldapAttribute($ldap_attribs, 'mail');
180+
$c->contact_phone = $this->ldapAttribute($ldap_attribs, 'telephonenumber');
181+
}
182+
if (mb_strlen((string) $c->contact_first_name) <= 1) {
183+
// CContact requires a first name; use the login name when the
184+
// directory has none.
185+
$c->contact_first_name = $username;
186+
}
187+
$c->store();
188+
if (!$c->contact_id && '' != (string) $c->contact_email) {
189+
// Most likely another contact already uses this e-mail address.
190+
$c->contact_email = '';
191+
$c->clearErrors();
192+
$c->store();
193+
}
194+
if ($c->contact_id && count($ldap_attribs)) {
195+
$c->setContactMethods(array('phone_mobile' => $this->ldapAttribute($ldap_attribs, 'mobile')));
196+
}
182197

183198
$u = new CUser();
184199
$u->user_username = $username;

0 commit comments

Comments
 (0)