jeudi 18 janvier 2018

Laravel 5.4: User permissions customized by section

I am creating a management panel where you should be able to assign each user permissions to access and manage each of the sections.

In the users table, I have the following:

id
name
password
news
reports
events
documents
communities
videos
tags
active
created_at
updated_at

The fields: news, reports, events, documents, communities, videos and tags, are tinyint (1) fields with value "0" or "1". If the value is equal to "1", the user has permission; and if it is equal to "0", the user does not have permission. These fields correspond to each one of the sections of the panel.

I have made the basic access and authorization connection successfully, but now I do not know how to check access permissions for each section according to what the user has or not restricted. I have seen some permission packages and user roles, but they do not do what I need. Some help?

In LoginController I have:

class LoginController extends Controller
{
    protected $redirectTo = 'panel.index';

    public function username()
    {
        return 'name';
    }

    public function acceso(Request $request)
    {
        $data['name'] = $request->name;
        $password = $request->password;

        if (Auth::attempt(['name' => $data['name'], 'password' => $password, 'active' => 1])) {
            // Authentication passed...
            return view('panel.login.index', compact('data'));
        }
        else {
            return back()->with('message' , 'Ups! El nombre o la clave no son correctos.');
        }
    }

    public function index()
    {
        Auth::logout();
        return view('panel.index');
    }

}

In each controller I have put the __construct():

public function __construct()
{
    $this->middleware('auth');
}

In routes I have:

// Login
    Route::resource('login', 'LoginController');
    Route::get('/panel', 'LoginController@index');
    Route::post('login/index', ['as' => 'login.acceso', 'uses' => 'LoginController@acceso']);
    Route::post('login', ['as' => 'login', 'uses' => 'LoginController@index']);

And this is in the view to login:

{!! Form::open(['method' => 'POST', 'route' => 'login.acceso', 'class' => 'form', 'id' => 'acceso_form']) !!}
    Bienvenido al Panel de Gestión.</br>
    Por favor, introduce el nombre de usuario y la clave:<br><br>

    {!! Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'usuario']) !!}
    {!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'clave']) !!}
    {!! Form::submit('ACCEDER', ['class' =>'btn btn-primary']) !!}


{!! Form::close() !!}
@if(session()->has('message'))
    <div class="dialogo_requerido" style="width: 80%; margin: 2em 10% 0 10%"></div>
@endif



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2FQAbfP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire