Skip to content

Commit 45bcf50

Browse files
committed
Initial commit
0 parents  commit 45bcf50

File tree

6 files changed

+331
-0
lines changed

6 files changed

+331
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
/.project

composer.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "bluzphp/module-auth",
3+
"type": "bluz-module",
4+
"license": "MIT",
5+
"description": "bluz auth module",
6+
"require": {
7+
"bluzphp/composer-plugin": "~0.2"
8+
},
9+
"extra": {
10+
"bluz": {
11+
"module_name": "auth"
12+
}
13+
}
14+
}

src/models/Auth/AuthInterface.php

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: yuklia
5+
* Date: 06.05.15
6+
* Time: 18:41
7+
*/
8+
namespace Application\Auth;
9+
10+
/**
11+
* Interface AuthInterface
12+
* @package Application\Auth
13+
*/
14+
interface AuthInterface
15+
{
16+
/**
17+
* @param array $data
18+
* @param \Application\Users\Row $user
19+
* @return void
20+
*/
21+
public function registration($data, $user);
22+
23+
/**
24+
* @return void
25+
*/
26+
public function authProcess();
27+
28+
/**
29+
* @return array
30+
* @throws \Application\Exception
31+
*/
32+
public function getOptions();
33+
34+
/**
35+
* @param \Application\Auth\Row $auth
36+
* @return mixed
37+
*/
38+
public function alreadyRegisteredLogic($auth);
39+
40+
/**
41+
* @return array
42+
*/
43+
public function getProfile();
44+
}

src/models/Auth/AuthProvider.php

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

src/modules/auth/controllers/auth.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Auth controller
4+
*
5+
* @author yuklia
6+
* @created 05.05.15 17:30
7+
*/
8+
namespace Application;
9+
10+
use Application\Auth\AuthProvider;
11+
use Bluz\Controller\Controller;
12+
use Bluz\Proxy\Messages;
13+
14+
/**
15+
* @param string $provider
16+
* @return \closure
17+
*/
18+
return function ($provider = '') {
19+
/**
20+
* @var Controller $this
21+
*/
22+
try {
23+
$auth = new AuthProvider($provider);
24+
$auth->setIdentity($this->user());
25+
$auth->authProcess();
26+
} catch (Exception $e) {
27+
Messages::addError($e->getMessage());
28+
}
29+
30+
return false;
31+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Auth end-point controller
4+
*
5+
* @author yuklia
6+
* @created 05.05.15 17:30
7+
*/
8+
namespace Application;
9+
10+
use Application\Users;
11+
12+
/**
13+
* @return \closure
14+
*/
15+
return function () {
16+
\Hybrid_Endpoint::process();
17+
};

0 commit comments

Comments
 (0)