Skip to content

Commit

Permalink
Merge pull request #69 from ADmad/model-instances
Browse files Browse the repository at this point in the history
Model instances
  • Loading branch information
ADmad committed Jul 6, 2016
2 parents 15990ee + c34251b commit 89d4dcf
Showing 1 changed file with 57 additions and 34 deletions.
91 changes: 57 additions & 34 deletions src/Auth/HybridAuthAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ class HybridAuthAuthenticate extends BaseAuthenticate
*/
protected $_initDone = false;

/**
* User model.
*
* @var \Cake\Datasource\RepositoryInterface
*/
protected $_userModel;

/**
* Social profile model
*
* @var \Cake\Datasource\RepositoryInterface
*/
protected $_profileModel;

/**
* Constructor
*
Expand All @@ -70,7 +84,7 @@ public function __construct(ComponentRegistry $registry, $config)
}

/**
* Initialize HybridAuth
* Initialize HybridAuth and this authenticator.
*
* @param \Cake\Network\Request $request Request instance.
* @return void
Expand All @@ -82,6 +96,9 @@ protected function _init(Request $request)
return;
}

$this->_userModel = TableRegistry::get($this->_config['userModel']);
$this->_profileModel = TableRegistry::get($this->_config['profileModel']);

$request->session()->start();

$hybridConfig = Configure::read('HybridAuth');
Expand Down Expand Up @@ -236,8 +253,8 @@ protected function _checkProvider($data)
*
* @param \Hybrid_Provider_Model $adapter Hybrid auth adapter instance.
* @return array User record
* @throws \Exception Thrown when a profile cannot be retrieved
* @throws \RuntimeException Thrown when the user has not created a listener, or the entity cannot be persisted
* @throws \Exception Thrown when a profile cannot be retrieved.
* @throws \RuntimeException Thrown when profile entity cannot be persisted.
*/
protected function _getUser($adapter)
{
Expand All @@ -259,35 +276,21 @@ protected function _getUser($adapter)
$user = $profile->user;
$profile->unsetProperty('user');
} elseif ($providerProfile->email) {
$UsersTable = TableRegistry::get($config['userModel']);
$user = $UsersTable
->find($config['finder'])
$user = $this->_userModel->find($config['finder'])
->where([
$UsersTable->aliasField($config['fields']['email']) => $providerProfile->email
$this->_userModel->aliasField($config['fields']['email']) => $providerProfile->email
])
->first();
}

$profile = $this->_profileEntity($profile ?: null);

if (!$user) {
$event = $this->dispatchEvent(
'HybridAuth.newUser',
['profile' => $profile]
);

if (empty($event->result) || !($event->result instanceof EntityInterface)) {
throw new \RuntimeException('
You must attach a listener for "HybridAuth.newUser" event
which saves new user record and returns an user entity.
');
}

$user = $event->result;
$user = $this->_newUser($profile);
}

$profile->{$config['profileModelFkField']} = $user->{$UsersTable->primaryKey()};
$profile = TableRegistry::get($config['profileModel'])->save($profile);
$profile->{$config['profileModelFkField']} = $user->{$this->_userModel->primaryKey()};
$profile = $this->_profileModel->save($profile);
if (!$profile) {
throw new \RuntimeException('Unable to save social profile.');
}
Expand All @@ -297,6 +300,33 @@ protected function _getUser($adapter)
return $user->toArray();
}

/**
* Get new user entity.
*
* It dispatches a `HybridAuth.newUser` event. A listener must return
* an entity for new user record.
*
* @param \Cake\ORM\Entity $profile Social profile entity.
* @return \Cake\ORM\Entity User entity.
* @throws \RuntimeException Thrown when the user entity is not returned by event listener.
*/
protected function _newUser($profile)
{
$event = $this->dispatchEvent(
'HybridAuth.newUser',
['profile' => $profile]
);

if (empty($event->result) || !($event->result instanceof EntityInterface)) {
throw new \RuntimeException('
You must attach a listener for "HybridAuth.newUser" event
which saves new user record and returns an user entity.
');
}

return $event->result;
}

/**
* Get query to fetch social profile record.
*
Expand All @@ -305,21 +335,15 @@ protected function _getUser($adapter)
*/
protected function _query($identifier)
{
$config = $this->_config;
list(, $userAlias) = pluginSplit($config['userModel']);
list(, $userAlias) = pluginSplit($this->_config['userModel']);
$provider = $this->adapter()->id;

$table = TableRegistry::get($config['profileModel']);
$query = $table->find('all');

$query
return $this->_profileModel->find()
->where([
$table->aliasField('provider') => $provider,
$table->aliasField('identifier') => $identifier
$this->_profileModel->aliasField('provider') => $provider,
$this->_profileModel->aliasField('identifier') => $identifier
])
->contain([$userAlias]);

return $query;
}

/**
Expand All @@ -331,8 +355,7 @@ protected function _query($identifier)
protected function _profileEntity($profile = null)
{
if (!$profile) {
$ProfileTable = TableRegistry::get($this->_config['profileModel']);
$profile = $ProfileTable->newEntity([
$profile = $this->_profileModel->newEntity([
'provider' => $this->adapter()->id,
]);
}
Expand Down

0 comments on commit 89d4dcf

Please sign in to comment.