feat: multi-tenant architecture and tenant isolation changes

This commit is contained in:
2026-08-12 15:33:25 +08:00
parent 28d41f2bb8
commit 80c0a2ea42
896 changed files with 60053 additions and 16812 deletions

View File

@@ -138,9 +138,19 @@ class EmployeeController extends Controller
->where('status', 'active')
->get(['id', 'name', 'department_id']);
// Get plan limits for company users and staff users (only in SaaS mode)
// Get plan limits for company users and staff users (under tenancy or SaaS mode)
$planLimits = null;
if (isSaas()) {
if (function_exists('tenant') && tenant()) {
$tenantId = tenant('id');
$entitlement = \App\Models\TenantEntitlement::where('tenant_id', $tenantId)->first();
$maxEmployees = $entitlement ? (int) $entitlement->max_employees : (int) (getCompanySetting('max_employees') ?? 25);
$currentUserCount = User::where('type', 'employee')->count();
$planLimits = [
'current_users' => $currentUserCount,
'max_users' => $maxEmployees,
'can_create' => $maxEmployees <= 0 || $currentUserCount < $maxEmployees,
];
} else if (isSaas()) {
if ($authUser->type === 'company' && $authUser->plan) {
$currentUserCount = User::where('type', 'employee')->whereIn('created_by', getCompanyAndUsersId())->count();
$planLimits = [
@@ -308,7 +318,15 @@ class EmployeeController extends Controller
}
// Plan limit check
if (isSaas()) {
if (function_exists('tenant') && tenant()) {
$tenantId = tenant('id');
$entitlement = \App\Models\TenantEntitlement::where('tenant_id', $tenantId)->first();
$maxEmployees = $entitlement ? (int) $entitlement->max_employees : (int) (getCompanySetting('max_employees') ?? 25);
$count = User::where('type', 'employee')->count();
if ($maxEmployees > 0 && $count >= $maxEmployees) {
return redirect()->back()->with('error', __("Employee limit reached ({$maxEmployees} max staff allowed). Cannot add more employees."))->withInput();
}
} else if (isSaas()) {
$creator = User::find(creatorId());
if ($creator && $creator->type === 'company' && $creator->plan) {
$count = User::where('type', 'employee')->whereIn('created_by', getCompanyAndUsersId())->count();