Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

src/Controller/Component/CookieLoginComponent.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Controller\Component;
3
4
use App\Event\Badges;
5
use App\Event\Logs;
6
use Cake\Controller\Component;
7
use Cake\Event\Event;
8
use Cake\Event\EventManager;
9
use Cake\I18n\Time;
10
use Cake\ORM\TableRegistry;
11
12
class CookieLoginComponent extends Component
13
{
14
    /**
15
     * Controller object
16
     *
17
     * @var \Cake\Controller\Controller
18
     */
19
    protected $_controller;
20
21
    /**
22
     * Initialize properties.
23
     *
24
     * @param array $config The config data.
25
     *
26
     * @return void
27
     */
28
    public function initialize(array $config)
29
    {
30
        $this->_controller = $this->_registry->getController();
31
    }
32
33
    /**
34
     * Handle the cookie login.
35
     *
36
     * @return false|void
37
     */
38
    public function handle()
39
    {
40
        if ($this->_controller->Auth->user() || !$this->_controller->Cookie->read('CookieAuth')) {
41
            return false;
42
        }
43
        $this->Users = TableRegistry::get('Users');
44
45
        $userLogin = $this->_controller->Auth->identify();
46
47
        if (!$userLogin || !$userLogin['is_deleted'] == false) {
48
            $this->_controller->Cookie->delete('CookieAuth');
49
50
            return false;
51
        }
52
        $this->_controller->loadComponent('TwoFactorAuth');
53
54
        //Verify if the user use 2FA and if yes, if he's authorized.
55
        if ($userLogin['two_factor_auth_enabled'] == true && $this->_controller->TwoFactorAuth->isAuthorized($userLogin['id']) === false) {
56
            $this->_controller->Cookie->delete('CookieAuth');
57
        } else {
58
            $this->_controller->Auth->setUser($userLogin);
59
60
            $user = $this->Users->newEntity($userLogin, ['accessibleFields' => ['id' => true]]);
61
            $user->isNew(false);
62
            $user->id = $userLogin['id'];
63
64
            $user->last_login = new Time();
65
            $user->last_login_ip = $this->request->clientIp();
66
67
            $this->Users->save($user);
68
69
            //Badges Event.
70
            EventManager::instance()->attach(new Badges($this->_controller));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
            $badge = new Event('Model.Users.register', $this->_controller, [
72
                'user' => $user
73
            ]);
74
            EventManager::instance()->dispatch($badge);
75
76
            //Logs Event.
77
            EventManager::instance()->attach(new Logs());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
            $event = new Event('Log.User', $this->_controller, [
79
                'user_id' => $user->id,
80
                'username' => $user->username,
81
                'user_ip' => $this->_controller->request->clientIp(),
82
                'user_agent' => $this->_controller->request->header('User-Agent'),
83
                'action' => 'user.connection.auto'
84
            ]);
85
            EventManager::instance()->dispatch($event);
86
        }
87
    }
88
}
89