71 return $this->encryptor->getHash($this->data[self::KEY_PASSWORD],
true);
83 $adminId = $this->saveAdminUser();
84 $this->saveAdminUserRole($adminId);
95 private function saveAdminUser()
101 'password' => $passwordHash,
104 $result = $this->connection->fetchRow(
105 'SELECT user_id, username, email FROM ' . $this->getTableName(
'admin_user') .
' ' .
106 'WHERE username = :username OR email = :email',
107 [
'username' => $this->data[self::KEY_USER],
'email' => $this->data[self::KEY_EMAIL]],
115 $adminData[
'modified'] = date(
'Y-m-d H:i:s');
117 $this->connection->update(
118 $this->getTableName(
'admin_user'),
120 $this->connection->quoteInto(
'username = ?', $this->data[self::KEY_USER])
126 $this->connection->insert(
127 $this->getTableName(
'admin_user'),
130 $adminId = $this->connection->lastInsertId();
132 $this->trackPassword($adminId, $passwordHash);
144 private function trackPassword($adminId, $passwordHash)
146 $this->connection->insert(
147 $this->getTableName(
'admin_passwords'),
149 'user_id' => $adminId,
150 'password_hash' => $passwordHash,
151 'last_updated' =>
time()
166 if (empty($this->data[self::KEY_PASSWORD])) {
167 throw new \Exception(
168 '"Password" is required. Enter and try again.' 172 if (strcasecmp($this->data[self::KEY_PASSWORD], $this->data[self::KEY_USER]) == 0) {
173 throw new \Exception(
174 'Password cannot be the same as the user name.' 179 $result = $this->connection->fetchRow(
180 "SELECT user_id, username, email FROM {$this->getTableName('admin_user')} " 181 .
"WHERE username = :username OR email = :email",
182 [
'username' => $this->data[self::KEY_USER],
'email' => $this->data[self::KEY_EMAIL]]
189 $username =
$result[
'username'];
191 if ((strcasecmp(
$email, $this->data[self::KEY_EMAIL]) == 0) &&
192 (strcasecmp($username, $this->data[self::KEY_USER]) != 0)) {
194 throw new \Exception(
195 'An existing user has the given email but different username. ' 196 .
'Username and email both need to match an existing user or both be new.' 199 if ((strcasecmp($username, $this->data[self::KEY_USER]) == 0) &&
200 (strcasecmp(
$email, $this->data[self::KEY_EMAIL]) != 0)) {
202 throw new \Exception(
203 'An existing user has the given username but different email. ' 204 .
'Username and email both need to match an existing user or both be new.' 217 private function saveAdminUserRole($adminId)
219 $result = $this->connection->fetchRow(
220 'SELECT * FROM ' . $this->getTableName(
'authorization_role') .
' ' .
221 'WHERE user_id = :user_id AND user_type = :user_type',
227 'parent_id' => $this->retrieveAdministratorsRoleId(),
230 'user_id' => $adminId,
234 $this->connection->insert($this->getTableName(
'authorization_role'), $adminRoleData);
244 private function retrieveAdministratorsRoleId()
247 $administratorsRoleData = [
253 'role_name' =>
'Administrators',
255 $result = $this->connection->fetchRow(
256 'SELECT * FROM ' . $this->getTableName(
'authorization_role') .
' ' .
257 'WHERE parent_id = :parent_id AND tree_level = :tree_level AND role_type = :role_type AND ' .
258 'user_id = :user_id AND user_type = :user_type AND role_name = :role_name',
259 $administratorsRoleData
262 throw new \Exception(
'No Administrators role was found, data fixture needs to be run');
275 private function getTableName(
$table)
277 if (!empty($this->data[self::KEY_PREFIX])) {
278 return $this->connection->getTableName($this->data[self::KEY_PREFIX] .
$table);
281 return $this->connection->getTableName(
$table);
__construct(AdapterInterface $connection, EncryptorInterface $encryptor, array $data)