145 lines
4.7 KiB
PHP
145 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace Zoomyboy\Owncloud\Components;
|
|
|
|
use Cache;
|
|
use Cms\Classes\ComponentBase;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Support\Facades\Lang;
|
|
use Illuminate\Validation\Rule;
|
|
use Input;
|
|
use Mail;
|
|
use Winter\Storm\Support\Facades\Validator;
|
|
use Zoomyboy\Event\Classes\Spreadsheet;
|
|
use Zoomyboy\Event\Classes\SpreadsheetHeader;
|
|
use Zoomyboy\Owncloud\Classes\Filesystem;
|
|
use Zoomyboy\Owncloud\Models\Settings;
|
|
|
|
class Form extends ComponentBase
|
|
{
|
|
public array $groups = [];
|
|
|
|
/**
|
|
* Gets the details for the component.
|
|
*/
|
|
public function componentDetails()
|
|
{
|
|
return [
|
|
'name' => 'Form Component',
|
|
'description' => 'No description provided yet...',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Returns the properties provided by the component.
|
|
*/
|
|
public function defineProperties()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function onRun(): void
|
|
{
|
|
// $filesystem = app(Filesystem::class)->client();
|
|
|
|
// $filesystem->write('lorem/gandalf.txt', 'AAAABBBB');
|
|
(new Spreadsheet('Anmeldezahlen BeLa'))
|
|
->headers([
|
|
new SpreadsheetHeader('Vorname'),
|
|
new SpreadsheetHeader('Nachname'),
|
|
new SpreadsheetHeader('Straße'),
|
|
new SpreadsheetHeader('PLZ'),
|
|
new SpreadsheetHeader('Ort'),
|
|
])
|
|
->sheet('Gandalf', [
|
|
['Philipp', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Philipp', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Philipp', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Philipp', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
])
|
|
->sheet('Gravenrose', [
|
|
['Jane', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Jane', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Jane', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
['Jane2', 'Lang', 'Itterstr 3', '42719', 'solingen'],
|
|
])
|
|
->generate();
|
|
|
|
$groups = Cache::remember('oc_groupsa', 1, function () {
|
|
return $this->getGroups();
|
|
});
|
|
$this->groups = collect($groups)
|
|
->filter(fn ($g) => !in_array($g, Settings::get('force_groups')))
|
|
->filter(fn ($g) => !in_array($g, Settings::get('disallowed_groups')))
|
|
->map(fn ($group) => ['id' => $group, 'name' => $group])
|
|
->toArray();
|
|
}
|
|
|
|
public function onSubmit()
|
|
{
|
|
$rules = [
|
|
'firstname' => 'required|string|max:255',
|
|
'lastname' => 'required|string|max:255',
|
|
'username' => ['required', 'string', 'max:255', 'regex:/^[a-zA-Z0-9]+$/', Rule::notIn($this->getUsers())],
|
|
'email' => 'required|email|string|max:255',
|
|
'group' => 'required|string|max:255',
|
|
'function' => 'required|string|max:255',
|
|
'oc_groups' => 'present|array',
|
|
'oc_groups.*' => 'string',
|
|
];
|
|
$validator = Validator::make(Input::all(), $rules, [
|
|
'username.not_in' => 'OwnCloud Nutzername wird bereits verwendet.',
|
|
'username.regex' => 'OwnCloud Nutzername darf nur aus Buchstaben und Zahlen bestehen',
|
|
], Lang::get('zoomyboy.owncloud::validation.attributes'));
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json($validator->errors(), 422);
|
|
}
|
|
|
|
$payload = base64_encode(json_encode($validator->validated()));
|
|
Mail::send('owncloud_request', [
|
|
'link' => url('/owncloud-confirm/'.$payload),
|
|
'payload' => $validator->validated(),
|
|
], function ($message) {
|
|
foreach (Settings::get('admins') as $admin) {
|
|
$message->to($admin);
|
|
}
|
|
});
|
|
|
|
return response()->json([], 201);
|
|
}
|
|
|
|
private function getGroups(): array
|
|
{
|
|
$response = $this->getClient()->get('/ocs/v1.php/cloud/groups');
|
|
|
|
$r = [];
|
|
$xml = simplexml_load_string((string) $response->getBody());
|
|
foreach ($xml->xpath('/ocs/data/groups/element') as $element) {
|
|
$r[] = (string) $element;
|
|
}
|
|
|
|
return $r;
|
|
}
|
|
|
|
private function getUsers(): array
|
|
{
|
|
$response = $this->getClient()->get('/ocs/v1.php/cloud/users');
|
|
|
|
$r = [];
|
|
$xml = simplexml_load_string((string) $response->getBody());
|
|
foreach ($xml->xpath('/ocs/data/users/element') as $element) {
|
|
$r[] = (string) $element;
|
|
}
|
|
|
|
return $r;
|
|
}
|
|
|
|
private function getClient(): Client
|
|
{
|
|
$auth = base64_encode(Settings::get('username').':'.Settings::get('password'));
|
|
|
|
return new Client(['base_uri' => Settings::get('url'), 'headers' => ['Authorization' => 'Basic '.$auth]]);
|
|
}
|
|
}
|