diff --git a/appinfo/app.php b/appinfo/app.php index 6712b61..9f5a408 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -22,14 +22,5 @@ * */ -$app = new \OCA\Preferred_Providers\AppInfo\Application(); +$app = \OC::$server->query(\OCA\Preferred_Providers\AppInfo\Application::class); $app->register(); - -// --- register js for user management------------------------------------------ -$eventDispatcher = \OC::$server->getEventDispatcher(); -$eventDispatcher->addListener( - 'OC\Settings\Users::loadAdditionalScripts', - function() { - \OCP\Util::addScript('preferred_providers', 'users-management'); - } -); \ No newline at end of file diff --git a/css/admin-settings.css b/css/admin-settings.css index 0b59fe2..331b32a 100644 --- a/css/admin-settings.css +++ b/css/admin-settings.css @@ -11,7 +11,7 @@ height: 44px; margin-left: 10px; } -#groups { +#groups-section select { width: 250px; height: 100px; background-image: none; diff --git a/js/admin-settings.js b/js/admin-settings.js index 1409d2f..77ab63c 100644 --- a/js/admin-settings.js +++ b/js/admin-settings.js @@ -16,3 +16,19 @@ $('#groups:not(:disabled)').change(_.debounce(function() { $('#groups').attr('disabled', false).removeClass('icon-loading'); }, 'json'); }, 250)); + +$('#groups_unconfirmed:not(:disabled)').change(_.debounce(function() { + $('#groups_unconfirmed').attr('disabled', true).addClass('icon-loading'); + var groups = $('#groups_unconfirmed').val(); + $.post(OC.linkToOCS('apps/preferred_providers/api/v1', 2)+ 'groups', {groups: groups, for: 'unconfirmed'}, function(response) { + $('#groups_unconfirmed').attr('disabled', false).removeClass('icon-loading'); + }, 'json'); +}, 250)); + +$('#groups_confirmed:not(:disabled)').change(_.debounce(function() { + $('#groups_confirmed').attr('disabled', true).addClass('icon-loading'); + var groups = $('#groups_confirmed').val(); + $.post(OC.linkToOCS('apps/preferred_providers/api/v1', 2)+ 'groups', {groups: groups, for: 'confirmed'}, function(response) { + $('#groups_confirmed').attr('disabled', false).removeClass('icon-loading'); + }, 'json'); +}, 250)); \ No newline at end of file diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index e5d89d0..7a8defc 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,23 +24,30 @@ namespace OCA\Preferred_Providers\AppInfo; +use OCA\Preferred_Providers\Hook\LoginHook; use OCA\Preferred_Providers\Notification\Notifier; use OCP\AppFramework\App; use OCP\IServerContainer; -use OCA\Preferred_Providers\Hook\LoginHook; +use OCP\Util; class Application extends App { - /** @var string */ - protected $appName = 'preferred_providers'; + const APP_ID = 'preferred_providers'; public function __construct() { - parent::__construct($this->appName); + parent::__construct(self::APP_ID); } public function register() { $this->registerNotifier($this->getContainer()->getServer()); $this->getContainer()->query(LoginHook::class)->register(); + + $eventDispatcher = $this->getContainer()->getServer()->getEventDispatcher(); + $eventDispatcher->addListener('OC\Settings\Users::loadAdditionalScripts', + function() { + Util::addScript(self::APP_ID, 'users-management'); + } + ); } protected function registerNotifier(IServerContainer $server) { diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 8b3fb28..d2d0df2 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -163,9 +163,11 @@ public function requestAccount(string $token = '', string $email = '') { $newUser = $this->userManager->createUser($email, $password); // add user to groups - $groups = $this->config->getAppValue($this->appName, 'provider_groups', false); - if ($groups !== false) { - $groupIds = explode(',', $groups); + $groups = $this->config->getAppValue($this->appName, 'provider_groups', ''); + $unconfirmedGroups = $this->config->getAppValue($this->appName, 'provider_groups_unconfirmed', ''); + + if (!empty($groups)) { + $groupIds = array_merge(explode(',', $groups), explode(',', $unconfirmedGroups)); foreach ($groupIds as $groupId) { if ($this->groupManager->groupExists($groupId)) { $this->groupManager->get($groupId)->addUser($newUser); diff --git a/lib/Controller/MailController.php b/lib/Controller/MailController.php index ef0abfd..9cf8390 100644 --- a/lib/Controller/MailController.php +++ b/lib/Controller/MailController.php @@ -32,6 +32,7 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use OCP\IGroupManager; use OCP\IL10N; use OCP\IRequest; use OCP\IURLGenerator; @@ -64,6 +65,9 @@ class MailController extends Controller { /** @var VerifyMailHelper */ private $verifyMailHelper; + /** @var IGroupManager */ + private $groupManager; + /** * Account constructor. @@ -77,6 +81,7 @@ class MailController extends Controller { * @param ITimeFactory $timeFactory * @param IURLGenerator $urlGenerator * @param VerifyMailHelper $verifyMailHelper + * @param VerifyMailHelper $verifyMailHelper */ public function __construct(string $appName, IRequest $request, @@ -86,7 +91,8 @@ public function __construct(string $appName, ICrypto $crypto, ITimeFactory $timeFactory, IURLGenerator $urlGenerator, - VerifyMailHelper $verifyMailHelper) { + VerifyMailHelper $verifyMailHelper, + IGroupManager $groupManager) { parent::__construct($appName, $request); $this->appName = $appName; $this->config = $config; @@ -96,6 +102,7 @@ public function __construct(string $appName, $this->timeFactory = $timeFactory; $this->urlGenerator = $urlGenerator; $this->verifyMailHelper = $verifyMailHelper; + $this->groupManager = $groupManager; } @@ -136,6 +143,27 @@ public function confirmMailAddress(string $email, string $token) { ]); } + // add/remove user to groups + $unconfirmedGroups = $this->config->getAppValue($this->appName, 'provider_groups_unconfirmed', ''); + $confirmedGroups = $this->config->getAppValue($this->appName, 'provider_groups_confirmed', ''); + + if ($unconfirmedGroups !== '') { + $groupIds = explode(',', $unconfirmedGroups); + foreach ($groupIds as $groupId) { + if ($this->groupManager->groupExists($groupId)) { + $this->groupManager->get($groupId)->removeUser($user); + } + } + } + if ($confirmedGroups !== '') { + $groupIds = explode(',', $confirmedGroups); + foreach ($groupIds as $groupId) { + if ($this->groupManager->groupExists($groupId)) { + $this->groupManager->get($groupId)->addUser($user); + } + } + } + // redirect to home, user should already be logged return new RedirectResponse($this->urlGenerator->getAbsoluteURL('/')); } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index a7b1bff..852430e 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -152,13 +152,20 @@ public function resetToken(): DataResponse { * @return DataResponse * @throws OCSNotFoundException */ - public function setGroups(array $groups): DataResponse { + public function setGroups(array $groups, string $for = 'all'): DataResponse { foreach ($groups as $groupId) { if (!$this->groupManager->groupExists($groupId)) { throw new OCSNotFoundException($groupId . ' does not exists'); } } - $this->config->setAppValue('preferred_providers', 'provider_groups', implode(',', $groups)); + + if ($for === 'all') { + $this->config->setAppValue('preferred_providers', 'provider_groups', implode(',', $groups)); + } elseif ($for === 'confirmed' || $for === 'unconfirmed') { + $this->config->setAppValue('preferred_providers', 'provider_groups_' . $for, implode(',', $groups)); + } else { + throw new OCSBadRequestException(); + } return new DataResponse(['groups' => $groups]); } diff --git a/lib/Settings/Admin.php b/lib/Settings/Admin.php index 5e0889b..7a49c19 100644 --- a/lib/Settings/Admin.php +++ b/lib/Settings/Admin.php @@ -27,11 +27,15 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\IGroupManager; +use OCP\IInitialStateService; use OCP\Security\ISecureRandom; use OCP\Settings\ISettings; class Admin implements ISettings { + /** @var string */ + private $appName; + /** @var IConfig */ private $config; @@ -41,6 +45,9 @@ class Admin implements ISettings { /** @var IGroupManager */ private $groupManager; + /** @var IInitialStateService */ + private $initialStateService; + /** * Admin constructor. * @@ -48,12 +55,17 @@ class Admin implements ISettings { * @param ISecureRandom $secureRandom * @param IGroupManager $groupManager */ - public function __construct(IConfig $config, + public function __construct(string $appName, + IConfig $config, ISecureRandom $secureRandom, - IGroupManager $groupManager) { + IGroupManager $groupManager, + IInitialStateService $initialStateService) { + $this->appName = $appName; $this->config = $config; $this->secureRandom = $secureRandom; $this->groupManager = $groupManager; + $this->initialStateService = $initialStateService; + } /** @@ -61,20 +73,26 @@ public function __construct(IConfig $config, */ public function getForm() { // Generate new token if none exists - $provider_token = $this->config->getAppValue('preferred_providers', 'provider_token', false); - $provider_groups = $this->config->getAppValue('preferred_providers', 'provider_groups', ''); + $provider_token = $this->config->getAppValue($this->appName, 'provider_token', false); if (!$provider_token) { $provider_token = md5($this->secureRandom->generate(10)); - $this->config->setAppValue('preferred_providers', 'provider_token', $provider_token); + $this->config->setAppValue($this->appName, 'provider_token', $provider_token); } + // Get groups settings + $provider_groups = $this->config->getAppValue($this->appName, 'provider_groups', ''); + $provider_groups_confirmed = $this->config->getAppValue($this->appName, 'provider_groups_confirmed', ''); + $provider_groups_unconfirmed = $this->config->getAppValue($this->appName, 'provider_groups_unconfirmed', ''); + $parameters = [ 'provider_token' => $provider_token, 'provider_groups' => explode(',', $provider_groups), + 'provider_groups_confirmed' => explode(',', $provider_groups_confirmed), + 'provider_groups_unconfirmed' => explode(',', $provider_groups_unconfirmed), 'groups' => $this->groupManager->search('') ]; - return new TemplateResponse('preferred_providers', 'settings-admin', $parameters, ''); + return new TemplateResponse($this->appName, 'settings-admin', $parameters); } /** diff --git a/templates/settings-admin.php b/templates/settings-admin.php index 89520d2..0c95933 100644 --- a/templates/settings-admin.php +++ b/templates/settings-admin.php @@ -47,4 +47,24 @@ + +

t('The following groups will be set to every unconfirmed account and removed when confirmed')); ?>:

+ + +

t('The following groups will be set to every confirmed account')); ?>:

+ \ No newline at end of file