Skip to content

Commit

Permalink
Merge pull request #17 from nextcloud/enhancement/allow-groups-confir…
Browse files Browse the repository at this point in the history
…med-unconfirmed
  • Loading branch information
skjnldsv authored May 15, 2020
2 parents 3c5d467 + 6bdd445 commit 4b0fb7c
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 27 deletions.
11 changes: 1 addition & 10 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
);
2 changes: 1 addition & 1 deletion css/admin-settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
height: 44px;
margin-left: 10px;
}
#groups {
#groups-section select {
width: 250px;
height: 100px;
background-image: none;
Expand Down
16 changes: 16 additions & 0 deletions js/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
15 changes: 11 additions & 4 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions lib/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 29 additions & 1 deletion lib/Controller/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +65,9 @@ class MailController extends Controller {
/** @var VerifyMailHelper */
private $verifyMailHelper;

/** @var IGroupManager */
private $groupManager;


/**
* Account constructor.
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -96,6 +102,7 @@ public function __construct(string $appName,
$this->timeFactory = $timeFactory;
$this->urlGenerator = $urlGenerator;
$this->verifyMailHelper = $verifyMailHelper;
$this->groupManager = $groupManager;
}


Expand Down Expand Up @@ -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('/'));
}
Expand Down
11 changes: 9 additions & 2 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
30 changes: 24 additions & 6 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,40 +45,54 @@ class Admin implements ISettings {
/** @var IGroupManager */
private $groupManager;

/** @var IInitialStateService */
private $initialStateService;

/**
* Admin constructor.
*
* @param IConfig $config
* @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;

}

/**
* @return TemplateResponse
*/
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);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@
<option value="<?php p($gid); ?>" <?php p(in_array($gid, $_['provider_groups']) ? 'selected="selected"' : ''); ?>><?php p($gname); ?></option>
<?php }?>
</select>

<p><?php p($l->t('The following groups will be set to every unconfirmed account and removed when confirmed')); ?>:</p>
<select multiple id="groups_unconfirmed">
<?php foreach($_['groups'] as $group) {
$gid = $group->getGid();
$gname = $group->getDisplayName();
?>
<option value="<?php p($gid); ?>" <?php p(in_array($gid, $_['provider_groups_unconfirmed']) ? 'selected="selected"' : ''); ?>><?php p($gname); ?></option>
<?php }?>
</select>

<p><?php p($l->t('The following groups will be set to every confirmed account')); ?>:</p>
<select multiple id="groups_confirmed">
<?php foreach($_['groups'] as $group) {
$gid = $group->getGid();
$gname = $group->getDisplayName();
?>
<option value="<?php p($gid); ?>" <?php p(in_array($gid, $_['provider_groups_confirmed']) ? 'selected="selected"' : ''); ?>><?php p($gname); ?></option>
<?php }?>
</select>
</div>

2 comments on commit 4b0fb7c

@mritzmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skjnldsv Is it possible to tag a new release and publish it in the store? :)

@skjnldsv
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mritzmann
Yes, it's on my todo this week :)
I was on sick leave for 3 weeks, i'm still catching up 👍

Please sign in to comment.