Last active
December 21, 2015 08:39
-
-
Save csrui/6279811 to your computer and use it in GitHub Desktop.
CakePHP - Shell to reset / create a default admin user
This file contains hidden or 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 | |
/** | |
* 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' => '[email protected]', | |
'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