http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 16 LOC: 337 Statements: 69
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Auth.php 100.0% 100.0% 100.0%
 
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster_Controller
12
 * @subpackage Plugins
13
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
14
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
15
 * @version   $Id: Auth.php 418 2010-10-18 21:40:08Z jonathanhawk $
16
 */
17
namespace Xyster\Controller\Plugin;
18
/**
19
 * Authentication plugin
20
 *
21
 * @category  Xyster
22
 * @package   Xyster_Controller
23
 * @subpackage Plugins
24
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
25
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
26
 */
27
class Auth extends \Zend_Controller_Plugin_Abstract
28 1
{
29
    /**
30
     * The acl
31
     *
32
     * @var \Zend_Acl
33
     */
34
    protected $_acl;
35
36
    /**
37
     * The Auth adapter
38
     *
39
     * @var \Zend_Auth_Adapter_Interface
40
     */
41
    protected $_adapter;
42
43
    /**
44
     * The dispatch action for authentication failure
45
     *
46
     * @var string
47
     */
48
    protected $_failAction = 'index';
49
50
    /**
51
     * The dispatch controller for authentication failure
52
     *
53
     * @var string
54
     */
55
    protected $_failController = 'login';
56
57
    /**
58
     * The dispatch module for authentication failure
59
     *
60
     * @var string
61
     */
62
    protected $_failModule;
63
64
    /**
65
     * The role provider
66
     *
67
     * @var \Xyster\Acl\Role\IProvider
68
     */
69
    protected $_provider;
70
71
    /**
72
     * The current authenticated role
73
     *
74
     * @var \Zend_Acl_Role_Interface
75
     */
76
    protected $_role;
77
78
    /**
79
     * Whether the 'routeStartup' method has already been called
80
     *
81
     * @var boolean
82
     */
83
    protected $_started = false;
84
85
    /**
86
     * The dispatch action for authentication success
87
     *
88
     * @var string
89
     */
90
    protected $_successAction = 'success';
91
92
    /**
93
     * The dispatch controller for authentication success
94
     *
95
     * @var string
96
     */
97
    protected $_successController = 'login';
98
99
    /**
100
     * The dispatch module for authentication success
101
     *
102
     * @var string
103
     */
104
    protected $_successModule;
105
106
    /**
107
     * Called before Zend_Controller_Front determines the dispatch route
108
     *
109
     * @param \Zend_Controller_Request_Abstract $request
110
     */
111
    public function routeStartup(\Zend_Controller_Request_Abstract $request)
112
    {
113 4
        $this->_started = true;
114
115 4
        $this->_authenticate();
116
    }
117
118
    /**
119
     * Gets the ACL assigned to the plugin
120
     *
121
     * @return \Zend_Acl
122
     */
123
    public function getAcl()
124
    {
125 1
        return $this->_acl;
126
    }
127
128
    /**
129
     * Gets the dispatch action for authentication failure
130
     *
131
     * @return string
132
     */
133
    public function getFailAction()
134
    {
135 2
        return $this->_failAction;
136
    }
137
138
    /**
139
     * Gets the dispatch controller for authentication failure
140
     *
141
     * @return string
142
     */
143
    public function getFailController()
144
    {
145 2
        return $this->_failController;
146
    }
147
148
    /**
149
     * Gets the dispatch module for authentication failure
150
     *
151
     * @return string
152
     */
153
    public function getFailModule()
154
    {
155 2
        if ($this->_failModule === null) {
156 1
            $this->_failModule = \Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
157 1
        }
158 2
        return $this->_failModule;
159
    }
160
161
    /**
162
     * Gets the authenticated role
163
     *
164
     * @return \Zend_Acl_Role_Interface
165
     */
166
    public function getRole()
167
    {
168 3
        $auth = \Zend_Auth::getInstance();
169 3
        if ( !$this->_role && $auth->hasIdentity() ) {
170 3
            $identity = $auth->getIdentity();
171 3
            $this->_role = $this->getRoleProvider()->getRole($identity);
172 3
        }
173
174 3
        return $this->_role;
175
    }
176
177
    /**
178
     * Gets the role provider used to translate the identity into a role
179
     *
180
     * @return \Xyster\Acl\Role\IProvider
181
     */
182
    public function getRoleProvider()
183
    {
184 5
        if ( !$this->_provider ) {
185 4
            $this->_provider = new \Xyster\Acl\Role\Provider();
186 4
        }
187 5
        return $this->_provider;
188
    }
189
190
    /**
191
     * Gets the dispatch action for authentication success
192
     *
193
     * @return string
194
     */
195
    public function getSuccessAction()
196
    {
197 3
        return $this->_successAction;
198
    }
199
200
    /**
201
     * Gets the dispatch controller for authentication success
202
     *
203
     * @return string
204
     */
205
    public function getSuccessController()
206
    {
207 3
        return $this->_successController;
208
    }
209
210
    /**
211
     * Gets the dispatch module for authentication success
212
     *
213
     * @return string
214
     */
215
    public function getSuccessModule()
216
    {
217 3
        if ($this->_successModule === null) {
218 2
            $this->_successModule = \Zend_Controller_Front::getInstance()->getDispatcher()->getDefaultModule();
219 2
        }
220 3
        return $this->_successModule;
221
    }
222
223
    /**
224
     * Sets the ACL to which the authenticated role will be added
225
     *
226
     * @param \Zend_Acl $acl
227
     * @return \Xyster\Controller\Plugin\Auth provides a fluent interface
228
     */
229
    public function setAcl( \Zend_Acl $acl )
230
    {
231 10
        $this->_acl = $acl;
232 10
        return $this;
233
    }
234
235
    /**
236
     * Sets the authentication adapter
237
     *
238
     * @param \Zend_Auth_Adapter_Interface $adapter
239
     * @return \Xyster\Controller\Plugin\Auth provides a fluent interface
240
     */
241
    public function setAuthAdapter( \Zend_Auth_Adapter_Interface $adapter )
242
    {
243 3
        $this->_adapter = $adapter;
244 3
        if ( $this->_started ) {
245
            // if the plugin already tried to authenticate, use this new adapter
246 1
            $this->_authenticate();
247 1
        }
248 3
        return $this;
249
    }
250
251
    /**
252
     * Sets the dispatch location for a failed authentication
253
     *
254
     * @param string $module The dispatch module
255
     * @param string $controller The dispatch controller
256
     * @param string $action The dispatch action
257
     * @return \Xyster\Controller\Plugin\Auth provides a fluent interface
258
     */
259
    public function setFailure( $module, $controller, $action )
260
    {
261 1
        $this->_failModule = $module;
262 1
        $this->_failController = $controller;
263 1
        $this->_failAction = $action;
264
265 1
        return $this;
266
    }
267
268
    /**
269
     * Sets the role provider used to translate the identity into a role
270
     *
271
     * @param Xyster_Acl_Role_Provider_Interface $provider
272
     * @return \Xyster\Controller\Plugin\Auth provides a fluent interface
273
     */
274
    public function setRoleProvider( \Xyster\Acl\Role\IProvider $provider )
275
    {
276 1
        $this->_provider = $provider;
277 1
        return $this;
278
    }
279
280
    /**
281
     * Sets the dispatch location for a successful authentication
282
     *
283
     * @param string $module The dispatch module
284
     * @param string $controller The dispatch controller
285
     * @param string $action The dispatch action
286
     * @return \Xyster\Controller\Plugin\Auth provides a fluent interface
287
     */
288
    public function setSuccess( $module, $controller, $action )
289
    {
290 1
        $this->_successModule = $module;
291 1
        $this->_successController = $controller;
292 1
        $this->_successAction = $action;
293
294 1
        return $this;
295
    }
296
297
    /**
298
     * Does the actual auth work
299
     *
300
     */
301
    protected function _authenticate()
302
    {
303 4
        $auth = \Zend_Auth::getInstance();
304 4
        if ( !$auth->hasIdentity() ) {
305
        // no need to call the adapter if the user is authenticated
306 4
            if ( !$this->_adapter ) {
307
                // if we don't have an adapter, there's nothing to do
308 2
                return;
309
            } else {
310 3
                $result = $auth->authenticate($this->_adapter);
311 3
                $request = $this->getRequest();
312 3
                if ( $result->isValid() ) {
313
                    // if the authentication is valid send to the success action
314 2
                    $request->setModuleName($this->getSuccessModule())
315 2
                        ->setControllerName($this->getSuccessController())
316 2
                        ->setActionName($this->getSuccessAction())
317 2
                        ->setDispatched(false);
318 2
                } else {
319
                    // if the authentication fails send to the failure action
320 1
                    $request->setModuleName($this->getFailModule())
321 1
                        ->setControllerName($this->getFailController())
322 1
                        ->setActionName($this->getFailAction())
323 1
                        ->setParam('result', $result)
324 1
                        ->setDispatched(false);
325 1
                    return;
326
                }
327
            }
328 2
        }
329
330 2
        $role = $this->getRole();
331
332 2
        if ( $role instanceof \Zend_Acl_Role_Interface && $this->_acl &&
333 2
            !$this->_acl->hasRole($role) ) {
334 2
            $this->_acl->addRole($role, $this->getRoleProvider()->getRoleParents($role));
335 2
        }
336
    }
337 1
}


Report generated at 2010-10-18T17:19:48-04:00