UserShell.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. if (!defined('CLASS_USER')) {
  3. define('CLASS_USER', 'User');
  4. }
  5. App::uses('AppShell', 'Console/Command');
  6. App::uses('ComponentCollection', 'Controller');
  7. //App::uses('AuthExtComponent', 'Tools.Controller/Component');
  8. /**
  9. * create a new user from CLI
  10. *
  11. * @cakephp 2.x
  12. * @author Mark Scherer
  13. * @license MIT
  14. * 2011-11-05 ms
  15. */
  16. class UserShell extends AppShell {
  17. public $tasks = array();
  18. public $uses = array(CLASS_USER);
  19. //TODO: refactor (smaller sub-parts)
  20. public function main() {
  21. if (App::import('Component', 'AuthExt') && class_exists('AuthExtComponent')) {
  22. $this->Auth = new AuthExtComponent(new ComponentCollection());
  23. } else {
  24. App::import('Component', 'Auth');
  25. $this->Auth = new AuthComponent(new ComponentCollection());
  26. }
  27. while (empty($username)) {
  28. $username = $this->in(__('Username (2 characters at least)'));
  29. }
  30. while (empty($password)) {
  31. $password = $this->in(__('Password (2 characters at least)'));
  32. }
  33. $schema = $this->User->schema();
  34. if (isset($this->User->Role) && is_object($this->User->Role)) {
  35. $roles = $this->User->Role->find('list');
  36. if (!empty($roles)) {
  37. $this->out('');
  38. pr($roles);
  39. }
  40. $roleIds = array_keys($roles);
  41. while (!empty($roles) && empty($role)) {
  42. $role = $this->in(__('Role'), $roleIds);
  43. }
  44. } elseif (method_exists($this->User, 'roles')) {
  45. $roles = User::roles();
  46. if (!empty($roles)) {
  47. $this->out('');
  48. pr ($roles);
  49. }
  50. $roleIds = array_keys($roles);
  51. while (!empty($roles) && empty($role)) {
  52. $role = $this->in(__('Role'), $roleIds);
  53. }
  54. }
  55. if (empty($roles)) {
  56. $this->out('No Role found (either no table, or no data)');
  57. $role = $this->in(__('Please insert a role manually'));
  58. }
  59. $this->out('');
  60. $pwd = $this->Auth->password($password);
  61. $data = array('User'=>array(
  62. 'password' => $pwd,
  63. 'active' => 1
  64. ));
  65. if (!empty($username)) {
  66. $data['User']['username'] = $username;
  67. }
  68. if (!empty($email)) {
  69. $data['User']['email'] = $email;
  70. }
  71. if (!empty($role)) {
  72. $data['User']['role_id'] = $role;
  73. }
  74. if (!empty($schema['status']) && method_exists('User', 'statuses')) {
  75. $statuses = User::statuses();
  76. pr($statuses);
  77. while (empty($status)) {
  78. $status = $this->in(__('Please insert a status'), array_keys($statuses));
  79. }
  80. $data['User']['status'] = $status;
  81. }
  82. if (!empty($schema['email'])) {
  83. $provideEmail = $this->in(__('Provide Email? '),array('y', 'n'), 'n');
  84. if ($provideEmail === 'y') {
  85. $email = $this->in(__('Please insert an email'));
  86. $data['User']['email'] = $email;
  87. }
  88. if (!empty($schema['email_confirmed'])) {
  89. $data['User']['email_confirmed'] = 1;
  90. }
  91. }
  92. $this->out('');
  93. $continue = $this->in(__('Continue? '), array('y', 'n'), 'n');
  94. if ($continue != 'y') {
  95. $this->error('Not Executed!');
  96. }
  97. $this->out('');
  98. $this->hr();
  99. if ($this->User->save($data)) {
  100. $this->out('User inserted! ID: '.$this->User->id);
  101. } else {
  102. $this->error('User could not be inserted ('.print_r($this->User->validationErrors, true).')');
  103. }
  104. }
  105. }