Skip to content
Snippets Groups Projects
Commit fd13cd2b authored by Ere Maijala's avatar Ere Maijala Committed by Demian Katz
Browse files

More efficient user creation for database auth.

parent bfc71a91
No related merge requests found
...@@ -141,23 +141,18 @@ class Database extends AbstractBase ...@@ -141,23 +141,18 @@ class Database extends AbstractBase
} }
// If we got this far, we're ready to create the account: // If we got this far, we're ready to create the account:
$data = [ $user = $table->createRowForUsername($params['username']);
'username' => $params['username'], $user->firstname = $params['firstname'];
'firstname' => $params['firstname'], $user->lastname = $params['lastname'];
'lastname' => $params['lastname'], $user->email = $params['email'];
'email' => $params['email'],
'created' => date('Y-m-d H:i:s')
];
if ($this->passwordHashingEnabled()) { if ($this->passwordHashingEnabled()) {
$bcrypt = new Bcrypt(); $bcrypt = new Bcrypt();
$data['pass_hash'] = $bcrypt->create($params['password']); $user->pass_hash = $bcrypt->create($params['password']);
} else { } else {
$data['password'] = $params['password']; $user->password = $params['password'];
} }
// Create the row and send it back to the caller: $user->save();
$table->insert($data); return $user;
return $table->getByUsername($params['username'], false);
} }
/** /**
......
...@@ -57,8 +57,23 @@ class User extends Gateway ...@@ -57,8 +57,23 @@ class User extends Gateway
} }
/** /**
* Retrieve a user object from the database based on username; create a new * Create a row for the specified username.
* row if no existing match is found. *
* @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 string $username Username to use for retrieval.
* @param bool $create Should we create users that don't already exist? * @param bool $create Should we create users that don't already exist?
...@@ -68,12 +83,8 @@ class User extends Gateway ...@@ -68,12 +83,8 @@ class User extends Gateway
public function getByUsername($username, $create = true) public function getByUsername($username, $create = true)
{ {
$row = $this->select(['username' => $username])->current(); $row = $this->select(['username' => $username])->current();
if ($create && empty($row)) { return ($create && empty($row))
$row = $this->createRow(); ? $this->createRowForUsername($username) : $row;
$row->username = $username;
$row->created = date('Y-m-d H:i:s');
}
return $row;
} }
/** /**
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment