public function ldap_login($context) { if (!empty($context->username) || !empty($_POST['password'])) { //LDAP connection $ldap = ldap_connect(Symphony::Configuration()->get('server', 'ldap_authors'), Symphony::Configuration()->get('port', 'ldap_authors')); if ($ldap) { ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, Symphony::Configuration()->get('protocol_version', 'ldap_authors')); $filterdn = preg_replace('/\\%username\\%/', $context['username'], Symphony::Configuration()->get('filterdn', 'ldap_authors')); $basedn = Symphony::Configuration()->get('basedn', 'ldap_authors'); try { //Attempt to authenticate to the LDAP server $bind = ldap_bind($ldap, $filterdn . ',' . $basedn, $_POST['password']); $user = AuthorManager::fetchByUsername($context['username']); if (count($user) > 0 && $user->get('LDAP') === '1') { //LDAP user has visited before therefore login $this->login($user); return true; } else { //New LDAP user, we need to insert their details in the authors table $ldap_user = $this->ldap_retrieve_user($ldap, $basedn, $filterdn); if ($ldap_user) { //Get attributes and insert data $attrs = array(Symphony::Configuration()->get('first_name_key', 'ldap_authors'), Symphony::Configuration()->get('last_name_key', 'ldap_authors'), Symphony::Configuration()->get('email_key', 'ldap_authors')); $author_details = $this->ldap_retrieve_attributes($attrs, $ldap_user[0]); if (count($author_details) == 3) { $id = AuthorManager::add(array('username' => $context['username'], 'password' => $this->fake_password(10), 'first_name' => $author_details[0], 'last_name' => $author_details[1], 'email' => $author_details[2], 'user_type' => Symphony::Configuration()->get('default_author_type', 'ldap_authors'), 'primary' => 'no', 'LDAP' => true)); if ($id) { //Once user is inserted log them in $user = AuthorManager::fetchByID($id); $this->login($user); return true; } else { Symphony::$Log->pushToLog('[LDAP] Unable to insert LDAP user into Symphony authors table.', E_ERROR); } } else { Symphony::$Log->pushToLog('[LDAP] Unable to retireve first name, last name and email address from the LDAP server.', E_ERROR); } } else { Symphony::$Log->pushToLog('[LDAP] Authentication with the LDAP server was successful, however unable to find LDAP user details.', E_ERROR); } } } catch (Exception $e) { Symphony::$Log->pushToLog('[LDAP] Unable to bind to LDAP server, this could be misconfiguration or invalid credentials. (User: "******")', E_WARNING); } return false; } else { Symphony::$Log->pushToLog('[LDAP] Unable to connect to LDAP server, please check configuration.', E_ERROR); } } }
/** * This is the insert method for the Author. This takes the current * `$this->_fields` values and adds them to the database using either the * `AuthorManager::edit` or `AuthorManager::add` functions. An * existing user is determined by if an ID is already set. * * @see toolkit.AuthorManager#add() * @see toolkit.AuthorManager#edit() * @return integer|boolean * When a new Author is added or updated, an integer of the Author ID * will be returned, otherwise false will be returned for a failed update. */ public function commit() { if (!is_null($this->get('id'))) { $id = $this->get('id'); $this->remove('id'); if (AuthorManager::edit($id, $this->get())) { $this->set('id', $id); return $id; } else { return false; } } else { return AuthorManager::add($this->get()); } }
public function commit() { $fields = $this->_fields; if (isset($fields['id'])) { $id = $fields['id']; unset($fields['id']); return AuthorManager::edit($id, $fields); } else { return AuthorManager::add($fields); } }