1 | <?php |
||
2 | |||
3 | namespace Apps\Controller\Front\User; |
||
4 | |||
5 | use Apps\Model\Front\User\FormLogin; |
||
6 | use Apps\Model\Front\User\FormSocialAuth; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Arch\View; |
||
9 | use Ffcms\Core\Exception\ForbiddenException; |
||
10 | use Ffcms\Core\Exception\SyntaxException; |
||
11 | use Ffcms\Core\Helper\Type\Str; |
||
12 | use Ffcms\Core\Network\Request; |
||
13 | use Ffcms\Core\Network\Response; |
||
14 | |||
15 | /** |
||
16 | * Trait ActionSocialAuth |
||
17 | * @package Apps\Controller\Front\User |
||
18 | * @property View $view |
||
19 | * @property Response $response |
||
20 | * @property Request $request |
||
21 | * @method array getConfigs() |
||
22 | */ |
||
23 | trait ActionSocialAuth |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * Authorization in social networks over hybridauth layer. How its work: |
||
28 | * 1. User visit actionSocialauth and initialize openid instance |
||
29 | * 2. 3rd party software generate redirect to @api -> User::actionEndpoint() (as endpoint) where create hash's, tokens and other shit |
||
30 | * 3. After successful auth on service user redirect back to actionSocialauth and we can work with $userIdentity if no exceptions catched. |
||
31 | * Don't aks me "why did you do this sh@t"? I want to make container in User class, but this shit work only on direct call on endpoint. |
||
32 | * @param string $provider |
||
33 | * @return string |
||
34 | * @throws ForbiddenException |
||
35 | * @throws SyntaxException |
||
36 | */ |
||
37 | public function socialauth(string $provider) |
||
38 | { |
||
39 | // get hybridauth instance |
||
40 | /** @var \Hybrid_Auth $instance */ |
||
41 | $instance = App::$User->getOpenidInstance(); |
||
42 | if (!$instance) { |
||
43 | throw new ForbiddenException(__('OpenID auth is disabled')); |
||
44 | } |
||
45 | |||
46 | // try to get user identity data from remove service |
||
47 | $userIdentity = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
48 | try { |
||
49 | $adapter = $instance->authenticate($provider); |
||
50 | $userIdentity = $adapter->getUserProfile(); |
||
51 | } catch (\Exception $e) { |
||
52 | throw new SyntaxException(__('Authorization failed: %e%', ['e' => $e->getMessage()])); |
||
53 | } |
||
54 | |||
55 | // check if openid data provided |
||
56 | if (!$userIdentity || Str::likeEmpty($userIdentity->identifier)) { |
||
57 | throw new ForbiddenException(__('User data not provided!')); |
||
58 | } |
||
59 | |||
60 | // initialize model and pass user identity |
||
61 | $model = new FormSocialAuth($provider, $userIdentity); |
||
62 | // check if user is always registered |
||
63 | if ($model->identityExists()) { |
||
64 | $model->makeAuth(); |
||
65 | $this->response->redirect('/'); |
||
66 | return null; |
||
67 | } |
||
68 | // its a new identify, check if finish register form is submited |
||
69 | if ($model->send() && $model->validate()) { |
||
70 | if ($model->tryRegister()) { |
||
71 | // registration is completed, lets open new session |
||
72 | $loginModel = new FormLogin(); |
||
73 | $loginModel->openSession($model->_userObject); |
||
74 | $this->response->redirect('/'); // session is opened, refresh page |
||
75 | } else { // something gonna wrong, lets notify user |
||
76 | App::$Session->getFlashBag()->add('error', __('Login or email is always used on website')); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | // render output view |
||
81 | return $this->view->render('user/social_signup', [ |
||
82 | 'model' => $model |
||
83 | ]); |
||
84 | } |
||
85 | } |
||
86 |