From cf998a4ecc7eac49aca61c529b143bf64b0eb062 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Mon, 11 Jun 2018 23:10:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=83=E9=99=90=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/PermissionsController.php | 15 ++++++++++++ app/Models/User.php | 8 +++++++ app/Transformers/PermissionTransformer.php | 24 +++++++++++++++++++ app/Transformers/RoleTransformer.php | 24 +++++++++++++++++++ app/Transformers/UserTransformer.php | 7 ++++++ routes/api.php | 4 ++++ 6 files changed, 82 insertions(+) create mode 100644 app/Http/Controllers/Api/PermissionsController.php create mode 100644 app/Transformers/PermissionTransformer.php create mode 100644 app/Transformers/RoleTransformer.php diff --git a/app/Http/Controllers/Api/PermissionsController.php b/app/Http/Controllers/Api/PermissionsController.php new file mode 100644 index 0000000..37b27a5 --- /dev/null +++ b/app/Http/Controllers/Api/PermissionsController.php @@ -0,0 +1,15 @@ +user()->getAllPermissions(); + + return $this->response->collection($permissions, new PermissionTransformer()); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index a7ed908..7c9448a 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,12 +7,20 @@ use App\Traits\LastActivedAtHelper; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Auth; +use Spatie\Permission\Models\Role; use Spatie\Permission\Traits\HasRoles; use Tymon\JWTAuth\Contracts\JWTSubject; /** * Class User * @property string avatar + * @property string name + * @property string email + * @property string introduction + * @property string phone + * @property string weixin_unionid + * @property string weixin_openid + * @property Role roles * @package App\Models */ class User extends Authenticatable implements JWTSubject diff --git a/app/Transformers/PermissionTransformer.php b/app/Transformers/PermissionTransformer.php new file mode 100644 index 0000000..a9e8fed --- /dev/null +++ b/app/Transformers/PermissionTransformer.php @@ -0,0 +1,24 @@ + $permission->id, + 'name' => $permission->name, + ]; + } +} \ No newline at end of file diff --git a/app/Transformers/RoleTransformer.php b/app/Transformers/RoleTransformer.php new file mode 100644 index 0000000..37458b0 --- /dev/null +++ b/app/Transformers/RoleTransformer.php @@ -0,0 +1,24 @@ + $role->id, + 'name' => $role->name, + ]; + } +} \ No newline at end of file diff --git a/app/Transformers/UserTransformer.php b/app/Transformers/UserTransformer.php index 5d1ad1a..22ed43b 100644 --- a/app/Transformers/UserTransformer.php +++ b/app/Transformers/UserTransformer.php @@ -9,6 +9,8 @@ use League\Fractal\TransformerAbstract; class UserTransformer extends TransformerAbstract { + protected $availableIncludes = ['roles']; + public function transform(User $user) { return [ @@ -24,4 +26,9 @@ class UserTransformer extends TransformerAbstract 'updated_at' => $user->updated_at instanceof Carbon ? $user->updated_at->toDateTimeString() : $user->updated_at, ]; } + + public function includeRoles(User $user) + { + return $this->collection($user->roles, new RoleTransformer()); + } } \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 3413f66..53ceb6d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -96,6 +96,10 @@ $api->version('v1', [ // 标记消息通知为已读 $api->patch('user/read/notifications', 'NotificationsController@read') ->name('api.user.notifications.read'); + + // 当前登录用户权限 + $api->get('user/permissions', 'PermissionsController@index') + ->name('api.user.permissions.index'); }); });