UserShell.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /**
  8. * Create a new user from CLI
  9. *
  10. * @cakephp 2.x
  11. * @author Mark Scherer
  12. * @license MIT
  13. */
  14. class UserShell extends AppShell {
  15. public $uses = array(CLASS_USER);
  16. /**
  17. * UserShell::main()
  18. * //TODO: refactor (smaller sub-parts)
  19. *
  20. * @return void
  21. */
  22. public function main() {
  23. if (App::import('Component', 'Tools.AuthExt') && class_exists('AuthExtComponent')) {
  24. $this->Auth = new AuthExtComponent(new ComponentCollection());
  25. } else {
  26. App::import('Component', 'Auth');
  27. $this->Auth = new AuthComponent(new ComponentCollection());
  28. }
  29. while (empty($username)) {
  30. $username = $this->in(__('Username (2 characters at least)'));
  31. }
  32. while (empty($password)) {
  33. $password = $this->in(__('Password (2 characters at least)'));
  34. }
  35. $schema = $this->User->schema();
  36. if (isset($this->User->Role) && is_object($this->User->Role)) {
  37. $roles = $this->User->Role->find('list');
  38. if (!empty($roles)) {
  39. $this->out('');
  40. $this->out(print_r($roles, true));
  41. }
  42. $roleIds = array_keys($roles);
  43. while (!empty($roles) && empty($role)) {
  44. $role = $this->in(__('Role'), $roleIds);
  45. }
  46. } elseif (method_exists($this->User, 'roles')) {
  47. $roles = User::roles();
  48. if (!empty($roles)) {
  49. $this->out('');
  50. $this->out(print_r($roles, true));
  51. }
  52. $roleIds = array_keys($roles);
  53. while (!empty($roles) && empty($role)) {
  54. $role = $this->in(__('Role'), $roleIds);
  55. }
  56. }
  57. if (empty($roles)) {
  58. $this->out('No Role found (either no table, or no data)');
  59. $role = $this->in(__('Please insert a role manually'));
  60. }
  61. $this->out('');
  62. $this->User->Behaviors->load('Tools.Passwordable', array('confirm' => false));
  63. //$this->User->validate['pwd']
  64. $data = array('User' => array(
  65. 'pwd' => $password,
  66. 'active' => 1
  67. ));
  68. if (!empty($username)) {
  69. $usernameField = $this->User->displayField;
  70. $data['User'][$usernameField] = $username;
  71. }
  72. if (!empty($email)) {
  73. $data['User']['email'] = $email;
  74. }
  75. if (!empty($role)) {
  76. $data['User']['role_id'] = $role;
  77. }
  78. if (!empty($schema['status']) && method_exists('User', 'statuses')) {
  79. $statuses = User::statuses();
  80. $this->out(print_r($statuses, true));
  81. $status = $this->in(__('Please insert a status'), array_keys($statuses));
  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. return $this->error('Not Executed!');
  98. }
  99. $this->out('');
  100. $this->hr();
  101. if (!$this->User->save($data)) {
  102. return $this->error('User could not be inserted (' . print_r($this->User->validationErrors, true) . ')');
  103. }
  104. $this->out('User inserted! ID: ' . $this->User->id);
  105. }
  106. }