Skip to content

Commit c99bc8a

Browse files
committed
add default config
1 parent 490f01f commit c99bc8a

File tree

3 files changed

+89
-42
lines changed

3 files changed

+89
-42
lines changed

config.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
return [
3+
'timer' => 365, // 휴면처리를 할 마지막 로그인 시도로부터의 경과일
4+
'notify_timer' => 334, // 휴면처리 예고 알림을 발송할 마지막 로그인 시도로부터의 경과일
5+
'freeze_type' => 'freeze', // "delete"|"freeze" 휴면처리 방식, 삭제 or 분리
6+
'scheduled_at' => [
7+
'notify'=>'6:00', // 휴면처리 배치 작업 시작시점 (6:00 => 매일 6시)
8+
'freeze'=>'6:30', // 휴면처리 배치 작업 시작시점 (6:00 => 매일 6시)
9+
],
10+
'queue_size' => 1,
11+
'queue' => [
12+
'notify' => 'sync',
13+
'freeze' => 'sync'
14+
],
15+
'email' => [
16+
'notify' => ['subject' => '휴면게정 처리 예정 안내', 'content' => function($user, $type, $config) {
17+
18+
if($config['freeze_type'] === 'freeze') {
19+
return "{$user->getDisplayName()}님, {$user->createdAt->format('Y년 m월 d일')}에 가입한 계정이 최근 11개월간 이용되지 않아 1개월 후 휴면 상태로 전환될 예정입니다. <br> 휴면상태로 전환된 후에는 개인정보를 분리하여 보관하게 됩니다. 차후 휴면 상태로 전환된 계정으로 다시 서비스를 이용하기 위해서는 사이트의 <a href=\"{route('login')}\">로그인</a> 페이지에서 다시 로그인을 하시면 됩니다. 감사합니다 ";
20+
} else {
21+
return "{$user->getDisplayName()}님, {$user->createdAt->format('Y년 m월 d일')}에 가입한 계정이 최근 11개월간 이용되지 않아 1개월 후 탈퇴 처리될 예정입니다. <br> 탈퇴 처리된 후에는 다시 서비스를 이용하기 위해 사이트의 <a href=\"{route('auth.register')}\">회원가입</a> 페이지에서 새로운 계정으로 가입하여야 합니다. 감사합니다 ";
22+
}
23+
}],
24+
'freeze' => ['subject' => '휴면계정 처리 결과 안내', 'content' => function($user, $type, $config) {
25+
return "{$user->getDisplayName()}님, {$user->createdAt->format('Y년 m월 d일')}에 가입한 계정이 최근 1년간 이용되지 않아 휴면 상태로 전환되었습니다. <br> 사이트의 <a href=\"{route('login')}\">로그인</a> 페이지에서 다시 로그인하실 경우 계정이 복구되며, 서비스를 정상적으로 다시 이용할 수 있습니다. 감사합니다 ";
26+
}],
27+
'delete' => ['subject' => '휴면계정 삭제처리 안내', 'content' => function($user, $type, $config) {
28+
return "{$user->getDisplayName()}님, {$user->createdAt->format('Y년 m월 d일')}에 가입한 계정이 최근 1년간 이용되지 않아 탈퇴 처리되었습니다. <br> 사이트의 <a href=\"{route('auth.register')}\">회원가입</a> 페이지에서 새로운 계정으로 가입후 서비스를 다시 이용할 수 있습니다. 감사합니다 ";
29+
}],
30+
'unfreeze' => ['subject' => '휴면개정 복구 결과 안내', 'content' => function($user, $type, $config) {
31+
return "{$user->getDisplayName()}님, 장기간 서비스를 이용하지 않아 분리 보관했던 계정 정보가 다시 정상적으로 복구되었습니다. <br> 이제부터 서비스를 다시 이용할 수 있습니다 ";
32+
}],
33+
]
34+
];

plugin.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,15 @@ function ($artisan) use ($commands) {
5757
}
5858
);
5959

60-
61-
60+
// set configuration
61+
$config = config('services.freezer');
62+
$default = include('config.php');
63+
if ($config) {
64+
$new = array_replace_recursive($default, $config);
65+
config(['services.freezer' => $new]);
66+
} else {
67+
config(['services.freezer' => $default]);
68+
}
6269
}
6370

6471
/**

src/Handler.php

+46-40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
1212
* @link http://www.xpressengine.com
1313
*/
14+
1415
namespace Xpressengine\Plugins\Freezer;
1516

1617
use Carbon\Carbon;
@@ -65,20 +66,24 @@ public function config($field = null, $default = null)
6566
public function choose($action = 'freeze')
6667
{
6768
$users = new Collection();
68-
if($action === 'freeze') {
69+
if ($action === 'freeze') {
6970
$duration = $this->config('timer');
7071
$eventDate = Carbon::now()->subDays($duration);
7172
$users = $this->handler->where('loginAt', '<', $eventDate)->get();
72-
} elseif($action === 'notify') {
73+
} elseif ($action === 'notify') {
7374
$duration = $this->config('notify_timer');
7475
$eventDate = Carbon::now()->subDays($duration);
75-
$candidates = User::where('loginAt', '<', $eventDate)->with(['freeze_logs' => function ($q) {
76-
return $q->orderBy('createdAt', 'desc');
77-
}])->get();
76+
$candidates = User::where('loginAt', '<', $eventDate)->with(
77+
[
78+
'freeze_logs' => function ($q) {
79+
return $q->orderBy('createdAt', 'desc');
80+
}
81+
]
82+
)->get();
7883

7984
foreach ($candidates as $user) {
8085
$latestLog = $user->freeze_logs->first();
81-
if(data_get($latestLog, 'action') !== 'notify') {
86+
if (data_get($latestLog, 'action') !== 'notify') {
8287
$users->add($user);
8388
}
8489
}
@@ -99,25 +104,24 @@ public function notify($users = null)
99104
$size = $this->config('queue_size', 1);
100105
$queue = $this->config('queue.notify', 'sync');
101106

102-
if($users === null) {
107+
if ($users === null) {
103108
$users = $this->choose('notify');
104109
}
105110

106111
$user_ids = [];
107112
foreach ($users as $user) {
108113
$user_ids[] = $user->id;
109114

110-
if(count($user_ids) === $size) {
115+
if (count($user_ids) === $size) {
111116
$this->dispatch((new NotifyJob($user_ids, $freezeType))->onQueue($queue));
112117
$user_ids = [];
113118
}
114119
}
115-
if(count($user_ids)) {
116-
$this->dispatch(new NotifyJob($user_ids, $freezeType));
120+
if (count($user_ids)) {
121+
$this->dispatch((new NotifyJob($user_ids, $freezeType))->onQueue($queue));
117122
}
118123

119124
return $users->count();
120-
121125
}
122126

123127
/**
@@ -133,28 +137,28 @@ public function freeze($users = null)
133137
$size = $this->config('queue_size', 1);
134138
$queue = $this->config('queue.freeze', 'sync');
135139

136-
if($users === null) {
140+
if ($users === null) {
137141
$users = $this->choose('freeze');
138142
}
139143

140144
$user_ids = [];
141145
foreach ($users as $user) {
142146
$user_ids[] = $user->id;
143147

144-
if(count($user_ids) === $size) {
145-
$this->dispatch(new FreezeJob($user_ids, $freezeType));
148+
if (count($user_ids) === $size) {
149+
$this->dispatch((new FreezeJob($user_ids, $freezeType))->onQueue($queue));
146150
$user_ids = [];
147151
}
148152
}
149-
if(count($user_ids)) {
153+
if (count($user_ids)) {
150154
$this->dispatch((new FreezeJob($user_ids, $freezeType))->onQueue($queue));
151155
}
152156
return $users->count();
153157
}
154158

155159
public function freezeUser($user_ids)
156160
{
157-
if(is_string($user_ids)) {
161+
if (is_string($user_ids)) {
158162
$user_ids = [$user_ids];
159163
}
160164

@@ -165,16 +169,16 @@ public function freezeUser($user_ids)
165169
$this->moveData('freeze', $user->id);
166170
$this->sendEmail($user, 'freeze');
167171
} catch (\Exception $e) {
168-
$this->logging($user->id, 'freeze' , ['message'=>$e->getMessage()], 'failed');
172+
$this->logging($user->id, 'freeze', ['message' => $e->getMessage()], 'failed');
169173
throw $e;
170174
}
171-
$this->logging($user->id, 'freeze' , $user->toArray(), 'successed');
175+
$this->logging($user->id, 'freeze', $user->toArray(), 'successed');
172176
}
173177
}
174178

175179
public function deleteUser($user_ids)
176180
{
177-
if(is_string($user_ids)) {
181+
if (is_string($user_ids)) {
178182
$user_ids = [$user_ids];
179183
}
180184

@@ -185,7 +189,7 @@ public function deleteUser($user_ids)
185189
$this->handler->leave($user->id);
186190
$this->sendEmail($user, 'delete');
187191
} catch (\Exception $e) {
188-
$this->logging($user->id, 'delete', ['message'=>$e->getMessage()], 'failed');
192+
$this->logging($user->id, 'delete', ['message' => $e->getMessage()], 'failed');
189193
throw $e;
190194
}
191195
$this->logging($user->id, 'delete');
@@ -194,7 +198,7 @@ public function deleteUser($user_ids)
194198

195199
public function notifyUser($user_ids)
196200
{
197-
if(is_string($user_ids)) {
201+
if (is_string($user_ids)) {
198202
$user_ids = [$user_ids];
199203
}
200204

@@ -204,7 +208,7 @@ public function notifyUser($user_ids)
204208
try {
205209
$this->sendEmail($user, 'notify');
206210
} catch (\Exception $e) {
207-
$this->logging($user->id, 'notify', ['message'=>$e->getMessage()], 'failed');
211+
$this->logging($user->id, 'notify', ['message' => $e->getMessage()], 'failed');
208212
throw $e;
209213
}
210214
$this->logging($user->id, 'notify');
@@ -218,48 +222,50 @@ public function unfreeze($user_id)
218222
$user = $this->handler->find($user_id);
219223
$this->sendEmail($user, 'unfreeze');
220224
} catch (\Exception $e) {
221-
$this->logging($user_id, 'unfreeze', ['message'=>$e->getMessage()], 'failed');
225+
$this->logging($user_id, 'unfreeze', ['message' => $e->getMessage()], 'failed');
222226
throw $e;
223227
}
224228
$this->logging($user->id, 'unfreeze');
225229
}
226230

227231
protected function moveData($type, $user_id)
228232
{
229-
if($type === 'freeze') {
230-
$origin = 'origin'; $target = 'target';
233+
if ($type === 'freeze') {
234+
$origin = 'origin';
235+
$target = 'target';
231236
} else {
232-
$origin = 'target'; $target = 'origin';
237+
$origin = 'target';
238+
$target = 'origin';
233239
}
234240

235241
// copy user table
236242
$table = ['origin' => 'user', 'target' => 'freezer_user'];
237243
$user = DB::table($table[$origin])->find($user_id);
238244
DB::table($table[$origin])->delete($user_id);
239-
DB::table($table[$target])->insert((array)$user);
245+
DB::table($table[$target])->insert((array) $user);
240246

241247
// copy user_account table
242248
$table = ['origin' => 'user_account', 'target' => 'freezer_user_account'];
243249
$accounts = DB::table($table[$origin])->where('userId', $user_id)->get();
244250
DB::table($table[$origin])->where('userId', $user_id)->delete();
245251
foreach ($accounts as $account) {
246-
DB::table($table[$target])->insert((array)$account);
252+
DB::table($table[$target])->insert((array) $account);
247253
}
248254

249255
// copy user_email table
250256
$table = ['origin' => 'user_email', 'target' => 'freezer_user_email'];
251257
$emails = DB::table($table[$origin])->where('userId', $user_id)->get();
252258
DB::table($table[$origin])->where('userId', $user_id)->delete();
253259
foreach ($emails as $email) {
254-
DB::table($table[$target])->insert((array)$email);
260+
DB::table($table[$target])->insert((array) $email);
255261
}
256262

257263
// copy user_group_user
258264
$table = ['origin' => 'user_group_user', 'target' => 'freezer_user_group_user'];
259265
$group_users = DB::table($table[$origin])->where('userId', $user_id)->get();
260266
DB::table($table[$origin])->where('userId', $user_id)->delete();
261267
foreach ($group_users as $group) {
262-
DB::table($table[$target])->insert((array)$group);
268+
DB::table($table[$target])->insert((array) $group);
263269
}
264270
}
265271

@@ -283,9 +289,9 @@ protected function sendEmail($user, $type)
283289
$content = $content($user, $type, $this->config());
284290

285291
// type = freeze, delete, unfreeze, notify
286-
$view = $this->plugin->view('views.email') ;
292+
$view = $this->plugin->view('views.email');
287293
$emailAddr = $user->email;
288-
if($emailAddr) {
294+
if ($emailAddr) {
289295
app('mailer')->queue(
290296
$view,
291297
compact('content'),
@@ -300,12 +306,12 @@ function ($message) use ($emailAddr, $subject) {
300306
public function attempt($credentials = [])
301307
{
302308

303-
if(array_has($credentials, 'password')) { // 이메일/비번 로그인
309+
if (array_has($credentials, 'password')) { // 이메일/비번 로그인
304310
$email = array_get($credentials, 'email');
305311
$userInfo = DB::table('freezer_user')->where('email', $email)->first();
306-
if($userInfo !== null) {
312+
if ($userInfo !== null) {
307313
$plain = $credentials['password'];
308-
if(app('hash')->check($plain, $userInfo->password)) {
314+
if (app('hash')->check($plain, $userInfo->password)) {
309315
return $userInfo->id;
310316
}
311317
}
@@ -316,11 +322,11 @@ public function attempt($credentials = [])
316322
// account info가 freeze 되어 있다면 바로 반환
317323
// email info가 freeze 되어 있다면,
318324
$accountInfo = DB::table('freezer_user_account')->where('accountId', $account_id)->first();
319-
if($accountInfo !== null) {
325+
if ($accountInfo !== null) {
320326
return $accountInfo->userId;
321-
} elseif($email !== null) {
327+
} elseif ($email !== null) {
322328
$emailInfo = DB::table('freezer_user_email')->where('address', $email)->first();
323-
if($emailInfo !== null) {
329+
if ($emailInfo !== null) {
324330
return $emailInfo->userId;
325331
}
326332
}
@@ -330,10 +336,10 @@ public function attempt($credentials = [])
330336
if ($userInfo !== null) {
331337
return $userInfo->id;
332338
}
333-
} elseif(array_has($credentials, 'address')) { // 이메일 검사
339+
} elseif (array_has($credentials, 'address')) { // 이메일 검사
334340
$address = array_get($credentials, 'address');
335341
$emailInfo = DB::table('freezer_user_email')->where('address', $address)->first();
336-
if($emailInfo !== null) {
342+
if ($emailInfo !== null) {
337343
return $emailInfo->userId;
338344
}
339345
}

0 commit comments

Comments
 (0)