update folder location
This commit is contained in:
61
src/User/Factory/AuthItemFactory.php
Normal file
61
src/User/Factory/AuthItemFactory.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?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\Factory;
|
||||
|
||||
use Yii;
|
||||
use yii\rbac\Item;
|
||||
use Exception;
|
||||
|
||||
class AuthItemFactory
|
||||
{
|
||||
protected static $map = [
|
||||
Item::TYPE_ROLE => 'makeRole',
|
||||
Item::TYPE_PERMISSION => 'makePermission',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return \yii\rbac\Permission
|
||||
*/
|
||||
public static function makePermission($name)
|
||||
{
|
||||
return Yii::$app->getAuthManager()->createPermission($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return \yii\rbac\Role
|
||||
*/
|
||||
public static function makeRole($name)
|
||||
{
|
||||
return Yii::$app->getAuthManager()->createRole($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param $name
|
||||
*
|
||||
* @return \yii\rbac\Role|\yii\rbac\Permission
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function makeByType($type, $name)
|
||||
{
|
||||
if (array_key_exists($type, self::$map)) {
|
||||
return call_user_func([self::class, self::$map[$type]], $name);
|
||||
}
|
||||
|
||||
throw new Exception('Unknown strategy type');
|
||||
}
|
||||
}
|
||||
76
src/User/Factory/EmailChangeStrategyFactory.php
Normal file
76
src/User/Factory/EmailChangeStrategyFactory.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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\Factory;
|
||||
|
||||
use Da\User\Contracts\MailChangeStrategyInterface;
|
||||
use Da\User\Form\SettingsForm;
|
||||
use Da\User\Strategy\DefaultEmailChangeStrategy;
|
||||
use Da\User\Strategy\InsecureEmailChangeStrategy;
|
||||
use Da\User\Strategy\SecureEmailChangeStrategy;
|
||||
use Yii;
|
||||
use Exception;
|
||||
|
||||
class EmailChangeStrategyFactory
|
||||
{
|
||||
protected static $map = [
|
||||
MailChangeStrategyInterface::TYPE_INSECURE => InsecureEmailChangeStrategy::class,
|
||||
MailChangeStrategyInterface::TYPE_DEFAULT => DefaultEmailChangeStrategy::class,
|
||||
MailChangeStrategyInterface::TYPE_SECURE => SecureEmailChangeStrategy::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param $strategy
|
||||
* @param SettingsForm $form
|
||||
*
|
||||
* @return MailChangeStrategyInterface
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function makeByStrategyType($strategy, SettingsForm $form)
|
||||
{
|
||||
if (array_key_exists($strategy, static::$map)) {
|
||||
return Yii::$container->get(static::$map[$strategy], [$form]);
|
||||
}
|
||||
|
||||
throw new Exception('Unknown strategy type');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SettingsForm $form
|
||||
*
|
||||
* @return DefaultEmailChangeStrategy
|
||||
*/
|
||||
public static function makeDefaultEmailChangeStrategy(SettingsForm $form)
|
||||
{
|
||||
return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_DEFAULT], [$form]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SettingsForm $form
|
||||
*
|
||||
* @return InsecureEmailChangeStrategy
|
||||
*/
|
||||
public static function makeInsecureEmailChangeStrategy(SettingsForm $form)
|
||||
{
|
||||
return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_INSECURE], [$form]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SettingsForm $form
|
||||
*
|
||||
* @return SecureEmailChangeStrategy
|
||||
*/
|
||||
public static function makeSecureEmailChangeStrategy(SettingsForm $form)
|
||||
{
|
||||
return Yii::$container->get(static::$map[MailChangeStrategyInterface::TYPE_SECURE], [$form]);
|
||||
}
|
||||
}
|
||||
125
src/User/Factory/MailFactory.php
Normal file
125
src/User/Factory/MailFactory.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?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\Factory;
|
||||
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Module;
|
||||
use Da\User\Service\MailService;
|
||||
use Yii;
|
||||
|
||||
class MailFactory
|
||||
{
|
||||
/**
|
||||
* @param User $user
|
||||
*
|
||||
* @return MailService
|
||||
*/
|
||||
public static function makeWelcomeMailerService(User $user)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$to = $user->email;
|
||||
$from = $module->mailParams['fromEmail'];
|
||||
$subject = $module->mailParams['welcomeMailSubject'];
|
||||
$params = [
|
||||
'user' => $user,
|
||||
'token' => null,
|
||||
'module' => $module,
|
||||
'showPassword' => false,
|
||||
];
|
||||
|
||||
return static::makeMailerService($from, $to, $subject, 'welcome', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $email
|
||||
* @param Token $token
|
||||
*
|
||||
* @return MailService
|
||||
*/
|
||||
public static function makeRecoveryMailerService($email, Token $token = null)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$to = $email;
|
||||
$from = $module->mailParams['fromEmail'];
|
||||
$subject = $module->mailParams['recoveryMailSubject'];
|
||||
$params = [
|
||||
'user' => $token && $token->user ? $token->user : null,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
return static::makeMailerService($from, $to, $subject, 'recovery', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param Token|null $token
|
||||
*
|
||||
* @return MailService
|
||||
*/
|
||||
public static function makeConfirmationMailerService(User $user, Token $token = null)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$to = $user->email;
|
||||
$from = $module->mailParams['fromEmail'];
|
||||
$subject = $module->mailParams['confirmationMailSubject'];
|
||||
$params = [
|
||||
'user' => $token && $token->user ? $token->user : null,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
return static::makeMailerService($from, $to, $subject, 'recovery', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $user
|
||||
* @param Token $token
|
||||
*
|
||||
* @return MailService
|
||||
*/
|
||||
public static function makeReconfirmationMailerService(User $user, Token $token)
|
||||
{
|
||||
/** @var Module $module */
|
||||
$module = Yii::$app->getModule('user');
|
||||
$to = $token->type === Token::TYPE_CONFIRM_NEW_EMAIL
|
||||
? $user->unconfirmed_email
|
||||
: $user->email;
|
||||
|
||||
$from = $module->mailParams['fromEmail'];
|
||||
$subject = $module->mailParams['reconfirmationMailSubject'];
|
||||
$params = [
|
||||
'user' => $token && $token->user ? $token->user : null,
|
||||
'token' => $token,
|
||||
];
|
||||
|
||||
return static::makeMailerService($from, $to, $subject, 'recovery', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a MailerService.
|
||||
*
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
* @param string $subject
|
||||
* @param string $view
|
||||
* @param array $params
|
||||
*
|
||||
* @return MailService
|
||||
*/
|
||||
public static function makeMailerService($from, $to, $subject, $view, array $params = [])
|
||||
{
|
||||
return Yii::$container->get(MailService::class, [$from, $to, $subject, $view, $params, Yii::$app->getMailer()]);
|
||||
}
|
||||
}
|
||||
85
src/User/Factory/TokenFactory.php
Normal file
85
src/User/Factory/TokenFactory.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?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\Factory;
|
||||
|
||||
use Da\User\Model\Token;
|
||||
use Yii;
|
||||
|
||||
class TokenFactory
|
||||
{
|
||||
/**
|
||||
* @param $userId
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public static function makeConfirmationToken($userId)
|
||||
{
|
||||
$token = self::make($userId, Token::TYPE_CONFIRMATION);
|
||||
|
||||
$token->save(false);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public static function makeConfirmNewMailToken($userId)
|
||||
{
|
||||
$token = self::make($userId, Token::TYPE_CONFIRM_NEW_EMAIL);
|
||||
|
||||
$token->save(false);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public static function makeConfirmOldMailToken($userId)
|
||||
{
|
||||
$token = self::make($userId, Token::TYPE_CONFIRM_OLD_EMAIL);
|
||||
|
||||
$token->save(false);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
public static function makeRecoveryToken($userId)
|
||||
{
|
||||
$token = self::make($userId, Token::TYPE_RECOVERY);
|
||||
|
||||
$token->save(false);
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $userId
|
||||
* @param $type
|
||||
*
|
||||
* @return Token
|
||||
*/
|
||||
protected static function make($userId, $type)
|
||||
{
|
||||
return Yii::createObject(['class' => Token::class, 'user_id' => $userId, 'type' => $type]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user