refactor appinfo

This commit is contained in:
Simon Vieille 2020-08-06 15:00:24 +02:00
parent cdcc4ddd51
commit ddb7cc40ce
Signed by: deblan
GPG Key ID: 03383D15A1D31745
2 changed files with 118 additions and 66 deletions

View File

@ -1,71 +1,9 @@
<?php
/**
* @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/>.
*/
use OCA\SideMenu\AppInfo\Application;
namespace OCA\SideMenu\Appinfo;
$app = new Application();
use OC\Security\CSP\ContentSecurityPolicy;
use OCP\Util;
use OCP\IUserSession;
$config = \OC::$server->getConfig();
$cspnm = \OC::$server->getContentSecurityPolicyNonceManager();
$user = \OC::$server[IUserSession::class]->getUser();
$enabled = true;
if ($user !== null) {
$enabled = (bool) $config->getUserValue($user->getUid(), 'side_menu', 'enabled', '1');
}
if ($enabled) {
Util::addScript('side_menu', 'sideMenu');
Util::addStyle('side_menu', 'sideMenu');
$stylesheet = \OC::$server->getURLGenerator()->linkToRoute(
'side_menu.Css.stylesheet',
[
'v' => $config->getAppValue('side_menu', 'cache', '0'),
]
);
$script = \OC::$server->getURLGenerator()->linkToRoute(
'side_menu.Js.script',
[
'v' => $config->getAppValue('side_menu', 'cache', '0'),
]
);
Util::addHeader(
'link',
[
'href' => $stylesheet,
'rel' => 'stylesheet'
],
''
);
Util::addHeader(
'script',
[
'src' => $script,
'nonce' => $cspnm->getNonce(),
],
''
);
if ($app->isEnabled()) {
$app->registerAssets();
}

114
lib/AppInfo/Application.php Normal file
View File

@ -0,0 +1,114 @@
<?php
namespace OCA\SideMenu\AppInfo;
use OC;
use OC\Security\CSP\ContentSecurityPolicyNonceManager;
use OC\User\User;
use OCA\SideMenu\Service\AppRepository;
use OCP\AppFramework\App;
use OCP\IUserSession;
use OCP\Util;
use Psr\Container\ContainerInterface;
/**
* class Application.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class Application extends App
{
/**
* @var OC\AllConfig
*/
protected $config;
/**
* @var ContentSecurityPolicyNonceManager
*/
protected $cspnm;
/**
* @var User
*/
protected $user;
/**
* {@inheritdoc}
*/
public function __construct(array $urlParams = [])
{
parent::__construct('side_menu', $urlParams);
$this->config = OC::$server->getConfig();
$this->cspnm = OC::$server->getContentSecurityPolicyNonceManager();
$this->user = OC::$server[IUserSession::class]->getUser();
}
/**
* Checks if this app is enabled.
*/
public function isEnabled(): bool
{
$enabled = true;
if (null !== $this->user) {
$enabled = (bool) $this->config->getUserValue($this->user->getUid(), 'side_menu', 'enabled', '1');
}
return $enabled;
}
/**
* Registes services.
*/
public function registerServices()
{
$container = $this->getContainer();
$container->registerService('AppRepository', function (ContainerInterface $c) {
return new AppRepository(new OC_App());
});
}
/**
* Registers assets.
*/
public function registerAssets()
{
Util::addScript('side_menu', 'sideMenu');
Util::addStyle('side_menu', 'sideMenu');
$stylesheet = OC::$server->getURLGenerator()->linkToRoute(
'side_menu.Css.stylesheet',
[
'v' => $this->config->getAppValue('side_menu', 'cache', '0'),
]
);
$script = OC::$server->getURLGenerator()->linkToRoute(
'side_menu.Js.script',
[
'v' => $this->config->getAppValue('side_menu', 'cache', '0'),
]
);
Util::addHeader(
'link',
[
'href' => $stylesheet,
'rel' => 'stylesheet',
],
''
);
Util::addHeader(
'script',
[
'src' => $script,
'nonce' => $this->cspnm->getNonce(),
],
''
);
}
}