Add a key parameter to the new initial state API

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
pull/13924/head
Christoph Wurst 5 years ago
parent 49ae3a3daa
commit 0e6cb988a1
No known key found for this signature in database
GPG Key ID: CC42AC2A7F0E56D8

@ -37,5 +37,5 @@ t.exports=function(t){return null!=t&&(n(t)||function(t){return"function"==typeo
* 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/>.
*/
o.a.use(N);const I={setEnabled(t,e){o.a.set(t,"enabled",e)},setTotal(t,e){o.a.set(t,"total",e)},setUsed(t,e){o.a.set(t,"used",e)},setCodes(t,e){o.a.set(t,"codes",e)}},P={generate:({commit:t})=>(t("setEnabled",!1),function(){const t=OC.generateUrl("/apps/twofactor_backupcodes/settings/create");return L.a.post(t,{}).then(t=>t.data)}().then(({codes:e,state:n})=>(t("setEnabled",n.enabled),t("setTotal",n.total),t("setUsed",n.used),t("setCodes",e),!0)))};var R=new N.Store({strict:!1,state:{enabled:!1,total:0,used:0,codes:void 0},mutations:I,actions:P});o.a.prototype.t=t;const D=OCP.InitialState.loadState("twofactor_backupcodes");R.replaceState(D),new(o.a.extend(c))({store:R}).$mount("#twofactor-backupcodes-settings")},function(t,e,n){"use strict";function r(t,e){for(var n=[],r={},o=0;o<e.length;o++){var i=e[o],a=i[0],s={id:t+":"+o,css:i[1],media:i[2],sourceMap:i[3]};r[a]?r[a].parts.push(s):n.push(r[a]={id:a,parts:[s]})}return n}n.r(e),n.d(e,"default",function(){return v});var o="undefined"!=typeof document;if("undefined"!=typeof DEBUG&&DEBUG&&!o)throw new Error("vue-style-loader cannot be used in a non-browser environment. Use { target: 'node' } in your Webpack config to indicate a server-rendering environment.");var i={},a=o&&(document.head||document.getElementsByTagName("head")[0]),s=null,c=0,u=!1,f=function(){},l=null,p="data-vue-ssr-id",d="undefined"!=typeof navigator&&/msie [6-9]\b/.test(navigator.userAgent.toLowerCase());function v(t,e,n,o){u=n,l=o||{};var a=r(t,e);return h(a),function(e){for(var n=[],o=0;o<a.length;o++){var s=a[o];(c=i[s.id]).refs--,n.push(c)}e?h(a=r(t,e)):a=[];for(o=0;o<n.length;o++){var c;if(0===(c=n[o]).refs){for(var u=0;u<c.parts.length;u++)c.parts[u]();delete i[c.id]}}}}function h(t){for(var e=0;e<t.length;e++){var n=t[e],r=i[n.id];if(r){r.refs++;for(var o=0;o<r.parts.length;o++)r.parts[o](n.parts[o]);for(;o<n.parts.length;o++)r.parts.push(g(n.parts[o]));r.parts.length>n.parts.length&&(r.parts.length=n.parts.length)}else{var a=[];for(o=0;o<n.parts.length;o++)a.push(g(n.parts[o]));i[n.id]={id:n.id,refs:1,parts:a}}}}function m(){var t=document.createElement("style");return t.type="text/css",a.appendChild(t),t}function g(t){var e,n,r=document.querySelector("style["+p+'~="'+t.id+'"]');if(r){if(u)return f;r.parentNode.removeChild(r)}if(d){var o=c++;r=s||(s=m()),e=b.bind(null,r,o,!1),n=b.bind(null,r,o,!0)}else r=m(),e=function(t,e){var n=e.css,r=e.media,o=e.sourceMap;r&&t.setAttribute("media",r);l.ssrId&&t.setAttribute(p,e.id);o&&(n+="\n/*# sourceURL="+o.sources[0]+" */",n+="\n/*# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(o))))+" */");if(t.styleSheet)t.styleSheet.cssText=n;else{for(;t.firstChild;)t.removeChild(t.firstChild);t.appendChild(document.createTextNode(n))}}.bind(null,r),n=function(){r.parentNode.removeChild(r)};return e(t),function(r){if(r){if(r.css===t.css&&r.media===t.media&&r.sourceMap===t.sourceMap)return;e(t=r)}else n()}}var y,_=(y=[],function(t,e){return y[t]=e,y.filter(Boolean).join("\n")});function b(t,e,n,r){var o=n?"":r.css;if(t.styleSheet)t.styleSheet.cssText=_(e,o);else{var i=document.createTextNode(o),a=t.childNodes;a[e]&&t.removeChild(a[e]),a.length?t.insertBefore(i,a[e]):t.appendChild(i)}}}]);
o.a.use(N);const I={setEnabled(t,e){o.a.set(t,"enabled",e)},setTotal(t,e){o.a.set(t,"total",e)},setUsed(t,e){o.a.set(t,"used",e)},setCodes(t,e){o.a.set(t,"codes",e)}},P={generate:({commit:t})=>(t("setEnabled",!1),function(){const t=OC.generateUrl("/apps/twofactor_backupcodes/settings/create");return L.a.post(t,{}).then(t=>t.data)}().then(({codes:e,state:n})=>(t("setEnabled",n.enabled),t("setTotal",n.total),t("setUsed",n.used),t("setCodes",e),!0)))};var R=new N.Store({strict:!1,state:{enabled:!1,total:0,used:0,codes:void 0},mutations:I,actions:P});o.a.prototype.t=t;const D=OCP.InitialState.loadState("twofactor_backupcodes","state");R.replaceState(D),new(o.a.extend(c))({store:R}).$mount("#twofactor-backupcodes-settings")},function(t,e,n){"use strict";function r(t,e){for(var n=[],r={},o=0;o<e.length;o++){var i=e[o],a=i[0],s={id:t+":"+o,css:i[1],media:i[2],sourceMap:i[3]};r[a]?r[a].parts.push(s):n.push(r[a]={id:a,parts:[s]})}return n}n.r(e),n.d(e,"default",function(){return v});var o="undefined"!=typeof document;if("undefined"!=typeof DEBUG&&DEBUG&&!o)throw new Error("vue-style-loader cannot be used in a non-browser environment. Use { target: 'node' } in your Webpack config to indicate a server-rendering environment.");var i={},a=o&&(document.head||document.getElementsByTagName("head")[0]),s=null,c=0,u=!1,f=function(){},l=null,p="data-vue-ssr-id",d="undefined"!=typeof navigator&&/msie [6-9]\b/.test(navigator.userAgent.toLowerCase());function v(t,e,n,o){u=n,l=o||{};var a=r(t,e);return h(a),function(e){for(var n=[],o=0;o<a.length;o++){var s=a[o];(c=i[s.id]).refs--,n.push(c)}e?h(a=r(t,e)):a=[];for(o=0;o<n.length;o++){var c;if(0===(c=n[o]).refs){for(var u=0;u<c.parts.length;u++)c.parts[u]();delete i[c.id]}}}}function h(t){for(var e=0;e<t.length;e++){var n=t[e],r=i[n.id];if(r){r.refs++;for(var o=0;o<r.parts.length;o++)r.parts[o](n.parts[o]);for(;o<n.parts.length;o++)r.parts.push(g(n.parts[o]));r.parts.length>n.parts.length&&(r.parts.length=n.parts.length)}else{var a=[];for(o=0;o<n.parts.length;o++)a.push(g(n.parts[o]));i[n.id]={id:n.id,refs:1,parts:a}}}}function m(){var t=document.createElement("style");return t.type="text/css",a.appendChild(t),t}function g(t){var e,n,r=document.querySelector("style["+p+'~="'+t.id+'"]');if(r){if(u)return f;r.parentNode.removeChild(r)}if(d){var o=c++;r=s||(s=m()),e=b.bind(null,r,o,!1),n=b.bind(null,r,o,!0)}else r=m(),e=function(t,e){var n=e.css,r=e.media,o=e.sourceMap;r&&t.setAttribute("media",r);l.ssrId&&t.setAttribute(p,e.id);o&&(n+="\n/*# sourceURL="+o.sources[0]+" */",n+="\n/*# sourceMappingURL=data:application/json;base64,"+btoa(unescape(encodeURIComponent(JSON.stringify(o))))+" */");if(t.styleSheet)t.styleSheet.cssText=n;else{for(;t.firstChild;)t.removeChild(t.firstChild);t.appendChild(document.createTextNode(n))}}.bind(null,r),n=function(){r.parentNode.removeChild(r)};return e(t),function(r){if(r){if(r.css===t.css&&r.media===t.media&&r.sourceMap===t.sourceMap)return;e(t=r)}else n()}}var y,_=(y=[],function(t,e){return y[t]=e,y.filter(Boolean).join("\n")});function b(t,e,n,r){var o=n?"":r.css;if(t.styleSheet)t.styleSheet.cssText=_(e,o);else{var i=document.createTextNode(o),a=t.childNodes;a[e]&&t.removeChild(a[e]),a.length?t.insertBefore(i,a[e]):t.appendChild(i)}}}]);
//# sourceMappingURL=settings.js.map

File diff suppressed because one or more lines are too long

@ -157,7 +157,7 @@ class BackupCodesProvider implements IProvider, IProvidesPersonalSettings {
*/
public function getPersonalSettings(IUser $user): IPersonalProviderSettings {
$state = $this->storage->getBackupCodesState($user);
$this->initialStateService->provideInitialState($this->appName, $state);
$this->initialStateService->provideInitialState($this->appName, 'state', $state);
return new Personal();
}

@ -4,7 +4,7 @@ import store from './store';
Vue.prototype.t = t;
const initialState = OCP.InitialState.loadState('twofactor_backupcodes');
const initialState = OCP.InitialState.loadState('twofactor_backupcodes', 'state');
store.replaceState(
initialState
)

@ -4,6 +4,4 @@ script('twofactor_backupcodes', 'settings');
?>
<input type="hidden" id="twofactor-backupcodes-initial-state" value="<?php p($_['state']); ?>">
<div id="twofactor-backupcodes-settings"></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -3,7 +3,7 @@
*/
import * as AppConfig from './appconfig'
import * as Comments from './comments'
import initialState from './initialstate'
import * as InitialState from './initialstate'
import Loader from './loader'
import * as WhatsNew from './whatsnew'
@ -11,7 +11,7 @@ import * as WhatsNew from './whatsnew'
export default {
AppConfig,
Comments,
InitialState: initialState,
InitialState,
Loader,
WhatsNew,
};

@ -21,22 +21,20 @@
*/
/**
* @namespace OCP
* @class InitialState
* @namespace OCP.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);
}
},
export function loadState (app, key) {
const elem = document.querySelector(`#initial-state-${app}-${key}`);
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);
}
}

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OC;
use Closure;
use OCP\IInitialStateService;
use OCP\ILogger;
@ -32,43 +33,58 @@ class InitialStateService implements IInitialStateService {
/** @var ILogger */
private $logger;
/** @var array */
/** @var string[][] */
private $states = [];
/** @var array */
/** @var Closure[][] */
private $lazyStates = [];
public function __construct(ILogger $logger) {
$this->logger = $logger;
}
public function provideInitialState(string $appName, $data) {
public function provideInitialState(string $appName, string $key, $data): void {
// Scalars and JsonSerializable are fine
if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) {
$this->states[$appName] = json_encode($data);
if (!isset($this->states[$appName])) {
$this->states[$appName] = [];
}
$this->states[$appName][$key] = 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 provideLazyInitialState(string $appName, string $key, Closure $closure): void {
if (!isset($this->lazyStates[$appName])) {
$this->lazyStates[$appName] = [];
}
$this->lazyStates[$appName][$key] = $closure;
}
/**
* Invoke all callbacks to populate the `states` property
*/
private function invokeLazyStateCallbacks(): void {
foreach ($this->lazyStates as $app => $lazyStates) {
foreach ($lazyStates as $key => $lazyState) {
$this->provideInitialState($app, $key, $lazyState());
}
}
$this->lazyStates = [];
}
public function getInitialStates(): array {
$states = $this->states;
foreach ($this->lazyStates as $app => $lazyState) {
$state = $lazyState();
$this->invokeLazyStateCallbacks();
if (!($lazyState instanceof \JsonSerializable)) {
$this->logger->warning($app . ' provided an invalid lazy state');
$appStates = [];
foreach ($this->states as $app => $states) {
foreach ($states as $key => $value) {
$appStates["$app-$key"] = $value;
}
$states[$app] = json_encode($state);
}
return $states;
return $appStates;
}
}

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace OCP;
use Closure;
/**
* @since 16.0.0
*/
@ -36,9 +38,10 @@ interface IInitialStateService {
* @since 16.0.0
*
* @param string $appName
* @param string $key
* @param bool|int|float|string|array|\JsonSerializable $data
*/
public function provideInitialState(string $appName, $data);
public function provideInitialState(string $appName, string $key, $data): void;
/**
* Allows an app to provide its initial state via a lazy method.
@ -50,7 +53,8 @@ interface IInitialStateService {
* @since 16.0.0
*
* @param string $appName
* @param \Closure $closure Has to return an object that implements JsonSerializable
* @param string $key
* @param Closure $closure returns a primitive or an object that implements JsonSerializable
*/
public function provideLazyInitialState(string $appName, \Closure $closure);
public function provideLazyInitialState(string $appName, string $key, Closure $closure): void;
}

@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @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 Test;
use function json_encode;
use JsonSerializable;
use OC\InitialStateService;
use OCP\ILogger;
use stdClass;
class InitialStateServiceTest extends TestCase {
/** @var InitialStateService */
private $service;
protected function setUp() {
parent::setUp();
$this->service = new InitialStateService(
$this->createMock(ILogger::class)
);
}
public function staticData() {
return [
['string'],
[23],
[2.3],
[new class implements JsonSerializable {
public function jsonSerialize() {
return 3;
}
}],
];
}
/**
* @dataProvider staticData
*/
public function testStaticData($value) {
$this->service->provideInitialState('test', 'key', $value);
$data = $this->service->getInitialStates();
$this->assertEquals(
['test-key' => json_encode($value)],
$data
);
}
public function testStaticButInvalidData() {
$this->service->provideInitialState('test', 'key', new stdClass());
$data = $this->service->getInitialStates();
$this->assertEquals(
[],
$data
);
}
/**
* @dataProvider staticData
*/
public function testLazyData($value) {
$this->service->provideLazyInitialState('test', 'key', function() use ($value) {
return $value;
});
$data = $this->service->getInitialStates();
$this->assertEquals(
['test-key' => json_encode($value)],
$data
);
}
}
Loading…
Cancel
Save