update models + added social network account services

This commit is contained in:
Antonio Ramirez
2016-12-06 00:24:24 +01:00
parent be80b12a03
commit c5279eb13c
22 changed files with 1324 additions and 62 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace Da\User\Model;
use Da\User\Helper\SecurityHelper;
use Da\User\Query\UserQuery;
use Da\User\Traits\ContainerTrait;
use Da\User\Traits\ModuleTrait;
@ -9,6 +10,7 @@ use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\web\Application;
use yii\web\IdentityInterface;
/**
@ -52,6 +54,38 @@ class User extends ActiveRecord implements IdentityInterface
*/
protected $connectedAccounts;
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
/** @var SecurityHelper $security */
$security = $this->make(SecurityHelper::class);
if ($insert) {
$this->setAttribute('auth_key', $security->generateRandomString());
if (Yii::$app instanceof Application) {
$this->setAttribute('registration_ip', Yii::$app->request->getUserIP());
}
}
if (!empty($this->password)) {
$this->setAttribute(
'password_hash',
$security->generatePasswordHash($this->password, $this->getModule()->blowfishCost)
);
}
return parent::beforeSave($insert);
}
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
@ -161,14 +195,6 @@ class User extends ActiveRecord implements IdentityInterface
return static::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.');
}
/**
* @return bool whether is blocked or not.
*/
@ -185,6 +211,14 @@ class User extends ActiveRecord implements IdentityInterface
return $this->getAuth()->isAdmin($this->username);
}
/**
* @return bool
*/
public function getIsConfirmed()
{
return $this->confirmed_at !== null;
}
/**
* Checks whether a user has a specific role
*
@ -214,10 +248,11 @@ class User extends ActiveRecord implements IdentityInterface
/** @var SocialNetworkAccount[] $accounts */
$accounts = $this->hasMany($this->getClassMap()->get('Account'), ['user_id' => 'id'])->all();
foreach($accounts as $account) {
foreach ($accounts as $account) {
$this->connectedAccounts[$account->provider] = $account;
}
}
return $this->connectedAccounts;
}
@ -228,4 +263,12 @@ class User extends ActiveRecord implements IdentityInterface
{
return new UserQuery(static::class);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.');
}
}