Skip to content

Commit

Permalink
引入doctrine/annotations包,并获取注解作为权限名称
Browse files Browse the repository at this point in the history
  • Loading branch information
imzhi committed Nov 4, 2019
1 parent 11ccc58 commit b454210
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"mews/captcha": "^2.2",
"spatie/laravel-activitylog": "^2.8",
"spatie/laravel-permission": "^2.37",
"doctrine/annotations": "^1.8",
"laravel/framework": "~5.5"
},
"require-dev": {
Expand Down
15 changes: 15 additions & 0 deletions src/Annotations/PermissionAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Imzhi\JFAdmin\Annotations;

/**
* @Annotation
* @Target({"METHOD"})
*/
final class PermissionAnnotation
{
/**
* @var string
*/
public $name;
}
3 changes: 2 additions & 1 deletion src/Console/stubs/HomeController.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace App\JFAdmin\Controllers;

use App\Http\Controllers\Controller;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;

class HomeController extends Controller
{
/**
* 公共功能-后台首页
* @PermissionAnnotation(name="公共功能-后台首页")
*/
protected function showIndex()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Controllers\Controller;
use Illuminate\Validation\ValidationException;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;

class AuthController extends Controller
{
Expand All @@ -33,7 +34,7 @@ public function __construct()
}

/**
* 后台登录页面
* @PermissionAnnotation(name="后台登录页面")
*/
protected function showLoginForm()
{
Expand Down
29 changes: 15 additions & 14 deletions src/Controllers/ManageUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Imzhi\JFAdmin\Requests\ManageUserCreate;
use Imzhi\JFAdmin\Requests\ManageUserDistribute;
use Imzhi\JFAdmin\Requests\ManageUserRolesCreate;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;
use Imzhi\JFAdmin\Repositories\ManageUserRepository;
use Imzhi\JFAdmin\Requests\ManageUserRolesDistribute;
use Imzhi\JFAdmin\Requests\ManageUserPermissionsGroup;
Expand All @@ -25,7 +26,7 @@ public function __construct(Request $request, ManageUserRepository $manageUserRe
}

/**
* 管理员管理-成员列表页
* @PermissionAnnotation(name="管理员管理-成员列表页")
*/
protected function showList()
{
Expand All @@ -46,7 +47,7 @@ protected function showList()
}

/**
* 管理员管理-新增/编辑成员页
* @PermissionAnnotation(name="管理员管理-新增/编辑成员页")
*/
protected function showCreate($id = null)
{
Expand All @@ -72,7 +73,7 @@ protected function showCreate($id = null)
}

/**
* 管理员管理-新增/编辑成员操作
* @PermissionAnnotation(name="管理员管理-新增/编辑成员操作")
*/
protected function create(ManageUserCreate $request)
{
Expand Down Expand Up @@ -124,7 +125,7 @@ protected function create(ManageUserCreate $request)
}

/**
* 管理员管理-成员状态操作
* @PermissionAnnotation(name="管理员管理-成员状态操作")
*/
protected function status()
{
Expand Down Expand Up @@ -154,7 +155,7 @@ protected function status()
}

/**
* 管理员管理-角色列表页
* @PermissionAnnotation(name="管理员管理-角色列表页")
*/
protected function showRoles()
{
Expand All @@ -167,7 +168,7 @@ protected function showRoles()
}

/**
* 管理员管理-新增/编辑角色页
* @PermissionAnnotation(name="管理员管理-新增/编辑角色页")
*/
protected function showRolesCreate($id = null)
{
Expand All @@ -190,7 +191,7 @@ protected function showRolesCreate($id = null)
}

/**
* 管理员管理-新增/编辑角色操作
* @PermissionAnnotation(name="管理员管理-新增/编辑角色操作")
*/
protected function rolesCreate(ManageUserRolesCreate $request)
{
Expand Down Expand Up @@ -231,7 +232,7 @@ protected function rolesCreate(ManageUserRolesCreate $request)
}

/**
* 管理员管理-成员分配角色页
* @PermissionAnnotation(name="管理员管理-成员分配角色页")
*/
protected function showDistribute($id)
{
Expand All @@ -252,7 +253,7 @@ protected function showDistribute($id)
}

/**
* 管理员管理-成员分配角色操作
* @PermissionAnnotation(name="管理员管理-成员分配角色操作")
*/
protected function distribute(ManageUserDistribute $request)
{
Expand All @@ -276,7 +277,7 @@ protected function distribute(ManageUserDistribute $request)
}

/**
* 管理员管理-角色分配权限页
* @PermissionAnnotation(name="管理员管理-角色分配权限页")
*/
protected function showRolesDistribute($id)
{
Expand All @@ -298,7 +299,7 @@ protected function showRolesDistribute($id)
}

/**
* 管理员管理-角色分配权限操作
* @PermissionAnnotation(name="管理员管理-角色分配权限操作")
*/
protected function rolesDistribute(ManageUserRolesDistribute $request)
{
Expand All @@ -318,7 +319,7 @@ protected function rolesDistribute(ManageUserRolesDistribute $request)
}

/**
* 管理员管理-权限列表页
* @PermissionAnnotation(name="管理员管理-权限列表页")
*/
protected function showPermissions()
{
Expand All @@ -337,7 +338,7 @@ protected function showPermissions()
}

/**
* 管理员管理-权限批量检测操作
* @PermissionAnnotation(name="管理员管理-权限批量检测操作")
*/
protected function permissionsDetect()
{
Expand All @@ -346,7 +347,7 @@ protected function permissionsDetect()
}

/**
* 管理员管理-权限批量分组操作
* @PermissionAnnotation(name="管理员管理-权限批量分组操作")
*/
protected function permissionsGroup(ManageUserPermissionsGroup $request)
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Imzhi\JFAdmin\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Imzhi\JFAdmin\Requests\Pwd;
use App\Http\Controllers\Controller;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;
use Imzhi\JFAdmin\Repositories\AdminUserRepository;

class ProfileController extends Controller
Expand All @@ -19,7 +20,7 @@ public function __construct(Request $request, AdminUserRepository $adminUserRepo
}

/**
* 公共功能-修改密码页面
* @PermissionAnnotation(name="公共功能-修改密码页面")
*/
protected function showPwd()
{
Expand All @@ -28,7 +29,7 @@ protected function showPwd()
}

/**
* 公共功能-修改密码操作
* @PermissionAnnotation(name="公共功能-修改密码操作")
*/
protected function pwd(Pwd $request)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Controllers/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Imzhi\JFAdmin\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Imzhi\JFAdmin\Models\Setting;
use App\Http\Controllers\Controller;
use Imzhi\JFAdmin\Repositories\LogRepository;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;

class SettingController extends Controller
{
Expand All @@ -16,7 +17,7 @@ public function __construct(Request $request, LogRepository $logRepository)
}

/**
* 设置-操作日志列表页
* @PermissionAnnotation(name="设置-操作日志列表页")
*/
protected function showLog()
{
Expand Down
3 changes: 3 additions & 0 deletions src/JFAdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Imzhi\JFAdmin;

use Illuminate\Support\ServiceProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;

class JFAdminServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -70,6 +71,8 @@ public function boot()
$this->publishes([__DIR__ . '/../resources/views/layouts' => resource_path('views/vendor/jfadmin/layouts')], 'jfadmin-views');
$this->publishes([__DIR__ . '/../resources/views/home' => resource_path('views/vendor/jfadmin/home')], 'jfadmin-views');
}

AnnotationRegistry::registerFile(__DIR__ . '/Annotations/PermissionAnnotation.php');
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Repositories/ManageUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Imzhi\JFAdmin\Models\AdminUser;
use Imzhi\JFAdmin\Models\Permission;
use Imzhi\JFAdmin\Models\PermissionExtra;
use Doctrine\Common\Annotations\AnnotationReader;
use Imzhi\JFAdmin\Annotations\PermissionAnnotation;

class ManageUserRepository
{
Expand Down Expand Up @@ -248,6 +250,8 @@ public function permissions(array $args = [])

public function permissionsDetect()
{
$reader = new AnnotationReader;

$routes_data = [];
foreach (Route::getRoutes() as $route) {
$route_name = $route->getName();
Expand All @@ -261,10 +265,11 @@ public function permissionsDetect()
if (!method_exists($action_arr[0], $action_arr[1])) {
continue;
}

try {
$reflection_method = new ReflectionMethod($action_arr[0], $action_arr[1]);
$comment = explode("\n", $reflection_method->getDocComment())[1];
$comment = trim($comment, " \t\n\r\0\x0B*");
$permission_annotation = $reader->getMethodAnnotation($reflection_method, PermissionAnnotation::class);
$permission_name = $permission_annotation->name;
} catch (Exception $e) {
Log::debug('jfadmin::manageuser.permissions.detect err', [
'message' => $e->getMessage(),
Expand All @@ -274,7 +279,8 @@ public function permissionsDetect()
]);
return response()->fai(['msg' => "检测出错。控制器方法 {$action_name} 缺少注释。"]);
}
$routes_data[$route_name] = $comment;

$routes_data[$route_name] = $permission_name;
}
}
$routes_key = array_keys($routes_data);
Expand Down

0 comments on commit b454210

Please sign in to comment.