This article illustrates the implementation method of LARAVEL5 rights management. Share to everyone for your reference, specific as follows:
Thoughts on the Authority management
Recently used laravel design background, the background needs to have a privilege management. Rights management is essentially divided into two parts, first is authentication, then permissions. The authentication section is very good to do, is the administrator login, record session. This laravel also comes with Auth to achieve this. The most troublesome is the authority authentication.
Authority authentication is essentially about who has the authority to manage something. Here are two aspects of the dimension, who is the user dimension, in the user dimension, the granularity of rights management can be a user, can also be a group of users, if the user group, then the logic involved is a user can be in multiple groups inside? On the other hand, the management of something, this thing is the dimension of things, a page is a thing, a page of an element is also a thing, or to the big say, a function is a thing. So the most important thing to do authority management is to confirm the granularity of these two dimensions. This is not a technical matter, this is a need to discuss the needs.
Based on the above thinking, I want to do the rights management, in the user dimension, is based on personal. Is that everyone's permissions are different. In the dimension of things, I set the routing to the smallest unit to set permissions management for a single route.
The next thing to think about is what to use to mark permissions, to use bits, to use characters, or to use integral types. Later I chose the character, based on two considerations: 1 characters are easy to understand, find in the database is also more convenient 2 I did not follow a certain permission to find people with this permission needs, that is, there is no need to reverse the search, the use of bits, integral type, etc. are of little significance.
Next, consider how to combine with Laravel, and since you want to set access permissions for each route, then of course I want to be able to configure it in Laravel route.php routing management. It is best to have a parameter to set the permission when Route::get. The advantage of doing this is that permissions are easy to set up. When you decide to route, you write the right control. The downside, it's also clear, is that three ways of Laravel routing can only be written in one way. It's the way route::(method).
The basic decision is ready to open.
Routing Design
The basic route is this.
Copy Code code as follows:
Route::p ost ('/admin/validate ', [' Uses ' => ' admincontroller@postvalidate ', ' Permissions ' =>[' admin.validate '), ' Admin.index ']]);
This sets a permissions attribute after the basic routing action, which is designed to be a few groups, because a POST request, for example, may be triggered on a page, or it may be triggered on another page, Then this post request requires permission to have two page routes at the same time.
Here use Admin.validate permission control, so that you can group permissions, admin is related to the group of Admin, in the database, I will store a two-dimensional array, [admin] => [' Validate ', ' index ']; stored as a two-dimensional array instead of the benefits of one-dimensional, the general backstage display is two dimensions, one is the head of the tab bar, one is the left side of the Nav bar, that is, this two-dimensional array and the background of the Tab,nav column is one by one corresponding.
Middleware Design
OK, let's hang up the middleware and set all the routes to go this middleware
<?php namespace App\http\middleware; use illuminate\support\facades\session;
Closure;
Class Permission {/** * Handle an incoming request. * * @param \illuminate\http\request $request * @param \closure $next * @return Mixed/Public function hand
Le ($request, Closure $next) {$permits = $this->getpermission ($request);
$admin = \app\http\middleware\authenticate::getauthuser ();
As long as you have permission, you can enter this request foreach ($permits as $permit) {if ($permit = = ' * ') {return $next ($request);
} if ($admin->haspermission ($permit)) {return $next ($request);
} echo "does not have permission, please contact Administrator"; exit;
//Gets the permissions required for the current route public function getpermission ($request) {$actions = $request->route ()->getaction ();
if (Empty ($actions [' Permissions ']) {echo "route does not set permissions"; exit;
return $actions [' Permissions ']; }
}
The key here is the Getpermission function, from $request->route ()->getaction () to get the action definition for this route, Then get the routing permissions defined in route.php from the Permissions field.
And then there's a middleware:
Admin−>haspermission (admin−>haspermission (permit);
This is related to model design.
Model design
<?php namespace App\models\admin;
Use App\models\model as Basemodel;
Class Admin extends Basemodel {protected $table = ' admin ';
Determines whether there is a permission public function haspermission ($permission) {$permission _db = $this->permissions;
if (In_array ($permission, $permission _db)) {return true;
return false;
}//permission is a two-dimensional array of public function Getpermissionsattribute ($value) {if (empty ($value)) {return [];
$data = Json_decode ($value, true);
$ret = [];
foreach ($data as $key => $value) {$ret [] = $key; foreach ($value as $value 2) {$ret [] = ' {$key}.{
$value 2} ";
} return Array_unique ($ret);
///Global Settings permission Public Function Setpermissionsattribute ($value) {$ret = [];
foreach ($value as $item) {$keys = explode ('. ', $item);
if (count ($keys)!= 2) {continue;
} $ret [$keys [0]][] = $keys [1]; } $this->attributes[' permissions '] =Json_encode ($ret);
}
}
In the database, I stored the two-dimensional array as JSON, using Laravel's attribute of Get and set methods, to complete the database of JSON and external program logic connection. Then Haspermission is very relaxed, direct judgment In_array OK.
Subsequent
The logic of this permission authentication is clear. Then if a tab or NAV in the page needs to be displayed to users with different permissions, just judge in view
@if ($admin->haspermission (' Admin.index '))
@endif
You can tell if the user can see this tab.
Summarize
This is not a complex user rights implementation, but I feel that has been able to meet most of the background requirements. Of course can optimize the point may be many,
For example, permission is not able to support the regular, haspermission if stored in NoSQL or PG, is not the JSON data parsing, directly a DB request can determine whether there are permission or something?
More interested in laravel related content readers can view the site topics: "Laravel Framework Introduction and Advanced Course", "PHP Excellent Development Framework Summary", "Smarty Template Primer Tutorial", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course ", PHP string (String) Usage summary," PHP+MYSQL Database operation Introduction Tutorial "and" PHP common database Operation Skills Summary "
I hope this article will help you with the PHP program design based on Laravel framework.