Last active
August 20, 2019 20:05
-
-
Save MayMeow/c03b0cda19e0703bede81190fa13c56e to your computer and use it in GitHub Desktop.
Creating default users and groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Migrations\AbstractMigration; | |
class CreateDefaultUser extends AbstractMigration | |
{ | |
/** | |
* Change Method. | |
* | |
* More information on this method is available here: | |
* http://docs.phinx.org/en/latest/migrations.html#the-change-method | |
* @return void | |
*/ | |
public function up() | |
{ | |
$adminGroupUUID = \Cake\Utility\Text::uuid(); | |
$api_key_plain = \Cake\Utility\Security::hash(\Cake\Utility\Security::randomBytes(32), 'sha256', false); | |
$table = $this->table('cake_php_roles'); | |
$table->truncate(); | |
$data = [ | |
[ | |
'name' => \Identity\Factories\RoleFactory::ADMINISTRATORS_ROLE, | |
'description' => 'Default Administrators Group', | |
'uuid' => $adminGroupUUID | |
], | |
[ | |
'name' => \Identity\Factories\RoleFactory::OPERATORS_ROLE, | |
'description' => 'Default Operators Group', | |
'uuid' => \Cake\Utility\Text::uuid(), | |
], | |
[ | |
'name' => \Identity\Factories\RoleFactory::USERS_ROLE, | |
'description' => 'Default Users Group', | |
'uuid' => \Cake\Utility\Text::uuid(), | |
] | |
]; | |
$table->insert($data)->save(); | |
$groups = \Cake\ORM\TableRegistry::getTableLocator()->get('cake_php_roles'); | |
$groupID = $groups->find()->where(['uuid' => $adminGroupUUID])->select(['id'])->first(); | |
$userData = [ | |
'email' => '[email protected]', | |
'password' => (new \Cake\Auth\DefaultPasswordHasher())->hash('pa$$word'), | |
'role_id' => $groupID->id, | |
'created' => \Cake\I18n\FrozenTime::now()->toDateTimeString(), //updated because has problem with mysql database timezone | |
'modified' => \Cake\I18n\FrozenTime::now()->toDateTimeString(), | |
'api_key_plain' => $api_key_plain, | |
'api_key' => (new \Cake\Auth\DefaultPasswordHasher())->hash($api_key_plain) | |
]; | |
$userTable = $this->table('cake_php_users'); | |
$userTable->truncate(); | |
$userTable->insert($userData)->save(); | |
} | |
public function down() | |
{ | |
$table = $this->table('cake_php_roles'); | |
$table->truncate(); | |
$userTable = $this->table('cake_php_users'); | |
$userTable->truncate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment