Files
hub/app/Http/Controllers/ViewSwitcherController.php
kelly 078e4f380c feat(analytics): Add missing permission/view-as controllers, views, and traits
Fifth audit revealed additional permission system components:

**Controllers:**
- app/Http/Controllers/ViewAsController.php
  → Start/end user impersonation sessions
- app/Http/Controllers/ViewSwitcherController.php
  → Module view switching functionality
- app/Http/Controllers/Business/UserPermissionsController.php
  → Update user permissions, apply role templates

**Views:**
- resources/views/business/users/permissions-modal.blade.php
  → Permission editing UI modal
- resources/views/components/view-as-banner.blade.php
  → Shows active impersonation banner
- resources/views/components/view-switcher.blade.php
  → Module view switcher component

**Traits:**
- app/Traits/HasHashid.php
  → Hashid generation for models (used by Product, Brand, etc.)

These complete the permission and impersonation system that analytics
controllers depend on for access control.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 21:07:44 -07:00

57 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Helpers\BusinessHelper;
use Illuminate\Http\Request;
class ViewSwitcherController extends Controller
{
/**
* Switch the active view (sales/manufacturing/compliance) for the current session
*/
public function switch(Request $request)
{
$view = $request->input('view');
// Validate view
if (! in_array($view, ['sales', 'manufacturing', 'compliance'])) {
return back()->with('error', 'Invalid view selected');
}
$business = BusinessHelper::current();
if (! $business) {
return back()->with('error', 'No business context');
}
// Check if business has access to this view
if ($view === 'manufacturing' && ! $business->has_manufacturing) {
return back()->with('error', 'Manufacturing module not enabled for this business');
}
if ($view === 'compliance' && ! $business->has_compliance) {
return back()->with('error', 'Compliance module not enabled for this business');
}
// Store selected view in session
session(['current_view' => $view]);
$viewNames = [
'sales' => 'Sales',
'manufacturing' => 'Manufacturing',
'compliance' => 'Compliance',
];
return back()->with('success', 'Switched to '.$viewNames[$view].' view');
}
/**
* Get the currently selected view
*/
public static function getCurrentView(): string
{
return session('current_view', 'sales');
}
}