feat: [users] 사용자 관리에 직급/직책 입력 UI 추가

- 사용자 수정/생성 화면에 직급(position_key), 직책(job_title_key) 선택 필드 추가
- HR 사원관리의 position-add-modal 재사용 ([+] 버튼으로 새 직급/직책 추가)
- UserService에서 tenant_user_profiles 테이블에 저장 (updateOrInsert)
- UpdateUserRequest, StoreUserRequest에 validation 규칙 추가
This commit is contained in:
김보곤
2026-02-28 08:07:21 +09:00
parent ac3b72cac6
commit 0ee6b9f77a
6 changed files with 147 additions and 5 deletions

View File

@@ -3,11 +3,13 @@
namespace App\Http\Controllers;
use App\Models\Department;
use App\Models\HR\Position;
use App\Models\Role;
use App\Models\Tenants\Tenant;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
class UserController extends Controller
@@ -40,6 +42,10 @@ public function create(): View
$roles = $tenantId ? Role::where('tenant_id', $tenantId)->orderBy('name')->get() : collect();
$departments = $tenantId ? Department::where('tenant_id', $tenantId)->where('is_active', true)->orderBy('name')->get() : collect();
// 직급/직책 목록
$ranks = $tenantId ? Position::forTenant($tenantId)->ranks()->where('is_active', true)->ordered()->get() : collect();
$titles = $tenantId ? Position::forTenant($tenantId)->titles()->where('is_active', true)->ordered()->get() : collect();
// 본사 테넌트 여부 확인 (본사: 이메일 인증, 그 외: 비밀번호 직접 입력)
$isHQ = false;
if ($tenantId) {
@@ -47,7 +53,7 @@ public function create(): View
$isHQ = $tenant?->tenant_type === 'HQ';
}
return view('users.create', compact('roles', 'departments', 'isHQ'));
return view('users.create', compact('roles', 'departments', 'isHQ', 'ranks', 'titles'));
}
/**
@@ -76,6 +82,19 @@ public function edit(int $id): View
$userRoleIds = $tenantId ? $user->userRoles()->where('tenant_id', $tenantId)->pluck('role_id')->toArray() : [];
$userDepartmentIds = $tenantId ? $user->departmentUsers()->where('tenant_id', $tenantId)->pluck('department_id')->toArray() : [];
return view('users.edit', compact('user', 'roles', 'departments', 'userRoleIds', 'userDepartmentIds'));
// 직급/직책 목록
$ranks = $tenantId ? Position::forTenant($tenantId)->ranks()->where('is_active', true)->ordered()->get() : collect();
$titles = $tenantId ? Position::forTenant($tenantId)->titles()->where('is_active', true)->ordered()->get() : collect();
// tenant_user_profiles에서 현재 position_key, job_title_key 조회
$profile = $tenantId
? DB::table('tenant_user_profiles')
->where('tenant_id', $tenantId)
->where('user_id', $user->id)
->first(['position_key', 'job_title_key'])
: null;
return view('users.edit', compact('user', 'roles', 'departments', 'userRoleIds', 'userDepartmentIds',
'ranks', 'titles', 'profile'));
}
}

View File

@@ -63,6 +63,8 @@ public function rules(): array
'role_ids.*' => 'integer|exists:roles,id',
'department_ids' => 'nullable|array',
'department_ids.*' => 'integer|exists:departments,id',
'position_key' => 'nullable|string|max:64',
'job_title_key' => 'nullable|string|max:64',
];
// 비본사 테넌트: 비밀번호 직접 입력 필수

View File

@@ -68,6 +68,8 @@ public function rules(): array
'role_ids.*' => 'integer|exists:roles,id',
'department_ids' => 'nullable|array',
'department_ids.*' => 'integer|exists:departments,id',
'position_key' => 'nullable|string|max:64',
'job_title_key' => 'nullable|string|max:64',
];
}

View File

@@ -105,8 +105,11 @@ public function createUser(array $data): User
$data['password'] = Hash::make($plainPassword);
}
// password_confirmation은 User 모델의 fillable이 아니므로 제거
// User 모델의 fillable이 아닌 필드 분리
unset($data['password_confirmation']);
$positionKey = $data['position_key'] ?? null;
$jobTitleKey = $data['job_title_key'] ?? null;
unset($data['position_key'], $data['job_title_key']);
// is_active 처리
$data['is_active'] = isset($data['is_active']) && $data['is_active'] == '1';
@@ -136,6 +139,23 @@ public function createUser(array $data): User
$this->syncDepartments($user, $tenantId, $departmentIds);
}
// position_key, job_title_key → tenant_user_profiles 저장
if ($tenantId) {
$profileFields = [];
if (! empty($positionKey)) {
$profileFields['position_key'] = $positionKey;
}
if (! empty($jobTitleKey)) {
$profileFields['job_title_key'] = $jobTitleKey;
}
if (! empty($profileFields)) {
DB::table('tenant_user_profiles')->updateOrInsert(
['tenant_id' => $tenantId, 'user_id' => $user->id],
$profileFields
);
}
}
// 본사만 비밀번호 안내 메일 발송 (비본사는 관리자가 직접 알려줌)
if ($plainPassword !== null) {
$this->sendPasswordMail($user, $plainPassword, true);
@@ -228,8 +248,25 @@ public function updateUser(int $id, array $data): bool
$this->syncDepartments($user, $tenantId, $departmentIds);
}
// role_ids, department_ids는 User 모델의 fillable이 아니므로 제거
unset($data['role_ids'], $data['department_ids']);
// position_key, job_title_key → tenant_user_profiles 저장
if ($tenantId) {
$profileFields = [];
if (array_key_exists('position_key', $data)) {
$profileFields['position_key'] = $data['position_key'] ?: null;
}
if (array_key_exists('job_title_key', $data)) {
$profileFields['job_title_key'] = $data['job_title_key'] ?: null;
}
if (! empty($profileFields)) {
DB::table('tenant_user_profiles')->updateOrInsert(
['tenant_id' => $tenantId, 'user_id' => $id],
$profileFields
);
}
}
// role_ids, department_ids, position/job_title은 User 모델의 fillable이 아니므로 제거
unset($data['role_ids'], $data['department_ids'], $data['position_key'], $data['job_title_key']);
return $user->update($data);
}

View File

@@ -66,6 +66,45 @@ class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none foc
</div>
</div>
<!-- 직급/직책 -->
<div class="mb-8">
<h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">직급/직책</h2>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">직급</label>
<div class="flex gap-2">
<select name="position_key" id="position_key"
class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="">선택하세요</option>
@foreach($ranks as $rank)
<option value="{{ $rank->key }}">{{ $rank->name }}</option>
@endforeach
</select>
<button type="button" onclick="openPositionModal('rank')"
class="px-3 py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg transition text-sm font-bold"
title="직급 추가">+</button>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">직책</label>
<div class="flex gap-2">
<select name="job_title_key" id="job_title_key"
class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="">선택하세요</option>
@foreach($titles as $title)
<option value="{{ $title->key }}">{{ $title->name }}</option>
@endforeach
</select>
<button type="button" onclick="openPositionModal('title')"
class="px-3 py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg transition text-sm font-bold"
title="직책 추가">+</button>
</div>
</div>
</div>
</div>
@include('hr.employees.partials.position-add-modal')
<!-- 비밀번호 -->
<div class="mb-8">
<h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">비밀번호</h2>

View File

@@ -70,6 +70,49 @@ class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none foc
</div>
</div>
<!-- 직급/직책 -->
<div class="mb-8">
<h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">직급/직책</h2>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">직급</label>
<div class="flex gap-2">
<select name="position_key" id="position_key"
class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="">선택하세요</option>
@foreach($ranks as $rank)
<option value="{{ $rank->key }}" {{ ($profile?->position_key ?? '') === $rank->key ? 'selected' : '' }}>
{{ $rank->name }}
</option>
@endforeach
</select>
<button type="button" onclick="openPositionModal('rank')"
class="px-3 py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg transition text-sm font-bold"
title="직급 추가">+</button>
</div>
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">직책</label>
<div class="flex gap-2">
<select name="job_title_key" id="job_title_key"
class="flex-1 px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500">
<option value="">선택하세요</option>
@foreach($titles as $title)
<option value="{{ $title->key }}" {{ ($profile?->job_title_key ?? '') === $title->key ? 'selected' : '' }}>
{{ $title->name }}
</option>
@endforeach
</select>
<button type="button" onclick="openPositionModal('title')"
class="px-3 py-2 bg-gray-100 hover:bg-gray-200 text-gray-600 rounded-lg transition text-sm font-bold"
title="직책 추가">+</button>
</div>
</div>
</div>
</div>
@include('hr.employees.partials.position-add-modal')
<!-- 비밀번호 초기화 -->
<div class="mb-8">
<h2 class="text-lg font-semibold text-gray-800 mb-4 pb-2 border-b">비밀번호</h2>