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'); }); });