权限角色
This commit is contained in:
parent
12024e65d8
commit
cf998a4ecc
15
app/Http/Controllers/Api/PermissionsController.php
Normal file
15
app/Http/Controllers/Api/PermissionsController.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,12 +7,20 @@ use App\Traits\LastActivedAtHelper;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
use Spatie\Permission\Traits\HasRoles;
|
use Spatie\Permission\Traits\HasRoles;
|
||||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class User
|
* Class User
|
||||||
* @property string avatar
|
* @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
|
* @package App\Models
|
||||||
*/
|
*/
|
||||||
class User extends Authenticatable implements JWTSubject
|
class User extends Authenticatable implements JWTSubject
|
||||||
|
|
24
app/Transformers/PermissionTransformer.php
Normal file
24
app/Transformers/PermissionTransformer.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
24
app/Transformers/RoleTransformer.php
Normal file
24
app/Transformers/RoleTransformer.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ use League\Fractal\TransformerAbstract;
|
||||||
|
|
||||||
class UserTransformer extends TransformerAbstract
|
class UserTransformer extends TransformerAbstract
|
||||||
{
|
{
|
||||||
|
protected $availableIncludes = ['roles'];
|
||||||
|
|
||||||
public function transform(User $user)
|
public function transform(User $user)
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -24,4 +26,9 @@ class UserTransformer extends TransformerAbstract
|
||||||
'updated_at' => $user->updated_at instanceof Carbon ? $user->updated_at->toDateTimeString() : $user->updated_at,
|
'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());
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -96,6 +96,10 @@ $api->version('v1', [
|
||||||
// 标记消息通知为已读
|
// 标记消息通知为已读
|
||||||
$api->patch('user/read/notifications', 'NotificationsController@read')
|
$api->patch('user/read/notifications', 'NotificationsController@read')
|
||||||
->name('api.user.notifications.read');
|
->name('api.user.notifications.read');
|
||||||
|
|
||||||
|
// 当前登录用户权限
|
||||||
|
$api->get('user/permissions', 'PermissionsController@index')
|
||||||
|
->name('api.user.permissions.index');
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user