Add fileshare index

This commit is contained in:
philipp lang 2024-06-27 00:00:29 +02:00
parent b15b806948
commit 8609a648fd
12 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace App\Fileshare\Actions;
use App\Fileshare\Models\FileshareConnection;
use App\Fileshare\Resources\FileshareConnectionResource;
use Inertia\Inertia;
use Lorisleiva\Actions\Concerns\AsAction;
class IndexAction
{
use AsAction;
public function handle()
{
session()->put('menu', 'setting');
session()->put('title', 'Datei-Verbindungen');
return Inertia::render('fileshare/Index', [
'data' => FileshareConnectionResource::collection(FileshareConnection::paginate(15)),
]);
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Fileshare\ConnectionTypes;
use Spatie\LaravelData\Data;
abstract class ConnectionType extends Data
{
abstract public function check(): bool;
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Fileshare\ConnectionTypes;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
#[MapInputName(SnakeCaseMapper::class)]
#[MapOutputName(SnakeCaseMapper::class)]
class OwncloudConnection extends ConnectionType
{
public function __construct(
public string $user,
public string $password,
public string $baseUrl,
) {
}
public function check(): bool
{
try {
$response = Http::withoutVerifying()->withBasicAuth($this->user, $this->password)->acceptJson()->get($this->baseUrl . '/ocs/v1.php/cloud/capabilities?format=json');
return $response->ok();
} catch (ConnectionException $e) {
return false;
}
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Fileshare;
use App\Fileshare\Actions\IndexAction;
use App\Setting\Contracts\Indexable;
use App\Setting\LocalSettings;
class FileshareSettings extends LocalSettings implements Indexable
{
public static function group(): string
{
return 'fileshare';
}
public static function slug(): string
{
return 'fileshare';
}
public static function indexAction(): string
{
return IndexAction::class;
}
public static function title(): string
{
return 'Datei-Verbindungen';
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Fileshare\Models;
use App\Fileshare\ConnectionTypes\ConnectionType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FileshareConnection extends Model
{
use HasFactory;
public $casts = [
'type' => ConnectionType::class,
];
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Fileshare\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class FileshareConnectionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array<string, mixed>
*/
public function toArray($request)
{
return [
'name' => $this->name,
'is_active' => $this->type->check(),
];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Setting;
use App\Fileshare\FileshareSettings;
use App\Form\FormSettings;
use App\Invoice\InvoiceSettings;
use App\Mailgateway\MailgatewaySettings;
@ -32,5 +33,6 @@ class SettingServiceProvider extends ServiceProvider
app(SettingFactory::class)->register(MailgatewaySettings::class);
app(SettingFactory::class)->register(NamiSettings::class);
app(SettingFactory::class)->register(FormSettings::class);
app(SettingFactory::class)->register(FileshareSettings::class);
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace Database\Factories\Fileshare\Models;
use App\Fileshare\ConnectionTypes\ConnectionType;
use App\Fileshare\Models\FileshareConnection;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<FileshareConnection>
*/
class FileshareConnectionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = FileshareConnection::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'type' => '{}',
'name' => '',
];
}
public function type(ConnectionType $type): self
{
return $this->state(['type' => $type]);
}
public function name(string $name): self
{
return $this->state(['name' => $name]);
}
}

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('fileshare_connections', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->json('type');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('fileshare_connections');
}
};

View File

@ -0,0 +1,23 @@
<?php
namespace Tests\Fileshare;
use App\Fileshare\ConnectionTypes\OwncloudConnection;
use App\Fileshare\Models\FileshareConnection;
use Tests\FileshareTestCase;
class ConnectionIndexActionTest extends FileshareTestCase
{
public function testItListsOwncloudConnectionsThatAreActive(): void
{
$this->withoutExceptionHandling()->login()->loginNami()->withOwncloudUser('badenpowell', 'secret');
FileshareConnection::factory()
->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')]))
->name('lokaler Server')
->create();
$this->get('/setting/fileshare')
->assertInertiaPath('data.data.0.name', 'lokaler Server')
->assertInertiaPath('data.data.0.is_active', true);
}
}

View File

@ -0,0 +1,46 @@
version: '3'
services:
owncloud:
image: owncloud/server:10.10.0
ports:
- 5566:8080
depends_on:
- mariadb
environment:
- OWNCLOUD_DOMAIN=http://localhost:5566
- OWNCLOUD_TRUSTED_DOMAINS=
- OWNCLOUD_DB_TYPE=mysql
- OWNCLOUD_DB_NAME=owncloud
- OWNCLOUD_DB_USERNAME=owncloud
- OWNCLOUD_DB_PASSWORD=owncloud
- OWNCLOUD_DB_HOST=mariadb
- OWNCLOUD_ADMIN_USERNAME=admin
- OWNCLOUD_ADMIN_PASSWORD=admin
- OWNCLOUD_MYSQL_UTF8MB4=true
- OWNCLOUD_REDIS_ENABLED=false
- OWNCLOUD_REDIS_HOST=false
healthcheck:
test: ['CMD', '/usr/bin/healthcheck']
interval: 30s
timeout: 10s
retries: 5
volumes:
- ./oc_tmp/files:/mnt/data
mariadb:
image: mariadb:10.11
environment:
- MYSQL_ROOT_PASSWORD=owncloud
- MYSQL_USER=owncloud
- MYSQL_PASSWORD=owncloud
- MYSQL_DATABASE=owncloud
- MARIADB_AUTO_UPGRADE=1
command: ['--max-allowed-packet=128M', '--innodb-log-file-size=64M']
healthcheck:
test: ['CMD', 'mysqladmin', 'ping', '-u', 'root', '--password=owncloud']
interval: 10s
timeout: 5s
retries: 5
volumes:
- ./oc_tmp/db:/var/lib/mysql

View File

@ -0,0 +1,39 @@
<?php
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
abstract class FileshareTestCase extends TestCase
{
use DatabaseTransactions;
protected $adminUser = 'admin';
protected $adminPassword = 'admin';
public function setUp(): void
{
parent::setUp();
foreach ($this->http()->get('/ocs/v1.php/cloud/users?format=json')->json('ocs.data.users') as $user) {
if ($user === $this->adminUser) {
continue;
}
$this->http()->delete('/ocs/v1.php/cloud/users/' . $user);
}
}
public function withOwncloudUser(string $username, string $password): self
{
$this->http()->asForm()->post('/ocs/v1.php/cloud/users?format=json', ['password' => $password, 'userid' => $username]);
return $this;
}
private function http(): PendingRequest
{
return Http::withOptions(['base_uri' => env('TEST_OWNCLOUD_DOMAIN')])->withBasicAuth($this->adminUser, $this->adminPassword)->acceptJson();
}
}