当前位置:网站首页>Laravel permission export
Laravel permission export
2022-06-13 02:22:00 【fareast_ mzh】
1. Admin The interface is written under the directory , The following operations are required :
1.1 Local execution php artisan permission:record command ;( stay database Lower generation permission.log file )
1.2 The generated permission Upload the file to the development , Development environment execution php artisan permission:migrate

php artisan config:clear
./app/Console/Commands/ExportPermission.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ExportPermission extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'export:permission {param}';
/**
* The console command description.
*
* @var string
*/
protected $description = ' export permission journal and TXT';
/**
* Create a new command instance.
*
* @return void
*/
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$param = $this->argument('param'); // "staff"
$fullRouteUri = app()->routes->getRoutes();
$routes = self::getRouteUriContent($fullRouteUri, $param);
self::diffPermissionJsContent($routes,$param);
$this->info(' journal :database/permission/permission.log SQL:database/permission/permission.sql');
}
/**
* FunctionName:getRouteUriContent
* Description:001 Get the data in the route
* Author:lwl
* @param $routes
* @param string $param
* @return array
*/
protected static function getRouteUriContent($routes, string $param)
{
$appName = config('app.name');
$data = array();
foreach ($routes as $key => $value) {
/** @var $value \Illuminate\Routing\Route */
$uri = $value->uri;
$method = $value->methods[0];
if (strpos($uri, $appName . "/{$param}") !== false) {
$namePrefix = $value->action['name_prefix'];
$diffNamePrefix = explode('@', $namePrefix);
$route = end($diffNamePrefix) . '/' . $diffNamePrefix[0] . '/' . $value->action['as'];
$data[$key] = '/' . str_replace("/$param", '', $route) . '@@' . $method;
}
}
return array_values($data);
}
/**
* FunctionName:diffPermissionJsContent
* Description:002 contrast Permission.js Content And then write
* Author:lwl
* @param array $routeUri
* @param string $param
*/
protected static function diffPermissionJsContent(array $routeUri,string $param)
{
$path = database_path('permission/permission.js');
if (!is_file($path)) {
fopen($path, "w");
}
$hasContent = file_get_contents($path);
// Has it been implemented command yes
if (!empty($hasContent)) {
$oldRouteUri = explode(PHP_EOL, $hasContent);
$newRouteUri = array_diff($routeUri, $oldRouteUri);
$content = empty($newRouteUri) ? [] : $newRouteUri;
} else { // no
$content = $routeUri;
}
if (!empty($content)) {
self::putPermissionLog($content,$param);
self::putPermissionJsContent($content);
self::putPermissionTxtContent($param,$content);
}
}
/**
* FunctionName:putPermissionLog
* Description:003 write in log
* Author:lwl
* @param array $content
* @param string $param
*/
protected static function putPermissionLog(array $content,string $param)
{
$dataTime = date('Y-m-d H:i:s');
$permissionLogContent = '';
$appName = config('app.name');
foreach ($content as $value){
$value = trim($value,'/');
$valueArray = explode('@@', $value);
$method = end($valueArray);
$minValue = str_replace($appName.'/',$appName.'.',$value);
$routePrefix = str_replace($appName.'/','',$valueArray[0]);
$routePrefixArray = str_replace('.','\/',$routePrefix);
$routeJson = "{'url':'\/$appName\/$param\/$routePrefixArray','method':$method}}";
$permissionLogContent .= '[' . $dataTime . '] CREATE: ' . $minValue . ' > {"name":"' . $minValue . '","type":"api","scopes":["__' . $appName . '","' . $param . '"],"content":' . $routeJson . '}' . PHP_EOL;
}
$permissionLogHandle = fopen(database_path('permission/permission.log'), 'a');
fputs($permissionLogHandle, $permissionLogContent);
fclose($permissionLogHandle);
}
/**
* FunctionName:putPermissionJsContent
* Description:004 write in permission.js
* Author:lwl
* @param array $permissionJsContents
*/
protected static function putPermissionJsContent(array $permissionJsContents)
{
$permissionJsHandle = fopen(database_path('permission/permission.js'), 'a');
$permissionJsContent = '';
foreach ($permissionJsContents as $value) {
$permissionJsContent .= $value . PHP_EOL;
}
fputs($permissionJsHandle, $permissionJsContent);
fclose($permissionJsHandle);
}
/**
* FunctionName:putPermissionTxtContent
* Description:005 write in permission.txt
* Author:lwl
* @param string $param
* @param array $content
*/
protected static function putPermissionTxtContent(string $param,array $content)
{
$appName = config('app.name');
$nowDate = date('Y-m-d H:i:s');
$outPath = database_path('permission/permission.sql');
/** @var $out resource */
$out = fopen($outPath, 'a');
foreach (array_values($content) as $item) {
if (empty($item)) {
continue;
}
$routeNameArray = explode('@@', $item);
$name = $routeNameArray[0];
$newName = str_replace( '/', '.', trim($name,'/'));
$routePrefix = str_replace($appName . '.', '', $newName);
$method = end($routeNameArray);
$url = "\\\/{$appName}\\\/$param\\\/" . str_replace('.', '\\\/', $routePrefix);
$s = <<<EOF
INSERT INTO `staff`.`permissions`(`name`, `type`, `scopes`, `content`, `translations`, `created_at`, `updated_at`) VALUES ('{$newName}', 'api', '[\"__{$appName}\",\"staff\"]', '{\"url\":"{$url}",\"method\":\"{$method}\"}', NULL, '{$nowDate}', '{$nowDate}');\n
EOF;
fwrite($out, $s);
}
fclose($out);
}
}

php artisan permission:migrate
php artisan export:permission staff
journal :database/permission/permission.log SQL:database/permission/permission.sql
边栏推荐
- Chapter7-13_ Dialogue State Tracking (as Question Answering)
- (novice to) detailed tutorial on machine / in-depth learning with colab from scratch
- [learning notes] xr872 GUI littlevgl 8.0 migration (file system)
- Introduction to armv8/armv9 - learning this article is enough
- 4.11 introduction to firmware image package
- Mac下搭建MySQL环境
- Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
- Termux SSH first shell start
- STM32 timer interrupt learning notes
- C language complex type description
猜你喜欢

Common web page status return code crawler

Paper reading - jukebox: a generic model for music
![[learning notes] xr872 GUI littlevgl 8.0 migration (file system)](/img/9b/0bf88354e8cfdbcc1ea91311c9a823.jpg)
[learning notes] xr872 GUI littlevgl 8.0 migration (file system)

Installing Oracle with docker for Mac

Solution of depth learning for 3D anisotropic images
![Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]](/img/39/d5c4d424a160635791c4645d6f2e10.png)
Leetcode 450. 删除二叉搜索树中的节点 [二叉搜索树]

Sqlserver2008 denied select permission on object'***** '(database'*****', schema'dbo')

Huawei equipment is configured with dual reflectors to optimize the backbone layer of the virtual private network

Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human
![[learning notes] xr872 GUI littlevgl 8.0 migration (display part)](/img/5e/fc8c3fe3029c36648fbc3f48bc0c2f.jpg)
[learning notes] xr872 GUI littlevgl 8.0 migration (display part)
随机推荐
js获取元素
Paper reading - beat tracking by dynamic programming
(novice to) detailed tutorial on machine / in-depth learning with colab from scratch
[work notes] the problem of high leakage current in standby mode of dw7888 motor driver chip
1000粉丝啦~
[pytorch]fixmatch code explanation (super detailed)
Leetcode daily question - 890 Find and replace mode
Yovo3 and yovo3 tiny structure diagram
STM32 timer interrupt learning notes
Chapter7-12_ Controllable Chatbot
Chapter7-11_ Deep Learning for Question Answering (2/2)
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
[learning notes] xr872 audio driver framework analysis
C language compressed string is saved to binary file, and the compressed string is read from binary file and decompressed.
Swiper horizontal rotation grid
Resource arrangement
About the fact that I gave up the course of "Guyue private room course ROS manipulator development from introduction to actual combat" halfway
ROS learning-6 detailed explanation of publisher programming syntax
redis 多个服务器共用一个
Paper reading - jukebox: a generic model for music