2014-10-03 17:14:22 +04:00
|
|
|
<?php
|
2020-06-08 20:03:46 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2014-10-03 17:14:22 +04:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
|
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Maxence Lange <maxence@artificial-owl.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author zulan <git@zulan.net>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-10-03 17:14:22 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
namespace OCA\Settings\AppInfo;
|
2014-10-03 17:14:22 +04:00
|
|
|
|
2019-02-18 19:38:38 +03:00
|
|
|
use BadMethodCallException;
|
2016-10-27 18:41:15 +03:00
|
|
|
use OC\AppFramework\Utility\TimeFactory;
|
2016-10-05 17:31:28 +03:00
|
|
|
use OC\Authentication\Token\IProvider;
|
2019-02-18 19:38:38 +03:00
|
|
|
use OC\Authentication\Token\IToken;
|
2020-07-16 18:08:03 +03:00
|
|
|
use OC\Group\Manager;
|
2016-04-25 15:56:11 +03:00
|
|
|
use OC\Server;
|
2019-09-17 17:33:27 +03:00
|
|
|
use OCA\Settings\Activity\Provider;
|
|
|
|
use OCA\Settings\Hooks;
|
|
|
|
use OCA\Settings\Mailer\NewUserMailHelper;
|
|
|
|
use OCA\Settings\Middleware\SubadminMiddleware;
|
2020-08-03 18:03:06 +03:00
|
|
|
use OCA\Settings\Search\SectionSearch;
|
2019-02-18 19:38:38 +03:00
|
|
|
use OCP\Activity\IManager as IActivityManager;
|
2016-05-18 13:03:22 +03:00
|
|
|
use OCP\AppFramework\App;
|
2020-06-08 20:03:46 +03:00
|
|
|
use OCP\AppFramework\Bootstrap\IBootContext;
|
|
|
|
use OCP\AppFramework\Bootstrap\IBootstrap;
|
|
|
|
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
2020-07-21 15:36:40 +03:00
|
|
|
use OCP\AppFramework\IAppContainer;
|
2017-04-07 23:42:43 +03:00
|
|
|
use OCP\Defaults;
|
2019-03-04 14:46:59 +03:00
|
|
|
use OCP\IGroup;
|
2020-07-16 18:08:03 +03:00
|
|
|
use OCP\IGroupManager;
|
2019-02-18 19:38:38 +03:00
|
|
|
use OCP\ILogger;
|
2020-07-16 11:34:53 +03:00
|
|
|
use OCP\IServerContainer;
|
2019-03-04 14:46:59 +03:00
|
|
|
use OCP\IUser;
|
2016-10-05 17:31:28 +03:00
|
|
|
use OCP\Settings\IManager;
|
2016-05-18 13:03:22 +03:00
|
|
|
use OCP\Util;
|
2019-02-18 19:38:38 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2014-10-03 17:14:22 +04:00
|
|
|
|
2020-06-08 20:03:46 +03:00
|
|
|
class Application extends App implements IBootstrap {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const APP_ID = 'settings';
|
2014-10-03 17:14:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $urlParams
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function __construct(array $urlParams=[]) {
|
2020-02-09 22:06:08 +03:00
|
|
|
parent::__construct(self::APP_ID, $urlParams);
|
2020-06-08 20:03:46 +03:00
|
|
|
}
|
2014-10-03 17:14:22 +04:00
|
|
|
|
2020-06-08 20:03:46 +03:00
|
|
|
public function register(IRegistrationContext $context): void {
|
2016-10-05 17:31:28 +03:00
|
|
|
// Register Middleware
|
2020-06-08 20:03:46 +03:00
|
|
|
$context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
|
|
|
|
$context->registerMiddleware(SubadminMiddleware::class);
|
2020-08-03 18:03:06 +03:00
|
|
|
$context->registerSearchProvider(SectionSearch::class);
|
2016-11-22 15:33:23 +03:00
|
|
|
|
2014-10-03 17:14:22 +04:00
|
|
|
/**
|
|
|
|
* Core class wrappers
|
|
|
|
*/
|
2014-12-04 16:15:55 +03:00
|
|
|
/** FIXME: Remove once OC_User is non-static and mockable */
|
2020-06-08 20:03:46 +03:00
|
|
|
$context->registerService('isAdmin', function () {
|
2014-12-04 16:15:55 +03:00
|
|
|
return \OC_User::isAdminUser(\OC_User::getUser());
|
|
|
|
});
|
|
|
|
/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
|
2020-07-16 11:34:53 +03:00
|
|
|
$context->registerService('isSubAdmin', function () {
|
2015-10-27 16:09:45 +03:00
|
|
|
$userObject = \OC::$server->getUserSession()->getUser();
|
|
|
|
$isSubAdmin = false;
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($userObject !== null) {
|
2015-10-27 16:09:45 +03:00
|
|
|
$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
|
|
|
|
}
|
|
|
|
return $isSubAdmin;
|
2015-01-30 16:16:16 +03:00
|
|
|
});
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->registerService('userCertificateManager', function (IAppContainer $appContainer) {
|
2020-07-16 11:34:53 +03:00
|
|
|
/** @var IServerContainer $serverContainer */
|
2020-07-21 15:36:40 +03:00
|
|
|
$serverContainer = $appContainer->get(IServerContainer::class);
|
2020-07-16 11:34:53 +03:00
|
|
|
return $serverContainer->getCertificateManager();
|
2016-10-05 17:31:28 +03:00
|
|
|
}, false);
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->registerService('systemCertificateManager', function (IAppContainer $appContainer) {
|
2020-07-16 11:34:53 +03:00
|
|
|
/** @var IServerContainer $serverContainer */
|
2020-07-21 15:36:40 +03:00
|
|
|
$serverContainer = $appContainer->query('ServerContainer');
|
2020-07-16 11:34:53 +03:00
|
|
|
return $serverContainer->getCertificateManager(null);
|
2016-10-05 17:31:28 +03:00
|
|
|
}, false);
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->registerService(IProvider::class, function (IAppContainer $appContainer) {
|
2020-07-16 11:34:53 +03:00
|
|
|
/** @var IServerContainer $serverContainer */
|
2020-07-21 15:36:40 +03:00
|
|
|
$serverContainer = $appContainer->query(IServerContainer::class);
|
2020-07-16 11:34:53 +03:00
|
|
|
return $serverContainer->query(IProvider::class);
|
2015-12-22 19:42:28 +03:00
|
|
|
});
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->registerService(IManager::class, function (IAppContainer $appContainer) {
|
2020-07-16 11:34:53 +03:00
|
|
|
/** @var IServerContainer $serverContainer */
|
2020-07-21 15:36:40 +03:00
|
|
|
$serverContainer = $appContainer->query(IServerContainer::class);
|
2020-07-16 11:34:53 +03:00
|
|
|
return $serverContainer->getSettingsManager();
|
2016-08-09 00:31:26 +03:00
|
|
|
});
|
2017-04-07 15:51:05 +03:00
|
|
|
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->registerService(NewUserMailHelper::class, function (IAppContainer $appContainer) {
|
2017-04-07 15:51:05 +03:00
|
|
|
/** @var Server $server */
|
2020-07-21 15:36:40 +03:00
|
|
|
$server = $appContainer->query(IServerContainer::class);
|
2017-04-07 23:42:43 +03:00
|
|
|
/** @var Defaults $defaults */
|
|
|
|
$defaults = $server->query(Defaults::class);
|
2017-04-07 15:51:05 +03:00
|
|
|
|
|
|
|
return new NewUserMailHelper(
|
2017-04-07 23:42:43 +03:00
|
|
|
$defaults,
|
2017-04-07 15:51:05 +03:00
|
|
|
$server->getURLGenerator(),
|
2018-09-24 23:54:57 +03:00
|
|
|
$server->getL10NFactory(),
|
2017-04-07 15:51:05 +03:00
|
|
|
$server->getMailer(),
|
|
|
|
$server->getSecureRandom(),
|
|
|
|
new TimeFactory(),
|
|
|
|
$server->getConfig(),
|
|
|
|
$server->getCrypto(),
|
|
|
|
Util::getDefaultEmailAddress('no-reply')
|
|
|
|
);
|
|
|
|
});
|
2020-06-08 20:03:46 +03:00
|
|
|
}
|
2019-02-18 19:38:38 +03:00
|
|
|
|
2020-06-08 20:03:46 +03:00
|
|
|
public function boot(IBootContext $context): void {
|
2020-07-21 15:36:40 +03:00
|
|
|
$context->injectFn(function (EventDispatcherInterface $dispatcher, IAppContainer $appContainer) {
|
|
|
|
$dispatcher->addListener('app_password_created', function (GenericEvent $event) use ($appContainer) {
|
2020-07-16 18:08:03 +03:00
|
|
|
if (($token = $event->getSubject()) instanceof IToken) {
|
|
|
|
/** @var IActivityManager $activityManager */
|
2020-07-21 15:36:40 +03:00
|
|
|
$activityManager = $appContainer->get(IActivityManager::class);
|
2020-07-16 18:08:03 +03:00
|
|
|
/** @var ILogger $logger */
|
2020-07-21 15:36:40 +03:00
|
|
|
$logger = $appContainer->get(ILogger::class);
|
2020-07-16 18:08:03 +03:00
|
|
|
|
|
|
|
$activity = $activityManager->generateEvent();
|
|
|
|
$activity->setApp('settings')
|
|
|
|
->setType('security')
|
|
|
|
->setAffectedUser($token->getUID())
|
|
|
|
->setAuthor($token->getUID())
|
|
|
|
->setSubject(Provider::APP_TOKEN_CREATED, ['name' => $token->getName()])
|
|
|
|
->setObject('app_token', $token->getId());
|
|
|
|
|
|
|
|
try {
|
|
|
|
$activityManager->publish($activity);
|
|
|
|
} catch (BadMethodCallException $e) {
|
|
|
|
$logger->logException($e, ['message' => 'could not publish activity', 'level' => ILogger::WARN]);
|
|
|
|
}
|
2019-02-18 19:38:38 +03:00
|
|
|
}
|
2020-07-16 18:08:03 +03:00
|
|
|
});
|
2019-02-18 19:38:38 +03:00
|
|
|
});
|
2017-04-12 16:19:45 +03:00
|
|
|
|
2017-04-13 12:58:19 +03:00
|
|
|
Util::connectHook('OC_User', 'post_setPassword', $this, 'onChangePassword');
|
2017-04-12 16:19:45 +03:00
|
|
|
Util::connectHook('OC_User', 'changeUser', $this, 'onChangeInfo');
|
2018-10-10 15:12:10 +03:00
|
|
|
|
2020-07-16 18:08:03 +03:00
|
|
|
$context->injectFn(function (IGroupManager $groupManager) {
|
|
|
|
/** @var IGroupManager|Manager $groupManager */
|
|
|
|
$groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'removeUserFromGroup']);
|
|
|
|
$groupManager->listen('\OC\Group', 'postAddUser', [$this, 'addUserToGroup']);
|
|
|
|
});
|
2019-03-04 14:46:59 +03:00
|
|
|
|
2018-10-10 15:12:10 +03:00
|
|
|
Util::connectHook('\OCP\Config', 'js', $this, 'extendJsConfig');
|
2017-04-12 16:19:45 +03:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:46:59 +03:00
|
|
|
public function addUserToGroup(IGroup $group, IUser $user): void {
|
|
|
|
/** @var Hooks $hooks */
|
|
|
|
$hooks = $this->getContainer()->query(Hooks::class);
|
|
|
|
$hooks->addUserToGroup($group, $user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeUserFromGroup(IGroup $group, IUser $user): void {
|
|
|
|
/** @var Hooks $hooks */
|
|
|
|
$hooks = $this->getContainer()->query(Hooks::class);
|
|
|
|
$hooks->removeUserFromGroup($group, $user);
|
|
|
|
}
|
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
|
2017-04-13 12:49:02 +03:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
* @throws \Exception
|
2017-04-13 12:58:19 +03:00
|
|
|
* @throws \OCP\AppFramework\QueryException
|
2017-04-13 12:49:02 +03:00
|
|
|
*/
|
2017-04-13 12:58:19 +03:00
|
|
|
public function onChangePassword(array $parameters) {
|
|
|
|
/** @var Hooks $hooks */
|
|
|
|
$hooks = $this->getContainer()->query(Hooks::class);
|
|
|
|
$hooks->onChangePassword($parameters['uid']);
|
2017-04-12 16:19:45 +03:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:58:19 +03:00
|
|
|
/**
|
|
|
|
* @param array $parameters
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @throws \BadMethodCallException
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \OCP\AppFramework\QueryException
|
|
|
|
*/
|
|
|
|
public function onChangeInfo(array $parameters) {
|
2017-04-12 16:19:45 +03:00
|
|
|
if ($parameters['feature'] !== 'eMailAddress') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:58:19 +03:00
|
|
|
/** @var Hooks $hooks */
|
|
|
|
$hooks = $this->getContainer()->query(Hooks::class);
|
2017-04-13 13:34:34 +03:00
|
|
|
$hooks->onChangeEmail($parameters['user'], $parameters['old_value']);
|
2017-04-12 16:19:45 +03:00
|
|
|
}
|
2018-10-10 15:12:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $settings
|
|
|
|
*/
|
|
|
|
public function extendJsConfig(array $settings) {
|
|
|
|
$appConfig = json_decode($settings['array']['oc_appconfig'], true);
|
|
|
|
|
|
|
|
$publicWebFinger = \OC::$server->getConfig()->getAppValue('core', 'public_webfinger', '');
|
|
|
|
if (!empty($publicWebFinger)) {
|
|
|
|
$appConfig['core']['public_webfinger'] = $publicWebFinger;
|
|
|
|
}
|
|
|
|
|
2019-07-16 02:03:57 +03:00
|
|
|
$publicNodeInfo = \OC::$server->getConfig()->getAppValue('core', 'public_nodeinfo', '');
|
|
|
|
if (!empty($publicNodeInfo)) {
|
|
|
|
$appConfig['core']['public_nodeinfo'] = $publicNodeInfo;
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:12:10 +03:00
|
|
|
$settings['array']['oc_appconfig'] = json_encode($appConfig);
|
|
|
|
}
|
2014-10-03 17:14:22 +04:00
|
|
|
}
|