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 = Member::currentUser();
40 if($member) $authenticated = true;
41 }
42
43
44 if(!$authenticated) {
45 header("WWW-Authenticate: Basic realm=\"$realm\"");
46 header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
47
48 if(isset($_SERVER['PHP_AUTH_USER'])) {
49 echo _t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised");
50 } else {
51 echo _t('BasicAuth.ENTERINFO', "Please enter a username and password.");
52 }
53
54 die();
55 }
56
57 if(!Permission::checkMember($member->ID, $permissionCode)) {
58 header("WWW-Authenticate: Basic realm=\"$realm\"");
59 header($_SERVER['SERVER_PROTOCOL'] . ' 401 Unauthorized');
60
61 if(isset($_SERVER['PHP_AUTH_USER'])) {
62 echo _t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator.");
63 }
64
65 die();
66 }
67
68 return $member;
69 }
70
71 72 73 74 75 76 77 78 79 80 81 82 83 84
85 static function protect_entire_site($protect = true) {
86 return self::$entire_site_protected = $protect;
87 }
88
89 90 91
92 static function enable() {
93 user_error("BasicAuth::enable() is deprecated. Use BasicAuth::protect_entire_site() instead.", E_USER_NOTICE);
94 return self::protect_entire_site();
95 }
96
97 98 99
100 static function disable() {
101 user_error("BasicAuth::disable() is deprecated. Use BasicAuth::protect_entire_site(false) instead.", E_USER_NOTICE);
102 return self::protect_entire_site(false);
103 }
104
105 106 107 108
109 static function protect_site_if_necessary() {
110 if(self::$entire_site_protected) {
111 self::requireLogin("SilverStripe test website. Use your CMS login.", "ADMIN");
112 }
113 }
114
115 }
116
[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.
-