Creating a Custom Shell for Adding Users for Use With AuthComponent

This article is also available in the following languages:
By 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:

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

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


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:

<?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:


cake/console/cake create_user

Enjoy!

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

Comments

  • Posted 02/11/10 09:04:12 AM
    Very handy - brilliantly simple! Thanks!
  • Posted 10/29/08 11:21:29 AM
    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 10/28/08 02:42:49 PM
    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 02/05/09 02:42:56 PM
      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?

Comments are closed for articles over a year old