UserShell.php 2.8 KB

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