Skip to content

Instantly share code, notes, and snippets.

@csrui
Last active December 21, 2015 08:39
Show Gist options
  • Save csrui/6279811 to your computer and use it in GitHub Desktop.
Save csrui/6279811 to your computer and use it in GitHub Desktop.
CakePHP - Shell to reset / create a default admin user
<?php
/**
* Shell to reset / create a default admin user
*
* @source https://gist.github.com/csrui/6279811
* @url http://book.cakephp.org/2.0/en/console-and-shells.html
**/
class UserShell extends AppShell {
public $uses = array('User');
public function main() {
if ($this->in('Are you sure you want to reset admin account? [Y/n]', array('Y', 'n'), 'n') == 'Y') {
$this->reset_admin();
}
}
public function reset_admin() {
$this->out('Reseting Admin user...');
if (!method_exists($this->User, 'initDefaultUser')) {
$this->out('Model User doesn\'t have method User::initDefaultUser');
if ($this->in('Do you want to use default? [Y/n]', array('Y', 'n'), 'n') == 'Y') {
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
$passwordHasher = new SimplePasswordHasher();
$result = $this->User->save(array(
'id' => 1,
'username' => 'admin',
'email' => 'admin@domain.com',
'password' => $passwordHasher->hash('admin')
));
}
} else {
$result = $this->User->initDefaultUser();
}
# Output operation success
if ($result) {
$this->out('Done!');
} else {
$this->out('Failed.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment