CakePHP Hash Console
Mar 01, 2010
- under:
- cakephp
A cakephp shell for generating a hash using your applications salt. Just drop it in and output and/or save a new user from the commandline.
1. Create the file /app/vendors/shells/hash.php in your app and paste the below in:
Show Plain Text- <?php
- /**
- * @desc simple shell for generating and storing a user's md5 credentials
- * @tutorial add hash.php to app/vendors/shells and run /cake/console/cake hash
- * @author joshskeen josh@joshskeen.com
- *
- */
- class HashShell extends Shell{
- var $username_field = 'username';
- var $password_field = 'password';
- var $usermodel = 'User';
- function main(){
- $salt = Configure::read('Security.salt');
- App::import('Security');
- $pass_to_hash = $this->in("enter password to hash:");
- $this->out('HASH -> ' . $hash, "\n");
- $write_to_db = $this->in("write to database? [y/n default = n]");
- if($this->filterInput($write_to_db, false))
- {
- $this->write_to_db($hash);
- }
- }
- function write_to_db($hash){
- $username = $this->in("enter username for new user [default = 'admin']");
- if($username == ''){
- $username = 'admin';
- }
- $user_model_name = $this->in('User model name? [default = User]');
- if($user_model_name != ''){
- $this->usermodel = $user_model_name;
- }
- App::import('Model', $this->usermodel);
- $usermodel = new $this->usermodel;
- $assume_fieldnames = $this->in('assume username and password for name and hashed pwd fields? [y/n default = y]');
- if(!$this->filterInput($assume_fieldnames, true)){
- $this->username_field = $this->in('user name field:');
- $this->password_field = $this->in('password field:');
- }
- $this->data[$this->usermodel][$this->username_field] = $username;
- $this->data[$this->usermodel][$this->password_field] = $hash;
- if($usermodel->save($this->data))
- {
- $this->out('save completed. Exiting...');
- }else
- {
- $retry = $this->in('Unexpected error. Retry? [y/n default = n]');
- if($this->filterInput($retry, false)){
- $this->main();
- }
- }
- }
- function filterInput($input, $default = false){
- if($input != ''){
- if($input == 'y' || $input == 'yes'){
- return true;
- }else{
- return false;
- }
- }else
- {
- return $default;
- }
- }
- }
2. running /cake/console/cake hash will generate the hash based on your app's salt, and store a user with the hash in the db optionally.