From fd13cd2b5b3b7f04863bda06c712abf3ff0acc7e Mon Sep 17 00:00:00 2001 From: Ere Maijala <ere.maijala@helsinki.fi> Date: Wed, 25 Mar 2015 12:53:46 -0400 Subject: [PATCH] More efficient user creation for database auth. --- module/VuFind/src/VuFind/Auth/Database.php | 21 +++++++---------- module/VuFind/src/VuFind/Db/Table/User.php | 27 +++++++++++++++------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/module/VuFind/src/VuFind/Auth/Database.php b/module/VuFind/src/VuFind/Auth/Database.php index 8d930d16eba..f350266aa99 100644 --- a/module/VuFind/src/VuFind/Auth/Database.php +++ b/module/VuFind/src/VuFind/Auth/Database.php @@ -141,23 +141,18 @@ class Database extends AbstractBase } // If we got this far, we're ready to create the account: - $data = [ - 'username' => $params['username'], - 'firstname' => $params['firstname'], - 'lastname' => $params['lastname'], - 'email' => $params['email'], - 'created' => date('Y-m-d H:i:s') - ]; - + $user = $table->createRowForUsername($params['username']); + $user->firstname = $params['firstname']; + $user->lastname = $params['lastname']; + $user->email = $params['email']; if ($this->passwordHashingEnabled()) { $bcrypt = new Bcrypt(); - $data['pass_hash'] = $bcrypt->create($params['password']); + $user->pass_hash = $bcrypt->create($params['password']); } else { - $data['password'] = $params['password']; + $user->password = $params['password']; } - // Create the row and send it back to the caller: - $table->insert($data); - return $table->getByUsername($params['username'], false); + $user->save(); + return $user; } /** diff --git a/module/VuFind/src/VuFind/Db/Table/User.php b/module/VuFind/src/VuFind/Db/Table/User.php index 08c893d533c..9444c836e07 100644 --- a/module/VuFind/src/VuFind/Db/Table/User.php +++ b/module/VuFind/src/VuFind/Db/Table/User.php @@ -57,8 +57,23 @@ class User extends Gateway } /** - * Retrieve a user object from the database based on username; create a new - * row if no existing match is found. + * Create a row for the specified username. + * + * @param string $username Username to use for retrieval. + * + * @return UserRow + */ + public function createRowForUsername($username) + { + $row = $this->createRow(); + $row->username = $username; + $row->created = date('Y-m-d H:i:s'); + return $row; + } + + /** + * Retrieve a user object from the database based on username; when requested, + * create a new row if no existing match is found. * * @param string $username Username to use for retrieval. * @param bool $create Should we create users that don't already exist? @@ -68,12 +83,8 @@ class User extends Gateway public function getByUsername($username, $create = true) { $row = $this->select(['username' => $username])->current(); - if ($create && empty($row)) { - $row = $this->createRow(); - $row->username = $username; - $row->created = date('Y-m-d H:i:s'); - } - return $row; + return ($create && empty($row)) + ? $this->createRowForUsername($username) : $row; } /** -- GitLab