feat: 회원가입 시 기본 메뉴 자동 생성 및 권한 설정
- MenuBootstrapService 생성: 새 테넌트를 위한 기본 메뉴 9개 자동 생성
- 대시보드
- 기초정보관리 (제품/거래처/BOM 관리)
- 시스템 관리 (사용자/권한/부서 관리)
- RegisterService 수정: 메뉴 생성 후 권한 자동 설정
- 생성된 메뉴에 대한 권한(menu.{id}) 자동 생성
- system_manager 역할에 모든 메뉴 권한 할당
- 기존 테이블 구조에 맞게 구현 (code, route_name, depth, description 컬럼 미사용)
- message.registered 수정: '회원가입 처리'로 변경 (에러 메시지 개선)
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
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;
|
||||
|
||||
@@ -48,7 +50,10 @@ public static function register(array $params): array
|
||||
],
|
||||
]);
|
||||
|
||||
// 3. Create User with hashed password and options
|
||||
// 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'],
|
||||
@@ -60,7 +65,7 @@ public static function register(array $params): array
|
||||
],
|
||||
]);
|
||||
|
||||
// 4. Create TenantUserProfile (tenant-user mapping)
|
||||
// 5. Create TenantUserProfile (tenant-user mapping)
|
||||
TenantUserProfile::create([
|
||||
'user_id' => $user->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
@@ -68,11 +73,11 @@ public static function register(array $params): array
|
||||
'is_active' => 1, // 활성화
|
||||
]);
|
||||
|
||||
// 5. Set tenant context for permissions
|
||||
// 6. 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)
|
||||
// 7. Create 'system_manager' role
|
||||
$role = Role::create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'guard_name' => 'api',
|
||||
@@ -80,10 +85,28 @@ public static function register(array $params): array
|
||||
'description' => '시스템 관리자',
|
||||
]);
|
||||
|
||||
// 7. Assign system_manager role to user
|
||||
// 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);
|
||||
|
||||
// 8. Return user and tenant data
|
||||
// 11. Return user and tenant data
|
||||
return [
|
||||
'user' => [
|
||||
'id' => $user->id,
|
||||
|
||||
Reference in New Issue
Block a user