cineflex/app/Http/Controllers/Managing/UsersController.php

82 lines
2.2 KiB
PHP
Raw Normal View History

2023-02-02 08:17:38 +01:00
<?php
namespace App\Http\Controllers\Managing;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('atleast:manage');
$this->middleware('permission:READ_USERS')->only('index', 'show');
$this->middleware('permission:CREATE_USERS')->only('create', 'store');
$this->middleware('permission:UPDATE_USERS')->only('edit', 'update');
$this->middleware('permission:DELETE_USERS')->only('destroy');
}
public function index()
{
return view('manage.users.index', ['title' => "Manage Users", 'users' => \App\Models\User::all()]);
}
public function create()
{
return view('manage.users.create');
}
public function store()
{
$user = new \App\Models\User();
$user->name = request('name');
$user->email = request('email');
$user->password = request('password');
$user->role = request('role');
$user->save();
// user assignments (user_assignments.user_id, user_assignments.cinema_id)
$cinemas = request('cinemas');
if ($cinemas) {
foreach ($cinemas as $cinema) {
$user->cinemas()->attach($cinema);
}
}
return redirect()->route('manage.users');
}
public function show($id)
{
return view('manage.users.user', ['user' => \App\Models\User::findOrfail($id)]);
}
public function update($id)
{
$user = \App\Models\User::findOrfail($id);
$user->name = request('name');
$user->email = request('email');
$user->password = request('password');
$user->role = request('role');
$user->save();
// user assignments (user_assignments.user_id, user_assignments.cinema_id)
$cinemas = request('cinemas');
if ($cinemas) {
foreach ($cinemas as $cinema) {
$user->cinemas()->attach($cinema);
}
}
return redirect()->route('manage.users');
}
public function destroy($id)
{
$user = \App\Models\User::findOrfail($id);
$user->delete();
return redirect()->route('manage.users');
}
}