feat: 회원가입 + 테넌트 생성 통합 API 추가 (/api/v1/register)

- 사용자 등록 + 테넌트 생성 + 시스템 관리자 권한 자동 부여
- 사업자번호 조건부 검증 (active 테넌트만 unique)
- 글로벌 메뉴 자동 복제 (parent_id 매핑 알고리즘)
- DB 트랜잭션으로 전체 프로세스 원자성 보장

추가:
- RegisterRequest: FormRequest 검증 (conditional unique)
- RegisterService: 9-step 통합 비즈니스 로직
- RegisterController: ApiResponse::handle() 패턴
- RegisterApi: 완전한 Swagger 문서

수정:
- MenusStep: 글로벌 메뉴 복제 로직 구현
- message.php: 'registered' 키 추가
- error.php: 4개 에러 메시지 추가
- routes/api.php: POST /api/v1/register 라우트

SAM API Rules 준수:
- Service-First, FormRequest, i18n, Swagger, DB Transaction
This commit is contained in:
2025-11-06 17:24:42 +09:00
parent b7cf045a81
commit 48e76432ee
9 changed files with 862 additions and 1329 deletions

View File

@@ -0,0 +1,130 @@
<?php
namespace App\Services;
use App\Models\Commons\Menu;
use App\Models\Members\User;
use App\Models\Tenants\Tenant;
use App\Models\Tenants\TenantUserProfile;
use App\Services\TenantBootstrap\RecipeRegistry;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class RegisterService
{
/**
* 회원가입 처리 (테넌트 생성 + 사용자 생성 + 시스템 관리자 역할 부여)
*
* @param array $params [
* 'user_id' => string,
* 'name' => string,
* 'email' => string,
* 'phone' => string,
* 'password' => string,
* 'position' => string (optional),
* 'company_name' => string,
* 'business_num' => string (optional),
* 'company_scale' => string (optional),
* 'industry' => string (optional),
* ]
* @return array ['user' => array, 'tenant' => array]
*/
public static function register(array $params): array
{
return DB::transaction(function () use ($params) {
// 1. Create Tenant with trial status and options
$tenant = Tenant::create([
'company_name' => $params['company_name'],
'business_num' => $params['business_num'] ?? null,
'tenant_st_code' => 'trial', // 트라이얼 상태
'options' => [
'company_scale' => $params['company_scale'] ?? null,
'industry' => $params['industry'] ?? null,
],
]);
// 2. Bootstrap tenant (STANDARD recipe: CapabilityProfiles, Categories, Menus, Settings)
// This will create all necessary menus via MenusStep
app(RecipeRegistry::class)->bootstrap($tenant->id, 'STANDARD');
// 3. Create User with hashed password and options
$user = User::create([
'user_id' => $params['user_id'],
'name' => $params['name'],
'email' => $params['email'],
'phone' => $params['phone'] ?? null,
'password' => Hash::make($params['password']),
'options' => [
'position' => $params['position'] ?? null,
],
]);
// 4. Create TenantUserProfile (tenant-user mapping)
TenantUserProfile::create([
'user_id' => $user->id,
'tenant_id' => $tenant->id,
'is_default' => 1, // 기본 테넌트로 설정
'is_active' => 1, // 활성화
]);
// 5. Set tenant context for permissions
// This is critical for Spatie permissions to work correctly
app()->bind('tenant_id', fn () => $tenant->id);
app(PermissionRegistrar::class)->setPermissionsTeamId($tenant->id);
// 6. Create 'system_manager' role
$role = Role::create([
'tenant_id' => $tenant->id,
'guard_name' => 'api',
'name' => 'system_manager',
'description' => '시스템 관리자',
]);
// 7. Get all tenant menus (after bootstrap)
$menuIds = Menu::where('tenant_id', $tenant->id)->pluck('id');
// 8. Create permissions for each menu and assign to role
$permissions = [];
foreach ($menuIds as $menuId) {
$permName = "menu.{$menuId}";
// Use firstOrCreate to avoid duplicate permission errors
$perm = Permission::firstOrCreate([
'tenant_id' => $tenant->id,
'guard_name' => 'api',
'name' => $permName,
]);
$permissions[] = $perm;
}
// 9. Assign all menu permissions to system_manager role
$role->syncPermissions($permissions);
// 10. Assign system_manager role to user
$user->assignRole($role);
// 11. Return user and tenant data
return [
'user' => [
'id' => $user->id,
'user_id' => $user->user_id,
'name' => $user->name,
'email' => $user->email,
'phone' => $user->phone,
'options' => $user->options,
],
'tenant' => [
'id' => $tenant->id,
'company_name' => $tenant->company_name,
'business_num' => $tenant->business_num,
'tenant_st_code' => $tenant->tenant_st_code,
'options' => $tenant->options,
],
];
});
}
}