forked from cakephp/plugins.cakephp.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserShell.php
More file actions
66 lines (58 loc) · 1.67 KB
/
Copy pathUserShell.php
File metadata and controls
66 lines (58 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Users\Shell;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
/**
* User shell command.
*/
class UserShell extends Shell
{
/**
* main() method.
*
* @return bool|int Success or error code.
*/
public function main()
{
$config = Configure::read('Users');
$data = [];
$fields = [
$config['fields']['username'],
$config['fields']['password'],
];
foreach ($fields as $field) {
$value = null;
$fieldName = Inflector::humanize($field);
while (empty($value)) {
$value = $this->in(sprintf('%s?', $fieldName));
}
$data[$field] = $value;
}
$this->out('');
$continue = $this->in('Continue?', ['y', 'n'], 'n');
if ($continue !== 'y') {
return $this->error('User not saved.');
}
$this->out('');
$this->hr();
$table = TableRegistry::get($config['userModel']);
$entity = $table->newEntity($data, ['validate' => false]);
if (!$table->save($entity)) {
return $this->error(sprintf('User could not be inserted: %s', print_r($entity->errors(), true)));
}
$this->out(sprintf('User inserted! ID: %d, Data: %s', $entity->id, print_r($entity->toArray(), true)));
}
/**
* UserShell
*
* @return ConsoleOptionParser
*/
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->description('The User shell can create a user on the fly for local development.');
return $parser;
}
}