Creating a Custom Shell for Adding Users for Use With AuthComponent

By Johannes Fahrenkrug (jfahrenkrug)
AuthComponent is great and there are tutorials for letting users register an account. But what if you just want a convenient way to add a few admin users? This custom shell will help you!
The AuthComponent guide at http://manual.cakephp.org/view/172/authentication is very useful. Read it. I have a simple admin tool that uses AuthComponent. I also want a convenient way to create admin users, though and I surely don't want to allow everyone to register their own admin account. So I wrote a custom shell. This shell assumes that you have a User model like this:

Model Class:

Download code <?php 
class User extends AppModel {
    var 
$name 'User';
}
?>

It should be backed by a database table named 'users' with this layout:

Download code
CREATE TABLE users (   
  id integer auto_increment,    
  username char(50),    
  password char(50),    
  PRIMARY KEY (id)
);

To create users, put this custom shell code into the file app/vendors/shells/create_user.php:
Download code
<?php 
class CreateUserShell extends Shell {
    var 
$uses = array('User');

    function 
main() {
        
App::import('Component','Auth');
        
$this->Auth = new AuthComponent(null);
      
        
$this->out('Create Admin User:');
        
$this->hr();
        
        while (empty(
$username)) {
          
$username $this->in('Username:');
          if (empty(
$username)) $this->out('Username must not be empty!');
        }
        
        while (empty(
$pwd1)) {
          
$pwd1 $this->in('Password:');
          if (empty(
$pwd1)) $this->out('Password must not be empty!');
        }
        
        while (empty(
$pwd2)) {
          
$pwd2 $this->in('Password Confirmation:');
          if (
$pwd1 !== $pwd2) {
            
$this->out('Passwort and confirmation do not match!');
            
$pwd2 NULL;
          }
        }
        
        
// we got all the data, let's create the user        
        
$this->User->create();
              if (
$this->User->save(array('username' => $username'password' => $this->Auth->password($pwd1)))) {
                  
$this->out('Admin User created successfully!');
              } else {
                  
$this->out('ERROR while creating the Admin User!!!');
              }
    }
}
?>

That's it, now you can run your spiffy new shell script like so:

Download code
cake/console/cake create_user

Enjoy!

For more code goodness, visit my blog at http://blog.springenwerk.com.

 

Comments 763

CakePHP Team Comments Author Comments
 

Comment

1 running shell doesn't work for me

Hi,

I have created a test shell called report.php in my /vendors/shells directory and it contains:
class ReportShell extends Shell {
function main()
{
$this->out('hello');
}
}
?>
But when I try to do:
[root@www shells]# cake report

All I get is the default cake shell output:
Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
Current Paths:
-app: shells
-working: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors/shells
-root: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors
-core: /usr/lib/cake/

Changing Paths:
your working path should be the same as your application path
to change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp

Available Shells:

cake/console/libs/:
testsuite
api
acl
bake
schema
console
i18n

To run a command, type 'cake shell_name [args]'
To get help on a specific command, type 'cake shell_name help'


What am I doing wrong? Please help. Thanks!

Posted Oct 28, 2008 by Charlotte Gapasin
 

Comment

2 Available Shells

How do I add my /vendors/shells to my Available Shells?

All I get is the default cake shell output:
Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
Current Paths:
-app: shells
-working: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors/shells
-root: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors
-core: /usr/lib/cake/

Changing Paths:
your working path should be the same as your application path
to change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp

Available Shells:

cake/console/libs/:
testsuite
api
acl
bake
schema
console
i18n

To run a command, type 'cake shell_name [args]'
To get help on a specific command, type 'cake shell_name help'
Posted Oct 29, 2008 by Charlotte Gapasin
 

Comment

3 Have you tried

Welcome to CakePHP v1.2.0.7296 RC2 Console
---------------------------------------------------------------
Current Paths:
-app: shells
-working: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors/shells
-root: /var/www/vhosts/ninthlink.com/httpdocs/pdf/pdfadmin/vendors
-core: /usr/lib/cake/

Changing Paths:
your working path should be the same as your application path
to change your path use the '-app' param.
Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp

Have you tried invoking cake using an absolute path and a reference to you app dir?
Posted Feb 5, 2009 by MrWilliams