-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCEMENT Allowing custom messages and permission codes in BasicAut…
…h::protect_entire_site() ENHANCEMENT Making $permissionCode argument optional for BasicAuth::requireLogin(). If not set the logic only checks for a valid account (but no group memberships) ENHANCEMENT Using SS_HTTPResponse_Exception instead of header()/die() in BasicAuth::requireLogin() to make it more testable git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@107867 467b73ca-7a2a-4603-9d3b-597d59a354a9
- Loading branch information
Showing
4 changed files
with
200 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
/** | ||
* @package sapphire | ||
* @subpackage tests | ||
*/ | ||
|
||
class BasicAuthTest extends FunctionalTest { | ||
|
||
static $fixture_file = 'sapphire/tests/security/BasicAuthTest.yml'; | ||
|
||
function tearDown() { | ||
parent::tearDown(); | ||
|
||
BasicAuth::protect_entire_site(false); | ||
} | ||
|
||
function testBasicAuthEnabledWithoutLogin() { | ||
$origUser = @$_SERVER['PHP_AUTH_USER']; | ||
$origPw = @$_SERVER['PHP_AUTH_PW']; | ||
|
||
unset($_SERVER['PHP_AUTH_USER']); | ||
unset($_SERVER['PHP_AUTH_PW']); | ||
|
||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertEquals(401, $response->getStatusCode()); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = $origUser; | ||
$_SERVER['PHP_AUTH_PW'] = $origPw; | ||
} | ||
|
||
function testBasicAuthDoesntCallActionOrFurtherInitOnAuthFailure() { | ||
$origUser = @$_SERVER['PHP_AUTH_USER']; | ||
$origPw = @$_SERVER['PHP_AUTH_PW']; | ||
|
||
unset($_SERVER['PHP_AUTH_USER']); | ||
unset($_SERVER['PHP_AUTH_PW']); | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertFalse(BasicAuthTest_ControllerSecuredWithPermission::$index_called); | ||
$this->assertFalse(BasicAuthTest_ControllerSecuredWithPermission::$post_init_called); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'test'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertTrue(BasicAuthTest_ControllerSecuredWithPermission::$index_called); | ||
$this->assertTrue(BasicAuthTest_ControllerSecuredWithPermission::$post_init_called); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = $origUser; | ||
$_SERVER['PHP_AUTH_PW'] = $origPw; | ||
} | ||
|
||
function testBasicAuthEnabledWithPermission() { | ||
$origUser = @$_SERVER['PHP_AUTH_USER']; | ||
$origPw = @$_SERVER['PHP_AUTH_PW']; | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'wrongpassword'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'test'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertEquals(401, $response->getStatusCode(), 'Valid user without required permission has no access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'test'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission'); | ||
$this->assertEquals(200, $response->getStatusCode(), 'Valid user with required permission has access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = $origUser; | ||
$_SERVER['PHP_AUTH_PW'] = $origPw; | ||
} | ||
|
||
function testBasicAuthEnabledWithoutPermission() { | ||
$origUser = @$_SERVER['PHP_AUTH_USER']; | ||
$origPw = @$_SERVER['PHP_AUTH_PW']; | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'wrongpassword'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission'); | ||
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-without-groups@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'test'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission'); | ||
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = 'user-in-mygroup@test.com'; | ||
$_SERVER['PHP_AUTH_PW'] = 'test'; | ||
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission'); | ||
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access'); | ||
|
||
$_SERVER['PHP_AUTH_USER'] = $origUser; | ||
$_SERVER['PHP_AUTH_PW'] = $origPw; | ||
} | ||
|
||
} | ||
|
||
class BasicAuthTest_ControllerSecuredWithPermission extends ContentController implements TestOnly { | ||
|
||
static $post_init_called = false; | ||
|
||
static $index_called = false; | ||
|
||
function init() { | ||
self::$post_init_called = false; | ||
self::$index_called = false; | ||
|
||
BasicAuth::protect_entire_site(true, 'MYCODE'); | ||
|
||
parent::init(); | ||
|
||
self::$post_init_called = true; | ||
} | ||
|
||
function index() { | ||
self::$index_called = true; | ||
} | ||
|
||
} | ||
|
||
class BasicAuthTest_ControllerSecuredWithoutPermission extends ContentController implements TestOnly { | ||
|
||
function init() { | ||
BasicAuth::protect_entire_site(true, null); | ||
|
||
parent::init(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Group: | ||
mygroup: | ||
Code: mygroup | ||
Member: | ||
user-in-mygroup: | ||
Email: user-in-mygroup@test.com | ||
Password: test | ||
Groups: =>Group.mygroup | ||
user-without-groups: | ||
Email: user-without-groups@test.com | ||
Password: test | ||
Permission: | ||
mycode: | ||
Code: MYCODE | ||
Group: =>Group.mygroup |