Files
sam-api/app/Services/RegisterService.php
hskwon c558606954 feat: 회원가입 API 응답에 메뉴 리스트 추가
주요 변경사항:
- RegisterService.php: 생성된 메뉴 정보를 조회해서 응답에 포함
- RegisterApi.php: Swagger 문서에 menus 배열 스키마 추가

응답 구조:
{
  user: {...},
  tenant: {...},
  menus: [
    {id, parent_id, name, url, icon, sort_order, is_external, external_url}
  ]
}

기술 세부사항:
- 생성된 메뉴 ID 배열을 사용해 Menu::whereIn() 조회
- parent_id, sort_order 순서로 정렬
- 로그인 API와 동일한 메뉴 정보 구조 제공
2025-11-10 09:44:22 +09:00

138 lines
5.2 KiB
PHP

<?php
namespace App\Services;
use App\Helpers\TenantCodeGenerator;
use App\Models\Commons\Menu;
use App\Models\Members\User;
use App\Models\Tenants\Tenant;
use App\Models\Tenants\TenantUserProfile;
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. Generate unique tenant code from company name
$code = TenantCodeGenerator::generate($params['company_name']);
// 2. Create Tenant with trial status and options
$tenant = Tenant::create([
'company_name' => $params['company_name'],
'code' => $code,
'business_num' => $params['business_num'] ?? null,
'tenant_st_code' => 'trial', // 트라이얼 상태
'options' => [
'company_scale' => $params['company_scale'] ?? null,
'industry' => $params['industry'] ?? null,
],
]);
// 3. Create default menus for tenant
$menuIds = MenuBootstrapService::createDefaultMenus($tenant->id);
// 4. 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,
],
]);
// 5. Create TenantUserProfile (tenant-user mapping)
TenantUserProfile::create([
'user_id' => $user->id,
'tenant_id' => $tenant->id,
'is_default' => 1, // 기본 테넌트로 설정
'is_active' => 1, // 활성화
]);
// 6. Set tenant context for permissions
app()->bind('tenant_id', fn () => $tenant->id);
app(PermissionRegistrar::class)->setPermissionsTeamId($tenant->id);
// 7. Create 'system_manager' role
$role = Role::create([
'tenant_id' => $tenant->id,
'guard_name' => 'api',
'name' => 'system_manager',
'description' => '시스템 관리자',
]);
// 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. Get created menus
$menus = Menu::whereIn('id', $menuIds)
->orderBy('parent_id')
->orderBy('sort_order')
->get(['id', 'parent_id', 'name', 'url', 'icon', 'sort_order', 'is_external', 'external_url'])
->toArray();
// 12. Return user, tenant, and menus 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,
],
'menus' => $menus,
];
});
}
}