http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 16 LOC: 352 Statements: 72

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


Report generated at 2007-11-05T09:09:01-05:00