user.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. if (!defined('CLASS_USER')) {
  3. define('CLASS_USER', 'User');
  4. }
  5. class UserShell extends Shell {
  6. var $tasks = array();
  7. var $uses = array(CLASS_USER);
  8. /*
  9. function initialize() {
  10. //Configure::write('debug', 2);
  11. parent::initialize();
  12. //$this->User = ClassRegistry::init('User');
  13. }
  14. */
  15. function help() {
  16. $this->out('command: cake user');
  17. }
  18. //TODO: refactor (smaller sub-parts)
  19. function main() {
  20. if (App::import('Component', 'AuthExt')) {
  21. $this->Auth = new AuthExtComponent();
  22. } else {
  23. App::import('Component', 'Auth');
  24. $this->Auth = new AuthComponent();
  25. }
  26. while (empty($username)) {
  27. $username = $this->in(__('Username (2 characters at least)', true));
  28. }
  29. while (empty($password)) {
  30. $password = $this->in(__('Password (2 characters at least)', true));
  31. }
  32. $schema = $this->User->schema();
  33. if (isset($this->User->Role) && is_object($this->User->Role)) {
  34. $roles = $this->User->Role->find('list');
  35. if (!empty($roles)) {
  36. $this->out('');
  37. pr($roles);
  38. }
  39. $roleIds = array_keys($roles);
  40. while (!empty($roles) && empty($role)) {
  41. $role = $this->in(__('Role', true), $roleIds);
  42. }
  43. } elseif (method_exists($this->User, 'roles')) {
  44. $roles = User::roles();
  45. if (!empty($roles)) {
  46. $this->out('');
  47. pr ($roles);
  48. }
  49. $roleIds = array_keys($roles);
  50. while (!empty($roles) && empty($role)) {
  51. $role = $this->in(__('Role', true), $roleIds);
  52. }
  53. }
  54. if (empty($roles)) {
  55. $this->out('No Role found (either no table, or no data)');
  56. $role = $this->in(__('Please insert a role manually', true));
  57. }
  58. $this->out('');
  59. $pwd = $this->Auth->password($password);
  60. $data = array('User'=>array(
  61. 'password' => $pwd,
  62. 'active' => 1
  63. ));
  64. if (!empty($username)) {
  65. $data['User']['username'] = $username;
  66. }
  67. if (!empty($email)) {
  68. $data['User']['email'] = $email;
  69. }
  70. if (!empty($role)) {
  71. $data['User']['role_id'] = $role;
  72. }
  73. if (!empty($schema['status']) && method_exists('User', 'statuses')) {
  74. $statuses = User::statuses();
  75. pr($statuses);
  76. while(empty($status)) {
  77. $status = $this->in(__('Please insert a status', true), array_keys($statuses));
  78. }
  79. $data['User']['status'] = $status;
  80. }
  81. if (!empty($schema['email'])) {
  82. $provideEmail = $this->in(__('Provide Email? ', true),array('y', 'n'), 'n');
  83. if ($provideEmail === 'y') {
  84. $email = $this->in(__('Please insert an email', true));
  85. $data['User']['email'] = $email;
  86. }
  87. if (!empty($schema['email_confirmed'])) {
  88. $data['User']['email_confirmed'] = 1;
  89. }
  90. }
  91. $this->out('');
  92. pr ($data);
  93. $this->out('');
  94. $this->out('');
  95. $continue = $this->in(__('Continue? ', true),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. }