diff --git a/src/Member/Commands/PermissionCommand.php b/src/Member/Commands/PermissionCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..97220d02c1167988804111add4aef77f143f6b5e --- /dev/null +++ b/src/Member/Commands/PermissionCommand.php @@ -0,0 +1,108 @@ + + * @copyright (c) 2017, iBenchu.org + * @datetime 2017-02-15 18:01 + */ + +namespace Notadd\Foundation\Member\Commands; + +use Illuminate\Console\Command; +use Illuminate\Console\ConfirmableTrait; +use Notadd\Foundation\Member\Permission; +use Symfony\Component\Console\Input\InputOption; + +class PermissionCommand extends Command +{ + use ConfirmableTrait; + + protected $name = 'permission'; + + protected $signature = 'permission + {key? : Register permission config file path key.} + {--path= : From file create permission.} + {--all : Export all permissions to database} + {--force : Force create}'; + + protected $description = 'Export Permissions to database'; + + /** + * @var \Notadd\Foundation\Member\PermissionManager + */ + protected $permissionManager; + + public function __construct() + { + parent::__construct(); + + $this->permissionManager = app('permission'); + } + + public function handle() + { + if (! $this->confirmToProceed()) { + return; + } + + $permissions = []; + + $key = $this->argument('key'); + if (! empty($key) && ! empty($realPath = $this->permissionManager->getFilePath($key)) && file_exists($realPath)) { + $permissions = (array) require $realPath; + } + + $path = $this->option('path'); + if (! empty($path) && file_exists($path)) { + $permissions = (array) require $path; + } + + if ($this->option('all')) { + $permissions = []; + $paths = $this->permissionManager->getFilePaths(); + foreach ($paths as $path) { + if (empty($path) || ! file_exists($path)) { + continue; + } + + $permissions = array_merge($permissions, (array) require $path); + } + } + + if (empty($permissions) || count($permissions) < 0) { + $this->info('没有可导入的权限.'); + return; + } + + $i = 0; + foreach ($permissions as $permission) { + if (! isset($permission['display_name']) || ! isset($permission['name']) || empty($permission['display_name']) || empty($permission['name'])) { + continue; + } + + if (Permission::where('name', $permission['name'])->count()) { + continue; + } + + Permission::addPermission($permission['name'], $permission['display_name'], isset($permission['description']) ? $permission['description'] : ''); + $i++; + } + + $this->info("导入 {$i} 个权限."); + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'], + + ['path', null, InputOption::VALUE_OPTIONAL, 'The path of permissions file to be executed.'], + ]; + } +} diff --git a/src/Member/MemberServiceProvider.php b/src/Member/MemberServiceProvider.php index cdfac8f252f2944f8b2ce0dfcde4fa0d34faa558..ae7892c694234e9434d43de1fad51fd1a4f7abff 100644 --- a/src/Member/MemberServiceProvider.php +++ b/src/Member/MemberServiceProvider.php @@ -9,6 +9,8 @@ namespace Notadd\Foundation\Member; use Illuminate\Support\ServiceProvider; +use Notadd\Foundation\Member\Commands\PermissionCommand; +use Notadd\Foundation\Member\Middleware\Permission; /** * Class MemberServiceProvider. @@ -28,5 +30,30 @@ class MemberServiceProvider extends ServiceProvider return $manager->manager(); }); + + $this->registerPermission(); + + $this->registerCommands(); + + $this->registerMiddleware(); + } + + public function registerMiddleware() + { + $this->app['router']->middleware('permission', Permission::class); + } + + public function registerCommands() + { + $this->commands([ + PermissionCommand::class, + ]); + } + + public function registerPermission() + { + $this->app->bind('permission', function ($app) { + return new PermissionManager; + }); } } diff --git a/src/Member/Middleware/Permission.php b/src/Member/Middleware/Permission.php new file mode 100644 index 0000000000000000000000000000000000000000..a291c627e19037e45c1c9b4ae2da6fb7aa7a86f2 --- /dev/null +++ b/src/Member/Middleware/Permission.php @@ -0,0 +1,52 @@ + + * @copyright (c) 2017, iBenchu.org + * @datetime 2017-02-16 13:41 + */ + +namespace Notadd\Foundation\Member\Middleware; + +use Closure; +use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; + +class Permission +{ + protected $auth; + + public function __construct() + { + $this->auth = app('auth'); + } + + /** + * Handle an incoming request. + * + * @param \Illuminate\Http\Request $request + * @param Closure $next + * @param $permissions + * + * @return mixed + */ + public function handle(Request $request, Closure $next, $permissions) + { + if ($this->auth->guest() || ! $request->user()->hasPermission(explode('|', $permissions))) { + if ($this->wantsJson()) { + return new JsonResponse('Forbidden', 403); + } + + abort(403); + } + + return $next($request); + } + + protected function wantsJson() + { + return (app('request')->ajax() || app('request')->wantsJson()) ? true : false; + } +} diff --git a/src/Member/PermissionManager.php b/src/Member/PermissionManager.php new file mode 100644 index 0000000000000000000000000000000000000000..7cd2cb2570699751715605d596db7fa5298f3754 --- /dev/null +++ b/src/Member/PermissionManager.php @@ -0,0 +1,51 @@ + + * @copyright (c) 2017, iBenchu.org + * @datetime 2017-02-15 18:57 + */ + +namespace Notadd\Foundation\Member; + +class PermissionManager +{ + const PATH_PREFIX = 'permission.paths.'; + + // protected $config; + // + // public function __construct($config) + // { + // $this->config = $config; + // } + + /** + * @param string $key + * @param string $path + */ + public function registerFilePath(string $key, string $path) + { + if (! app('config')->has(static::PATH_PREFIX . $key)) { + app('config')->set(static::PATH_PREFIX . $key, $path); + } + } + + /** + * @param string $key + * + * @return string + */ + public function getFilePath(string $key) + { + return app('config')->get(static::PATH_PREFIX . $key, ''); + } + + /** + * @return array + */ + public function getFilePaths() + { + return app('config')->get(rtrim(static::PATH_PREFIX, '.'), []); + } +}