목록이 뒤쪽 페이지로 갈수록 느려지는 원인은 건너뛰는 행의 넓은 컬럼까지 읽는 것이었다. 지연 조인(inner 는 키 컬럼만, outer 는 이번 페이지 행만 전체 컬럼)과 정렬 컬럼 해석을 코어 공통 트레이트로 두고 코어 8 + 확장 6 저장소에 적용했다. 감사 지적 처리: - 그룹 쿼리의 총 건수 오산 — count 는 groupBy 를 벗기지 않아 첫 그룹의 행 수를 총 건수로 돌려준다. getCountForPagination 으로 교정. - audit 전수 스캔이 버퍼 한계로 한 번도 동작하지 않던 결함과, 확장 경로를 놓치던 정규식 5건을 함께 고쳤다. 그동안 번들 확장의 위반이 보이지 않았다. raw 20건을 빌더 표현으로 치환하고 하드코딩 테이블명 16건을 모델 유래로 바꿨다. 이 과정에서 마이페이지 「내가 댓글 단 글」 검색이 조인 별칭 충돌로 SQL 오류를 내던 결함이 해소됐다. 재발 차단용 룰 2종을 신설하고, 컬럼 프루닝 룰을 error 로 승격했다 (전역 위반 0건 — warning 으로 두면 "0 error" 판정을 통과하며 무음으로 재누적된다). 정렬 허용 목록은 FormRequest 게이트를 덮어야 한다. 좁으면 검증을 통과한 정렬이 422 도 로그도 없이 기본값으로 되돌아간다(스케줄 실행 이력의 종료시각·소요시간 정렬이 그랬다). 호출처를 스캔해 매핑 완전성까지 검사하는 계약 테스트로 전수 강제한다. 해외 배송지 결함도 함께 고쳤다 — 조회는 city/state/postal_code 로 내려주고 폼은 intl_* 로 제출하는 비대칭 때문에, 수정 시 입력칸이 비어 보이고 저장하면 기존 값이 유실됐다. 쿠폰 수정의 유효기간 미검증과 1인당 사용 제한 부분 수정 차단도 함께 수정.
236 lines
7.1 KiB
PHP
236 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Contracts\Repositories\RoleRepositoryInterface;
|
|
use App\Enums\ExtensionOwnerType;
|
|
use App\Models\Role;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class RoleRepository implements RoleRepositoryInterface
|
|
{
|
|
/**
|
|
* 모든 역할을 조회합니다.
|
|
*
|
|
* @return Collection 역할 컬렉션
|
|
*/
|
|
public function getAll(): Collection
|
|
{
|
|
return Role::with(['permissions'])
|
|
->orderBy('id')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* 활성화된 역할들만 조회합니다.
|
|
*
|
|
* @return Collection 활성화된 역할 컬렉션
|
|
*/
|
|
public function getActiveRoles(): Collection
|
|
{
|
|
return Role::where('is_active', true)
|
|
->orderBy('id')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* ID로 역할을 찾습니다.
|
|
*
|
|
* @param int $id 역할 ID
|
|
* @return Role|null 찾은 역할 모델 또는 null
|
|
*/
|
|
public function findById(int $id): ?Role
|
|
{
|
|
return Role::with(['permissions'])->find($id);
|
|
}
|
|
|
|
/**
|
|
* 식별자로 역할을 찾습니다.
|
|
*
|
|
* @param string $identifier 역할 식별자
|
|
* @return Role|null 찾은 역할 모델 또는 null
|
|
*/
|
|
public function findByIdentifier(string $identifier): ?Role
|
|
{
|
|
return Role::where('identifier', $identifier)->first();
|
|
}
|
|
|
|
/**
|
|
* 식별자로 역할에 소속된 사용자들을 조회합니다.
|
|
*
|
|
* 역할이 없거나 소속 사용자가 없으면 빈 컬렉션을 반환합니다.
|
|
*
|
|
* @param string $identifier 역할 식별자
|
|
* @return Collection 역할 소속 사용자 컬렉션
|
|
*/
|
|
public function getUsersByIdentifier(string $identifier): Collection
|
|
{
|
|
$role = Role::where('identifier', $identifier)->first();
|
|
|
|
return $role ? $role->users()->get() : new Collection;
|
|
}
|
|
|
|
/**
|
|
* 새로운 역할을 생성합니다.
|
|
*
|
|
* @param array $data 역할 생성 데이터
|
|
* @return Role 생성된 역할 모델
|
|
*/
|
|
public function create(array $data): Role
|
|
{
|
|
return Role::create($data);
|
|
}
|
|
|
|
/**
|
|
* 역할을 생성하거나 업데이트합니다.
|
|
*
|
|
* @param array $attributes 조회 조건
|
|
* @param array $values 생성/업데이트할 데이터
|
|
* @return Role 생성 또는 업데이트된 역할 모델
|
|
*/
|
|
public function updateOrCreate(array $attributes, array $values): Role
|
|
{
|
|
return Role::updateOrCreate($attributes, $values);
|
|
}
|
|
|
|
/**
|
|
* 기존 역할을 업데이트합니다.
|
|
*
|
|
* @param Role $role 업데이트할 역할 모델
|
|
* @param array $data 업데이트할 데이터
|
|
* @return bool 업데이트 성공 여부
|
|
*/
|
|
public function update(Role $role, array $data): bool
|
|
{
|
|
return $role->update($data);
|
|
}
|
|
|
|
/**
|
|
* 역할을 삭제합니다.
|
|
*
|
|
* @param Role $role 삭제할 역할 모델
|
|
* @return bool 삭제 성공 여부
|
|
*/
|
|
public function delete(Role $role): bool
|
|
{
|
|
return $role->delete();
|
|
}
|
|
|
|
/**
|
|
* 확장이 소유한 역할을 식별자로 찾습니다.
|
|
*
|
|
* @param string $identifier 역할 식별자
|
|
* @param ExtensionOwnerType $extensionType 확장 타입
|
|
* @param string $extensionIdentifier 확장 식별자
|
|
* @return Role|null 찾은 역할 모델 또는 null
|
|
*/
|
|
public function findExtensionRoleByIdentifier(string $identifier, ExtensionOwnerType $extensionType, string $extensionIdentifier): ?Role
|
|
{
|
|
return Role::where('identifier', $identifier)
|
|
->where('extension_type', $extensionType)
|
|
->where('extension_identifier', $extensionIdentifier)
|
|
->first();
|
|
}
|
|
|
|
/**
|
|
* 확장이 소유한 모든 역할을 조회합니다.
|
|
*
|
|
* @param ExtensionOwnerType $extensionType 확장 타입
|
|
* @param string $extensionIdentifier 확장 식별자
|
|
* @return Collection 해당 확장 소유 역할 컬렉션
|
|
*/
|
|
public function getByExtension(ExtensionOwnerType $extensionType, string $extensionIdentifier): Collection
|
|
{
|
|
return Role::where('extension_type', $extensionType)
|
|
->where('extension_identifier', $extensionIdentifier)
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* 역할에 권한을 할당합니다.
|
|
*
|
|
* 기존 권한을 유지하면서 새 권한만 추가합니다.
|
|
*
|
|
* @param Role $role 역할 모델
|
|
* @param int $permissionId 권한 ID
|
|
* @param array $pivotData 피벗 테이블에 저장할 추가 데이터
|
|
*/
|
|
public function attachPermission(Role $role, int $permissionId, array $pivotData = []): void
|
|
{
|
|
$role->permissions()->syncWithoutDetaching([
|
|
$permissionId => $pivotData,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 역할에서 권한을 해제합니다.
|
|
*
|
|
* @param Role $role 역할 모델
|
|
* @param int $permissionId 권한 ID
|
|
* @return int 해제된 권한 수
|
|
*/
|
|
public function detachPermission(Role $role, int $permissionId): int
|
|
{
|
|
return $role->permissions()->detach($permissionId);
|
|
}
|
|
|
|
/**
|
|
* 역할의 모든 권한을 해제합니다.
|
|
*
|
|
* @param Role $role 역할 모델
|
|
* @return int 해제된 권한 수
|
|
*/
|
|
public function detachAllPermissions(Role $role): int
|
|
{
|
|
return $role->permissions()->detach();
|
|
}
|
|
|
|
/**
|
|
* 역할 목록을 페이지네이션하여 조회합니다.
|
|
*
|
|
* @param array $filters 필터 조건
|
|
* @param int $perPage 페이지당 항목 수
|
|
* @return LengthAwarePaginator 페이지네이션된 역할 목록
|
|
*/
|
|
public function getPaginated(array $filters = [], int $perPage = 20): LengthAwarePaginator
|
|
{
|
|
$query = Role::query()
|
|
->withCount('users')
|
|
->with('permissions');
|
|
|
|
// 검색 필터
|
|
if (! empty($filters['search'])) {
|
|
$search = $filters['search'];
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('identifier', 'like', "%{$search}%")
|
|
->orWhere('name->ko', 'like', "%{$search}%")
|
|
->orWhere('name->en', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
// 활성화 상태 필터
|
|
if (isset($filters['is_active'])) {
|
|
$query->where('is_active', $filters['is_active']);
|
|
}
|
|
|
|
// 정렬 (코어/확장 소유 역할 먼저, 그 다음 사용자 생성 역할)
|
|
$query->orderByRaw('CASE WHEN extension_type IS NOT NULL THEN 0 ELSE 1 END')
|
|
->orderBy('created_at', 'desc');
|
|
|
|
// audit:allow repository-paginate-column-pruning reason: 역할 정의 테이블 — 행 수가 운영상 고정(수십 건)이고 넓은 컬럼이 없다
|
|
return $query->paginate($perPage);
|
|
}
|
|
|
|
/**
|
|
* 역할에 할당된 권한 개수를 반환합니다.
|
|
*
|
|
* @param Role $role 역할 모델
|
|
* @return int 권한 개수
|
|
*/
|
|
public function getPermissionCount(Role $role): int
|
|
{
|
|
return $role->permissions()->count();
|
|
}
|
|
}
|