-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.php
300 lines (261 loc) · 9.59 KB
/
plugin.php
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
namespace Xpressengine\Plugins\Importer;
ini_set('memory_limit', '384M');
use Illuminate\Console\Application as Artisan;
use Illuminate\Database\Schema\Blueprint;
use Route;
use Schema;
use Xpressengine\Plugin\AbstractPlugin;
use Xpressengine\Plugins\Importer\Commands\ImportCommand;
use Xpressengine\Plugins\Importer\Commands\RollbackCommand;
use Xpressengine\Plugins\Importer\Importers\AbstractImporter;
use Xpressengine\Plugins\Importer\Importers\DocumentImporter;
use Xpressengine\Plugins\Importer\Importers\Modules\AbstractModuleImporter;
use Xpressengine\Plugins\Importer\Importers\Modules\BoardImporter;
use Xpressengine\Plugins\Importer\Importers\UserImporter;
use Xpressengine\Plugins\Importer\Migrations\Migration;
class Plugin extends AbstractPlugin
{
public function register()
{
app()->singleton(
DocumentImporter::class,
function ($app) {
$board = new BoardImporter();
return new DocumentImporter(['board'=>$board]);
}
);
app()->alias(DocumentImporter::class, 'importer::document');
app()->singleton(
PasswordManager::class,
function ($app) {
return new PasswordManager();
}
);
app()->alias(PasswordManager::class, 'importer::password');
app()->singleton(
UserImporter::class,
function ($app) {
return new UserImporter(app('xe.user'), app('xe.user.image'), new PasswordManager());
}
);
app()->alias(UserImporter::class, 'importer::user');
app()->singleton(
Handler::class,
function ($app) {
$logger = new Logger(storage_path('logs/importer.log'));
$syncManager = new Synchronizer();
return new Handler(
$syncManager,
$logger,
[
'user' => app('importer::user'),
'document' => app('importer::document'),
]
);
}
);
app()->alias(Handler::class, 'importer::handler');
app()->singleton(
'importer::command.import',
function ($app) {
return new ImportCommand(app('importer::handler'));
}
);
app()->singleton(
'importer::command.rollback',
function ($app) {
return new RollbackCommand(app('importer::handler'));
}
);
$commands = ['importer::command.import', 'importer::command.rollback'];
Artisan::starting(function ($artisan) use ($commands) {
$artisan->resolveCommands($commands);
});
}
/**
* 이 메소드는 활성화(activate) 된 플러그인이 부트될 때 항상 실행됩니다.
*
* @return void
*/
public function boot()
{
AbstractImporter::setHandler(app('importer::handler'));
AbstractModuleImporter::setDocumentImporter(app('importer::document'));
$this->registerSettingsMenu();
$this->registerEvents();
$this->route();
}
protected function route()
{
// implement code
Route::settings(
$this->getId(),
function () {
Route::get(
'/',
[
'as' => 'importer::index',
'uses' => '\Xpressengine\Plugins\Importer\Controllers\SettingsController@index',
'settings_menu' => 'setting.importer'
]
);
Route::post(
'/',
[
'as' => 'importer::import',
'uses' => '\Xpressengine\Plugins\Importer\Controllers\SettingsController@doImport'
]
);
Route::post(
'/operation/delete',
[
'as' => 'importer::operation.delete',
'uses' => '\Xpressengine\Plugins\Importer\Controllers\SettingsController@deleteOperation'
]
);
}
);
}
/**
* 플러그인이 활성화될 때 실행할 코드를 여기에 작성한다.
*
* @param string|null $installedVersion 현재 XpressEngine에 설치된 플러그인의 버전정보
*
* @return void
*/
public function activate($installedVersion = null)
{
// implement code
}
/**
* 플러그인을 설치한다. 플러그인이 설치될 때 실행할 코드를 여기에 작성한다
*
* @return void
*/
public function install()
{
if (!Schema::hasTable('importer_sync')) {
Schema::create(
'importer_sync',
function (Blueprint $table) {
$table->engine = "InnoDB";
$table->string('origin_id', 100);
$table->string('new_id', 36);
$table->text('data');
$table->timestamp('created_at')->index();
$table->timestamp('updated_at')->index();
$table->integer('revision')->default(1);
$table->primary('origin_id');
}
);
}
if (!Schema::hasTable('importer_user_password')) {
Schema::create(
'importer_user_password',
function (/*Blueprint*/ $table) {
$table->engine = "InnoDB";
$table->string('origin_id', 100);
$table->string('user_id', 36);
$table->string('hash_function', 50);
$table->text('meta');
$table->string('password', 500);
$table->timestamp('created_at')->index();
$table->timestamp('updated_at')->index();
$table->primary('origin_id');
$table->unique('user_id');
}
);
}
}
/**
* 해당 플러그인이 설치된 상태라면 true, 설치되어있지 않다면 false를 반환한다.
* 이 메소드를 구현하지 않았다면 기본적으로 설치된 상태(true)를 반환한다.
*
* @return boolean 플러그인의 설치 유무
*/
public function checkInstalled()
{
return Schema::hasTable('importer_sync');
}
/**
* 플러그인을 업데이트한다.
*
* @return void
*/
public function update()
{
(new Migration())->up();
}
/**
* 해당 플러그인이 최신 상태로 업데이트가 된 상태라면 true, 업데이트가 필요한 상태라면 false를 반환함.
* 이 메소드를 구현하지 않았다면 기본적으로 최신업데이트 상태임(true)을 반환함.
*
* @return boolean 플러그인의 설치 유무,
*/
public function checkUpdated()
{
// implement code
if (!Schema::hasColumn('importer_sync', 'revision')) {
return false;
}
return parent::checkUpdated();
}
protected function registerSettingsMenu()
{
app('xe.register')->push(
'settings/menu',
'setting.importer',
[
'title' => '마이그레이션',
'description' => '타 CMS의 데이터를 이 사이트로 들여옵니다',
'display' => true,
'ordering' => 6000
]
);
}
protected function registerEvents()
{
// 회원로그인
intercept('Auth@attempt', 'importer::attempt', function($target, array $credentials = [], $remember = false, $login = true){
// 로그인 시도
$result = $target($credentials, $remember, $login);
$password = array_get($credentials, 'password');
if ($result === true || $password === null) {
return $result;
}
// 로그인에 실패했을 경우
$attempted = app('auth')->getLastAttempted();
if ($attempted === null) {
return $result;
}
// 비밀번호가 틀렸고, 비밀번호가 지정되어 있지 않은 회원일 경우
if ($attempted !== null && $attempted->password === null) {
$imported = app('importer::password')->getByUserId($attempted->id);
$result = app('importer::password')->attempt($imported, $password);
// 임포트 된 회원 비밀번호와 일치할 경우
if ($result === true) {
// disable password validator
$validator = app('validator');
$validator->extend(
'password',
function ($attribute, $value, $parameters) {
return true;
}
);
// 비밀번호 갱신
app('xe.user')->update($attempted, compact('password'));
// sync data 제거
/** @var Handler $handler */
$handler = app('importer::handler');
$handler->getSyncManager()->remove($imported->origin_id);
// imported password 제거
app('importer::password')->remove($imported);
app('auth')->login($attempted, $remember);
return true;
}
}
return false;
});
}
}