FrontControllerPlugin   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A preDispatch() 0 10 2
B handleDocumentAuthentication() 0 21 5
A isDocumentAuthenticationValid() 0 19 4
B sendHttpBasicAuthResponse() 0 28 3
1
<?php
2
3
    namespace DocumentAuthentication;
4
5
    use Pimcore\Model\Document\Page;
6
    use Pimcore\Model\User;
7
    use Pimcore\Tool\Authentication;
8
    use Pimcore\Tool\Frontend;
9
10
    class FrontControllerPlugin extends \Zend_Controller_Plugin_Abstract
11
    {
12
13
        public function preDispatch(\Zend_Controller_Request_Abstract $request)
14
        {
15
            parent::preDispatch($request);
16
17
            if ($request->getParam("document") instanceof Page) {
0 ignored issues
show
Bug introduced by
The class Pimcore\Model\Document\Page does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
18
19
                $this->handleDocumentAuthentication($request->getParam("document"));
20
            }
21
22
        }
23
24
        /**
25
         * @param Page $document
26
         */
27
        private function handleDocumentAuthentication($document)
28
        {
29
            if (is_object($document)) {
30
31
                if (!$document->getProperty(Plugin::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED)) {
32
                    return; // all OK, show page
33
                }
34
            }
35
36
            $user = Authentication::authenticateSession();
37
            if ($user instanceof User) {
0 ignored issues
show
Bug introduced by
The class Pimcore\Model\User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38
                return; // all OK, show page
39
            }
40
41
            if (self::isDocumentAuthenticationValid()) {
42
                return; // all OK, show page
43
            }
44
45
            $this->sendHttpBasicAuthResponse();
46
            exit;
47
        }
48
49
        /**
50
         * @return bool
51
         */
52
        private function isDocumentAuthenticationValid()
53
        {
54
55
            $config = Frontend::getWebsiteConfig();
56
57
            $username = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME, 'preview');
58
            $password = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, '');
59
60
            if (trim($password) == '') {
61
                // empty password - this is not good; Deny access!
62
                return false;
63
            }
64
65
            if (($_SERVER['PHP_AUTH_USER'] === $username) && ($_SERVER['PHP_AUTH_PW'] === $password)) {
66
                return true;
67
            }
68
69
            return false;
70
        }
71
72
        private function sendHttpBasicAuthResponse()
73
        {
74
            $config = Frontend::getWebsiteConfig();
75
            $password = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, null);
76
77
            if (($password === null) || (trim($password) == '')) {
78
79
                $notice = 'Missing or empty Website Property '
80
                    . Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD;
81
82
            } else {
83
84
                $notice = 'Authentication required';
85
            }
86
87
            /** @var $response \Zend_Controller_Response_Http */
88
            $response = $this->getResponse();
89
90
            $response->setHeader('Cache-Control', 'max-age=0');
91
            $response->setHttpResponseCode(401);
92
            $response->setHeader(
93
                'WWW-Authenticate',
94
                'Basic realm="' . $notice . '"'
95
            );
96
97
            $response->setBody('Unauthorized.');
98
            $response->sendResponse();
99
        }
100
    }
101