add services + more structural additions
This commit is contained in:
18
lib/User/Contracts/ServiceInterface.php
Normal file
18
lib/User/Contracts/ServiceInterface.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Da\User\Contracts;
|
||||
|
||||
/**
|
||||
*
|
||||
* ServiceInterface.php
|
||||
*
|
||||
* Date: 4/12/16
|
||||
* Time: 2:56
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
interface ServiceInterface
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function run();
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use Da\User\Module;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
@ -22,12 +23,24 @@ class AuthHelper
|
||||
*/
|
||||
public function hasRole($userId, $role)
|
||||
{
|
||||
if (Yii::$app->authManager) {
|
||||
$roles = array_keys(Yii::$app->authManager->getRolesByUser($userId));
|
||||
if (Yii::$app->getAuthManager()) {
|
||||
$roles = array_keys(Yii::$app->getAuthManager()->getRolesByUser($userId));
|
||||
|
||||
return in_array($role, $roles, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isAdmin($username)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$hasAdministratorPermissionName = Yii::$app->getAuthManager() && $module->administratorPermissionName
|
||||
? Yii::$app->getUser()->can($module->administratorPermissionName)
|
||||
: false;
|
||||
|
||||
return $hasAdministratorPermissionName || in_array($username, $module->administrators);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
60
lib/User/Helper/SecurityHelper.php
Normal file
60
lib/User/Helper/SecurityHelper.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Helper;
|
||||
|
||||
use yii\base\Security;
|
||||
|
||||
class SecurityHelper
|
||||
{
|
||||
/**
|
||||
* @var Security
|
||||
*/
|
||||
protected $security;
|
||||
|
||||
public function __construct(Security $security)
|
||||
{
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a secure hash from a password and a random salt.
|
||||
*
|
||||
* @param string $password
|
||||
* @param null|int $cost
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generatePasswordHash($password, $cost = null)
|
||||
{
|
||||
return $this->security->generatePasswordHash($password, $cost);
|
||||
}
|
||||
|
||||
public function validatePassword($password, $hash)
|
||||
{
|
||||
return $this->security->validatePassword($password, $hash);
|
||||
}
|
||||
|
||||
public function generatePassword($length)
|
||||
{
|
||||
$sets = [
|
||||
'abcdefghjkmnpqrstuvwxyz',
|
||||
'ABCDEFGHJKMNPQRSTUVWXYZ',
|
||||
'23456789',
|
||||
];
|
||||
$all = '';
|
||||
$password = '';
|
||||
foreach ($sets as $set) {
|
||||
$password .= $set[array_rand(str_split($set))];
|
||||
$all .= $set;
|
||||
}
|
||||
|
||||
$all = str_split($all);
|
||||
for ($i = 0; $i < $length - count($sets); $i++) {
|
||||
$password .= $all[array_rand($all)];
|
||||
}
|
||||
|
||||
$password = str_shuffle($password);
|
||||
|
||||
return $password;
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,13 @@
|
||||
<?php
|
||||
namespace Da\User\Model;
|
||||
|
||||
use Da\User\Helper\AuthHelper;
|
||||
use Da\User\Traits\ModuleTrait;
|
||||
use Da\User\Traits\ContainerTrait;
|
||||
use yii\base\NotSupportedException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\ActiveRecord;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\web\IdentityInterface;
|
||||
use Yii;
|
||||
|
||||
@ -25,14 +31,140 @@ use Yii;
|
||||
* @property integer $updated_at
|
||||
*
|
||||
* Defined relations:
|
||||
* @property Account[] $accounts
|
||||
* @property SocialNetworkAccount[] $socialNetworkAccounts
|
||||
* @property Profile $profile
|
||||
*/
|
||||
class User extends ActiveRecord implements IdentityInterface
|
||||
{
|
||||
/** @var string Plain password. Used for model validation. */
|
||||
use ModuleTrait;
|
||||
use ContainerTrait;
|
||||
|
||||
/**
|
||||
* @var string default user name regular expression.
|
||||
*/
|
||||
public $usernameRegex = '/^[-a-zA-Z0-9_\.@]+$/';
|
||||
/**
|
||||
* @var string Plain password. Used for model validation.
|
||||
*/
|
||||
public $password;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function behaviors()
|
||||
{
|
||||
return [
|
||||
TimestampBehavior::className(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'username' => Yii::t('user', 'Username'),
|
||||
'email' => Yii::t('user', 'Email'),
|
||||
'registration_ip' => Yii::t('user', 'Registration ip'),
|
||||
'unconfirmed_email' => Yii::t('user', 'New email'),
|
||||
'password' => Yii::t('user', 'Password'),
|
||||
'created_at' => Yii::t('user', 'Registration time'),
|
||||
'confirmed_at' => Yii::t('user', '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' => $this->usernameRegex],
|
||||
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
|
||||
'usernameTrim' => ['username', 'trim'],
|
||||
'usernameUnique' => [
|
||||
'username',
|
||||
'unique',
|
||||
'message' => Yii::t('user', '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('user', '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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public static function findIdentityByAccessToken($token, $type = null)
|
||||
{
|
||||
throw new NotSupportedException('Method "' . __CLASS__ . '::' . __METHOD__ . '" is not implemented.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether is blocked or not.
|
||||
*/
|
||||
@ -41,13 +173,50 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
return $this->blocked_at !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether the user is an admin or not
|
||||
*/
|
||||
public function getIsAdmin()
|
||||
{
|
||||
|
||||
return $this->getAuthHelper()->isAdmin($this->username);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a user has a specific role
|
||||
*
|
||||
* @param string $role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRole($role)
|
||||
{
|
||||
return $this->getAuthHelper()->hasRole($this->id, $role);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \yii\db\ActiveQuery
|
||||
*/
|
||||
public function getProfile()
|
||||
{
|
||||
return $this->hasOne($this->getClassMapHelper()->get('Profile'), ['user_id' => 'id']);
|
||||
}
|
||||
|
||||
protected $connectedAccounts;
|
||||
|
||||
/**
|
||||
* @return SocialNetworkAccount[] social connected accounts [ 'providerName' => socialAccountModel ]
|
||||
*/
|
||||
public function getSocialNetworkAccounts()
|
||||
{
|
||||
if ($this->connectedAccounts == null) {
|
||||
$accounts = $this->connectedAccounts = $this
|
||||
->hasMany($this->getClassMapHelper()->get('Account'), ['user_id' => 'id'])
|
||||
->all();
|
||||
|
||||
foreach($accounts as $account) {
|
||||
$this->connectedAccounts[$account->provider] = $account;
|
||||
}
|
||||
}
|
||||
return $this->connectedAccounts;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,6 +10,10 @@ class Module extends \yii\base\Module
|
||||
* @var bool whether to allow registration process or not.
|
||||
*/
|
||||
public $allowRegistration = true;
|
||||
/**
|
||||
* @var bool whether to generate passwords automatically and remove the password field from the registration form.
|
||||
*/
|
||||
public $generatePasswords = false;
|
||||
/**
|
||||
* @var bool whether to force email confirmation to.
|
||||
*/
|
||||
@ -18,6 +22,10 @@ class Module extends \yii\base\Module
|
||||
* @var bool whether to allow login accounts with unconfirmed emails.
|
||||
*/
|
||||
public $allowUnconfirmedEmailLogin = false;
|
||||
/**
|
||||
* @var bool whether to enable password recovery or not.
|
||||
*/
|
||||
public $allowPasswordRecovery = true;
|
||||
/**
|
||||
* @var string the class name of the strategy class to handle user's email change.
|
||||
*/
|
||||
|
||||
12
lib/User/Service/MailService.php
Normal file
12
lib/User/Service/MailService.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
|
||||
class MailService implements ServiceInterface
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
// TODO: Implement run() method.
|
||||
}
|
||||
}
|
||||
73
lib/User/Service/UserCreateService.php
Normal file
73
lib/User/Service/UserCreateService.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
use yii\base\InvalidCallException;
|
||||
use Exception;
|
||||
use yii\log\Logger;
|
||||
|
||||
/**
|
||||
*
|
||||
* UserCreateService.php
|
||||
*
|
||||
* Date: 4/12/16
|
||||
* Time: 2:55
|
||||
* @author Antonio Ramirez <hola@2amigos.us>
|
||||
*/
|
||||
class UserCreateService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $securityHelper;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(User $model, SecurityHelper $securityHelper, Logger $logger)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->securityHelper = $securityHelper;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$model = $this->model;
|
||||
|
||||
if ($model->getIsNewRecord() === false) {
|
||||
throw new InvalidCallException('Cannot create a new user from an existing one.');
|
||||
}
|
||||
|
||||
$transaction = $model->getDb()->beginTransaction();
|
||||
|
||||
try {
|
||||
$model->confirmed_at = time();
|
||||
$model->password = $model->password !== null
|
||||
? $model->password
|
||||
: $this->securityHelper->generatePassword(8);
|
||||
|
||||
// TODO: Trigger BEFORE CREATE EVENT
|
||||
|
||||
if (!$model->save()) {
|
||||
$transaction->rollBack();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Send welcome message
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
$this->logger->log($e->getMessage(), Logger::LEVEL_WARNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
38
lib/User/Traits/ContainerTrait.php
Normal file
38
lib/User/Traits/ContainerTrait.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Traits;
|
||||
|
||||
use Yii;
|
||||
use yii\di\Container;
|
||||
|
||||
/**
|
||||
* @property-read Container $di
|
||||
* @property-ready Da\User\Helper\AuthHelper $authHelper
|
||||
*/
|
||||
trait ContainerTrait
|
||||
{
|
||||
/**
|
||||
* @return Container
|
||||
*/
|
||||
public function getDi()
|
||||
{
|
||||
return Yii::$container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Da\User\Helper\AuthHelper
|
||||
*/
|
||||
public function getAuthHelper()
|
||||
{
|
||||
return Yii::$container->get('Da\User\Helper\AuthHelper');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Da\User\Helper\ClassMapHelper
|
||||
*/
|
||||
public function getClassMapHelper()
|
||||
{
|
||||
return Yii::$container->get('Da\User\Helper\ClassMapHelper');
|
||||
}
|
||||
|
||||
}
|
||||
21
lib/User/Traits/ModuleTrait.php
Normal file
21
lib/User/Traits/ModuleTrait.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Traits;
|
||||
|
||||
use Da\User\Helper\AuthHelper;
|
||||
use Da\User\Module;
|
||||
use Yii;
|
||||
|
||||
/**
|
||||
* @property-read Module $module
|
||||
*/
|
||||
trait ModuleTrait
|
||||
{
|
||||
/**
|
||||
* @return \Da\User\Module
|
||||
*/
|
||||
public function getModule()
|
||||
{
|
||||
return Yii::$app->getModule('user');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user