ResetShell.php 2.7 KB

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