1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Console\Commands; |
17
|
|
|
|
18
|
|
|
use Illuminate\Console\Command; |
19
|
|
|
use Illuminate\Support\Facades\Lang; |
20
|
|
|
use Illuminate\Contracts\Validation\Factory; |
21
|
|
|
|
22
|
|
|
class RoleUpdateCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The name and signature of the console command. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'fort:role:update |
30
|
|
|
{field? : The identifier of the role (id, email, rolename)} |
31
|
|
|
{--N|name= : The name of the role} |
32
|
|
|
{--S|slug= : The slug of the role} |
33
|
|
|
{--D|description= : The description of the role}'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The console command description. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $description = 'Update an existing role.'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the console command. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function handle() |
48
|
|
|
{ |
49
|
|
|
$data = array_filter([ |
50
|
|
|
|
51
|
|
|
// Required role attributes |
52
|
|
|
'name' => $this->option('name'), |
53
|
|
|
'slug' => $this->option('slug'), |
54
|
|
|
'description' => $this->option('description'), |
55
|
|
|
|
56
|
|
|
], [ |
57
|
|
|
$this, |
58
|
|
|
'filter' |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
// Get required argument |
62
|
|
|
$field = $this->argument('field') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.invalid')); |
63
|
|
|
|
64
|
|
|
// Find single role |
65
|
|
|
if (intval($field)) { |
66
|
|
|
$role = $this->laravel['rinvex.fort.role']->find($field); |
67
|
|
|
} else { |
68
|
|
|
$role = $this->laravel['rinvex.fort.role']->findWhere(['slug' => $field])->first(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (! $role) { |
72
|
|
|
return $this->error(Lang::get('rinvex.fort::artisan.role.invalid', ['field' => $field])); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$rules = [ |
76
|
|
|
'name' => 'sometimes|required|max:255', |
77
|
|
|
'slug' => 'sometimes|required|max:255|unique:'.config('rinvex.fort.tables.roles'), |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
if (! empty($data)) { |
81
|
|
|
$validator = app(Factory::class)->make($data, $rules); |
82
|
|
|
|
83
|
|
|
if ($validator->fails()) { |
84
|
|
|
$this->error('Errors:'); |
85
|
|
|
|
86
|
|
|
foreach ($validator->errors()->getMessages() as $key => $messages) { |
87
|
|
|
$this->error('- '.$key.': '.$messages[0]); |
88
|
|
|
} |
89
|
|
|
} else { |
90
|
|
|
$role->update($data); |
91
|
|
|
|
92
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.role.updated').' ['.Lang::get('rinvex.fort::artisan.role.id').': '.$role->id.', '.Lang::get('rinvex.fort::artisan.role.name').': '.$role->name.', '.Lang::get('rinvex.fort::artisan.role.slug').': '.$role->slug.']'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.role.nothing')); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Filter null and empty values. |
101
|
|
|
* |
102
|
|
|
* @param $value |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
protected function filter($value) |
107
|
|
|
{ |
108
|
|
|
return ($value !== null && $value !== ''); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.