Skip to content

Commit a2eb6fd

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents d8fe46a + b98dee5 commit a2eb6fd

5 files changed

+606
-0
lines changed

laravel/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
## 1. 一些基础命令
44
1. 创建一个 Crons 表迁移和模型
5+
56
```php
67
php artisan make:model Models/Cron -m
78
```
89

910
2. 添加表的相应字段
1011

1112
/database/migrations/2018_10_27_151143_create_crons_table.php
13+
1214
```
1315
/**
1416
* Run the migrations.
@@ -25,21 +27,29 @@ public function up()
2527
});
2628
}
2729
```
30+
2831
2.1 修改 `.env` 数据库配置项;
32+
2933
```
3034
DB_DATABASE=test
3135
DB_USERNAME=root
3236
DB_PASSWORD=root
3337
```
38+
3439
2.2 运行迁移生成表;
40+
3541
```
3642
php artisan migrate
3743
```
44+
3845
2.3 创建填充文件;
46+
3947
```
4048
php artisan make:seed CronTasksTableSeeder
4149
```
50+
4251
2.4 生成测试数据;
52+
4353
```
4454
public function run()
4555
{
@@ -61,14 +71,17 @@ public function run()
6171
]);
6272
}
6373
```
74+
6475
运行填充;
76+
6577
```
6678
php artisan db:seed --class=CronTasksTableSeeder
6779
```
6880

6981
3. 添加路由
7082

7183
/routes/web.php
84+
7285
```
7386
<?php
7487
use App\Models\Cron;
@@ -80,7 +93,9 @@ Route::get('search', function () {
8093
```
8194

8295
4. 定时任务和创建命令相关
96+
8397
启动定时任务
98+
8499
```shell
85100
# 使用 crontab 的定时任务调用 php artisan 调度任务:
86101
crontab -e
@@ -94,24 +109,30 @@ crontab -e
94109

95110

96111
## 2. TODO 待完成工作
112+
97113
~~1. 查询的公交线路存入数据库保存。(目前保存在文件中)已完成~~
98114

99115

100116
## 3. Laravel使用 iseed 扩展导出表数据
117+
101118
iseed地址: [https://github.com/orangehill/iseed](https://github.com/orangehill/iseed)
102119

103120
### 3.1 iseed 安装
121+
104122
```
105123
composer require orangehill/iseed
106124
```
107125

108126
### 3.2 使用方法
127+
109128
如生成 lines 表的 seeder 文件:
129+
110130
```
111131
php artisan iseed lines
112132
```
113133

114134
## 4. 开箱即用注册登录功能
135+
115136
```shell
116137
# 快速生成认证所需要的路由和视图
117138
php artisan make:auth

laravel/app/Http/Controllers/Bus/IndexController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ public function search(Request $request)
107107
return $this->out(200, $list);
108108
}
109109

110+
public function getMe($req)
111+
{
112+
echo 'test';
113+
return $req;
114+
}
110115
}
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
# Laravel5.5 权限控制 ACL 使用
2+
3+
Laravel 在 5.1.11 版本中加入了 Authorization ,可以让用户自定义权限,今天分享一种定义权限系统的方法。
4+
5+
6+
## 1. 创建角色与权限表
7+
8+
使用命令行创建角色与权限表:
9+
10+
```
11+
php artisan make:migration create_permissions_and_roles --create=permissions
12+
```
13+
14+
之后打开刚刚创建的文件,填入下面的代码:
15+
16+
```
17+
public function up()
18+
{
19+
Schema::create('roles', function (Blueprint $table) {
20+
$table->increments('id');
21+
$table->string('name');
22+
$table->string('label');
23+
$table->string('description')->nullable();
24+
$table->timestamps();
25+
});
26+
27+
Schema::create('permissions', function (Blueprint $table) {
28+
$table->increments('id');
29+
$table->string('name');
30+
$table->string('label');
31+
$table->string('description')->nullable();
32+
$table->timestamps();
33+
});
34+
35+
Schema::create('permission_role', function (Blueprint $table) {
36+
$table->integer('permission_id')->unsigned();
37+
$table->integer('role_id')->unsigned();
38+
39+
$table->foreign('permission_id')
40+
->references('id')
41+
->on('permissions')
42+
->onDelete('cascade');
43+
44+
$table->foreign('role_id')
45+
->references('id')
46+
->on('roles')
47+
->onDelete('cascade');
48+
49+
$table->primary(['permission_id', 'role_id']);
50+
});
51+
52+
Schema::create('role_user', function (Blueprint $table) {
53+
$table->integer('user_id')->unsigned();
54+
$table->integer('role_id')->unsigned();
55+
56+
$table->foreign('role_id')
57+
->references('id')
58+
->on('roles')
59+
->onDelete('cascade');
60+
61+
$table->foreign('user_id')
62+
->references('id')
63+
->on('users')
64+
->onDelete('cascade');
65+
66+
$table->primary(['role_id', 'user_id']);
67+
});
68+
}
69+
70+
public function down()
71+
{
72+
Schema::drop('roles');
73+
Schema::drop('permissions');
74+
Schema::drop('permission_role');
75+
Schema::drop('role_user');
76+
}
77+
```
78+
79+
上面的代码会创建角色表、权限表、角色与权限的中间表以及角色与用户的中间表。
80+
81+
## 2. 创建模型
82+
接下来使用命令行分别创建角色与权限模型:
83+
84+
```
85+
php artisan make:model Permission
86+
php artisan make:model Role
87+
```
88+
然后分别打开Permission.php、Role.php 以及 User.php ,加入下面的代码:
89+
90+
```
91+
// Permissions.php
92+
public function roles()
93+
{
94+
return $this->belongsToMany(Role::class);
95+
}
96+
97+
98+
// Role.php
99+
public function permissions()
100+
{
101+
return $this->belongsToMany(Permission::class);
102+
}
103+
//给角色添加权限
104+
public function givePermissionTo($permission)
105+
{
106+
return $this->permissions()->save($permission);
107+
}
108+
109+
// User.php
110+
public function roles()
111+
{
112+
return $this->belongsToMany(Role::class);
113+
}
114+
// 判断用户是否具有某个角色
115+
public function hasRole($role)
116+
{
117+
if (is_string($role)) {
118+
return $this->roles->contains('name', $role);
119+
}
120+
121+
return !! $role->intersect($this->roles)->count();
122+
}
123+
// 判断用户是否具有某权限
124+
public function hasPermission($permission)
125+
{
126+
return $this->hasRole($permission->roles);
127+
}
128+
// 给用户分配角色
129+
public function assignRole($role)
130+
{
131+
return $this->roles()->save(
132+
Role::whereName($role)->firstOrFail()
133+
);
134+
}
135+
```
136+
137+
上面的代码实现了给角色分配权限及给用户分配角色,然后还提供了判断用户是否具有某角色及某权限的方法。
138+
139+
之后就给使用Laravel提供的Authorization来定义权限控制了,打开 /app/Providers/AuthServiceProvider.php 文件,在 boot() 中添加代码:
140+
141+
```
142+
public function boot()
143+
{
144+
parent::registerPolicies($gate);
145+
146+
$permissions = \App\Permission::with('roles')->get();
147+
foreach ($permissions as $permission) {
148+
Gate::define($permission->name, function($user) use ($permission) {
149+
return $user->hasPermission($permission);
150+
});
151+
}
152+
}
153+
```
154+
155+
通过上面的方法就定义好了各个权限。下面就该填充数据了。
156+
157+
## 3. 填充数据
158+
159+
为方便起见,这里使用 tinker 命令行工具来添加几条测试数据:
160+
```
161+
php artisan tinker
162+
```
163+
之后进入命令行,依次输入下列命令:
164+
165+
```
166+
// 改变命名空间位置,避免下面每次都要输入 App
167+
namespace App
168+
169+
// 创建权限
170+
$permission_edit = new Permission
171+
172+
$permission_edit->name = 'edit-post'
173+
174+
$permission_edit->label = 'Can edit post'
175+
176+
$permission_edit->save()
177+
178+
$permission_delete = new Permission
179+
180+
$permission_delete->name = 'delete-post'
181+
182+
$permission_delete->label = 'Can delete post'
183+
184+
$permission_delete->save()
185+
186+
// 创建角色
187+
$role_editor = new Role
188+
189+
$role_editor->name = 'editor';
190+
191+
$role_editor->label = 'The editor of the site';
192+
193+
$role_editor->save()
194+
195+
$role_editor->givePermissionTo($permission_edit)
196+
197+
$role_admin = new Role
198+
199+
$role_admin->name = 'admin';
200+
201+
$role_admin->label = 'The admin of the site';
202+
203+
$role_admin->save()
204+
205+
// 给角色分配权限
206+
$role_admin->givePermissionTo($permission_edit)
207+
208+
$role_admin->givePermissionTo($permission_delete)
209+
210+
// 创建用户
211+
$editor = factory(User::class)->create()
212+
213+
// 给用户分配角色
214+
$editor->assignRole($role_editor->name)
215+
216+
$admin = factory(User::class)->create()
217+
218+
$admin->assignRole($role_admin->name)
219+
```
220+
221+
上面我们创建了两个权限:edit-post 和 delete-post,然后创建了 editor 和 admin 两个角色,editor 角色拥有 edit-post 的权限,而 admin 两个权限都有。之后生成了两个用户,分别给他们分配了 editor 和 admin 的角色,即:ID 1 用户拥有 editor 角色,因此只有 edit-post 权限,而 ID 2 用户拥有 admin 角色,因此具有 edit-post 和 delete-post 权限。下面我们来验证下是否正确。
222+
223+
打开 `routes.php` 文件:
224+
225+
```
226+
Route::get('/', function () {
227+
$userId = 1;
228+
$user = \App\User::find($userId);
229+
Auth::login($user);
230+
return view('welcome');
231+
})
232+
```
233+
234+
上面我们先验证 ID 1 用户的权限,然后修改 /resources/views/welcome.blade.php 文件:
235+
236+
```
237+
<!DOCTYPE html>
238+
<html>
239+
<head>
240+
<title>Laravel</title>
241+
</head>
242+
<body>
243+
<h1>权限测试</h1>
244+
<p>
245+
@can('edit-post')
246+
<a href="#">Edit Post</a>
247+
@endcan
248+
</p>
249+
<p>
250+
@can('delete-post')
251+
<a href="#">Delete Post</a>
252+
@endcan
253+
</p>
254+
</body>
255+
</html>
256+
```
257+
258+
在视图中我们通过 Laravel 提供的 @can 方法来判断用户是否具有某权限。
259+
260+
打开浏览器,访问上面定义的路由,可以看到视图中只出现了 Edit Post 链接。之后我们修改路由中用户ID为 2 ,然后再次刷新浏览器,可以看到,这次同时出现了 Edit Post 和 Delete Post 两个链接,说明我们定义的权限控制起作用了。
261+
262+
参考地址:https://9iphp.com/web/laravel/laravel-5-acl-define.html

0 commit comments

Comments
 (0)