Completed
Push — master ( c8b23c...15ade1 )
by Antonio
14s
created

SecurityController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 4
cts 4
cp 1
rs 8.8571
cc 1
eloc 15
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Controller;
13
14
use Da\User\Contracts\AuthClientInterface;
15
use Da\User\Event\FormEvent;
16
use Da\User\Event\UserEvent;
17
use Da\User\Form\LoginForm;
18
use Da\User\Model\User;
19
use Da\User\Query\SocialNetworkAccountQuery;
20
use Da\User\Service\SocialNetworkAccountConnectService;
21
use Da\User\Service\SocialNetworkAuthenticateService;
22
use Da\User\Traits\ContainerAwareTrait;
23
use Yii;
24
use yii\authclient\AuthAction;
25
use yii\base\Module;
26
use yii\filters\AccessControl;
27
use yii\filters\VerbFilter;
28
use yii\web\Controller;
29
use yii\web\Response;
30
use yii\widgets\ActiveForm;
31
32
class SecurityController extends Controller
33
{
34
    use ContainerAwareTrait;
35
36
    protected $socialNetworkAccountQuery;
37
38
    /**
39
     * SecurityController constructor.
40
     *
41
     * @param string                    $id
42
     * @param Module                    $module
43
     * @param SocialNetworkAccountQuery $socialNetworkAccountQuery
44
     * @param array                     $config
45
     */
46 6
    public function __construct(
47
        $id,
48
        Module $module,
49
        SocialNetworkAccountQuery $socialNetworkAccountQuery,
50
        array $config = []
51
    ) {
52 6
        $this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
53 6
        parent::__construct($id, $module, $config);
54 6
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 6
    public function behaviors()
60
    {
61
        return [
62 6
            'access' => [
63 6
                'class' => AccessControl::className(),
64
                'rules' => [
65
                    [
66
                        'allow' => true,
67
                        'actions' => ['login', 'auth', 'blocked'],
68
                        'roles' => ['?'],
69
                    ],
70
                    [
71
                        'allow' => true,
72
                        'actions' => ['login', 'auth', 'logout'],
73
                        'roles' => ['@'],
74
                    ],
75
                ],
76
            ],
77
            'verbs' => [
78 6
                'class' => VerbFilter::className(),
79
                'actions' => [
80
                    'logout' => ['post'],
81
                ],
82
            ],
83
        ];
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 6
    public function actions()
90
    {
91
        return [
92
            'auth' => [
93 6
                'class' => AuthAction::className(),
94
                // if user is not logged in, will try to log him in, otherwise
95
                // will try to connect social account to user.
96 6
                'successCallback' => Yii::$app->user->isGuest
97 6
                    ? [$this, 'authenticate']
98 6
                    : [$this, 'connect'],
99
            ],
100
        ];
101
    }
102
103
    /**
104
     * Controller action responsible for handling login page and actions.
105
     *
106
     * @return array|string|Response
107
     */
108 6
    public function actionLogin()
109
    {
110 6
        if (!Yii::$app->user->getIsGuest()) {
111
            return $this->goHome();
112
        }
113
114
        /** @var LoginForm $form */
115 6
        $form = $this->make(LoginForm::class);
116
        /** @var FormEvent $event */
117 6
        $event = $this->make(FormEvent::class, [$form]);
118
119 6
        if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
120
            Yii::$app->response->format = Response::FORMAT_JSON;
121
122
            return ActiveForm::validate($form);
123
        }
124
125 6
        if ($form->load(Yii::$app->request->post())) {
126 6
            $this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event);
127 6
            if ($form->login()) {
128 6
                $form->getUser()->updateAttributes(['last_login_at' => time()]);
129
130 6
                $this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event);
131
132 6
                return $this->goBack();
133
            }
134
        }
135
136 6
        return $this->render(
137 6
            'login',
138
            [
139 6
                'model' => $form,
140 6
                'module' => $this->module,
141
            ]
142
        );
143
    }
144
145
    public function actionLogout()
146
    {
147
        $event = $this->make(UserEvent::class, [Yii::$app->getUser()->getIdentity()]);
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
148
149
        $this->trigger(UserEvent::EVENT_BEFORE_LOGOUT, $event);
150
151
        if (Yii::$app->getUser()->logout()) {
152
            $this->trigger(UserEvent::EVENT_AFTER_LOGOUT, $event);
153
        }
154
155
        return $this->goHome();
156
    }
157
158
    public function authenticate(AuthClientInterface $client)
159
    {
160
        $this->make(SocialNetworkAuthenticateService::class, [$this, $this->action, $client])->run();
161
    }
162
163
    public function connect(AuthClientInterface $client)
164
    {
165
        if (Yii::$app->user->isGuest) {
166
            Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Something went wrong'));
167
168
            return;
169
        }
170
171
        $this->make(SocialNetworkAccountConnectService::class, [$this, $client])->run();
172
    }
173
}
174