1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Commands; |
4
|
|
|
|
5
|
|
|
use Slides\Connector\Auth\Sync\Syncer; |
6
|
|
|
use Slides\Connector\Auth\Helpers\ConsoleHelper; |
7
|
|
|
use Slides\Connector\Auth\Concerns\PassesModes; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class SyncExport |
11
|
|
|
* |
12
|
|
|
* @package Slides\Connector\Auth\Commands |
13
|
|
|
*/ |
14
|
|
|
class SyncExport extends \Illuminate\Console\Command |
15
|
|
|
{ |
16
|
|
|
use PassesModes; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'connector:sync-export |
24
|
|
|
{--path= : Allow syncing passwords (can rewrite remotely and locally) } |
25
|
|
|
{--users= : Export the specific users } |
26
|
|
|
{--passwords : Export the specific users }'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The console command description. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Export local users to sync users remotely (can be imported only remotely)'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
$this->displayModes(); |
43
|
|
|
|
44
|
|
|
$syncer = new Syncer($locals = $this->syncingUsers(), $this->modes()); |
45
|
|
|
|
46
|
|
|
if($locals->isEmpty()) { |
47
|
|
|
$this->info('No local users found.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if(!$this->confirm('There are ' . $locals->count() . ' local user(s) to export. Continue?', $this->option('no-interaction'))) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$syncer->setOutputCallback(function(string $message) { |
55
|
|
|
$this->info('[Syncer] ' . $message); |
56
|
|
|
}); |
57
|
|
|
|
58
|
|
|
$filePath = $this->filePath(); |
59
|
|
|
|
60
|
|
|
$duration = $this->measure(function() use ($syncer, $filePath) { |
61
|
|
|
$syncer->export($filePath); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->info('Dump has been saved to ' . $filePath); |
65
|
|
|
$this->info('Encryption key: ' . $syncer->getEncryptionKey()); |
66
|
|
|
|
67
|
|
|
$filename = basename($filePath); |
68
|
|
|
|
69
|
|
|
$this->output->note( |
70
|
|
|
'This encryption key is unique for each dump and supposed to be used safely.' |
71
|
|
|
. PHP_EOL . 'It\'s bound to this service and cannot be used on other tenants.' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->output->block('To sync the dump, run the following command on Authentication Service:'); |
75
|
|
|
$this->output->block("php artisan sync:import-dump {$filename} --key \"{$syncer->getEncryptionKey()}\"", null, 'fg=cyan;bg=default'); |
76
|
|
|
|
77
|
|
|
$this->info("Finished in {$duration}s."); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Measure an execution time of the callback. |
82
|
|
|
* |
83
|
|
|
* @param \Closure $callback |
84
|
|
|
* |
85
|
|
|
* @return float |
86
|
|
|
*/ |
87
|
|
|
public function measure(\Closure $callback) |
88
|
|
|
{ |
89
|
|
|
$start = microtime(true); |
90
|
|
|
|
91
|
|
|
$callback(); |
92
|
|
|
|
93
|
|
|
$end = microtime(true); |
94
|
|
|
|
95
|
|
|
return round($end - $start, 2); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve users to sync. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Support\Collection |
102
|
|
|
*/ |
103
|
|
|
private function syncingUsers() |
104
|
|
|
{ |
105
|
|
|
if(!count($ids = ConsoleHelper::stringToArray($this->option('users')))) { |
106
|
|
|
return Syncer::retrieveLocals(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return \Illuminate\Support\Facades\Auth::getProvider()->createModel() |
110
|
|
|
->newQuery() |
111
|
|
|
->whereIn('id', $ids) |
112
|
|
|
->get(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieve a file path. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function filePath() |
121
|
|
|
{ |
122
|
|
|
if(!$path = $this->option('path')) { |
123
|
|
|
$path = storage_path('app'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$datetime = (new \Carbon\Carbon())->format('Y_m_d_His'); |
127
|
|
|
|
128
|
|
|
return str_finish($path, '/') . 'sync_export_' . $datetime . '.gz'; |
129
|
|
|
} |
130
|
|
|
} |