1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSlant\LaravelTelegramGitNotifier\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
class ChangeOwnerConfigJson extends Command |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The name and signature of the console command. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $signature = 'config-json:change-owner |
15
|
|
|
{user? : The user to change owner} |
16
|
|
|
{group? : The group to change owner}'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'config-json'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute the console command. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function handle(): void |
31
|
|
|
{ |
32
|
|
|
if (PHP_OS_FAMILY !== 'Linux') { |
33
|
|
|
$this->error('This command only works on Linux'); |
34
|
|
|
|
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$user = $this->argument('user') ?: $this->getDefaultUserGroup(); |
39
|
|
|
$group = $this->argument('group') ?: $this->getDefaultUserGroup(); |
40
|
|
|
|
41
|
|
|
if (!is_string($user) || !is_string($group)) { |
42
|
|
|
$this->error('Failed to retrieve default user and group'); |
43
|
|
|
|
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->changeOwner($user, $group); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $user |
52
|
|
|
* @param string $group |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
private function changeOwner(string $user, string $group): void |
56
|
|
|
{ |
57
|
|
|
$jsonsPath = config('telegram-git-notifier.data_file.storage_folder'); |
58
|
|
|
if (is_string($jsonsPath) && file_exists($jsonsPath)) { |
59
|
|
|
shell_exec('chown -R '.escapeshellarg($user).':'.escapeshellarg($group).' '.escapeshellarg($jsonsPath)); |
60
|
|
|
} else { |
61
|
|
|
$this->error('The path to the jsons folder is not valid'); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the default user and group for the chown command. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private function getDefaultUserGroup(): string |
71
|
|
|
{ |
72
|
|
|
$defaultUserGroup = exec('ps aux | egrep "(apache|httpd|nginx)" | grep -v "root" | head -n1 | cut -d\ -f1'); |
73
|
|
|
if ($defaultUserGroup === false) { |
74
|
|
|
$this->error('Failed to retrieve default user and group'); |
75
|
|
|
|
76
|
|
|
return ''; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $defaultUserGroup; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|