|
| 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 | +} |
0 commit comments