style: Laravel Pint 코드 포맷팅 적용

- PSR-12 스타일 가이드 준수
- 302개 파일 스타일 이슈 자동 수정
- 코드 로직 변경 없음 (포맷팅만)
This commit is contained in:
2025-11-06 17:45:49 +09:00
parent 48e76432ee
commit cc206fdbed
294 changed files with 4476 additions and 2561 deletions

View File

@@ -20,6 +20,7 @@ protected function v(array $params, array $rules)
if ($v->fails()) {
return ['error' => $v->errors()->first(), 'code' => 422];
}
return $v->validated();
}
@@ -27,30 +28,34 @@ protected function v(array $params, array $rules)
public function index(array $params)
{
$p = $this->v($params, [
'q' => 'nullable|string|max:100',
'q' => 'nullable|string|max:100',
'is_active' => 'nullable|in:0,1',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$q = Department::query();
if (isset($p['is_active'])) {
$q->where('is_active', (int)$p['is_active']);
$q->where('is_active', (int) $p['is_active']);
}
if (!empty($p['q'])) {
if (! empty($p['q'])) {
$q->where(function ($w) use ($p) {
$w->where('name', 'like', '%' . $p['q'] . '%')
->orWhere('code', 'like', '%' . $p['q'] . '%');
$w->where('name', 'like', '%'.$p['q'].'%')
->orWhere('code', 'like', '%'.$p['q'].'%');
});
}
$q->orderBy('sort_order')->orderBy('name');
$perPage = $p['per_page'] ?? 20;
$page = $p['page'] ?? null;
$page = $p['page'] ?? null;
return $q->paginate($perPage, ['*'], 'page', $page);
}
@@ -62,30 +67,36 @@ public function store(array $params)
$userId = $this->apiUserId();
$p = $this->v($params, [
'code' => 'nullable|string|max:50',
'name' => 'required|string|max:100',
'code' => 'nullable|string|max:50',
'name' => 'required|string|max:100',
'description' => 'nullable|string|max:255',
'is_active' => 'nullable|in:0,1',
'sort_order' => 'nullable|integer',
'created_by' => 'nullable|integer|min:1',
'is_active' => 'nullable|in:0,1',
'sort_order' => 'nullable|integer',
'created_by' => 'nullable|integer|min:1',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
if (!empty($p['code'])) {
if (! empty($p['code'])) {
$exists = Department::query()->where('code', $p['code'])->exists();
if ($exists) return ['error' => '이미 존재하는 부서 코드입니다.', 'code' => 409];
if ($exists) {
return ['error' => '이미 존재하는 부서 코드입니다.', 'code' => 409];
}
}
$dept = Department::create([
'tenant_id' => $tenantId,
'code' => $p['code'] ?? null,
'name' => $p['name'],
'tenant_id' => $tenantId,
'code' => $p['code'] ?? null,
'name' => $p['name'],
'description' => $p['description'] ?? null,
'is_active' => isset($p['is_active']) ? (int)$p['is_active'] : 1,
'sort_order' => $p['sort_order'] ?? 0,
'created_by' => $userId ?? null,
'updated_by' => $userId ?? null,
'is_active' => isset($p['is_active']) ? (int) $p['is_active'] : 1,
'sort_order' => $p['sort_order'] ?? 0,
'created_by' => $userId ?? null,
'updated_by' => $userId ?? null,
]);
return $dept->fresh();
@@ -94,49 +105,62 @@ public function store(array $params)
/** 단건 */
public function show(int $id, array $params = [])
{
if (!$id) return ['error' => 'id가 필요합니다.', 'code' => 400];
if (! $id) {
return ['error' => 'id가 필요합니다.', 'code' => 400];
}
$res = Department::query()->find($id);
if (!$res) {
if (! $res) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
return $res;
}
/** 수정 */
public function update(int $id, array $params)
{
if (!$id) return ['error' => 'id가 필요합니다.', 'code' => 400];
if (! $id) {
return ['error' => 'id가 필요합니다.', 'code' => 400];
}
$p = $this->v($params, [
'code' => 'nullable|string|max:50',
'name' => 'nullable|string|max:100',
'code' => 'nullable|string|max:50',
'name' => 'nullable|string|max:100',
'description' => 'nullable|string|max:255',
'is_active' => 'nullable|in:0,1',
'sort_order' => 'nullable|integer',
'updated_by' => 'nullable|integer|min:1',
'is_active' => 'nullable|in:0,1',
'sort_order' => 'nullable|integer',
'updated_by' => 'nullable|integer|min:1',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$dept = Department::query()->find($id);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
if (array_key_exists('code', $p) && !is_null($p['code'])) {
if (array_key_exists('code', $p) && ! is_null($p['code'])) {
$exists = Department::query()
->where('code', $p['code'])
->where('id', '!=', $id)
->exists();
if ($exists) return ['error' => '이미 존재하는 부서 코드입니다.', 'code' => 409];
if ($exists) {
return ['error' => '이미 존재하는 부서 코드입니다.', 'code' => 409];
}
}
$dept->fill([
'code' => array_key_exists('code', $p) ? $p['code'] : $dept->code,
'name' => $p['name'] ?? $dept->name,
'code' => array_key_exists('code', $p) ? $p['code'] : $dept->code,
'name' => $p['name'] ?? $dept->name,
'description' => $p['description'] ?? $dept->description,
'is_active' => isset($p['is_active']) ? (int)$p['is_active'] : $dept->is_active,
'sort_order' => $p['sort_order'] ?? $dept->sort_order,
'updated_by' => $p['updated_by'] ?? $dept->updated_by,
'is_active' => isset($p['is_active']) ? (int) $p['is_active'] : $dept->is_active,
'sort_order' => $p['sort_order'] ?? $dept->sort_order,
'updated_by' => $p['updated_by'] ?? $dept->updated_by,
])->save();
return $dept->fresh();
@@ -145,18 +169,26 @@ public function update(int $id, array $params)
/** 삭제(soft) */
public function destroy(int $id, array $params)
{
if (!$id) return ['error' => 'id가 필요합니다.', 'code' => 400];
if (! $id) {
return ['error' => 'id가 필요합니다.', 'code' => 400];
}
$p = $this->v($params, [
'deleted_by' => 'nullable|integer|min:1',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$dept = Department::query()->find($id);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
if (!empty($p['deleted_by'])) {
if (! empty($p['deleted_by'])) {
$dept->deleted_by = $p['deleted_by'];
$dept->save();
}
@@ -169,20 +201,26 @@ public function destroy(int $id, array $params)
public function listUsers(int $deptId, array $params)
{
$p = $this->v($params, [
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$dept = Department::query()->find($deptId);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
$builder = $dept->departmentUsers()->with('user')
->orderByDesc('is_primary')->orderBy('id');
$perPage = $p['per_page'] ?? 20;
$page = $p['page'] ?? null;
$page = $p['page'] ?? null;
return $builder->paginate($perPage, ['*'], 'page', $page);
}
@@ -191,15 +229,21 @@ public function listUsers(int $deptId, array $params)
public function attachUser(int $deptId, array $params)
{
$p = $this->v($params, [
'user_id' => 'required|integer|min:1',
'user_id' => 'required|integer|min:1',
'is_primary' => 'nullable|in:0,1',
'joined_at' => 'nullable|date',
'joined_at' => 'nullable|date',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$dept = Department::query()->find($deptId);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
$result = DB::transaction(function () use ($dept, $p) {
$tenantId = $this->tenantId();
@@ -213,7 +257,7 @@ public function attachUser(int $deptId, array $params)
return ['error' => '이미 배정된 사용자입니다.', 'code' => 409];
}
if (!empty($p['is_primary']) && (int)$p['is_primary'] === 1) {
if (! empty($p['is_primary']) && (int) $p['is_primary'] === 1) {
DepartmentUser::whereNull('deleted_at')
->where('user_id', $p['user_id'])
->update(['is_primary' => 0]);
@@ -221,10 +265,10 @@ public function attachUser(int $deptId, array $params)
$payload = [
'department_id' => $dept->id,
'tenant_id' => $tenantId,
'user_id' => $p['user_id'],
'is_primary' => isset($p['is_primary']) ? (int)$p['is_primary'] : 0,
'joined_at' => !empty($p['joined_at']) ? Carbon::parse($p['joined_at']) : now(),
'tenant_id' => $tenantId,
'user_id' => $p['user_id'],
'is_primary' => isset($p['is_primary']) ? (int) $p['is_primary'] : 0,
'joined_at' => ! empty($p['joined_at']) ? Carbon::parse($p['joined_at']) : now(),
];
if ($du) {
@@ -238,7 +282,10 @@ public function attachUser(int $deptId, array $params)
return ['department_id' => $dept->id, 'user_id' => $p['user_id']];
});
if ($result instanceof JsonResponse) return $result;
if ($result instanceof JsonResponse) {
return $result;
}
return $result;
}
@@ -250,12 +297,14 @@ public function detachUser(int $deptId, int $userId, array $params)
->where('user_id', $userId)
->first();
if (!$du) return ['error' => '배정된 사용자를 찾을 수 없습니다.', 'code' => 404];
if (! $du) {
return ['error' => '배정된 사용자를 찾을 수 없습니다.', 'code' => 404];
}
$du->delete();
return [
'user_id' => $userId,
'user_id' => $userId,
'deleted_at' => now()->toDateTimeString(),
];
}
@@ -266,8 +315,12 @@ public function setPrimary(int $deptId, int $userId, array $params)
$p = $this->v($params, [
'is_primary' => 'required|in:0,1',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
$result = DB::transaction(function () use ($deptId, $userId, $p) {
$du = DepartmentUser::whereNull('deleted_at')
@@ -275,23 +328,26 @@ public function setPrimary(int $deptId, int $userId, array $params)
->where('user_id', $userId)
->first();
if (!$du) {
if (! $du) {
return ['error' => '배정된 사용자를 찾을 수 없습니다.', 'code' => 404];
}
if ((int)$p['is_primary'] === 1) {
if ((int) $p['is_primary'] === 1) {
DepartmentUser::whereNull('deleted_at')
->where('user_id', $userId)
->update(['is_primary' => 0]);
}
$du->is_primary = (int)$p['is_primary'];
$du->is_primary = (int) $p['is_primary'];
$du->save();
return ['user_id' => $userId, 'department_id' => $deptId, 'is_primary' => $du->is_primary];
});
if ($result instanceof JsonResponse) return $result;
if ($result instanceof JsonResponse) {
return $result;
}
return $result;
}
@@ -300,19 +356,25 @@ public function listPermissions(int $deptId, array $params)
{
// 1) 파라미터 검증
$p = $this->v($params, [
'menu_id' => 'nullable|integer|min:1',
'is_allowed' => 'nullable|in:0,1',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
'menu_id' => 'nullable|integer|min:1',
'is_allowed' => 'nullable|in:0,1',
'page' => 'nullable|integer|min:1',
'per_page' => 'nullable|integer|min:1|max:200',
]);
if ($p instanceof JsonResponse) return $p;
if (isset($p['error'])) return $p;
if ($p instanceof JsonResponse) {
return $p;
}
if (isset($p['error'])) {
return $p;
}
// 2) 부서 확인
$dept = Department::query()->find($deptId);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
$tenantId = (int)$dept->tenant_id;
$tenantId = (int) $dept->tenant_id;
$modelType = Department::class;
// 3) ALLOW/DENY 서브쿼리 준비 (컬럼 형태 통일)
@@ -328,8 +390,8 @@ public function listPermissions(int $deptId, array $params)
])
->where([
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_id' => $deptId,
'tenant_id' => $tenantId,
]);
$denySub = DB::table('permission_overrides')
@@ -343,9 +405,9 @@ public function listPermissions(int $deptId, array $params)
'updated_at as override_updated_at',
])
->where([
'tenant_id' => $tenantId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'model_id' => $deptId,
])
->where('effect', -1);
@@ -369,11 +431,11 @@ public function listPermissions(int $deptId, array $params)
// 5) 필터
if (isset($p['is_allowed'])) {
$q->where('u.is_allowed', (int)$p['is_allowed']);
$q->where('u.is_allowed', (int) $p['is_allowed']);
}
if (isset($p['menu_id'])) {
// 권한코드가 menu.{id}.xxx 형태라는 전제
$q->where('permissions.name', 'like', 'menu.' . (int)$p['menu_id'] . '.%');
$q->where('permissions.name', 'like', 'menu.'.(int) $p['menu_id'].'.%');
}
// 6) 정렬(ALLOW 우선, 그 다음 permission_id 오름차순)
@@ -381,7 +443,7 @@ public function listPermissions(int $deptId, array $params)
// 7) 페이지네이션
$perPage = $p['per_page'] ?? 20;
$page = $p['page'] ?? null;
$page = $p['page'] ?? null;
return $q->paginate($perPage, ['*'], 'page', $page);
}
@@ -391,88 +453,93 @@ public function upsertPermissions(int $deptId, array $payload): array
{
// 단일이면 items로 감싸기
$items = isset($payload['permission_id'])
? [ $payload ]
? [$payload]
: ($payload['items'] ?? $payload);
$v = Validator::make(['items'=>$items], [
$v = Validator::make(['items' => $items], [
'items' => 'required|array|max:1000',
'items.*.permission_id' => 'required|integer|min:1',
'items.*.is_allowed' => 'nullable|in:0,1', // 생략 시 1(허용)
'items.*.is_allowed' => 'nullable|in:0,1', // 생략 시 1(허용)
]);
if ($v->fails()) return ['error'=>$v->errors()->first(),'code'=>422];
if ($v->fails()) {
return ['error' => $v->errors()->first(), 'code' => 422];
}
$dept = Department::query()->find($deptId);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
$tenantId = (int)$dept->tenant_id;
$tenantId = (int) $dept->tenant_id;
$modelType = Department::class;
$ok = 0; $failed = [];
$ok = 0;
$failed = [];
DB::transaction(function () use ($items, $tenantId, $deptId, $modelType, &$ok, &$failed) {
foreach ($items as $i => $r) {
try {
$permissionId = (int)$r['permission_id'];
$isAllowed = array_key_exists('is_allowed', $r) ? (int)$r['is_allowed'] : 1;
$permissionId = (int) $r['permission_id'];
$isAllowed = array_key_exists('is_allowed', $r) ? (int) $r['is_allowed'] : 1;
if ($isAllowed === 1) {
// ALLOW: Spatie 표준에 넣고, 동일 권한의 DENY 제거
$exists = DB::table('model_has_permissions')->where([
'permission_id' => $permissionId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
])->exists();
if (!$exists) {
if (! $exists) {
DB::table('model_has_permissions')->insert([
'permission_id' => $permissionId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
]);
}
DB::table('permission_overrides')->where([
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'permission_id' => $permissionId,
'effect' => -1,
'effect' => -1,
])->delete();
} else {
// DENY: overrides(effect=-1) upsert, 그리고 ALLOW 제거
$exists = DB::table('permission_overrides')->where([
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'permission_id' => $permissionId,
])->exists();
if ($exists) {
DB::table('permission_overrides')->where([
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'permission_id' => $permissionId,
])->update(['effect' => -1, 'updated_at' => now()]);
} else {
DB::table('permission_overrides')->insert([
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'permission_id' => $permissionId,
'effect' => -1,
'created_at' => now(),
'updated_at' => now(),
'effect' => -1,
'created_at' => now(),
'updated_at' => now(),
]);
}
DB::table('model_has_permissions')->where([
'permission_id' => $permissionId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
])->delete();
}
@@ -488,9 +555,9 @@ public function upsertPermissions(int $deptId, array $payload): array
});
return [
'processed' => count($items),
'succeeded' => $ok,
'failed' => $failed
'processed' => count($items),
'succeeded' => $ok,
'failed' => $failed,
];
}
@@ -498,41 +565,46 @@ public function upsertPermissions(int $deptId, array $payload): array
public function revokePermissions(int $deptId, array $payload): array
{
$items = isset($payload['permission_id'])
? [ $payload ]
? [$payload]
: ($payload['items'] ?? $payload);
$v = Validator::make(['items'=>$items], [
$v = Validator::make(['items' => $items], [
'items' => 'required|array|max:1000',
'items.*.permission_id' => 'required|integer|min:1',
]);
if ($v->fails()) return ['error'=>$v->errors()->first(),'code'=>422];
if ($v->fails()) {
return ['error' => $v->errors()->first(), 'code' => 422];
}
$dept = Department::query()->find($deptId);
if (!$dept) return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
if (! $dept) {
return ['error' => '부서를 찾을 수 없습니다.', 'code' => 404];
}
$tenantId = (int)$dept->tenant_id;
$tenantId = (int) $dept->tenant_id;
$modelType = Department::class;
$ok = 0; $failed = [];
$ok = 0;
$failed = [];
DB::transaction(function () use ($items, $tenantId, $deptId, $modelType, &$ok, &$failed) {
foreach ($items as $i => $r) {
try {
$permissionId = (int)$r['permission_id'];
$permissionId = (int) $r['permission_id'];
// ALLOW 제거
DB::table('model_has_permissions')->where([
'permission_id' => $permissionId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
])->delete();
// DENY/임시허용 오버라이드 제거
DB::table('permission_overrides')->where([
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'tenant_id' => $tenantId,
'model_type' => $modelType,
'model_id' => $deptId,
'permission_id' => $permissionId,
])->delete();
@@ -550,7 +622,7 @@ public function revokePermissions(int $deptId, array $payload): array
return [
'processed' => count($items),
'succeeded' => $ok,
'failed' => $failed
'failed' => $failed,
];
}
}