ResetShell.php 2.8 KB

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