当前位置:网站首页>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
边栏推荐
- Huawei equipment is configured with CE dual attribution
- [Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
- 拍拍贷母公司信也季报图解:营收24亿 净利5.3亿同比降10%
- Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
- swiper 横向轮播 grid
- [work notes] xr872 codec driver migration and application program example (with chip debugging method)
- STM32 sensorless brushless motor drive
- Functional translation
- 【Unity】打包WebGL項目遇到的問題及解决記錄
- js-dom
猜你喜欢

Image table solid line and dashed line detection

Huawei equipment is configured with IP and virtual private network hybrid FRR

An image is word 16x16 words: transformers for image recognition at scale

Stm32+ze-08 formaldehyde sensor tutorial

哈夫曼树及其应用

speech production model

Huawei equipment is configured with CE dual attribution

Paper reading - jukebox: a generic model for music

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

Sensor: sht30 temperature and humidity sensor testing ambient temperature and humidity experiment (code attached at the bottom)
随机推荐
Basic exercises of test questions Fibonacci series
Think about the possibility of attacking secure memory through mmu/tlb/cache
cmake_ example
Is space time attention all you need for video understanding?
Basic principle of bilateral filtering
Leetcode daily question - 890 Find and replace mode
swiper 横向轮播 grid
Huawei equipment is configured with IP and virtual private network hybrid FRR
STM32 steering gear controller
Basic exercises of test questions letter graphics ※
Understanding and thinking about multi-core consistency
Deep learning the principle of armv8/armv9 cache
[unity] problems encountered in packaging webgl project and their solutions
ROS learning-8 pit for custom action programming
Yovo3 and yovo3 tiny structure diagram
regular expression
SQLserver2008 拒绝了对对象 '****' (数据库 '****',架构 'dbo')的 SELECT 权限
Mbedtls migration experience
Microsoft Pinyin opens U / V input mode
The precision of C language printf output floating point numbers