fix(core,admin_basic,board,ecommerce,page,gdpr,verification): 설정 미러·자동 정리·탈퇴 원자성·일괄 선택 범위 정비

공개이슈 ··· 및 그 전수 해소분.

- 설정 저장·복원·초기화가 상주 프로세스 미러를 갱신하도록 단일 소유자화
- 만료 데이터 자동 정리 예약 12종 신설, 파기는 기본키 배치로 분할
- 회원 탈퇴를 전 경로 원자화하고 관리자 상태변경 경로를 정식 탈퇴로 통일
- 일괄 처리 목록의 선택을 화면에 보이는 행으로 한정(selectionScope)
- 비밀번호 정책 문구를 도메인 중립 라벨로 정정
- 정적 검사 2종·검증 스킬 설계 정합성 축 신설
This commit is contained in:
HeuJung committed 2026-08-14 11:06:25 +09:00
1 parent 49c155509e
commit 4e1ec6e001
136 files changed
+7016 -504

No files matched your search

+22
View File
@@ -83,4 +83,26 @@ class ActivityLogService
return $count;
}
/**
* 보존 기간이 지난 활동 로그를 정리합니다 (자동 파기).
*
* 운영자가 고른 ID 를 지우는 deleteMany 와는 별개의 훅을 발행한다. 자동 파기는
* 대상을 ID 로 지목하지 않고(기간으로 정한다), 사람이 없는 예약 실행이라
* 본인인증 같은 대화형 가드를 태울 수 없기 때문이다. 확장은 이 훅으로
* 외부 보관 등 자기 처리를 붙인다.
*
* @param int $days 보존 기간 (일)
* @return int 삭제된 건수
*/
public function prune(int $days): int
{
HookManager::doAction('core.activity_log.before_prune', $days);
$count = $this->repository->deleteOlderThan($days);
HookManager::doAction('core.activity_log.after_prune', $days, $count);
return $count;
}
}
+48 -19
View File
@@ -13,6 +13,7 @@ use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -181,23 +182,40 @@ class AttachmentService
$attachmentableId = $attachment->attachmentable_id;
$collection = $attachment->collection;
// Before 훅
HookManager::doAction('core.attachment.before_delete', $attachment);
// DB 를 먼저 지우고 파일은 커밋 후에 지운다.
// 순서를 반대로 두면 DB 삭제가 실패했을 때 파일 없는 행이 남아 다운로드가 404 가 된다.
// 반대로 이 순서에서는 최악이어도 참조되지 않는 고아 파일만 남고 데이터는 온전하다.
$result = DB::transaction(function () use ($id, $attachment, $attachmentableType, $attachmentableId, $collection) {
// Before 훅
HookManager::doAction('core.attachment.before_delete', $attachment);
// 스토리지에서 파일 삭제
$this->storage->withDisk($attachment->disk)->delete('', $attachment->path);
// DB에서 영구 삭제
$deleted = $this->repository->forceDelete($id);
// DB에서 영구 삭제
$result = $this->repository->forceDelete($id);
// 삭제 후 남은 파일들의 순서 재정렬
if ($deleted && $attachmentableType && $attachmentableId) {
$this->repository->reorderAfterDelete($attachmentableType, $attachmentableId, $collection);
}
Log::info('첨부파일 삭제 완료', [
'attachment_id' => $id,
'hash' => $attachment->hash,
]);
return $deleted;
});
// 삭제 후 남은 파일들의 순서 재정렬
if ($result && $attachmentableType && $attachmentableId) {
$this->repository->reorderAfterDelete($attachmentableType, $attachmentableId, $collection);
if ($result) {
// 커밋 후 스토리지 파일 삭제 (실패해도 DB 는 되돌리지 않는다)
try {
$this->storage->withDisk($attachment->disk)->delete('', $attachment->path);
} catch (\Throwable $e) {
Log::warning('첨부파일 스토리지 삭제 실패 (고아 파일 잔존)', [
'attachment_id' => $id,
'path' => $attachment->path,
'error' => $e->getMessage(),
]);
}
Log::info('첨부파일 삭제 완료', [
'attachment_id' => $id,
'hash' => $attachment->hash,
]);
}
// After 훅
@@ -378,18 +396,29 @@ class AttachmentService
// Before 훅
HookManager::doAction('core.attachment.before_bulk_delete', $identifier);
// 해당 첨부파일들의 스토리지 파일 삭제
// 삭제 대상 스냅샷 + 파일 경로를 먼저 수집한다 (DB 삭제 후에는 조회할 수 없다).
$attachments = $this->repository->getBySourceIdentifier($identifier);
$attachmentIds = $attachments->pluck('id')->toArray();
$snapshots = $attachments->keyBy('id')->map(fn ($a) => $a->toArray())->toArray();
$files = $attachments->map(fn ($a) => ['disk' => $a->disk, 'path' => $a->path])->all();
foreach ($attachments as $attachment) {
$this->storage->withDisk($attachment->disk)->delete('', $attachment->path);
// DB 일괄 삭제를 먼저 커밋한다 — 루프 중간에 실패하면 파일만 일부 사라지고
// DB 행은 전부 남아 다운로드가 404 가 되기 때문이다.
$count = DB::transaction(fn () => $this->repository->deleteBySourceIdentifier($identifier));
// 커밋 후 파일 정리 (실패해도 DB 는 되돌리지 않는다 — 고아 파일은 비파괴)
foreach ($files as $file) {
try {
$this->storage->withDisk($file['disk'])->delete('', $file['path']);
} catch (\Throwable $e) {
Log::warning('첨부파일 스토리지 일괄 삭제 실패 (고아 파일 잔존)', [
'source_identifier' => $identifier,
'path' => $file['path'],
'error' => $e->getMessage(),
]);
}
}
// DB에서 삭제
$count = $this->repository->deleteBySourceIdentifier($identifier);
Log::info('소스 식별자 기준 첨부파일 일괄 삭제', [
'source_identifier' => $identifier,
'deleted_count' => $count,
+31 -18
View File
@@ -15,6 +15,7 @@ use App\Extension\HookManager;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
@@ -347,19 +348,27 @@ class AuthService
'status' => $status,
];
$user = $this->userRepository->create($userData);
// 계정 생성 이후 단계에서 실패하면 이메일만 점유한 유령 계정이 남아
// 같은 이메일로 재가입조차 불가능해진다. 생성~토큰 발급을 하나로 묶는다.
[$user, $token] = DB::transaction(function () use ($userData, $data, $now) {
$user = $this->userRepository->create($userData);
// 약관 동의 이력 기록
$this->recordConsents($user, $data, $now);
// 약관 동의 이력 기록
$this->recordConsents($user, $data, $now);
// 'user' 역할 자동 할당 (UserService 패턴과 동일)
$userRole = $this->roleRepository->findByIdentifier('user');
if ($userRole) {
$user->roles()->sync([$userRole->id]);
$user->flushPermissionCaches();
}
// 'user' 역할 자동 할당 (UserService 패턴과 동일)
$userRole = $this->roleRepository->findByIdentifier('user');
if ($userRole) {
$user->roles()->sync([$userRole->id]);
}
$token = $user->createToken('auth-token', ['*'], $this->getTokenExpiresAt())->plainTextToken;
$token = $user->createToken('auth-token', ['*'], $this->getTokenExpiresAt())->plainTextToken;
return [$user, $token];
});
// 커밋 후 부수효과 (캐시는 DB 트랜잭션의 롤백 대상이 아니다)
$user->flushPermissionCaches();
// Hook 발생 (회원가입 완료) — 알림 발송은 NotificationHookListener,
// signup_after_create 정책이 enabled 면 InitiateIdentityChallengeAfterRegister 가 challenge 발행.
@@ -612,16 +621,20 @@ class AuthService
]);
}
// Hook 발생 (비밀번호 재설정 시작)
HookManager::doAction('core.auth.before_reset_password', $user);
// 비밀번호를 바꾼 뒤 토큰 삭제가 실패하면 그 재설정 토큰이 계속 유효한 채로
// 남아 재사용된다(보안). 두 단계를 하나로 묶는다.
DB::transaction(function () use ($user, $password, $record) {
// Hook 발생 (비밀번호 재설정 시작)
HookManager::doAction('core.auth.before_reset_password', $user);
// 비밀번호 업데이트
$this->userRepository->update($user, [
'password' => Hash::make($password),
]);
// 비밀번호 업데이트
$this->userRepository->update($user, [
'password' => Hash::make($password),
]);
// 사용된 토큰 삭제
$record->delete();
// 사용된 토큰 삭제
$record->delete();
});
// Hook 발생 (비밀번호 변경 완료) — 알림 발송은 NotificationHookListener가 처리
HookManager::doAction('core.auth.after_password_changed', $user);
+9 -5
View File
@@ -11,6 +11,7 @@ use App\Models\Menu;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
class MenuService
@@ -182,13 +183,16 @@ class MenuService
]);
}
// Hook: 메뉴 삭제 전
HookManager::doAction('core.menu.before_delete', $menu);
// 역할 연결만 끊기고 메뉴는 남는 상태를 막기 위해 두 단계를 하나로 묶는다.
$result = DB::transaction(function () use ($menu) {
// Hook: 메뉴 삭제 전
HookManager::doAction('core.menu.before_delete', $menu);
// 역할 연결 해제 (명시적 삭제 - CASCADE 의존 금지)
$menu->roles()->detach();
// 역할 연결 해제 (명시적 삭제 - CASCADE 의존 금지)
$menu->roles()->detach();
$result = $this->menuRepository->delete($menu);
return $this->menuRepository->delete($menu);
});
// Hook: 메뉴 삭제 후
HookManager::doAction('core.menu.after_delete', $menu->id);
+21
View File
@@ -93,6 +93,27 @@ class NotificationLogService
return $result;
}
/**
* 보존 기간이 지난 발송 이력을 정리합니다 (자동 파기).
*
* 운영자가 고른 ID 를 지우는 bulkDelete 와는 별개의 훅을 발행한다 — 자동 파기는
* 대상을 기간으로 정하고 사람 없이 예약 실행되므로, 대화형 가드를 전제한 훅에
* 얹으면 예약이 그 가드에 걸린다. 확장은 이 훅으로 자기 처리를 붙인다.
*
* @param int $days 보존 기간 (일)
* @return int 삭제된 건수
*/
public function prune(int $days): int
{
HookManager::doAction('core.notification_log.before_prune', $days);
$count = $this->repository->deleteOlderThan($days);
HookManager::doAction('core.notification_log.after_prune', $days, $count);
return $count;
}
/**
* 다건 삭제합니다.
*
+9
View File
@@ -5,6 +5,7 @@ namespace App\Services;
use App\Extension\HookManager;
use App\Extension\PluginManager;
use App\Extension\TemplateManager;
use App\Support\ExtensionSettingsMirror;
use App\Support\SensitiveSettingMask;
use App\Traits\FiltersFrontendSchema;
use App\Traits\NormalizesSettingsData;
@@ -189,6 +190,10 @@ class PluginSettingsService
// 캐시 초기화
if ($result) {
unset($this->settingsCache[$identifier]);
// 같은 프로세스의 config 미러도 즉시 다시 채운다 (공개이슈 #109) —
// 이 호출이 없으면 상주 프로세스가 저장 후에도 옛 값을 계속 읽는다.
app(ExtensionSettingsMirror::class)->refreshPlugin($identifier);
}
// After 훅
@@ -247,6 +252,10 @@ class PluginSettingsService
// 캐시 초기화
unset($this->settingsCache[$identifier]);
// 초기화도 값을 바꾸는 쓰기다 — 미러를 두면 같은 프로세스가 초기화 전 값을
// 계속 읽는다 (저장 경로와 동일 결함, 공개이슈 #109).
app(ExtensionSettingsMirror::class)->refreshPlugin($identifier);
// After 훅
HookManager::doAction('core.plugin_settings.after_reset', $identifier);
+13 -8
View File
@@ -11,6 +11,7 @@ use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class RoleService
@@ -183,16 +184,20 @@ class RoleService
throw new ExtensionOwnedRoleDeleteException;
}
// 훅: 삭제 전
HookManager::doAction('core.role.before_delete', $role);
// 관계 해제 후 삭제가 실패하면 그 역할을 가진 사용자 전원이 권한만 잃고
// 역할은 그대로 남는다. 전 단계를 하나로 묶는다.
$result = DB::transaction(function () use ($role) {
// 훅: 삭제 전
HookManager::doAction('core.role.before_delete', $role);
// 관계 해제 (명시적 삭제 - CASCADE 의존 금지)
$this->roleRepository->detachAllPermissions($role);
$role->menus()->detach();
$role->users()->detach();
// 관계 해제 (명시적 삭제 - CASCADE 의존 금지)
$this->roleRepository->detachAllPermissions($role);
$role->menus()->detach();
$role->users()->detach();
// 역할 삭제
$result = $this->roleRepository->delete($role);
// 역할 삭제
return $this->roleRepository->delete($role);
});
// 훅: 삭제 후
HookManager::doAction('core.role.after_delete', $role->id);
+28 -5
View File
@@ -122,13 +122,16 @@ class ScheduleService
public function delete(Schedule $schedule): bool
{
try {
// before_delete 훅 실행
HookManager::doAction('core.schedule.before_delete', $schedule);
// 이력만 지워지고 스케줄은 남는 상태를 막기 위해 두 단계를 하나로 묶는다.
$result = DB::transaction(function () use ($schedule) {
// before_delete 훅 실행
HookManager::doAction('core.schedule.before_delete', $schedule);
// 실행 이력 삭제 (명시적 삭제 - CASCADE 의존 금지)
$schedule->histories()->delete();
// 실행 이력 삭제 (명시적 삭제 - CASCADE 의존 금지)
$schedule->histories()->delete();
$result = $this->scheduleRepository->delete($schedule);
return $this->scheduleRepository->delete($schedule);
});
// after_delete 훅 실행
HookManager::doAction('core.schedule.after_delete', $schedule->id);
@@ -488,6 +491,26 @@ class ScheduleService
];
}
/**
* 보존 기간이 지난 실행 이력을 정리합니다 (자동 파기).
*
* 운영자가 고른 ID 를 지우는 bulkDeleteHistory 와 달리 대상을 기간으로 정하고
* 사람 없이 예약 실행되므로 별개의 훅을 발행합니다.
*
* @param int $days 보존 기간 (일)
* @return int 삭제된 건수
*/
public function pruneHistory(int $days): int
{
HookManager::doAction('core.schedule.before_prune_history', $days);
$deletedCount = $this->historyRepository->deleteOlderThan($days);
HookManager::doAction('core.schedule.after_prune_history', $days, $deletedCount);
return $deletedCount;
}
/**
* 활성화된 스케줄들을 조회합니다.
*
+12 -2
View File
@@ -9,6 +9,7 @@ use App\Extension\HookManager;
use App\Http\Resources\AttachmentResource;
use App\Seo\Contracts\SeoCacheManagerInterface;
use App\Support\ConfigCacheHelper;
use App\Support\ExtensionSettingsMirror;
use App\Support\OpcacheStatus;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth;
@@ -43,6 +44,11 @@ class SettingsService
{
$this->cache->forget('settings.system');
ConfigCacheHelper::rebuild();
// 같은 프로세스의 in-memory 미러도 즉시 다시 채운다 (공개이슈 #109).
// 이 호출이 없으면 상주 프로세스(큐 워커·schedule:work·Reverb)는 저장 후에도
// 부팅 시점의 옛 값을 영원히 읽는다 — FPM 에서만 드러나지 않는 결함이다.
app(ExtensionSettingsMirror::class)->refreshCore();
}
/**
@@ -703,7 +709,9 @@ class SettingsService
$result = $this->configRepository->set($key, $value);
if ($result) {
$this->cache->forget('settings.system');
// 인라인 무효화 대신 공통 경로를 탄다 — saveSettings/saveAdvancedSettings 와
// 같은 처리를 받아야 미러 재채움·디스크 config 캐시 재생성이 빠지지 않는다.
$this->invalidateSettingsCache();
}
// After 훅
@@ -738,7 +746,9 @@ class SettingsService
$result = $this->configRepository->restore($backupPath);
if ($result) {
$this->cache->forget('settings.system');
// 복원도 설정 전체를 갈아엎는 쓰기다 — 캐시만 비우고 미러를 두면
// 같은 프로세스가 복원 전 값을 계속 읽는다 (저장 경로와 동일 결함, 공개이슈 #109).
$this->invalidateSettingsCache();
}
return $result;
+255 -52
View File
@@ -10,6 +10,7 @@ use App\Extension\HookManager;
use App\Helpers\PermissionHelper;
use App\Helpers\TimezoneHelper;
use App\Models\ActivityLog;
use App\Models\Attachment;
use App\Models\User;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -17,6 +18,7 @@ use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
class UserService
@@ -170,6 +172,23 @@ class UserService
}
}
// 관리자가 상태만 '탈퇴'로 바꾸는 경로도 정식 탈퇴 로직으로 통일한다.
//
// 여기서 status/withdrawn_at 을 먼저 저장하면 withdraw() 의 멱등 가드가
// 이미 탈퇴한 계정으로 판정해 익명화와 훅이 조용히 생략된다.
// 따라서 상태 필드는 제거하고, 다른 필드 갱신을 마친 뒤 withdrawUser() 에 위임한다.
$withdrawViaStatus = isset($data['status'])
&& $data['status'] === UserStatus::Withdrawn->value
&& ! $user->isWithdrawn();
if ($withdrawViaStatus) {
unset($data['status']);
// 차단 대상(관리자/수퍼관리자)이면 다른 필드도 저장되지 않아야 하므로
// 갱신 전에 가드를 평가한다.
$this->assertWithdrawable($user);
}
// 상태 변경 감지 및 타임스탬프 자동 설정
$oldStatus = $user->status;
$newStatus = $data['status'] ?? null;
@@ -178,7 +197,8 @@ class UserService
$newStatusEnum = UserStatus::from($newStatus);
$data = match ($newStatusEnum) {
UserStatus::Blocked => array_merge($data, ['blocked_at' => now()]),
UserStatus::Withdrawn => array_merge($data, ['withdrawn_at' => now()]),
// 탈퇴는 위 위임 경로(withdrawUser)가 전담하므로 여기로는 도달하지 않는다.
UserStatus::Withdrawn => $data,
UserStatus::Active => array_merge($data, ['blocked_at' => null, 'withdrawn_at' => null]),
UserStatus::Inactive => $data,
};
@@ -187,22 +207,45 @@ class UserService
// 스냅샷 캡처 (ChangeDetector용)
$snapshot = $user->toArray();
$this->userRepository->update($user, $data);
// 탈퇴 위임 시 아바타는 커밋 후에 지운다 (트랜잭션 안에서 지우면 롤백돼도 파일은 안 돌아온다).
$avatarAttachment = $withdrawViaStatus ? $user->avatarAttachment : null;
// 상태가 Active 외로 변경되었으면 토큰 삭제 (즉시 로그아웃)
if ($newStatus && $newStatus !== $oldStatus && $newStatus !== UserStatus::Active->value) {
$user->tokens()->delete();
}
// 프로필 갱신·토큰 정리·역할 동기화가 서로 어긋나지 않도록 하나로 묶는다.
//
// 상태 변경 경유 탈퇴는 **탈퇴까지 같은 트랜잭션**에 넣는다 — 프로필 갱신을
// 먼저 커밋하고 탈퇴를 따로 커밋하면, 탈퇴가 실패했을 때 이름·이메일 변경만
// 남아 "전부 성공하거나 전부 취소" 가 그 경로에서만 깨진다.
DB::transaction(function () use ($user, $data, $newStatus, $oldStatus, $roleIds, $withdrawViaStatus) {
$this->userRepository->update($user, $data);
// 역할 동기화
// 상태가 Active 외로 변경되었으면 토큰 삭제 (즉시 로그아웃)
if ($newStatus && $newStatus !== $oldStatus && $newStatus !== UserStatus::Active->value) {
$user->tokens()->delete();
}
// 역할 동기화
if ($roleIds !== null) {
$user->roles()->sync($roleIds);
}
// 익명화 + before_withdraw 훅 + 연계 데이터 정리 (본인 탈퇴와 동일 코드)
if ($withdrawViaStatus) {
$this->performWithdrawWrites($user);
}
});
// 커밋 후 부수효과 (캐시는 롤백 대상이 아니다)
if ($roleIds !== null) {
$user->roles()->sync($roleIds);
$user->flushPermissionCaches();
}
// After 훅: 사용자 객체와 원본 데이터, 스냅샷 전달
HookManager::doAction('core.user.after_update', $user, $originalData, $snapshot);
if ($withdrawViaStatus) {
$this->runWithdrawAfterEffects($user, $avatarAttachment);
}
// 비밀번호 변경 시 알림 훅 발화 — 발송은 NotificationHookListener가 처리
if ($passwordChanged) {
HookManager::doAction('core.auth.after_password_changed', $user);
@@ -214,12 +257,93 @@ class UserService
throw $e;
}
if ($e instanceof CannotDeleteSuperAdminException) {
throw $e;
}
throw ValidationException::withMessages([
'general' => [__('user.update_failed', ['error' => $e->getMessage()])],
]);
}
}
/**
* 탈퇴 가능한 계정인지 검사합니다.
*
* 탈퇴 경로(본인 탈퇴 / 관리자 상태 변경 / 일괄 상태 변경)가 모두 같은 기준을
* 쓰도록 한 곳에 둡니다 — 한쪽만 느슨하면 그 경로가 우회로가 됩니다.
*
* @param User $user 검사 대상 사용자
*
* @throws CannotDeleteSuperAdminException 슈퍼 관리자인 경우
* @throws ValidationException 관리자 역할을 가진 경우
*/
private function assertWithdrawable(User $user): void
{
// 슈퍼 관리자는 탈퇴 불가
if ($user->isSuperAdmin()) {
throw new CannotDeleteSuperAdminException;
}
// 관리자 역할을 가진 계정은 탈퇴 방지
if ($user->isAdmin()) {
throw ValidationException::withMessages([
'general' => [__('user.withdraw_admin_forbidden')],
]);
}
}
/**
* 탈퇴의 DB 변경만 수행합니다 (호출자가 연 트랜잭션 안에서 실행).
*
* 관리자가 상태 변경으로 탈퇴시키는 경로는 프로필 갱신과 탈퇴를 **한 트랜잭션**으로
* 묶어야 한다 — 두 번에 나눠 커밋하면 탈퇴가 실패했을 때 프로필 변경만 남는다.
* 그래서 DB 변경분을 이 메서드로 떼어 두 경로가 같은 코드를 공유한다.
*
* @param User $user 탈퇴 처리할 사용자 모델
* @return bool 탈퇴 처리 성공 여부
*/
private function performWithdrawWrites(User $user): bool
{
// 훅 실행 (탈퇴 전)
HookManager::doAction('core.user.before_withdraw', $user);
// 약관 동의 이력 삭제 (명시적 삭제 - CASCADE 의존 금지)
$user->consents()->delete();
// 토큰 삭제 (로그아웃 처리)
$user->tokens()->delete();
// 탈퇴 처리 (suffix 추가 및 상태 변경)
return $user->withdraw();
}
/**
* 탈퇴 커밋 후 부수효과를 수행합니다.
*
* @param User $user 탈퇴 처리된 사용자 모델
* @param Attachment|null $avatarAttachment 탈퇴 전 캡처한 아바타 첨부
*/
private function runWithdrawAfterEffects(User $user, $avatarAttachment): void
{
// 아바타 파일 삭제는 실패해도 탈퇴를 되돌리지 않는다
// (고아 파일은 비파괴이고, 탈퇴를 되살리는 쪽이 훨씬 해롭다).
if ($avatarAttachment) {
try {
$this->attachmentService->delete($avatarAttachment->id);
} catch (Exception $e) {
Log::warning('탈퇴 아바타 삭제 실패', [
'user_id' => $user->id,
'attachment_id' => $avatarAttachment->id,
'error' => $e->getMessage(),
]);
}
}
// 훅 실행 (탈퇴 후)
HookManager::doAction('core.user.after_withdraw', $user);
}
/**
* 사용자를 탈퇴 처리합니다.
*
@@ -235,39 +359,19 @@ class UserService
public function withdrawUser(User $user): bool
{
try {
// 슈퍼 관리자는 탈퇴 불가
if ($user->isSuperAdmin()) {
throw new CannotDeleteSuperAdminException;
}
// 슈퍼 관리자 / 관리자 탈퇴 차단 (전 탈퇴 경로 공통 기준)
$this->assertWithdrawable($user);
// 관리자 역할을 가진 계정은 탈퇴 방지
if ($user->isAdmin()) {
throw ValidationException::withMessages([
'general' => [__('user.withdraw_admin_forbidden')],
]);
}
// 훅 실행 (탈퇴 전)
HookManager::doAction('core.user.before_withdraw', $user);
// 약관 동의 이력 삭제 (명시적 삭제 - CASCADE 의존 금지)
$user->consents()->delete();
// 아바타 Attachment 삭제 (다형성 관계)
// 아바타는 파일 삭제를 동반하므로 트랜잭션 밖(커밋 후)에서 처리한다 —
// 트랜잭션 안에서 지우면 롤백되어도 파일은 돌아오지 않는다.
$avatarAttachment = $user->avatarAttachment;
if ($avatarAttachment) {
$this->attachmentService->delete($avatarAttachment->id);
}
// 토큰 삭제 (로그아웃 처리)
$user->tokens()->delete();
// DB 변경은 전부 성공하거나 전부 취소된다 (공개이슈 #112).
// 마지막 단계인 withdraw() 가 실패하면 동의 이력·토큰 삭제도 함께 되돌아간다.
$result = DB::transaction(fn () => $this->performWithdrawWrites($user));
// 탈퇴 처리 (suffix 추가 및 상태 변경)
$result = $user->withdraw();
// 훅 실행 (탈퇴 후)
if ($result) {
HookManager::doAction('core.user.after_withdraw', $user);
$this->runWithdrawAfterEffects($user, $avatarAttachment);
}
return $result;
@@ -310,27 +414,42 @@ class UserService
// 삭제 전 사용자 데이터 보관 (after_delete 훅에서 사용)
$userData = $user->only(['id', 'uuid', 'name', 'email']);
// Before 훅
HookManager::doAction('core.user.before_delete', $user);
// 아바타는 파일 삭제를 동반하므로 커밋 후에 처리한다 (롤백되어도 파일은 안 돌아온다).
$avatarAttachment = $user->avatarAttachment;
// 역할 연결 해제 (명시적 삭제 - CASCADE 의존 금지)
$user->roles()->detach();
// 마지막 delete 가 FK 등으로 실패하면 역할·동의·토큰만 사라지고 계정은 남는
// 껍데기 활성 계정이 된다 (실회귀 이력 존재). 전 단계를 하나로 묶는다.
$result = DB::transaction(function () use ($user) {
// Before 훅
HookManager::doAction('core.user.before_delete', $user);
// 역할 연결 해제 (명시적 삭제 - CASCADE 의존 금지)
$user->roles()->detach();
// 약관 동의 이력 삭제
$user->consents()->delete();
// API 토큰 삭제
$user->tokens()->delete();
return $this->userRepository->delete($user);
});
// 커밋 성공 후 부수효과
$user->flushPermissionCaches();
// 약관 동의 이력 삭제
$user->consents()->delete();
// API 토큰 삭제
$user->tokens()->delete();
// 아바타 Attachment 삭제 (다형성 관계)
$avatarAttachment = $user->avatarAttachment;
if ($avatarAttachment) {
$this->attachmentService->delete($avatarAttachment->id);
try {
$this->attachmentService->delete($avatarAttachment->id);
} catch (Exception $e) {
Log::warning('사용자 삭제 아바타 정리 실패', [
'user_id' => $userData['id'] ?? null,
'attachment_id' => $avatarAttachment->id,
'error' => $e->getMessage(),
]);
}
}
$result = $this->userRepository->delete($user);
// After 훅: 삭제된 사용자 데이터 전달
HookManager::doAction('core.user.after_delete', $userData);
@@ -579,6 +698,12 @@ class UserService
$statusEnum = UserStatus::from($status);
// 일괄 '탈퇴'는 건별 정식 탈퇴로 전환한다 — 상태 컬럼만 바꾸면 익명화와
// before/after_withdraw 훅이 통째로 생략되어, 본인 탈퇴와 결과가 달라진다.
if ($statusEnum === UserStatus::Withdrawn) {
return $this->bulkWithdraw($uuids, $status);
}
// UUID → 정수 ID 변환 (내부 쿼리용)
$userIds = $this->userRepository->getIdsByUuids($uuids);
@@ -588,7 +713,8 @@ class UserService
$updateData = ['status' => $statusEnum->value];
$updateData = match ($statusEnum) {
UserStatus::Blocked => array_merge($updateData, ['blocked_at' => now()]),
UserStatus::Withdrawn => array_merge($updateData, ['withdrawn_at' => now()]),
// 탈퇴는 위에서 건별 withdrawUser() 로 분기하므로 여기로는 도달하지 않는다.
UserStatus::Withdrawn => $updateData,
UserStatus::Active => array_merge($updateData, ['blocked_at' => null, 'withdrawn_at' => null]),
UserStatus::Inactive => $updateData,
};
@@ -608,6 +734,83 @@ class UserService
return [
'updated_count' => $updatedCount,
'failed_count' => 0,
'failed_reasons' => [],
];
}
/**
* 탈퇴 실패 예외를 운영자에게 보여줄 한 줄 사유로 바꿉니다.
*
* @param Exception $e 탈퇴 시도에서 발생한 예외
* @return string 운영자에게 보여줄 실패 사유
*/
private function describeWithdrawFailure(Exception $e): string
{
if ($e instanceof ValidationException) {
$first = collect($e->errors())->flatten()->first();
if (is_string($first) && $first !== '') {
return $first;
}
}
if ($e instanceof CannotDeleteSuperAdminException) {
return __('exceptions.cannot_delete_super_admin');
}
// 알 수 없는 예외의 원문 메시지는 사유로 쓰지 않는다 — 내부 사정(SQL·경로·클래스명)이
// 운영자 화면으로 새고, 화면 문구 조립에서 파라미터 구분자로 쓰이는 문자(`|` `&` `=`)가
// 섞이면 안내가 잘린다. 사유는 언어 파일이 소유하는 문구로만 구성한다.
return __('user.withdraw_failed_unknown');
}
/**
* 일괄 탈퇴를 건별 정식 탈퇴로 수행합니다.
*
* @param array<int, string> $uuids 대상 사용자 UUID 배열
* @param string $status 요청 상태값 (훅 전달용)
* @return array{updated_count: int, failed_count: int, failed_reasons: array<int, string>} 처리 결과
*/
private function bulkWithdraw(array $uuids, string $status): array
{
$updatedCount = 0;
$failedCount = 0;
$failedReasons = [];
foreach ($uuids as $uuid) {
$user = $this->userRepository->findByUuid($uuid);
if (! $user) {
$failedCount++;
$failedReasons[] = __('user.not_found');
continue;
}
try {
$this->withdrawUser($user);
$updatedCount++;
} catch (Exception $e) {
$failedCount++;
$failedReasons[] = $this->describeWithdrawFailure($e);
Log::warning('일괄 탈퇴 건 실패', [
'uuid' => $uuid,
'error' => $e->getMessage(),
]);
}
}
// after_bulk_update 훅 실행
HookManager::doAction('sirsoft-core.user.after_bulk_update', $uuids, $status, $updatedCount);
// 사유를 함께 돌려주지 않으면 화면은 "몇 건 실패" 까지만 말할 수 있다 —
// 운영자는 왜 안 됐는지 알 방법이 없어 같은 조작을 반복하게 된다.
return [
'updated_count' => $updatedCount,
'failed_count' => $failedCount,
'failed_reasons' => array_values(array_unique($failedReasons)),
];
}