UserShell.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. */
  15. class UserShell extends AppShell {
  16. public $tasks = array();
  17. public $uses = array(CLASS_USER);
  18. //TODO: refactor (smaller sub-parts)
  19. public function main() {
  20. if (App::import('Component', 'AuthExt') && class_exists('AuthExtComponent')) {
  21. $this->Auth = new AuthExtComponent(new ComponentCollection());
  22. } else {
  23. App::import('Component', 'Auth');
  24. $this->Auth = new AuthComponent(new ComponentCollection());
  25. }
  26. //ConnectionManager::sourceList()
  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. $this->out(print_r($roles, true));
  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. $this->out(print_r($roles, true));
  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. $usernameField = $this->User->displayField; //'username';
  67. $data['User'][$usernameField] = $username;
  68. }
  69. if (!empty($email)) {
  70. $data['User']['email'] = $email;
  71. }
  72. if (!empty($role)) {
  73. $data['User']['role_id'] = $role;
  74. }
  75. if (!empty($schema['status']) && method_exists('User', 'statuses')) {
  76. $statuses = User::statuses();
  77. $this->out(print_r($statuses, true));
  78. $status = $this->in(__('Please insert a status'), array_keys($statuses));
  79. $data['User']['status'] = $status;
  80. }
  81. if (!empty($schema['email'])) {
  82. $provideEmail = $this->in(__('Provide Email? '), array('y', 'n'), 'n');
  83. if ($provideEmail === 'y') {
  84. $email = $this->in(__('Please insert an email'));
  85. $data['User']['email'] = $email;
  86. }
  87. if (!empty($schema['email_confirmed'])) {
  88. $data['User']['email_confirmed'] = 1;
  89. }
  90. }
  91. $this->out('');
  92. $continue = $this->in(__('Continue? '), array('y', 'n'), 'n');
  93. if ($continue !== 'y') {
  94. return $this->error('Not Executed!');
  95. }
  96. $this->out('');
  97. $this->hr();
  98. if ($this->User->save($data)) {
  99. $this->out('User inserted! ID: '.$this->User->id);
  100. } else {
  101. return $this->error('User could not be inserted ('.print_r($this->User->validationErrors, true).')');
  102. }
  103. }
  104. }