Completed
Pull Request — master (#8)
by
unknown
05:52
created

CookieAuthenticate::authenticate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 6
eloc 13
nc 5
nop 2
1
<?php
2
namespace Xety\Cake3CookieAuth\Auth;
3
4
use Cake\Auth\BaseAuthenticate;
5
use Cake\Controller\ComponentRegistry;
6
use Cake\Controller\Component\CookieComponent;
7
use Cake\Event\Event;
8
use Cake\Network\Request;
9
use Cake\Network\Response;
10
11
class CookieAuthenticate extends BaseAuthenticate
12
{
13
14
    /**
15
     * Constructor.
16
     *
17
     * @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request.
18
     * @param array $config Array of config to use.
19
     */
20
    public function __construct(ComponentRegistry $registry, array $config = [])
21
    {
22
        $this->_registry = $registry;
23
        $this->config($config);
24
    }
25
26
    /**
27
     * Authenticate a user based on the cookies information.
28
     *
29
     * @param \Cake\Network\Request  $request  The request instance.
30
     * @param \Cake\Network\Response $response The response instance.
31
     *
32
     * @return mixed
33
     *
34
     * @throws \RuntimeException When the CookieComponent is not loaded.
35
     */
36
    public function authenticate(Request $request, Response $response)
37
    {
38
        if (!isset($this->_registry->Cookie) || !$this->_registry->Cookie instanceof CookieComponent) {
39
            throw new \RuntimeException('You need to load the CookieComponent.');
40
        }
41
42
        $cookies = $this->_registry->Cookie->read('CookieAuth');
43
        if (empty($cookies)) {
44
            return false;
45
        }
46
47
        extract($this->_config['fields']);
48
        if (empty($cookies[$username])) {
49
            return false;
50
        }
51
52
        $user = $this->_findUser($cookies[$username], $cookies[$password]);
53
        if ($user) {
54
            return $user;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * Returns a list of all events that this authenticate class will listen to.
62
     *
63
     * @return array
64
     */
65
    public function implementedEvents()
66
    {
67
        return [
68
            'Auth.logout' => 'logout'
69
        ];
70
    }
71
72
    /**
73
     * Delete cookies when an user logout.
74
     *
75
     * @param \Cake\Event\Event  $event The logout Event.
76
     * @param array $user The user about to be logged out.
77
     *
78
     * @return void
79
     */
80
    public function logout(Event $event, array $user)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        $this->_registry->Cookie->delete('CookieAuth');
83
    }
84
}
85