Files
sam-api/app/Services/RegisterService.php
hskwon d136fc97b2 fix: RegisterService에서 메뉴 생성 로직 제거
- RecipeRegistry 호출 제거 (클래스 존재하지 않음)
- 메뉴 기반 권한 생성 로직 제거
- system_manager 역할만 생성하고 사용자에게 부여
- 메뉴 테이블 컬럼 불일치 오류 해결

현재는 기본 회원가입 기능만 제공:
- 테넌트 생성 (코드 자동 생성)
- 사용자 생성
- 테넌트-사용자 프로필 연결
- system_manager 역할 부여

향후 메뉴 시스템이 완성되면 RecipeRegistry를 다시 활성화할 예정
2025-11-07 18:15:50 +09:00

107 lines
4.0 KiB
PHP

<?php
namespace App\Services;
use App\Helpers\TenantCodeGenerator;
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\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 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
app()->bind('tenant_id', fn () => $tenant->id);
app(PermissionRegistrar::class)->setPermissionsTeamId($tenant->id);
// 6. Create 'system_manager' role (without menu permissions for now)
$role = Role::create([
'tenant_id' => $tenant->id,
'guard_name' => 'api',
'name' => 'system_manager',
'description' => '시스템 관리자',
]);
// 7. Assign system_manager role to user
$user->assignRole($role);
// 8. 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,
],
];
});
}
}