Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/twofactor_backupcodes/js/settings.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/twofactor_backupcodes/js/settings.js.map

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions apps/twofactor_backupcodes/lib/Provider/BackupCodesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings;
use OCP\Authentication\TwoFactorAuth\IProvider;
use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IUser;
use OCP\Template;
Expand All @@ -46,18 +47,25 @@ class BackupCodesProvider implements IProvider, IProvidesPersonalSettings {

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

/**
* @param string $appName
* @param BackupCodeStorage $storage
* @param IL10N $l10n
* @param AppManager $appManager
*/
public function __construct(string $appName, BackupCodeStorage $storage, IL10N $l10n, AppManager $appManager) {
public function __construct(string $appName,
BackupCodeStorage $storage,
IL10N $l10n,
AppManager $appManager,
IInitialStateService $initialStateService) {
$this->appName = $appName;
$this->l10n = $l10n;
$this->storage = $storage;
$this->appManager = $appManager;
$this->initialStateService = $initialStateService;
}

/**
Expand Down Expand Up @@ -149,7 +157,8 @@ public function isActive(IUser $user): bool {
*/
public function getPersonalSettings(IUser $user): IPersonalProviderSettings {
$state = $this->storage->getBackupCodesState($user);
return new Personal(base64_encode(json_encode($state)));
$this->initialStateService->provideInitialState($this->appName, $state);
return new Personal();
}

}
Expand Down
12 changes: 1 addition & 11 deletions apps/twofactor_backupcodes/lib/Settings/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@
use OCP\Template;

class Personal implements IPersonalProviderSettings {

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

public function __construct(string $state) {
$this->state = $state;
}

public function getBody(): Template {
$template = new Template('twofactor_backupcodes', 'personal');
$template->assign('state', $this->state);
return $template;
return new Template('twofactor_backupcodes', 'personal');
}

}
4 changes: 2 additions & 2 deletions apps/twofactor_backupcodes/src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import store from './store';

Vue.prototype.t = t;

const initialStateElem = document.getElementById('twofactor-backupcodes-initial-state');
const initialState = OCP.InitialState.loadState('twofactor_backupcodes');
store.replaceState(
JSON.parse(atob(initialStateElem.value))
initialState
)

const View = Vue.extend(PersonalSettings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OC\App\AppManager;
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IUser;
use OCP\Template;
Expand All @@ -45,6 +46,9 @@ class BackupCodesProviderTest extends TestCase {
/** @var AppManager|PHPUnit_Framework_MockObject_MockObject */
private $appManager;

/** @var IInitialStateService|PHPUnit_Framework_MockObject_MockObject */
private $initialState;

/** @var BackupCodesProvider */
private $provider;

Expand All @@ -55,8 +59,9 @@ protected function setUp() {
$this->storage = $this->createMock(BackupCodeStorage::class);
$this->l10n = $this->createMock(IL10N::class);
$this->appManager = $this->createMock(AppManager::class);
$this->initialState = $this->createMock(IInitialStateService::class);

$this->provider = new BackupCodesProvider($this->appName, $this->storage, $this->l10n, $this->appManager);
$this->provider = new BackupCodesProvider($this->appName, $this->storage, $this->l10n, $this->appManager, $this->initialState);
}

public function testGetId() {
Expand Down
2 changes: 1 addition & 1 deletion core/js/dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/main.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions core/src/OCP/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
*
*/
import loader from './loader'
import initialState from './initialstate'

/** @namespace OCP */
export default {
Loader: loader,
InitialState: initialState,
};
42 changes: 42 additions & 0 deletions core/src/OCP/initialstate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @copyright Copyright (c) 2019 Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @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 OCP
* @class InitialState
*/
export default {
loadState: function(app) {
const elem = document.querySelector('#initial-state-' + app);
if (elem === null) {
console.error('Could not find initial state of ' + app);
throw new Error('Could not find initial state of ' + app);
}

try {
return JSON.parse(atob(elem.value));
} catch (e) {
console.error('Could not parse initial state of ' + app);
throw new Error('Could not parse initial state of ' + app);
}
},
}
3 changes: 3 additions & 0 deletions core/templates/layout.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
</head>
<body id="body-public" class="layout-base">
<?php include 'layout.noscript.warning.php'; ?>
<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
<?php }?>
<div id="content" class="app-public" role="main">
<?php print_unescaped($_['content']); ?>
</div>
Expand Down
3 changes: 3 additions & 0 deletions core/templates/layout.guest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
</head>
<body id="<?php p($_['bodyid']);?>">
<?php include 'layout.noscript.warning.php'; ?>
<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
<?php }?>
<div class="wrapper">
<div class="v-align">
<?php if ($_['bodyid'] === 'body-login' ): ?>
Expand Down
3 changes: 3 additions & 0 deletions core/templates/layout.public.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
</head>
<body id="<?php p($_['bodyid']);?>">
<?php include('layout.noscript.warning.php'); ?>
<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
<?php }?>
<div id="notification-container">
<div id="notification"></div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions core/templates/layout.user.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<body id="<?php p($_['bodyid']);?>">
<?php include 'layout.noscript.warning.php'; ?>

<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
<?php }?>

<a href="#app-content" class="button primary skip-navigation skip-content"><?php p($l->t('Skip to main content')); ?></a>
<a href="#app-navigation" class="button primary skip-navigation"><?php p($l->t('Skip to navigation of app')); ?></a>

Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@
'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php',
'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php',
'OCP\\IImage' => $baseDir . '/lib/public/IImage.php',
'OCP\\IInitialStateService' => $baseDir . '/lib/public/IInitialStateService.php',
'OCP\\IL10N' => $baseDir . '/lib/public/IL10N.php',
'OCP\\ILogger' => $baseDir . '/lib/public/ILogger.php',
'OCP\\IMemcache' => $baseDir . '/lib/public/IMemcache.php',
Expand Down Expand Up @@ -854,6 +855,7 @@
'OC\\Http\\Client\\ClientService' => $baseDir . '/lib/private/Http/Client/ClientService.php',
'OC\\Http\\Client\\Response' => $baseDir . '/lib/private/Http/Client/Response.php',
'OC\\Http\\CookieHelper' => $baseDir . '/lib/private/Http/CookieHelper.php',
'OC\\InitialStateService' => $baseDir . '/lib/private/InitialStateService.php',
'OC\\Installer' => $baseDir . '/lib/private/Installer.php',
'OC\\IntegrityCheck\\Checker' => $baseDir . '/lib/private/IntegrityCheck/Checker.php',
'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException' => $baseDir . '/lib/private/IntegrityCheck/Exceptions/InvalidSignatureException.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php',
'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php',
'OCP\\IImage' => __DIR__ . '/../../..' . '/lib/public/IImage.php',
'OCP\\IInitialStateService' => __DIR__ . '/../../..' . '/lib/public/IInitialStateService.php',
'OCP\\IL10N' => __DIR__ . '/../../..' . '/lib/public/IL10N.php',
'OCP\\ILogger' => __DIR__ . '/../../..' . '/lib/public/ILogger.php',
'OCP\\IMemcache' => __DIR__ . '/../../..' . '/lib/public/IMemcache.php',
Expand Down Expand Up @@ -884,6 +885,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Http\\Client\\ClientService' => __DIR__ . '/../../..' . '/lib/private/Http/Client/ClientService.php',
'OC\\Http\\Client\\Response' => __DIR__ . '/../../..' . '/lib/private/Http/Client/Response.php',
'OC\\Http\\CookieHelper' => __DIR__ . '/../../..' . '/lib/private/Http/CookieHelper.php',
'OC\\InitialStateService' => __DIR__ . '/../../..' . '/lib/private/InitialStateService.php',
'OC\\Installer' => __DIR__ . '/../../..' . '/lib/private/Installer.php',
'OC\\IntegrityCheck\\Checker' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Checker.php',
'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Exceptions/InvalidSignatureException.php',
Expand Down
74 changes: 74 additions & 0 deletions lib/private/InitialStateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @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;

use OCP\IInitialStateService;
use OCP\ILogger;

class InitialStateService implements IInitialStateService {

/** @var ILogger */
private $logger;

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

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

public function __construct(ILogger $logger) {
$this->logger = $logger;
}

public function provideInitialState(string $appName, $data) {
// Scalars and JsonSerializable are fine
if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
$this->states[$appName] = json_encode($data);
return;
}

$this->logger->warning('Invalid data provided to provideInitialState by ' . $appName);
}

public function provideLazyInitialState(string $appName, \Closure $closure) {
$this->lazyStates[$appName] = $closure;
}

public function getInitialStates(): array {
$states = $this->states;
foreach ($this->lazyStates as $app => $lazyState) {
$state = $lazyState();

if (!($lazyState instanceof \JsonSerializable)) {
$this->logger->warning($app . ' provided an invalid lazy state');
}

$states[$app] = json_encode($state);
}

return $states;
}

}
3 changes: 3 additions & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
use OCP\Group\ISubAdmin;
use OCP\ICacheFactory;
use OCP\IDBConnection;
use OCP\IInitialStateService;
use OCP\IL10N;
use OCP\IServerContainer;
use OCP\ITempManager;
Expand Down Expand Up @@ -1204,6 +1205,8 @@ public function __construct($webRoot, \OC\Config $config) {

$this->registerAlias(ISubAdmin::class, SubAdmin::class);

$this->registerAlias(IInitialStateService::class, InitialStateService::class);

$this->connectDispatcher();
}

Expand Down
4 changes: 4 additions & 0 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ public function __construct( $renderAs, $appId = '' ) {

}
}

/** @var InitialStateService $initialState */
$initialState = \OC::$server->query(InitialStateService::class);
$this->assign('initialStates', $initialState->getInitialStates());
}

/**
Expand Down
56 changes: 56 additions & 0 deletions lib/public/IInitialStateService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[email protected]>
*
* @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 OCP;

/**
* @since 16.0.0
*/
interface IInitialStateService {
/**
* Allows an app to provide its initial state to the template system.
* Use this if you know your initial state sill be used for example if
* you are in the render function of you controller.
*
* @since 16.0.0
*
* @param string $appName
* @param bool|int|float|string|array|\JsonSerializable $data
*/
public function provideInitialState(string $appName, $data);

/**
* Allows an app to provide its initial state via a lazy method.
* This will call the closure when the template is being generated.
* Use this if your app is injected into pages. Since then the render method
* is not called explicitly. But we do not want to load the state on webdav
* requests for example.
*
* @since 16.0.0
*
* @param string $appName
* @param \Closure $closure Has to return an object that implements JsonSerializable
*/
public function provideLazyInitialState(string $appName, \Closure $closure);
}