Skip to content

Commit

Permalink
event on some .well-known/ request
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Dec 9, 2020
1 parent e02c9ec commit 382d2f9
Show file tree
Hide file tree
Showing 10 changed files with 780 additions and 31 deletions.
7 changes: 7 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,9 @@
'OCP\\User\\Events\\UserLoggedOutEvent' => $baseDir . '/lib/public/User/Events/UserLoggedOutEvent.php',
'OCP\\User\\GetQuotaEvent' => $baseDir . '/lib/public/User/GetQuotaEvent.php',
'OCP\\Util' => $baseDir . '/lib/public/Util.php',
'OCP\\WellKnown\\Event\\WellKnownEvent' => $baseDir . '/lib/public/WellKnown/Event/WellKnownEvent.php',
'OCP\\WellKnown\\IWellKnownManager' => $baseDir . '/lib/public/WellKnown/IWellKnownManager.php',
'OCP\\WellKnown\\Model\\IWellKnown' => $baseDir . '/lib/public/WellKnown/Model/IWellKnown.php',
'OCP\\WorkflowEngine\\EntityContext\\IContextPortation' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IContextPortation.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayName' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayName.php',
'OCP\\WorkflowEngine\\EntityContext\\IDisplayText' => $baseDir . '/lib/public/WorkflowEngine/EntityContext/IDisplayText.php',
Expand Down Expand Up @@ -1383,6 +1386,10 @@
'OC\\User\\NoUserException' => $baseDir . '/lib/private/User/NoUserException.php',
'OC\\User\\Session' => $baseDir . '/lib/private/User/Session.php',
'OC\\User\\User' => $baseDir . '/lib/private/User/User.php',
'OC\\WellKnown\\Exceptions\\NotManagedWellKnownRequestException' => $baseDir . '/lib/private/WellKnown/Exceptions/NotManagedWellKnownRequestException.php',
'OC\\WellKnown\\Exceptions\\WellKnownRequestException' => $baseDir . '/lib/private/WellKnown/Exceptions/WellKnownRequestException.php',
'OC\\WellKnown\\Model\\WellKnown' => $baseDir . '/lib/private/WellKnown/Model/WellKnown.php',
'OC\\WellKnown\\WellKnownManager' => $baseDir . '/lib/private/WellKnown/WellKnownManager.php',
'OC_API' => $baseDir . '/lib/private/legacy/OC_API.php',
'OC_App' => $baseDir . '/lib/private/legacy/OC_App.php',
'OC_DB' => $baseDir . '/lib/private/legacy/OC_DB.php',
Expand Down
41 changes: 14 additions & 27 deletions lib/composer/composer/autoload_static.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020, Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OC\WellKnown\Exceptions;

use Exception;
use Throwable;

/**
* Class NotManagedWellKnownRequestException
*
* @package OC\WellKnown\Exceptions
* @since 21.0.0
*/
class NotManagedWellKnownRequestException extends Exception {


/** @var int */
private $errorCode;


/**
* WellKnownRequestException constructor.
*
* @param int $errorCode
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(int $errorCode = 404, string $message = '', int $code = 0, ?Throwable $previous = null) {
parent::__construct($message, $code, $previous);
$this->errorCode = $errorCode;
}


/**
* @return int
* @since 21.0.0
*/
public function getErrorCode(): int {
return $this->errorCode;
}
}
67 changes: 67 additions & 0 deletions lib/private/WellKnown/Exceptions/WellKnownRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020, Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OC\WellKnown\Exceptions;

use Exception;
use Throwable;

/**
* Class WellKnownRequestException
*
* @package OC\WellKnown\Exceptions
* @since 21.0.0
*/
class WellKnownRequestException extends Exception {


/** @var int */
private $errorCode;


/**
* WellKnownRequestException constructor.
*
* @param int $errorCode
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(int $errorCode = 404, string $message = '', int $code = 0, ?Throwable $previous = null) {
parent::__construct($message, $code, $previous);
$this->errorCode = $errorCode;
}


/**
* @return int
* @since 21.0.0
*/
public function getErrorCode(): int {
return $this->errorCode;
}
}
222 changes: 222 additions & 0 deletions lib/private/WellKnown/Model/WellKnown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

declare(strict_types=1);

/**
* @copyright 2020, Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/


namespace OC\WellKnown\Model;

use JsonSerializable;
use OCP\IRequest;
use OCP\WellKnown\Model\IWellKnown;

/**
* @since 21.0.0
*
* @package OC\WellKnown\Model
*/
final class WellKnown implements IWellKnown, JsonSerializable {


/** @var string */
private $service;

/** @var IRequest */
private $request;

/** @var string */
private $subject = '';

/** @var array */
private $aliases = [];

/** @var array */
private $properties = [];

/** @var array */
private $rels = [];

/** @var array */
private $links = [];


/**
* WellKnown constructor.
*
* @param string $service
* @param IRequest $request
*
* @since 21.0.0
*/
public function __construct(string $service, IRequest $request) {
$this->request = $request;
$this->service = $service;
$this->subject = $request->getParam('resource', '');
}


/**
* @return string
* @since 21.0.0
*/
public function getService(): string {
return $this->service;
}

/**
* @param string $service
*
* @return bool
* @since 21.0.0
*/
public function isService(string $service): bool {
return ($this->service === $service);
}


/**
* @return IRequest
* @since 21.0.0
*/
public function getRequest(): IRequest {
return $this->request;
}


/**
* @return string
* @since 21.0.0
*/
public function getSubject(): string {
return $this->subject;
}


/**
* @return array
* @since 21.0.0
*/
public function getRels(): array {
return $this->rels;
}


/**
* @param string $alias
*
* @return $this
* @since 21.0.0
*/
public function addAlias(string $alias): IWellKnown {
if (!in_array($alias, $this->aliases)) {
$this->aliases[] = $alias;
}

return $this;
}

/**
* @return array
* @since 21.0.0
*/
public function getAliases(): array {
return $this->aliases;
}


/**
* @param string $property
* @param $value
*
* @return IWellKnown
* @since 21.0.0
*/
public function addProperty(string $property, $value): IWellKnown {
$this->properties[$property] = $value;

return $this;
}

/**
* @return array
* @since 21.0.0
*/
public function getProperties(): array {
return $this->properties;
}


/**
* Please refer to official RFC regarding the generation of $link:
* - https://tools.ietf.org/html/rfc7033#section-4.4.4
*
* @param array $link
* @psalm-param array{rel: string, type: string, href: string, titles: array, properties: array} $link
* @return IWellKnown
*
* @return IWellKnown
* @since 21.0.0
*/
public function addLink(array $link): IWellKnown {
$this->links[] = $link;

return $this;
}

/**
* @param JsonSerializable $object
*
* @return IWellKnown
* @since 21.0.0
*/
public function addLinkSerialized(JsonSerializable $object): IWellKnown {
$this->links[] = $object;

return $this;
}

/**
* @return array
* @since 21.0.0
*/
public function getLinks(): array {
return $this->links;
}


/**
* @return array
* @since 21.0.0
*/
public function jsonSerialize(): array {
$data = [
'subject' => $this->getSubject(),
'properties' => $this->getProperties(),
'aliases' => $this->getAliases(),
'links' => $this->getLinks()
];

return array_filter($data);
}
}
Loading

0 comments on commit 382d2f9

Please sign in to comment.