UserShell.php 2.8 KB

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