Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions
AdminAccount Class Reference

Public Member Functions

 __construct (AdapterInterface $connection, EncryptorInterface $encryptor, array $data)
 
 save ()
 
 validateUserMatches ()
 

Data Fields

const KEY_USER = 'admin-user'
 
const KEY_PASSWORD = 'admin-password'
 
const KEY_EMAIL = 'admin-email'
 
const KEY_FIRST_NAME = 'admin-firstname'
 
const KEY_LAST_NAME = 'admin-lastname'
 
const KEY_PREFIX = 'db-prefix'
 

Protected Member Functions

 generatePassword ()
 

Detailed Description

Definition at line 15 of file AdminAccount.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( AdapterInterface  $connection,
EncryptorInterface  $encryptor,
array  $data 
)

Default Constructor

Parameters
AdapterInterface$connection
EncryptorInterface$encryptor
array$data

Definition at line 54 of file AdminAccount.php.

58  {
59  $this->connection = $connection;
60  $this->encryptor = $encryptor;
61  $this->data = $data;
62  }
$connection
Definition: bulk.php:13

Member Function Documentation

◆ generatePassword()

generatePassword ( )
protected

Generate password string

Returns
string

Definition at line 69 of file AdminAccount.php.

70  {
71  return $this->encryptor->getHash($this->data[self::KEY_PASSWORD], true);
72  }

◆ save()

save ( )

Save administrator account and user role to DB.

If the administrator account exists, update it.

Returns
void

Definition at line 81 of file AdminAccount.php.

82  {
83  $adminId = $this->saveAdminUser();
84  $this->saveAdminUserRole($adminId);
85  }

◆ validateUserMatches()

validateUserMatches ( )

Validates that the username and email both match the user, and that password exists and is different from user name.

Returns
void
Exceptions

Definition at line 164 of file AdminAccount.php.

165  {
166  if (empty($this->data[self::KEY_PASSWORD])) {
167  throw new \Exception(
168  '"Password" is required. Enter and try again.'
169  );
170  }
171 
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.'
175  );
176  }
177 
178  try {
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]]
183  );
184  } catch (\Exception $e) {
185  return; // New installation, no need to validate existing users.
186  }
187 
188  $email = $result['email'];
189  $username = $result['username'];
190 
191  if ((strcasecmp($email, $this->data[self::KEY_EMAIL]) == 0) &&
192  (strcasecmp($username, $this->data[self::KEY_USER]) != 0)) {
193  // email matched but username did not
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.'
197  );
198  }
199  if ((strcasecmp($username, $this->data[self::KEY_USER]) == 0) &&
200  (strcasecmp($email, $this->data[self::KEY_EMAIL]) != 0)) {
201  // username matched but email did not
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.'
205  );
206  }
207  }
$email
Definition: details.phtml:13

Field Documentation

◆ KEY_EMAIL

const KEY_EMAIL = 'admin-email'

Definition at line 22 of file AdminAccount.php.

◆ KEY_FIRST_NAME

const KEY_FIRST_NAME = 'admin-firstname'

Definition at line 23 of file AdminAccount.php.

◆ KEY_LAST_NAME

const KEY_LAST_NAME = 'admin-lastname'

Definition at line 24 of file AdminAccount.php.

◆ KEY_PASSWORD

const KEY_PASSWORD = 'admin-password'

Definition at line 21 of file AdminAccount.php.

◆ KEY_PREFIX

const KEY_PREFIX = 'db-prefix'

Definition at line 25 of file AdminAccount.php.

◆ KEY_USER

const KEY_USER = 'admin-user'

#+ Data keys

Definition at line 20 of file AdminAccount.php.


The documentation for this class was generated from the following file: