forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite the simple version in a cleaner way
- Loading branch information
1 parent
52c1de9
commit 473258f
Showing
9 changed files
with
157 additions
and
28 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Plugin; | ||
use Illuminate\Http\Request; | ||
use App\Plugins\Hooks\ResourceAccessorHook; | ||
use PluginManager; | ||
|
||
class PluginResourceController extends Controller | ||
{ | ||
public function getResource(Request $request, Plugin $plugin, string $path = null) | ||
{ | ||
if (is_null($path)) { | ||
// not much we can do for this path | ||
abort(404); | ||
} | ||
|
||
// If we have a hook for resources, we call it. | ||
return PluginManager::call(ResourceAccessorHook::class, ['request' => $request, 'path' => $path], $plugin->plugin_name)->first() ?? abort(404); | ||
// if not, we return abort(404) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/* | ||
* ExampleSettingsPlugin.php | ||
* | ||
* -Description- | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* @package LibreNMS | ||
* @link http://librenms.org | ||
* @copyright 2024 PipoCanaja | ||
* @author PipoCanaja <pipocanaja@gmail.com> | ||
*/ | ||
|
||
namespace App\Plugins\ExamplePlugin; | ||
|
||
use App\Plugins\Hooks\ResourceAccessorHook; | ||
use App\Models\User; | ||
use App\Models\Device; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
|
||
// This ResourceHook is called from a Controller, and allows any plugin to publish files, images, etc, to be downloaded | ||
|
||
class ResourceAccessor extends ResourceAccessorHook | ||
{ | ||
public function authorize(User $user): bool | ||
{ | ||
// In this example, we check if the user has a custom role/permission | ||
// return $user->can('download-reports') | ||
return true; | ||
} | ||
|
||
// process the request, and returns | ||
// - A response() as defined here : https://laravel.com/docs/responses | ||
// - An array with the following syntax: | ||
// ['action' => 'abort', 'abort_type' => 403], more details here : https://laravel.com/docs/errors#http-exceptions | ||
|
||
public function processRequest(Request $request, string $path, array $settings) | ||
{ | ||
// you are free to change the path here. You can also generate files on the fly. | ||
$full_path = base_path('/app/Plugins/ExamplePlugin/files/' . $path); | ||
|
||
// if you use a real file, better check if it exists and is readable, and return the proper abort instruction for the framework | ||
if (! is_file($full_path)) { | ||
return (['action' => 'abort', 'abort_type' => 404]); | ||
} | ||
if (! is_readable($full_path)) { | ||
return (['action' => 'abort', 'abort_type' => 403]); | ||
} | ||
|
||
$result = response()->file($full_path); // to return an image or a file to be displayed inline | ||
//$result = response()->download($full_path); // to force the browser to download the file | ||
|
||
return ($result); | ||
} | ||
} |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/* | ||
* ResourceAccessorHook.php | ||
* | ||
* -Description- | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* @package LibreNMS | ||
* @link http://librenms.org | ||
* @copyright 2024 PipoCanaja | ||
* @author PipoCanaja <pipocanaja@gmail.com> | ||
*/ | ||
|
||
namespace App\Plugins\Hooks; | ||
|
||
use App\Models\User; | ||
use Illuminate\Contracts\Foundation\Application; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
|
||
abstract class ResourceAccessorHook | ||
{ | ||
public string $view = ''; // We don't use | ||
|
||
public function authorize(User $user): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function processRequest(Request $request, string $path, array $settings) | ||
{ | ||
return (['action' => 'abort', 'abort_type' => 404]); | ||
} | ||
|
||
final public function handle(string $pluginName, Request $request, string $path, array $settings, Application $app) | ||
{ | ||
return ($app->call([$this, 'processRequest'], [ | ||
'request' => $request, | ||
'path' => $path, | ||
'settings' => $settings, | ||
])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters