Merge pull request #13489 from nextcloud/enh/settings/2fa-security-store

Do not load 2fa admin settings async
This commit is contained in:
Roeland Jago Douma 2019-02-28 13:32:26 +01:00 committed by GitHub
commit a43ffc7dec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 175 additions and 60 deletions

View File

@ -25,25 +25,35 @@
namespace OC\Settings\Admin; namespace OC\Settings\Admin;
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\Encryption\IManager; use OCP\Encryption\IManager;
use OCP\IInitialStateService;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Settings\ISettings; use OCP\Settings\ISettings;
class Security implements ISettings { class Security implements ISettings {
/** @var IManager */ /** @var IManager */
private $manager; private $manager;
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** /** @var MandatoryTwoFactor */
* @param IManager $manager private $mandatoryTwoFactor;
* @param IUserManager $userManager
*/ /** @var IInitialStateService */
public function __construct(IManager $manager, IUserManager $userManager) { private $initialState;
public function __construct(IManager $manager,
IUserManager $userManager,
MandatoryTwoFactor $mandatoryTwoFactor,
IInitialStateService $initialState) {
$this->manager = $manager; $this->manager = $manager;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->mandatoryTwoFactor = $mandatoryTwoFactor;
$this->initialState = $initialState;
} }
/** /**
@ -61,6 +71,12 @@ class Security implements ISettings {
} }
} }
$this->initialState->provideInitialState(
'settings',
'mandatory2FAState',
$this->mandatoryTwoFactor->getState()
);
$parameters = [ $parameters = [
// Encryption API // Encryption API
'encryptionEnabled' => $this->manager->isEnabled(), 'encryptionEnabled' => $this->manager->isEnabled(),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -77,33 +77,24 @@
}, },
data () { data () {
return { return {
state: {
enforced: false,
enforcedGroups: [],
excludedGroups: [],
},
loading: false, loading: false,
groups: [], groups: [],
loadingGroups: false, loadingGroups: false,
} }
}, },
computed: {
state: function() {
return {
enforced: this.$store.state.enforced,
enforcedGroups: this.$store.state.enforcedGroups,
excludedGroups: this.$store.state.excludedGroups,
}
}
},
mounted () { mounted () {
this.loading = true // Groups are loaded dynamically, but the assigned ones *should*
Axios.get(OC.generateUrl('/settings/api/admin/twofactorauth')) // be valid groups, so let's add them as initial state
.then(resp => resp.data) this.groups = _.sortedUniq(this.state.enforcedGroups.concat(this.state.excludedGroups))
.then(state => {
this.state = state
// Groups are loaded dynamically, but the assigned ones *should*
// be valid groups, so let's add them as initial state
this.groups = _.sortedUniq(this.state.enforcedGroups.concat(this.state.excludedGroups))
this.loading = false
})
.catch(err => {
console.error('Could not load two-factor state', err)
throw err
})
}, },
methods: { methods: {
searchGroup: _.debounce(function (query) { searchGroup: _.debounce(function (query) {

View File

@ -1,10 +1,17 @@
import Vue from 'vue' import Vue from 'vue'
import AdminTwoFactor from './components/AdminTwoFactor.vue' import AdminTwoFactor from './components/AdminTwoFactor.vue'
import store from './store/admin-security'
__webpack_nonce__ = btoa(OC.requestToken) __webpack_nonce__ = btoa(OC.requestToken)
Vue.prototype.t = t; Vue.prototype.t = t;
store.replaceState(
OCP.InitialState.loadState('settings', 'mandatory2FAState')
)
const View = Vue.extend(AdminTwoFactor) const View = Vue.extend(AdminTwoFactor)
new View().$mount('#two-factor-auth-settings') new View({
store
}).$mount('#two-factor-auth-settings')

View File

@ -0,0 +1,63 @@
/*
* @copyright 2019 Roeland Jago Douma <roeland@famdouma.nl>
*
* @author 2019 Roeland Jago Douma <roeland@famdouma.nl>
*
* @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/>.
*/
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export const mutations = {
setEnforced(state, enabled) {
Vue.set(state, 'enforced', enabled)
},
setEnforcedGroups(state, total) {
Vue.set(state, 'enforcedGroups', total)
},
setExcludedGroups(state, used) {
Vue.set(state, 'excludedGroups', used)
}
}
export const actions = {
save ({commit}, ) {
commit('setEnabled', false);
return generateCodes()
.then(({codes, state}) => {
commit('setEnabled', state.enabled);
commit('setTotal', state.total);
commit('setUsed', state.used);
commit('setCodes', codes);
return true;
});
}
}
export default new Vuex.Store({
strict: process.env.NODE_ENV !== 'production',
state: {
enforced: false,
enforcedGroups: [],
excludedGroups: [],
},
mutations,
actions
})

View File

@ -30,6 +30,7 @@ script('settings', 'vue-settings-admin-security');
<div id="two-factor-auth" class="section"> <div id="two-factor-auth" class="section">
<h2><?php p($l->t('Two-Factor Authentication'));?></h2> <h2><?php p($l->t('Two-Factor Authentication'));?></h2>
<input type="hidden" id="two-factor-auth-settings-initial-state" value="<?php p(base64_encode(json_encode($_['mandatory2FAState']))); ?>">
<div id="two-factor-auth-settings"></div> <div id="two-factor-auth-settings"></div>
</div> </div>

View File

@ -23,10 +23,13 @@
namespace Test\Settings\Admin; namespace Test\Settings\Admin;
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
use OC\Encryption\Manager; use OC\Encryption\Manager;
use OC\Settings\Admin\Security; use OC\Settings\Admin\Security;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IInitialStateService;
use OCP\IUserManager; use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class SecurityTest extends TestCase { class SecurityTest extends TestCase {
@ -36,15 +39,23 @@ class SecurityTest extends TestCase {
private $manager; private $manager;
/** @var IUserManager */ /** @var IUserManager */
private $userManager; private $userManager;
/** @var MandatoryTwoFactor|MockObject */
private $mandatoryTwoFactor;
/** @var IInitialStateService|MockObject */
private $initialState;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->manager = $this->getMockBuilder('\OC\Encryption\Manager')->disableOriginalConstructor()->getMock(); $this->manager = $this->getMockBuilder('\OC\Encryption\Manager')->disableOriginalConstructor()->getMock();
$this->userManager = $this->getMockBuilder(IUserManager::class)->getMock(); $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
$this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
$this->initialState = $this->createMock(IInitialStateService::class);
$this->admin = new Security( $this->admin = new Security(
$this->manager, $this->manager,
$this->userManager $this->userManager,
$this->mandatoryTwoFactor,
$this->initialState
); );
} }