ResetShell.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. # enhancement for plugin user model
  3. if (!defined('CLASS_USER')) {
  4. define('CLASS_USER', 'User');
  5. }
  6. App::uses('AppShell', 'Console/Command');
  7. /**
  8. * Reset user data
  9. *
  10. * @cakephp 2.x
  11. * @author Mark Scherer
  12. * @license MIT
  13. */
  14. class ResetShell extends AppShell {
  15. public $tasks = array();
  16. //public $uses = array('User');
  17. public $Auth = null;
  18. public function main() {
  19. $this->help();
  20. }
  21. /**
  22. * Reset all emails - e.g. your admin email (for local development)
  23. */
  24. public function email() {
  25. $this->out('email:');
  26. App::uses('Validation', 'Utility');
  27. while (empty($email) || !Validation::email($email)) {
  28. $email = $this->in(__('New email address (must have a valid form at least)'));
  29. }
  30. $this->User = ClassRegistry::init(CLASS_USER);
  31. if (!$this->User->hasField('email')) {
  32. return $this->error(CLASS_USER.' model doesnt have an email field!');
  33. }
  34. $this->hr();
  35. $this->out('resetting...');
  36. Configure::write('debug', 2);
  37. $this->User->recursive = -1;
  38. $this->User->updateAll(array('User.email'=>'\''.$email.'\''), array('User.email !='=>$email));
  39. $count = $this->User->getAffectedRows();
  40. $this->out($count.' emails resetted - DONE');
  41. }
  42. /**
  43. * Reset all pwds to a simply pwd (for local development)
  44. */
  45. public function pwd() {
  46. $components = array('Tools.AuthExt', 'Auth');
  47. foreach ($components as $component) {
  48. if (App::import('Component', $component)) {
  49. $component .= 'Component';
  50. list($plugin, $component) = pluginSplit($component);
  51. $this->Auth = new $component(new ComponentCollection());
  52. break;
  53. }
  54. }
  55. if (!is_object($this->Auth)) {
  56. return $this->error('No Auth Component found');
  57. }
  58. $this->out('Using: '.get_class($this->Auth).' (Abort with STRG+C)');
  59. if (!empty($this->args[0]) && mb_strlen($this->args[0]) >= 2) {
  60. $pwToHash = $this->args[0];
  61. }
  62. while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
  63. $pwToHash = $this->in(__('Password to Hash (2 characters at least)'));
  64. }
  65. $this->hr();
  66. $this->out('pwd:');
  67. $this->out($pwToHash);
  68. $pw = $this->Auth->password($pwToHash);
  69. $this->hr();
  70. $this->out('hash:');
  71. $this->out($pw);
  72. $this->hr();
  73. $this->out('resetting...');
  74. $this->User = ClassRegistry::init(CLASS_USER);
  75. if (!$this->User->hasField('password')) {
  76. return $this->error(CLASS_USER.' model doesnt have a password field!');
  77. }
  78. if (method_exists($this->User, 'escapeValue')) {
  79. $newPwd = $this->User->escapeValue($pw);
  80. } else {
  81. $newPwd = '\''.$pw.'\'';
  82. }
  83. $this->User->recursive = -1;
  84. $this->User->updateAll(array('password'=>$newPwd), array('password !='=>$pw));
  85. $count = $this->User->getAffectedRows();
  86. $this->out($count.' pwds resetted - DONE');
  87. }
  88. public function help() {
  89. $this->out('-- pwd: Hash and Reset all user passwords with Auth(Ext) Component --');
  90. $this->out('-- email: Reset all user emails --');
  91. }
  92. }