AuthProvider::getProfile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Application\Auth;
11
12
use Application\Auth;
13
use Application\Users;
14
use Bluz\Application\Exception\ApplicationException;
15
use Bluz\Auth\AuthException;
16
use Bluz\Auth\EntityInterface;
17
use Bluz\Proxy\Config;
18
use Bluz\Proxy\Messages;
19
use Bluz\Proxy\Response;
20
21
/**
22
 * Hybrid Auth integration
23
 * @author yuklia <[email protected]>
24
 * @package Application\Auth
25
 */
26
class AuthProvider implements AuthInterface
27
{
28
    /**
29
     * @var EntityInterface $identity
30
     */
31
    protected $identity;
32
33
    /**
34
     * @var \Hybrid_Auth $hybridauth
35
     */
36
    protected $hybridauth;
37
38
    /**
39
     * @var \Hybrid_Provider_Adapter $authAdapter
40
     */
41
    protected $authAdapter;
42
43
    /**
44
     * the same name as was mentioned in hybridauth config section providers
45
     * @var string
46
     */
47
    protected $providerName;
48
49
    public function __construct($providerName)
50
    {
51
        if (!in_array(ucfirst($providerName), $this->getAvailableProviders())) {
52
            throw new ApplicationException(__('Provider `%s` is not defined
53
            in configuration file', ucfirst($providerName)));
54
        }
55
        $this->providerName = ucfirst($providerName);
56
    }
57
58
    /**
59
     * @return \Hybrid_Auth
60
     */
61
    public function getHybridauth()
62
    {
63
        if (!$this->hybridauth) {
64
            $this->hybridauth = new \Hybrid_Auth($this->getOptions());
65
        }
66
67
        return $this->hybridauth;
68
    }
69
70
    /**
71
     * @param \Hybrid_Auth $hybridauth
72
     */
73
    public function setHybridauth($hybridauth)
74
    {
75
        $this->hybridauth = $hybridauth;
76
    }
77
78
    /**
79
     * @param EntityInterface $identity
80
     */
81
    public function setIdentity($identity)
82
    {
83
        $this->identity = $identity;
84
    }
85
86
    /**
87
     * @return EntityInterface $user
88
     */
89
    public function getIdentity()
90
    {
91
        return $this->identity;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getProviderName()
98
    {
99
        return $this->providerName;
100
    }
101
102
    /**
103
     * @param string $providerName
104
     */
105
    public function setProviderName($providerName)
106
    {
107
        $this->providerName = $providerName;
108
    }
109
110
    /**
111
     * @return \Hybrid_Provider_Adapter
112
     * @throws \Exception
113
     */
114
    public function getAuthAdapter()
115
    {
116
        if (!$this->authAdapter) {
117
            /** @var \Hybrid_Provider_Adapter $authProvider */
118
            $this->authAdapter = $this->getHybridauth()->authenticate($this->providerName);
119
120
            if (!$this->authAdapter->isUserConnected()) {
121
                throw new AuthException('Cannot connect to current provider!');
122
            }
123
        }
124
125
        return $this->authAdapter;
126
    }
127
128
    /**
129
     * @param \Hybrid_Provider_Adapter $authAdapter
130
     */
131
    public function setAuthAdapter($authAdapter)
132
    {
133
        $this->authAdapter = $authAdapter;
134
    }
135
136
    /**
137
     * @param \Hybrid_User_Profile $data
138
     * @param  \Application\Users\Row $user
139
     * @return void
140
     */
141
    public function registration($data, $user)
142
    {
143
        $row = new Auth\Row();
144
        $row->userId = $user->id;
145
        $row->provider = strtolower($this->providerName);
146
        $row->foreignKey = $data->identifier;
147
        $row->token = $this->authAdapter->getAccessToken()['access_token'];
148
        $row->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret']) ? : '';
149
        $row->tokenType = Auth\Table::TYPE_ACCESS;
150
        $row->save();
151
152
        Messages::addNotice('Your account was linked to %s successfully !', $this->providerName);
153
        Response::redirectTo('users', 'profile', ['id' => $user->id]);
154
    }
155
156
    /**
157
     * @return void
158
     */
159
    public function authProcess()
160
    {
161
        $this->authAdapter = $this->getAuthAdapter();
162
        $profile = $this->getProfile();
163
164
        $auth = Auth\Table::getAuthRow(strtolower($this->providerName), $profile->identifier);
165
166
        if ($this->identity) {
167
            if ($auth) {
168
                Messages::addNotice('You have already linked to %s', $this->providerName);
169
                Response::redirectTo('users', 'profile', ['id' => $this->identity->id]);
170
            } else {
171
                $user = Users\Table::findRow($this->identity->id);
172
                $this->registration($profile, $user);
173
            }
174
        }
175
176
        if ($auth) {
177
            $this->alreadyRegisteredLogic($auth);
178
        } else {
179
            Messages::addError('First you need to be linked to %s', $this->providerName);
180
            Response::redirectTo('users', 'signin');
181
        }
182
    }
183
184
    /**
185
     * @return array
186
     * @throws \Application\Exception
187
     */
188
    public function getOptions()
189
    {
190
        return Config::getData('hybridauth');
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function getAvailableProviders()
197
    {
198
        return array_keys(Config::getData('hybridauth')['providers']);
199
    }
200
201
    /**
202
     * @param $auth
203
     * @return mixed
204
     */
205
    public function alreadyRegisteredLogic($auth)
206
    {
207
        $user = Users\Table::findRow($auth->userId);
208
209
        if ($user->status !== Users\Table::STATUS_ACTIVE) {
210
            Messages::addError('User is not active');
211
        }
212
213
        Table::tryLogin($user);
214
        Response::redirectTo('index', 'index');
215
    }
216
217
    /**
218
     * @return \Hybrid_User_Profile
219
     */
220
    public function getProfile()
221
    {
222
        return $this->authAdapter->getUserProfile();
223
    }
224
}
225