Skip to content

Controllers

Exemplo de controller no Laravel ACL

Vejamos um controller típico do ribafs/laravel-acl

Controller Clients

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests;

use App\Models\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ClientController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');        
    }

    public function index(Request $request)
    {
        $auth = Auth::user()->hasRole('super', 'manager', 'user');
        if((!$auth)){
            return view('home');
        }else{
            $keyword = $request->get('search');
            $perPage = 5;

            if (!empty($keyword)) {
                $clients = Client::where('name', 'LIKE', "%$keyword%")
                ->orWhere('email', 'LIKE', "%$keyword%")
                ->latest()->orderBy('id')->paginate($perPage);
            } else {
                $clients = Client::latest()->orderBy('id')->paginate($perPage);
            }

            return view('admin.clients.index', compact('clients'));
        }
    }

    public function create()
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{
            return view('admin.clients.create');
        }
    }

    public function store(Request $request)
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{

            $requestData = $request->all();

            Client::create($requestData);

            return redirect('admin/clients')->with('flash_message', 'Client added!');
        }
    }

    public function show($id)
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{
            $client = Client::findOrFail($id);

            return view('admin.clients.show', compact('client'));
        }
    }

    public function edit($id)
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{
            $client = Client::findOrFail($id);

            return view('admin.clients.edit', compact('client'));
        }
    }

    public function update(Request $request, $id)
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{

            $requestData = $request->all();

            $client = Client::findOrFail($id);
            $client->update($requestData);

            return redirect('admin/clients')->with('flash_message', 'Client updated!');
        }
    }

    public function destroy($id)
    {
        $auth = Auth::user()->hasRole('super', 'manager');
        if((!$auth)){
            return view('home');
        }else{
            Client::destroy($id);

            return redirect('admin/clients')->with('flash_message', 'Client deleted!');
        }
    }
}

Usando o método can()

Então podemos controlar em nossos actions dos controller o acesso através das permissões dos usuários

Testar se o usuário logado pode usar a permissão all-all

1
2
3
4
5
public function index(Request $request){
    if ($request->user()->can('all-all')) {
        return 'Seja muito bem vindo seu Super';
    }
}

Views

Vejamos duas views como exemplo das views do pacote.

Nosso pacote conta com um layout customizado e também com as views home.blade.php e welcome.blade.php customizadas.