/** * Login function takes an associative array of fields that contain * an Identity field (Email/Username) and a Password field. They keys * should be the Field's `element_name`. * An optional parameter, `$isHashed` refers to if the password provided * is hashed already, or requires hashing prior to logging in. * * @throws Exception * @param array $credentials * @param boolean $isHashed * Defaults to false * @return boolean */ public function login(array $credentials, $isHashed = false) { $username = $email = $password = null; $data = extension_Members::$_errors = array(); // Map POST data to simple terms if (isset($credentials[$this->section->getFieldHandle('identity')])) { $username = $credentials[$this->section->getFieldHandle('identity')]; } if (isset($credentials[$this->section->getFieldHandle('email')])) { $email = $credentials[$this->section->getFieldHandle('email')]; } // Allow login via username OR email. This normalises the $data array from the custom // field names to simple names for ease of use. if (isset($username)) { $data['username'] = Symphony::Database()->cleanValue($username); } else { if (isset($email) && !is_null($this->section->getFieldHandle('email'))) { $data['email'] = Symphony::Database()->cleanValue($email); } } // Map POST data for password to `$password` if (isset($credentials[$this->section->getFieldHandle('authentication')])) { $password = $credentials[$this->section->getFieldHandle('authentication')]; $data['password'] = !empty($password) ? $password : ''; } // Check to ensure that we actually have some data to try and log a user in with. if (empty($data['password']) && isset($credentials[$this->section->getFieldHandle('authentication')])) { extension_Members::$_errors[$this->section->getFieldHandle('authentication')] = array('label' => $this->section->getField('authentication')->get('label'), 'type' => 'missing', 'message-id' => EventMessages::FIELD_MISSING, 'message' => __('%s is a required field.', array($this->section->getField('authentication')->get('label')))); } if (isset($data['username']) && empty($data['username'])) { extension_Members::$_errors[$this->section->getFieldHandle('identity')] = array('label' => $this->section->getField('identity')->get('label'), 'type' => 'missing', 'message-id' => EventMessages::FIELD_MISSING, 'message' => __('%s is a required field.', array($this->section->getField('identity')->get('label')))); } if (isset($data['email']) && empty($data['email'])) { extension_Members::$_errors[$this->section->getFieldHandle('email')] = array('label' => $this->section->getField('email')->get('label'), 'type' => 'missing', 'message-id' => EventMessages::FIELD_MISSING, 'message' => __('%s is a required field.', array($this->section->getField('email')->get('label')))); } else { if (!fieldMemberEmail::applyValidationRule($email)) { extension_Members::$_errors[$this->section->getFieldHandle('email')] = array('message' => __('\'%s\' contains invalid characters.', array($this->section->getField('email')->get('label'))), 'message-id' => EventMessages::FIELD_INVALID, 'type' => 'invalid', 'label' => $this->section->getField('email')->get('label')); return null; } } // If there is errors already, no point continuing, return false if (!empty(extension_Members::$_errors)) { return false; } if ($id = $this->findMemberIDFromCredentials($data, $isHashed)) { try { self::$member_id = $id; $this->initialiseCookie(); $this->initialiseMemberObject(); $this->cookie->set('id', $id); $this->cookie->set('members-section-id', $this->getMember()->get('section_id')); if (isset($username)) { $this->cookie->set('username', $data['username']); } else { $this->cookie->set('email', $data['email']); } $this->cookie->set('password', $this->getMember()->getData($this->section->getField('authentication')->get('id'), true)->password); self::$isLoggedIn = true; } catch (Exception $ex) { // Or do something else? throw new Exception($ex); } return true; } $this->logout(); return false; }