Files
star-cloud/app/Http/Controllers/Admin/PermissionController.php
2026-03-13 17:35:22 +08:00

294 lines
8.7 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PermissionController extends Controller
{
// APP功能管理
public function appFeatures()
{
return view('admin.placeholder', [
'title' => 'APP功能管理',
'description' => 'APP功能權限設定',
]);
}
// 資料設定權限
public function dataConfig()
{
return view('admin.placeholder', [
'title' => '資料設定權限',
'description' => '資料設定功能權限',
]);
}
// 銷售管理權限
public function sales()
{
return view('admin.placeholder', [
'title' => '銷售管理權限',
'description' => '銷售管理功能權限',
]);
}
// 機台管理權限
public function machines()
{
return view('admin.placeholder', [
'title' => '機台管理權限',
'description' => '機台管理功能權限',
]);
}
// 倉庫管理權限
public function warehouses()
{
return view('admin.placeholder', [
'title' => '倉庫管理權限',
'description' => '倉庫管理功能權限',
]);
}
// 分析管理權限
public function analysis()
{
return view('admin.placeholder', [
'title' => '分析管理權限',
'description' => '分析管理功能權限',
]);
}
// 稽核管理權限
public function audit()
{
return view('admin.placeholder', [
'title' => '稽核管理權限',
'description' => '稽核管理功能權限',
]);
}
// 遠端管理權限
public function remote()
{
return view('admin.placeholder', [
'title' => '遠端管理權限',
'description' => '遠端管理功能權限',
]);
}
// Line管理權限
public function line()
{
return view('admin.placeholder', [
'title' => 'Line管理權限',
'description' => 'Line管理功能權限',
]);
}
// 權限角色設定
public function roles()
{
$limit = request()->input('limit', 10);
$roles = \Spatie\Permission\Models\Role::withCount('users')->latest()->paginate($limit)->withQueryString();
return view('admin.permission.roles', compact('roles'));
}
/**
* Store a newly created role in storage.
*/
public function storeRole(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255|unique:roles,name',
]);
\Spatie\Permission\Models\Role::create([
'name' => $validated['name'],
'guard_name' => 'web',
'is_system' => false,
]);
return redirect()->back()->with('success', __('Role created successfully.'));
}
/**
* Update the specified role in storage.
*/
public function updateRole(Request $request, $id)
{
$role = \Spatie\Permission\Models\Role::findOrFail($id);
if ($role->is_system) {
return redirect()->back()->with('error', __('System roles cannot be renamed.'));
}
$validated = $request->validate([
'name' => 'required|string|max:255|unique:roles,name,' . $id,
]);
$role->update(['name' => $validated['name']]);
return redirect()->back()->with('success', __('Role updated successfully.'));
}
/**
* Remove the specified role from storage.
*/
public function destroyRole($id)
{
$role = \Spatie\Permission\Models\Role::findOrFail($id);
if ($role->is_system) {
return redirect()->back()->with('error', __('System roles cannot be deleted.'));
}
if ($role->users()->count() > 0) {
return redirect()->back()->with('error', __('Cannot delete role with active users.'));
}
$role->delete();
return redirect()->back()->with('success', __('Role deleted successfully.'));
}
// 其他功能管理
public function others()
{
return view('admin.placeholder', [
'title' => '其他功能管理',
'description' => '其他特殊功能權限',
]);
}
// 帳號管理
public function accounts(Request $request)
{
$query = \App\Models\System\User::query()->with(['company', 'roles']);
// 租戶隔離:如果不是系統管理員,則只看自己公司的成員
if (!auth()->user()->isSystemAdmin()) {
$query->where('company_id', auth()->user()->company_id);
}
// 搜尋
if ($search = $request->input('search')) {
$query->where(function($q) use ($search) {
$q->where('name', 'like', "%{$search}%")
->orWhere('username', 'like', "%{$search}%")
->orWhere('email', 'like', "%{$search}%");
});
}
// 公司篩選 (僅限 super-admin)
if (auth()->user()->isSystemAdmin() && $request->filled('company_id')) {
$query->where('company_id', $request->company_id);
}
$limit = $request->input('limit', 10);
$users = $query->latest()->paginate($limit)->withQueryString();
$companies = auth()->user()->isSystemAdmin() ? \App\Models\System\Company::all() : collect();
return view('admin.data-config.accounts', compact('users', 'companies'));
}
/**
* Store a newly created account in storage.
*/
public function storeAccount(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users,username',
'email' => 'nullable|email|max:255|unique:users,email',
'password' => 'required|string|min:8',
'role' => 'required|string',
'status' => 'required|boolean',
'company_id' => 'nullable|exists:companies,id',
'phone' => 'nullable|string|max:20',
]);
$user = \App\Models\System\User::create([
'name' => $validated['name'],
'username' => $validated['username'],
'email' => $validated['email'],
'password' => \Illuminate\Support\Facades\Hash::make($validated['password']),
'status' => $validated['status'],
'company_id' => auth()->user()->isSystemAdmin() ? $validated['company_id'] : auth()->user()->company_id,
'phone' => $validated['phone'],
]);
$user->assignRole($validated['role']);
return redirect()->back()->with('success', __('Account created successfully.'));
}
/**
* Update the specified account in storage.
*/
public function updateAccount(Request $request, $id)
{
$user = \App\Models\System\User::findOrFail($id);
$validated = $request->validate([
'name' => 'required|string|max:255',
'username' => 'required|string|max:255|unique:users,username,' . $id,
'email' => 'nullable|email|max:255|unique:users,email,' . $id,
'password' => 'nullable|string|min:8',
'role' => 'required|string',
'status' => 'required|boolean',
'company_id' => 'nullable|exists:companies,id',
'phone' => 'nullable|string|max:20',
]);
$updateData = [
'name' => $validated['name'],
'username' => $validated['username'],
'email' => $validated['email'],
'status' => $validated['status'],
'phone' => $validated['phone'],
];
if (auth()->user()->isSystemAdmin()) {
$updateData['company_id'] = $validated['company_id'];
}
if (!empty($validated['password'])) {
$updateData['password'] = \Illuminate\Support\Facades\Hash::make($validated['password']);
}
$user->update($updateData);
$user->syncRoles([$validated['role']]);
return redirect()->back()->with('success', __('Account updated successfully.'));
}
/**
* Remove the specified account from storage.
*/
public function destroyAccount($id)
{
$user = \App\Models\System\User::findOrFail($id);
if ($user->id === auth()->id()) {
return redirect()->back()->with('error', __('You cannot delete your own account.'));
}
$user->delete();
return redirect()->back()->with('success', __('Account deleted successfully.'));
}
// AI智能預測
public function aiPrediction()
{
return view('admin.placeholder', [
'title' => 'AI智能預測',
'description' => 'AI功能權限設定',
]);
}
}