权限角色

This commit is contained in:
fthvgb1 2018-06-11 23:10:59 +08:00
parent 12024e65d8
commit cf998a4ecc
6 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<?php
namespace App\Http\Controllers\Api;
use App\Transformers\PermissionTransformer;
class PermissionsController extends Controller
{
public function index()
{
$permissions = $this->user()->getAllPermissions();
return $this->response->collection($permissions, new PermissionTransformer());
}
}

View File

@ -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

View File

@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/11
* Time: 22:35
*/
namespace App\Transformers;
use League\Fractal\TransformerAbstract;
use Spatie\Permission\Models\Permission;
class PermissionTransformer extends TransformerAbstract
{
public function transform(Permission $permission)
{
return [
'id' => $permission->id,
'name' => $permission->name,
];
}
}

View File

@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/11
* Time: 22:40
*/
namespace App\Transformers;
use League\Fractal\TransformerAbstract;
use Spatie\Permission\Models\Role;
class RoleTransformer extends TransformerAbstract
{
public function transform(Role $role)
{
return [
'id' => $role->id,
'name' => $role->name,
];
}
}

View File

@ -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());
}
}

View File

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