If you have a fresh setup of your app and no users in the database, its not easy to "register" a new one (with the admin role and everything).
The other case would be if you changed the security salt. Now you need new passwords, as well.
The second part is not yet covered by my script – the first one is, though.
The idea is to insert an admin user in order to login for the first time. All you need is a name, an email and a password.
<?php
class UserShell extends Shell {
var $tasks = array();
var $uses = array('User');
//TODO: refactor (smaller sub-parts)
function main() {
if (App::import('Component','AuthExt')) {
$this->Auth = new AuthExtComponent();
} else {
App::import('Component','Auth');
$this->Auth = new AuthComponent();
}
while (empty($username)) {
$continue = $this->in(__('Set \'username\'?', true),array('y', 'n', 'q'), 'y');
if ($continue == 'q') { die('Abort'); } elseif ($continue == 'n') { break; }
$username = $this->in(__('Username (2 characters at least)', true));
}
while (empty($password)) {
$password = $this->in(__('Password (2 characters at least)', true));
}
if (isset($this->User->Role) && is_object($this->User->Role)) {
$roles = $this->User->Role->find('list');
if (!empty($roles)) {
$this->out('');
pr ($roles);
}
$roleIds = array_keys($roles);
while (!empty($roles) && empty($role)) {
$role = $this->in(__('Role', true), $roleIds);
}
} elseif (method_exists($this->User, 'roles')) {
$roles = User::roles();
if (!empty($roles)) {
$this->out('');
pr ($roles);
}
$roleIds = array_keys($roles);
while (!empty($roles) && empty($role)) {
$role = $this->in(__('Role', true), $roleIds);
}
}
if (empty($roles)) {
$this->out('No Role found (either no table, or no data)');
$role = $this->in(__('Please insert a role manually', true));
}
$this->out('');
$pwd = $this->Auth->password($password);
$data = array('User'=>array(
'password' => $pwd,
'active' => 1
));
if (!empty($username)) {
$data['User']['username'] = $username;
}
if (!empty($email)) {
$data['User']['email'] = $email;
}
if (!empty($role)) {
$data['User']['role_id'] = $role;
}
$this->out('');
pr ($data);
$this->out('');
$this->out('');
$continue = $this->in(__('Continue? ', true),array('y', 'n'), 'n');
if ($continue != 'y') {
die('Not Executed!');
}
$this->out('');
$this->hr();
if ($this->User->save($data)) {
$this->out('User inserted! ID: '.$this->User->id);
} else {
$this->out('User could not be inserted (email, nick duplicate!!!)');
}
}
}
?>
Notes
The code obviously needs some refactoring. And its written for my applications – it might not work out of the box with other cake apps. Maybe we could make it more generic, as well.
It assumes that you have either AuthExt or Auth running for Authentication, that you have a User and a Role model and that the fields are "username", "email", "password" and "role_id". Additionally it sets "active" to 1 – this field is checked in my login procedure.
But this script should be fairly easily adjustable for your needs.
Ideas for further improvement are:
- user add (for adding)
- user edit (for editing specific user/password)
- user reset (to reset all passwords to 123 or whatever)