当前位置:网站首页>Laravel 权限导出
Laravel 权限导出
2022-06-13 02:19:00 【fareast_mzh】
1. Admin目录下写了接口,需要操作如下:
1.1 本地执行php artisan permission:record 命令;(在database下生成permission.log文件)
1.2 将生成的 permission 文件上传到开发,开发环境执行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 = '导出 permission 日志 和 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('日志:database/permission/permission.log SQL:database/permission/permission.sql');
}
/**
* FunctionName:getRouteUriContent
* Description:001 获取路由里的数据
* 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 对比 Permission.js 内容 然后写入
* 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);
//是否执行过 命令 是
if (!empty($hasContent)) {
$oldRouteUri = explode(PHP_EOL, $hasContent);
$newRouteUri = array_diff($routeUri, $oldRouteUri);
$content = empty($newRouteUri) ? [] : $newRouteUri;
} else { //否
$content = $routeUri;
}
if (!empty($content)) {
self::putPermissionLog($content,$param);
self::putPermissionJsContent($content);
self::putPermissionTxtContent($param,$content);
}
}
/**
* FunctionName:putPermissionLog
* Description:003 写入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 写入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 写入 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
日志:database/permission/permission.log SQL:database/permission/permission.sql
边栏推荐
- [pytorch]fixmatch code explanation - data loading
- Build MySQL environment under mac
- Easydl related documents and codes
- Leetcode 926. 将字符串翻转到单调递增 [前缀和]
- Mean Value Coordinates
- Yovo3 and yovo3 tiny structure diagram
- Deep learning the principle of armv8/armv9 cache
- Leetcode 473. 火柴拼正方形 [暴力+剪枝]
- [work with notes] NDK compiles the open source library ffmpeg
- 如何解决通过new Date()获取时间写出数据库与当前时间相差8小时问题【亲测有效】
猜你喜欢

Stm32 mpu6050 servo pan tilt support follow
![[pytorch] kaggle image classification competition arcface + bounding box code learning](/img/1e/5e921987754da1e1750acdadb36849.jpg)
[pytorch] kaggle image classification competition arcface + bounding box code learning

Parameter measurement method of brushless motor

ROS learning -5 how function packs with the same name work (workspace coverage)
![[work with notes] NDK compiles the open source library ffmpeg](/img/24/ed33e12a07e001fc708e0c023e479c.jpg)
[work with notes] NDK compiles the open source library ffmpeg

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

Huawei equipment is configured with CE dual attribution
![[open source] libinimini: a minimalist ini parsing library for single chip computers](/img/99/f2ded6c189bd45ea6c1e9f86fd64c1.jpg)
[open source] libinimini: a minimalist ini parsing library for single chip computers
![[pytorch] kaggle large image dataset data analysis + visualization](/img/b0/7b8aff44d6bedd7ca2c705f13a8556.jpg)
[pytorch] kaggle large image dataset data analysis + visualization

Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
随机推荐
SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
Paipai loan parent company Xinye quarterly report diagram: revenue of RMB 2.4 billion, net profit of RMB 530million, a year-on-year decrease of 10%
[single chip microcomputer] single timer in front and back platform program framework to realize multi delay tasks
16 embedded C language interview questions (Classic)
Configuring virtual private network FRR for Huawei equipment
Mac下搭建MySQL环境
How to do Internet for small enterprises
蓝牙模块:使用问题集锦
Armv8-m learning notes - getting started
[keras] generator for 3D u-net source code analysis py
Huawei equipment is configured with CE dual attribution
Anti crawling strategy (IP proxy, setting random sleep time, bilbili video information crawling, obtaining real URLs, processing special characters, processing timestamp, and multithreading)
Cumulative tax law: calculate how much tax you have paid in a year
Application and routine of C language typedef struct
[open source] libinimini: a minimalist ini parsing library for single chip computers
Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
ROS learning -5 how function packs with the same name work (workspace coverage)
华为设备配置IP和虚拟专用网混合FRR
Ruixing coffee 2022, extricating itself from difficulties and ushering in a smooth path
cmake_ example