update folder location
This commit is contained in:
130
src/User/Model/AbstractAuthItem.php
Normal file
130
src/User/Model/AbstractAuthItem.php
Normal file
@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Da\User\Validator\RbacItemsValidator;
|
||||
use Da\User\Validator\RbacRuleValidator;
|
||||
use yii\base\Model;
|
||||
use yii\rbac\Item;
|
||||
use Yii;
|
||||
|
||||
abstract class AbstractAuthItem extends Model
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $itemName;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $rule;
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $children;
|
||||
/**
|
||||
* @var \yii\rbac\Role|\yii\rbac\Permission
|
||||
*/
|
||||
public $item;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
if ($this->item instanceof Item) {
|
||||
$this->itemName = $this->item->name;
|
||||
$this->name = $this->item->name;
|
||||
$this->description = $this->item->description;
|
||||
$this->children = array_keys($this->getAuthManager()->getChildren($this->item->name));
|
||||
if ($this->item->ruleName !== null) {
|
||||
$this->rule = get_class($this->getAuthManager()->getRule($this->item->ruleName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'name' => Yii::t('usuario', 'Name'),
|
||||
'description' => Yii::t('usuario', 'Description'),
|
||||
'children' => Yii::t('usuario', 'Children'),
|
||||
'rule' => Yii::t('usuario', 'Rule'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
return [
|
||||
'create' => ['name', 'description', 'children', 'rule'],
|
||||
'update' => ['name', 'description', 'children', 'rule'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['itemName', 'safe'],
|
||||
['name', 'required'],
|
||||
['name', 'match', 'pattern' => '/^[\w][\w-.:]+[\w]$/'],
|
||||
[['name', 'description', 'rule'], 'trim'],
|
||||
[
|
||||
'name',
|
||||
function () {
|
||||
if ($this->getAuthManager()->getItem($this->name) !== null) {
|
||||
$this->addError('name', Yii::t('usuario', 'Auth item with such name already exists'));
|
||||
}
|
||||
},
|
||||
'when' => function () {
|
||||
return $this->scenario == 'create' || $this->item->name != $this->name;
|
||||
},
|
||||
],
|
||||
['children', RbacItemsValidator::class],
|
||||
['rule', RbacRuleValidator::class],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsNewRecord()
|
||||
{
|
||||
return $this->item === null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Item
|
||||
*/
|
||||
abstract public function getType();
|
||||
}
|
||||
65
src/User/Model/Assignment.php
Normal file
65
src/User/Model/Assignment.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Da\User\Validator\RbacItemsValidator;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\base\Model;
|
||||
use Yii;
|
||||
|
||||
class Assignment extends Model
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
public $items = [];
|
||||
public $user_id;
|
||||
public $updated = false;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @throws InvalidConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
|
||||
if ($this->user_id === null) {
|
||||
throw new InvalidConfigException('"user_id" must be set.');
|
||||
}
|
||||
|
||||
$this->items = array_keys($this->getAuthManager()->getItemsByUser($this->user_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'items' => Yii::t('usuario', 'Items'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
['user_id', 'required'],
|
||||
['items', RbacItemsValidator::class],
|
||||
['user_id', 'integer'],
|
||||
];
|
||||
}
|
||||
}
|
||||
22
src/User/Model/Permission.php
Normal file
22
src/User/Model/Permission.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use yii\rbac\Item;
|
||||
|
||||
class Permission extends AbstractAuthItem
|
||||
{
|
||||
public function getType()
|
||||
{
|
||||
return Item::TYPE_PERMISSION;
|
||||
}
|
||||
}
|
||||
168
src/User/Model/Profile.php
Normal file
168
src/User/Model/Profile.php
Normal file
@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\GravatarHelper;
|
||||
use Da\User\Query\ProfileQuery;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use Da\User\Validator\TimeZoneValidator;
|
||||
use Yii;
|
||||
use yii\db\ActiveRecord;
|
||||
use Exception;
|
||||
use DateTimeZone;
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* @property int $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 ModuleAwareTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* {@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('usuario', '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('usuario', 'Name'),
|
||||
'public_email' => Yii::t('usuario', 'Email (public)'),
|
||||
'gravatar_email' => Yii::t('usuario', 'Gravatar email'),
|
||||
'location' => Yii::t('usuario', 'Location'),
|
||||
'website' => Yii::t('usuario', 'Website'),
|
||||
'bio' => Yii::t('usuario', 'Bio'),
|
||||
'timezone' => Yii::t('usuario', '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::class), ['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
|
||||
*/
|
||||
public static function find()
|
||||
{
|
||||
return new ProfileQuery(static::class);
|
||||
}
|
||||
}
|
||||
22
src/User/Model/Role.php
Normal file
22
src/User/Model/Role.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use yii\rbac\Item;
|
||||
|
||||
class Role extends AbstractAuthItem
|
||||
{
|
||||
public function getType()
|
||||
{
|
||||
return Item::TYPE_ROLE;
|
||||
}
|
||||
}
|
||||
118
src/User/Model/SocialNetworkAccount.php
Normal file
118
src/User/Model/SocialNetworkAccount.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Query\SocialNetworkAccountQuery;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use Yii;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* /**
|
||||
* @property int $id Id
|
||||
* @property int $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 int $created_at
|
||||
* @property User $user User that this account is connected for
|
||||
*/
|
||||
class SocialNetworkAccount extends ActiveRecord
|
||||
{
|
||||
use ModuleAwareTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
* @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::class), ['id' => 'user_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SocialNetworkAccountQuery
|
||||
*/
|
||||
public static function find()
|
||||
{
|
||||
return new SocialNetworkAccountQuery(static::class);
|
||||
}
|
||||
}
|
||||
119
src/User/Model/Token.php
Normal file
119
src/User/Model/Token.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Query\TokenQuery;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use RuntimeException;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\Url;
|
||||
|
||||
/**
|
||||
* Token Active Record model.
|
||||
*
|
||||
* @property int $user_id
|
||||
* @property string $code
|
||||
* @property int $type
|
||||
* @property string $url
|
||||
* @property bool $isExpired
|
||||
* @property int $created_at
|
||||
* @property User $user
|
||||
*/
|
||||
class Token extends ActiveRecord
|
||||
{
|
||||
use ModuleAwareTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
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::class), ['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
|
||||
*/
|
||||
public static function find()
|
||||
{
|
||||
return new TokenQuery(static::class);
|
||||
}
|
||||
}
|
||||
289
src/User/Model/User.php
Normal file
289
src/User/Model/User.php
Normal file
@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the 2amigos/yii2-usuario project.
|
||||
*
|
||||
* (c) 2amigOS! <http://2amigos.us/>
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use Yii;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\web\Application;
|
||||
use yii\web\IdentityInterface;
|
||||
|
||||
/**
|
||||
* User ActiveRecord model.
|
||||
*
|
||||
* @property bool $isAdmin
|
||||
* @property bool $isBlocked
|
||||
* @property bool $isConfirmed whether user account has been confirmed or not
|
||||
*
|
||||
* Database fields:
|
||||
* @property int $id
|
||||
* @property string $username
|
||||
* @property string $email
|
||||
* @property string $unconfirmed_email
|
||||
* @property string $password_hash
|
||||
* @property string $auth_key
|
||||
* @property int $registration_ip
|
||||
* @property int $confirmed_at
|
||||
* @property int $blocked_at
|
||||
* @property int $flags
|
||||
* @property int $created_at
|
||||
* @property int $updated_at
|
||||
*
|
||||
* Defined relations:
|
||||
* @property SocialNetworkAccount[] $socialNetworkAccounts
|
||||
* @property Profile $profile
|
||||
*/
|
||||
class User extends ActiveRecord implements IdentityInterface
|
||||
{
|
||||
use ModuleAwareTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
// following constants are used on secured email changing process
|
||||
const OLD_EMAIL_CONFIRMED = 0b1;
|
||||
const NEW_EMAIL_CONFIRMED = 0b10;
|
||||
|
||||
/**
|
||||
* @var string Plain password. Used for model validation
|
||||
*/
|
||||
public $password;
|
||||
/**
|
||||
* @var array connected account list
|
||||
*/
|
||||
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}
|
||||
*/
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
TimestampBehavior::className(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'username' => Yii::t('usuario', 'Username'),
|
||||
'email' => Yii::t('usuario', 'Email'),
|
||||
'registration_ip' => Yii::t('usuario', 'Registration ip'),
|
||||
'unconfirmed_email' => Yii::t('usuario', 'New email'),
|
||||
'password' => Yii::t('usuario', 'Password'),
|
||||
'created_at' => Yii::t('usuario', 'Registration time'),
|
||||
'confirmed_at' => Yii::t('usuario', 'Confirmation time'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
return ArrayHelper::merge(
|
||||
parent::scenarios(),
|
||||
[
|
||||
'register' => ['username', 'email', 'password'],
|
||||
'connect' => ['username', 'email'],
|
||||
'create' => ['username', 'email', 'password'],
|
||||
'update' => ['username', 'email', 'password'],
|
||||
'settings' => ['username', 'email', 'password'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
// username rules
|
||||
'usernameRequired' => ['username', 'required', 'on' => ['register', 'create', 'connect', 'update']],
|
||||
'usernameMatch' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'],
|
||||
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
|
||||
'usernameTrim' => ['username', 'trim'],
|
||||
'usernameUnique' => [
|
||||
'username',
|
||||
'unique',
|
||||
'message' => Yii::t('usuario', 'This username has already been taken'),
|
||||
],
|
||||
|
||||
// email rules
|
||||
'emailRequired' => ['email', 'required', 'on' => ['register', 'connect', 'create', 'update']],
|
||||
'emailPattern' => ['email', 'email'],
|
||||
'emailLength' => ['email', 'string', 'max' => 255],
|
||||
'emailUnique' => [
|
||||
'email',
|
||||
'unique',
|
||||
'message' => Yii::t('usuario', 'This email address has already been taken'),
|
||||
],
|
||||
'emailTrim' => ['email', 'trim'],
|
||||
|
||||
// password rules
|
||||
'passwordRequired' => ['password', 'required', 'on' => ['register']],
|
||||
'passwordLength' => ['password', 'string', 'min' => 6, 'max' => 72, 'on' => ['register', 'create']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateAuthKey($authKey)
|
||||
{
|
||||
return $this->getAttribute('auth_key') === $authKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->getAttribute('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAuthKey()
|
||||
{
|
||||
return $this->getAttribute('auth_key');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function findIdentity($id)
|
||||
{
|
||||
return static::findOne($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether is blocked or not
|
||||
*/
|
||||
public function getIsBlocked()
|
||||
{
|
||||
return $this->blocked_at !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether the user is an admin or not
|
||||
*/
|
||||
public function getIsAdmin()
|
||||
{
|
||||
return $this->getAuth()->isAdmin($this->username);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether user account has been confirmed or not.
|
||||
* @return bool whether user account has been confirmed or not
|
||||
*/
|
||||
public function getIsConfirmed()
|
||||
{
|
||||
return $this->confirmed_at !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a user has a specific role.
|
||||
*
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole($role)
|
||||
{
|
||||
return $this->getAuth()->hasRole($this->id, $role);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getProfile()
|
||||
{
|
||||
return $this->hasOne($this->getClassMap()->get(Profile::class), ['user_id' => 'id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ]
|
||||
*/
|
||||
public function getSocialNetworkAccounts()
|
||||
{
|
||||
if ($this->connectedAccounts == null) {
|
||||
/** @var SocialNetworkAccount[] $accounts */
|
||||
$accounts = $this->hasMany($this->getClassMap()
|
||||
->get(SocialNetworkAccount::class), ['user_id' => 'id'])
|
||||
->all();
|
||||
|
||||
foreach ($accounts as $account) {
|
||||
$this->connectedAccounts[$account->provider] = $account;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->connectedAccounts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return UserQuery
|
||||
*/
|
||||
public static function find()
|
||||
{
|
||||
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