Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(2fa): Handle twofactor_enforced configuration parameter as boolean #44090

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(IConfig $config, IGroupManager $groupManager) {
*/
public function getState(): EnforcementState {
return new EnforcementState(
$this->config->getSystemValue('twofactor_enforced', 'false') === 'true',
$this->config->getSystemValueBool('twofactor_enforced', false),
$this->config->getSystemValue('twofactor_enforced_groups', []),
$this->config->getSystemValue('twofactor_enforced_excluded_groups', [])
);
Expand All @@ -56,7 +56,7 @@ public function getState(): EnforcementState {
* Set the state of enforced two-factor auth
*/
public function setState(EnforcementState $state) {
$this->config->setSystemValue('twofactor_enforced', $state->isEnforced() ? 'true' : 'false');
$this->config->setSystemValueBool('twofactor_enforced', $state->isEnforced());
$this->config->setSystemValue('twofactor_enforced_groups', $state->getEnforcedGroups());
$this->config->setSystemValue('twofactor_enforced_excluded_groups', $state->getExcludedGroups());
}
Expand Down
63 changes: 51 additions & 12 deletions tests/lib/Authentication/TwoFactorAuth/MandatoryTwoFactorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ protected function setUp(): void {
}

public function testIsNotEnforced() {
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, false],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'false'],
['twofactor_enforced_groups', [], []],
['twofactor_enforced_excluded_groups', [], []],
]);
Expand All @@ -68,10 +72,14 @@ public function testIsNotEnforced() {
}

public function testIsEnforced() {
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, true],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'true'],
['twofactor_enforced_groups', [], []],
['twofactor_enforced_excluded_groups', [], []],
]);
Expand All @@ -84,10 +92,14 @@ public function testIsEnforced() {
public function testIsNotEnforcedForAnybody() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('user123');
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, false],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'false'],
['twofactor_enforced_groups', [], []],
['twofactor_enforced_excluded_groups', [], []],
]);
Expand All @@ -100,10 +112,14 @@ public function testIsNotEnforcedForAnybody() {
public function testIsEnforcedForAGroupMember() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('user123');
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, true],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'true'],
['twofactor_enforced_groups', [], ['twofactorers']],
['twofactor_enforced_excluded_groups', [], []],
]);
Expand All @@ -120,10 +136,14 @@ public function testIsEnforcedForAGroupMember() {
public function testIsEnforcedForOtherGroups() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('user123');
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, true],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'true'],
['twofactor_enforced_groups', [], ['twofactorers']],
['twofactor_enforced_excluded_groups', [], []],
]);
Expand All @@ -138,10 +158,14 @@ public function testIsEnforcedForOtherGroups() {
public function testIsEnforcedButMemberOfExcludedGroup() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('user123');
$this->config
->method('getSystemValueBool')
->willReturnMap([
['twofactor_enforced', false, true],
]);
$this->config
->method('getSystemValue')
->willReturnMap([
['twofactor_enforced', 'false', 'true'],
['twofactor_enforced_groups', [], []],
['twofactor_enforced_excluded_groups', [], ['yoloers']],
]);
Expand All @@ -157,10 +181,15 @@ public function testIsEnforcedButMemberOfExcludedGroup() {

public function testSetEnforced() {
$this->config
->expects($this->exactly(3))
->expects($this->exactly(1))
->method('setSystemValueBool')
->willReturnMap([
['twofactor_enforced', true],
]);
$this->config
->expects($this->exactly(2))
->method('setSystemValue')
->willReturnMap([
['twofactor_enforced', 'true'],
['twofactor_enforced_groups', []],
['twofactor_enforced_excluded_groups', []],
]);
Expand All @@ -170,10 +199,15 @@ public function testSetEnforced() {

public function testSetEnforcedForGroups() {
$this->config
->expects($this->exactly(3))
->expects($this->exactly(1))
->method('setSystemValueBool')
->willReturnMap([
['twofactor_enforced', true],
]);
$this->config
->expects($this->exactly(2))
->method('setSystemValue')
->willReturnMap([
['twofactor_enforced', 'true'],
['twofactor_enforced_groups', ['twofactorers']],
['twofactor_enforced_excluded_groups', ['yoloers']],
]);
Expand All @@ -183,10 +217,15 @@ public function testSetEnforcedForGroups() {

public function testSetNotEnforced() {
$this->config
->expects($this->exactly(3))
->expects($this->exactly(1))
->method('setSystemValueBool')
->willReturnMap([
['twofactor_enforced', false],
]);
$this->config
->expects($this->exactly(2))
->method('setSystemValue')
->willReturnMap([
['twofactor_enforced', 'false'],
['twofactor_enforced_groups', []],
['twofactor_enforced_excluded_groups', []],
]);
Expand Down