1 <?php
2 3 4 5 6 7 8 9 10 11 12 13
14 class BasicAuth {
15 16 17
18 private static $entire_site_protected = false;
19
20 21 22 23 24 25 26 27 28
29 static function requireLogin($realm, $permissionCode) {
30 if(!Security::database_is_ready() || Director::is_cli()) return true;
31 $authenticated = false;
32
33 if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
34 $member = MemberAuthenticator::authenticate(array(
35 'Email' => $_SERVER['PHP_AUTH_USER'],
36 'Password' => $_SERVER['PHP_AUTH_PW'],
37 ), null);
38
39 if($member || Member::currentUser()) $authenticated = true;
40 }
41
42
43 if(!$authenticated) {
44 header("WWW-Authenticate: Basic realm=\"$realm\"");
45 header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
46
47 if(isset($_SERVER['PHP_AUTH_USER'])) {
48 echo _t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised");
49 } else {
50 echo _t('BasicAuth.ENTERINFO', "Please enter a username and password.");
51 }
52
53 die();
54 }
55
56 if(!Permission::checkMember($member->ID, $permissionCode)) {
57 header("WWW-Authenticate: Basic realm=\"$realm\"");
58 header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
59
60 if(isset($_SERVER['PHP_AUTH_USER'])) {
61 echo _t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator.");
62 }
63
64 die();
65 }
66
67 return $member;
68 }
69
70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 static function protect_entire_site($protect = true) {
85 return self::$entire_site_protected = $protect;
86 }
87
88 89 90
91 static function enable() {
92 user_error("BasicAuth::enable() is deprecated. Use BasicAuth::protect_entire_site() instead.", E_USER_NOTICE);
93 return self::protect_entire_site();
94 }
95
96 97 98
99 static function disable() {
100 user_error("BasicAuth::disable() is deprecated. Use BasicAuth::protect_entire_site(false) instead.", E_USER_NOTICE);
101 return self::protect_entire_site(false);
102 }
103
104 105 106 107
108 static function protect_site_if_necessary() {
109 if(self::$entire_site_protected) {
110 self::requireLogin("SilverStripe test website. Use your CMS login.", "ADMIN");
111 }
112 }
113
114 }
115
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-