UserShell.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //ConnectionManager::sourceList()
  28. while (empty($username)) {
  29. $username = $this->in(__('Username (2 characters at least)'));
  30. }
  31. while (empty($password)) {
  32. $password = $this->in(__('Password (2 characters at least)'));
  33. }
  34. $schema = $this->User->schema();
  35. if (isset($this->User->Role) && is_object($this->User->Role)) {
  36. $roles = $this->User->Role->find('list');
  37. if (!empty($roles)) {
  38. $this->out('');
  39. $this->out(print_r($roles, true));
  40. }
  41. $roleIds = array_keys($roles);
  42. while (!empty($roles) && empty($role)) {
  43. $role = $this->in(__('Role'), $roleIds);
  44. }
  45. } elseif (method_exists($this->User, 'roles')) {
  46. $roles = User::roles();
  47. if (!empty($roles)) {
  48. $this->out('');
  49. $this->out(print_r($roles, true));
  50. }
  51. $roleIds = array_keys($roles);
  52. while (!empty($roles) && empty($role)) {
  53. $role = $this->in(__('Role'), $roleIds);
  54. }
  55. }
  56. if (empty($roles)) {
  57. $this->out('No Role found (either no table, or no data)');
  58. $role = $this->in(__('Please insert a role manually'));
  59. }
  60. $this->out('');
  61. $pwd = $this->Auth->password($password);
  62. $data = array('User'=>array(
  63. 'password' => $pwd,
  64. 'active' => 1
  65. ));
  66. if (!empty($username)) {
  67. $usernameField = $this->User->displayField; //'username';
  68. $data['User'][$usernameField] = $username;
  69. }
  70. if (!empty($email)) {
  71. $data['User']['email'] = $email;
  72. }
  73. if (!empty($role)) {
  74. $data['User']['role_id'] = $role;
  75. }
  76. if (!empty($schema['status']) && method_exists('User', 'statuses')) {
  77. $statuses = User::statuses();
  78. $this->out(print_r($statuses, true));
  79. while (empty($status)) {
  80. $status = $this->in(__('Please insert a status'), array_keys($statuses));
  81. }
  82. $data['User']['status'] = $status;
  83. }
  84. if (!empty($schema['email'])) {
  85. $provideEmail = $this->in(__('Provide Email? '), array('y', 'n'), 'n');
  86. if ($provideEmail === 'y') {
  87. $email = $this->in(__('Please insert an email'));
  88. $data['User']['email'] = $email;
  89. }
  90. if (!empty($schema['email_confirmed'])) {
  91. $data['User']['email_confirmed'] = 1;
  92. }
  93. }
  94. $this->out('');
  95. $continue = $this->in(__('Continue? '), array('y', 'n'), 'n');
  96. if ($continue != 'y') {
  97. $this->error('Not Executed!');
  98. }
  99. $this->out('');
  100. $this->hr();
  101. if ($this->User->save($data)) {
  102. $this->out('User inserted! ID: '.$this->User->id);
  103. } else {
  104. $this->error('User could not be inserted ('.print_r($this->User->validationErrors, true).')');
  105. }
  106. }
  107. }