/** * Attempt to authenticate the user through a provider. * * @param AuthenticationProvider $authrequest The authetication request * @return Bool True on success */ static function authenticate($authrequest) { // Resolve the authentication backend $auth_class = User::getAuthenticationBackend(); // Assign the authentication backend to the request $authrequest->setAuthBackend($auth_class); if ($authrequest->isTokenValid()) { $authrequest->login(); return true; } return false; }
public function save() { if (!$this->uuid) { $this->uuid = uuid::v4(); } if (count($this->modified) > 0) { // Get a database reference $db = new DatabaseConnection(); // Determine what needs to be updated. $mtable = array('user' => false, 'userdata' => false, 'ambient' => false, 'credentials' => false); foreach ($this->modified as $mod) { switch ($mod) { case 'ambient': $mtable['ambient'] = true; break; case 'username': $mtable['user'] = true; break; case 'password': $mtable['credentials'] = true; break; case 'email': $mtable['user'] = true; break; case 'uuid': $mtable['user'] = true; break; case 'active': $mtable['user'] = true; break; case 'displayname': $mtable['userdata'] = true; break; case 'firstname': $mtable['userdata'] = true; break; case 'lastname': $mtable['userdata'] = true; break; case 'sex': $mtable['userdata'] = true; break; case 'country': $mtable['userdata'] = true; break; case 'flags': $mtable['user'] = true; break; case 'userid': break; default: throw new BadArgumentException("Unknown field modified: {$mod}"); } } $this->modified = array(); if (!$this->userid) { // Check to see if the username already exists if (user::find($this->username)) { throw new UserException("User already exists!"); } // Insert $ambient = serialize($this->ambient); $this->userid = $db->insertRow("INSERT INTO " . LEPTON_DB_PREFIX . "users (username,email,uuid,flags,active,registered) VALUES " . "(%s,%s,%s,%s,%d,NOW())", $this->username, $this->email, $this->uuid, $this->flags, $this->active ? 1 : 0); $db->updateRow("INSERT INTO " . LEPTON_DB_PREFIX . "userdata (displayname,firstname,lastname,sex,country,ambient,id) VALUES " . "(%s,%s,%s,%s,%s,%s,%d)", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $ambient, $this->userid); // Update credentials $backend = User::getAuthenticationBackend(); $backend->assignCredentials($this); } else { // Update if ($mtable['ambient'] && $mtable['userdata']) { // Update complete userdata table $ambient = serialize($this->ambient); $db->updateRow("Update " . LEPTON_DB_PREFIX . "userdata SET displayname=%s,firstname=%s,lastname=%s,sex=%s,country=%s,ambient=%s WHERE id=%d", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $ambient, $this->userid); } elseif ($mtable['ambient']) { // Update the ambient column $ambient = serialize($this->ambient); $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "userdata SET ambient=%s WHERE id=%d ", $ambient, $this->userid); } elseif ($mtable['userdata']) { // Update the userdata columns $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "userdata SET displayname=%s,firstname=%s,lastname=%s,sex=%s,country=%s WHERE id=%d", $this->displayname, $this->firstname, $this->lastname, $this->sex, $this->country, $this->userid); } if ($mtable['user']) { // Update users table $db->updateRow("UPDATE " . LEPTON_DB_PREFIX . "users SET username=%s,email=%s,uuid=%s,flags=%s,active=%s WHERE id=%d", $this->username, $this->email, $this->uuid, $this->flags, $this->active ? 1 : 0, $this->userid); } if ($mtable['credentials']) { // Update credentials $backend = User::getAuthenticationBackend(); $backend->assignCredentials($this); } } } return true; }