update models + added social network account services
This commit is contained in:
@ -1,12 +1,156 @@
|
||||
<?php
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\GravatarHelper;
|
||||
use Da\User\Query\ProfileQuery;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use Da\User\Traits\ModuleTrait;
|
||||
use Da\User\Validator\TimeZoneValidator;
|
||||
use Yii;
|
||||
use yii\db\ActiveRecord;
|
||||
use Exception;
|
||||
use DateTimeZone;
|
||||
use DateTime;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @property integer $user_id
|
||||
* @property string $name
|
||||
* @property string $public_email
|
||||
* @property string $gravatar_email
|
||||
* @property string $gravatar_id
|
||||
* @property string $location
|
||||
* @property string $website
|
||||
* @property string $bio
|
||||
* @property string $timezone
|
||||
*
|
||||
* @property User $user
|
||||
*/
|
||||
class Profile extends ActiveRecord
|
||||
{
|
||||
use ModuleTrait;
|
||||
use ContainerTrait;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if ($this->isAttributeChanged('gravatar_email')) {
|
||||
|
||||
$this->setAttribute(
|
||||
'gravatar_id',
|
||||
$this->make(GravatarHelper::class)->buildId(trim($this->getAttribute('gravatar_email')))
|
||||
);
|
||||
}
|
||||
|
||||
return parent::beforeSave($insert);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{%profile}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
'bioString' => ['bio', 'string'],
|
||||
'timeZoneValidation' => [
|
||||
'timezone',
|
||||
function ($attribute) {
|
||||
if ($this->make(TimeZoneValidator::class, [$attribute])->validate()) {
|
||||
$this->addError($attribute, Yii::t('user', 'Time zone is not valid'));
|
||||
}
|
||||
}
|
||||
],
|
||||
'publicEmailPattern' => ['public_email', 'email'],
|
||||
'gravatarEmailPattern' => ['gravatar_email', 'email'],
|
||||
'websiteUrl' => ['website', 'url'],
|
||||
'nameLength' => ['name', 'string', 'max' => 255],
|
||||
'publicEmailLength' => ['public_email', 'string', 'max' => 255],
|
||||
'gravatarEmailLength' => ['gravatar_email', 'string', 'max' => 255],
|
||||
'locationLength' => ['location', 'string', 'max' => 255],
|
||||
'websiteLength' => ['website', 'string', 'max' => 255],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'name' => Yii::t('user', 'Name'),
|
||||
'public_email' => Yii::t('user', 'Email (public)'),
|
||||
'gravatar_email' => Yii::t('user', 'Gravatar email'),
|
||||
'location' => Yii::t('user', 'Location'),
|
||||
'website' => Yii::t('user', 'Website'),
|
||||
'bio' => Yii::t('user', 'Bio'),
|
||||
'timezone' => Yii::t('user', 'Time zone'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the User's timezone.
|
||||
*
|
||||
* @return DateTimeZone
|
||||
*/
|
||||
public function getTimeZone()
|
||||
{
|
||||
try {
|
||||
return new DateTimeZone($this->timezone);
|
||||
} catch (Exception $e) {
|
||||
return new DateTimeZone(Yii::$app->getTimeZone());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the User's timezone
|
||||
*
|
||||
* @param DateTimeZone $timezone
|
||||
*/
|
||||
public function setTimeZone(DateTimeZone $timezone)
|
||||
{
|
||||
$this->setAttribute('timezone', $timezone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get User's local time
|
||||
*
|
||||
* @param DateTime|null $dateTime
|
||||
*
|
||||
* @return DateTime
|
||||
*/
|
||||
public function getLocalTimeZone(DateTime $dateTime = null)
|
||||
{
|
||||
return $dateTime === null ? new DateTime() : $dateTime->setTimezone($this->getTimeZone());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $size
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getAvatarUrl($size = 200)
|
||||
{
|
||||
return $this->make(GravatarHelper::class)->getUrl($this->gravatar_id, $size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ProfileQuery
|
||||
*/
|
||||
|
||||
@ -2,10 +2,103 @@
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Query\SocialNetworkAccountQuery;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use Da\User\Traits\ModuleTrait;
|
||||
use Yii;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* /**
|
||||
* @property integer $id Id
|
||||
* @property integer $user_id User id, null if account is not bind to user
|
||||
* @property string $provider Name of service
|
||||
* @property string $client_id Account id
|
||||
* @property string $data Account properties returned by social network (json encoded)
|
||||
* @property string $decodedData Json-decoded properties
|
||||
* @property string $code
|
||||
* @property string $email
|
||||
* @property string $username
|
||||
* @property integer $created_at
|
||||
*
|
||||
* @property User $user User that this account is connected for.
|
||||
*/
|
||||
class SocialNetworkAccount extends ActiveRecord
|
||||
{
|
||||
use ModuleTrait;
|
||||
use ContainerTrait;
|
||||
|
||||
/**
|
||||
* @var array json decoded properties
|
||||
*/
|
||||
protected $decodedData;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{%social_account}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Whether this social account is connected to user.
|
||||
*/
|
||||
public function getIsConnected()
|
||||
{
|
||||
return $this->user_id != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array json decoded properties
|
||||
*/
|
||||
public function getDecodedData()
|
||||
{
|
||||
if ($this->data !== null && $this->decodedData === null) {
|
||||
$this->decodedData = json_decode($this->data);
|
||||
}
|
||||
|
||||
return $this->decodedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string the connection url
|
||||
*/
|
||||
public function getConnectionUrl()
|
||||
{
|
||||
$code = Yii::$app->security->generateRandomString();
|
||||
$this->updateAttributes(['code' => md5($code)]);
|
||||
|
||||
return Url::to(['/usr/registration/connect', 'code' => $code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects account to a user
|
||||
*
|
||||
* @param User $user
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function connect(User $user)
|
||||
{
|
||||
return $this->updateAttributes(
|
||||
[
|
||||
'username' => null,
|
||||
'email' => null,
|
||||
'code' => null,
|
||||
'user_id' => $user->id,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SocialNetworkAccountQuery
|
||||
*/
|
||||
|
||||
@ -1,17 +1,106 @@
|
||||
<?php
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Query\TokenQuery;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use Da\User\Traits\ModuleTrait;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\Url;
|
||||
use RuntimeException;
|
||||
use Yii;
|
||||
|
||||
|
||||
/**
|
||||
* Token Active Record model.
|
||||
*
|
||||
* @property integer $user_id
|
||||
* @property string $code
|
||||
* @property integer $type
|
||||
* @property string $url
|
||||
* @property bool $isExpired
|
||||
* @property integer $created_at
|
||||
*
|
||||
* @property User $user
|
||||
*/
|
||||
class Token extends ActiveRecord
|
||||
{
|
||||
const TYPE_CONFIRMATION = 0;
|
||||
const TYPE_RECOVERY = 1;
|
||||
use ModuleTrait;
|
||||
use ContainerTrait;
|
||||
|
||||
const TYPE_CONFIRMATION = 0;
|
||||
const TYPE_RECOVERY = 1;
|
||||
const TYPE_CONFIRM_NEW_EMAIL = 2;
|
||||
const TYPE_CONFIRM_OLD_EMAIL = 3;
|
||||
|
||||
protected $routes = [
|
||||
self::TYPE_CONFIRMATION => '/user/registration/confirm',
|
||||
self::TYPE_RECOVERY => '/usr/recovery/reset',
|
||||
self::TYPE_CONFIRM_NEW_EMAIL => '/user/settings/confirm',
|
||||
self::TYPE_CONFIRM_OLD_EMAIL => '/usr/settings/confirm'
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function beforeSave($insert)
|
||||
{
|
||||
if ($insert) {
|
||||
$this->setAttribute('code', $this->make(SecurityHelper::class)->generateRandomString());
|
||||
static::deleteAll(['user_id' => $this->user_id, 'type' => $this->type]);
|
||||
$this->setAttribute('created_at', time());
|
||||
}
|
||||
|
||||
return parent::beforeSave($insert);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{%token}}';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function primaryKey()
|
||||
{
|
||||
return ['user_id', 'code', 'type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->hasOne($this->getClassMap()->get('User'), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return Url::to([$this->routes[$this->type], 'id' => $this->user_id, 'code' => $this->code], true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool Whether token has expired.
|
||||
*/
|
||||
public function getIsExpired()
|
||||
{
|
||||
if ($this->type == static::TYPE_RECOVERY) {
|
||||
$expirationTime = $this->getModule()->tokenRecoveryLifespan;
|
||||
} elseif ($this->type >= static::TYPE_CONFIRMATION && $this->type <= static::TYPE_CONFIRM_OLD_EMAIL) {
|
||||
$expirationTime = $this->getModule()->tokenConfirmationLifespan;
|
||||
} else {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
return ($this->created_at + $expirationTime) < time();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TokenQuery
|
||||
*/
|
||||
|
||||
@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user