update folder location
This commit is contained in:
45
src/User/Service/AccountConfirmationService.php
Normal file
45
src/User/Service/AccountConfirmationService.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\TokenQuery;
|
||||
|
||||
class AccountConfirmationService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $code;
|
||||
protected $tokenQuery;
|
||||
protected $userConfirmationService;
|
||||
|
||||
public function __construct(
|
||||
$code,
|
||||
User $model,
|
||||
UserConfirmationService $userConfirmationService,
|
||||
TokenQuery $tokenQuery
|
||||
) {
|
||||
$this->code = $code;
|
||||
$this->model = $model;
|
||||
$this->userConfirmationService = $userConfirmationService;
|
||||
$this->tokenQuery = $tokenQuery;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$token = $this->tokenQuery
|
||||
->whereUserId($this->model->id)
|
||||
->whereCode($this->code)
|
||||
->whereIsConfirmationType()
|
||||
->one();
|
||||
|
||||
if ($token instanceof Token && !$token->getIsExpired()) {
|
||||
$token->delete();
|
||||
|
||||
return $this->userConfirmationService->run();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
103
src/User/Service/AuthItemEditionService.php
Normal file
103
src/User/Service/AuthItemEditionService.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Factory\AuthItemFactory;
|
||||
use Da\User\Model\AbstractAuthItem;
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Exception;
|
||||
|
||||
class AuthItemEditionService implements ServiceInterface
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
use ContainerAwareTrait;
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(AbstractAuthItem $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
if (!$this->model->validate()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if ($this->model->getIsNewRecord()) {
|
||||
$item = AuthItemFactory::makeByType($this->model->getType(), $this->model->name);
|
||||
} else {
|
||||
$item = $this->model->item;
|
||||
}
|
||||
|
||||
$item->name = $this->model->name;
|
||||
$item->description = $this->model->description;
|
||||
|
||||
if (!empty($this->model->rule)) {
|
||||
$rule = $this->make($this->model->rule);
|
||||
if (null === $this->getAuthManager()->getRule($rule->name)) {
|
||||
$this->getAuthManager()->add($rule);
|
||||
}
|
||||
$item->ruleName = $rule->name;
|
||||
} else {
|
||||
$item->ruleName = null;
|
||||
}
|
||||
|
||||
if ($this->model->getIsNewRecord()) {
|
||||
$this->getAuthManager()->add($item);
|
||||
} else {
|
||||
$this->getAuthManager()->update($this->model->itemName, $item);
|
||||
$this->model->itemName = $item->name;
|
||||
}
|
||||
|
||||
$this->model->item = $item;
|
||||
|
||||
return $this->updateChildren();
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates Auth Item children.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function updateChildren()
|
||||
{
|
||||
$children = $this->getAuthManager()->getChildren($this->model->item->name);
|
||||
$childrenNames = array_keys($children);
|
||||
|
||||
if (is_array($this->model->children)) {
|
||||
|
||||
// remove those not linked anymore
|
||||
foreach (array_diff($childrenNames, $this->model->children) as $item) {
|
||||
if (!$this->getAuthManager()->removeChild($this->model->item, $children[$item])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// add new children
|
||||
foreach (array_diff($this->model->children, $childrenNames) as $item) {
|
||||
if (!$this->getAuthManager()->addChild($this->model->item, $this->getAuthManager()->getItem($item))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return $this->getAuthManager()->removeChildren($this->model->item);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
86
src/User/Service/EmailChangeService.php
Normal file
86
src/User/Service/EmailChangeService.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\MailChangeStrategyInterface;
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\TokenQuery;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use Yii;
|
||||
|
||||
class EmailChangeService implements ServiceInterface
|
||||
{
|
||||
use ModuleAwareTrait;
|
||||
|
||||
protected $code;
|
||||
protected $model;
|
||||
protected $tokenQuery;
|
||||
protected $userQuery;
|
||||
|
||||
public function __construct($code, User $model, TokenQuery $tokenQuery, UserQuery $userQuery)
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->model = $model;
|
||||
$this->tokenQuery = $tokenQuery;
|
||||
$this->userQuery = $userQuery;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
/** @var Token $token */
|
||||
$token = $this->tokenQuery
|
||||
->whereUserId($this->model->id)
|
||||
->whereCode($this->code)
|
||||
->whereIsTypes([Token::TYPE_CONFIRM_NEW_EMAIL, Token::TYPE_CONFIRM_OLD_EMAIL])
|
||||
->one();
|
||||
|
||||
if ($token === null || $token->getIsExpired()) {
|
||||
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your confirmation token is invalid or expired'));
|
||||
|
||||
return false;
|
||||
} else {
|
||||
$token->delete();
|
||||
if (empty($this->model->unconfirmed_email)) {
|
||||
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'An error occurred processing your request'));
|
||||
} elseif ($this->userQuery->whereEmail($this->model->unconfirmed_email)->exists() === false) {
|
||||
if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_SECURE) {
|
||||
if ($token->type === Token::TYPE_CONFIRM_NEW_EMAIL) {
|
||||
$this->model->flags |= User::NEW_EMAIL_CONFIRMED;
|
||||
Yii::$app->session->setFlash(
|
||||
'success',
|
||||
Yii::t(
|
||||
'usuario',
|
||||
'Awesome, almost there. '.
|
||||
'Now you need to click the confirmation link sent to your old email address.'
|
||||
)
|
||||
);
|
||||
} elseif ($token->type === Token::TYPE_CONFIRM_OLD_EMAIL) {
|
||||
$this->model->flags |= User::OLD_EMAIL_CONFIRMED;
|
||||
Yii::$app->session->setFlash(
|
||||
'success',
|
||||
Yii::t(
|
||||
'usuario',
|
||||
'Awesome, almost there. '.
|
||||
'Now you need to click the confirmation link sent to your new email address.'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
if ($this->getModule()->emailChangeStrategy === MailChangeStrategyInterface::TYPE_DEFAULT
|
||||
|| ($this->model->flags & User::NEW_EMAIL_CONFIRMED & $this->model->flags & User::OLD_EMAIL_CONFIRMED)
|
||||
) {
|
||||
$this->model->email = $this->model->unconfirmed_email;
|
||||
$this->model->unconfirmed_email = null;
|
||||
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Your email address has been changed'));
|
||||
}
|
||||
|
||||
return $this->model->save(false);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
67
src/User/Service/MailService.php
Normal file
67
src/User/Service/MailService.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Yii;
|
||||
use yii\mail\BaseMailer;
|
||||
|
||||
class MailService implements ServiceInterface
|
||||
{
|
||||
protected $viewPath = '@Da/User/resources/views/mail';
|
||||
|
||||
protected $from;
|
||||
protected $to;
|
||||
protected $subject;
|
||||
protected $view;
|
||||
protected $params = [];
|
||||
protected $mailer;
|
||||
|
||||
/**
|
||||
* MailService constructor.
|
||||
*
|
||||
* @param string $from
|
||||
* @param string $to
|
||||
* @param string $subject
|
||||
* @param string $view
|
||||
* @param array $params
|
||||
* @param MailerInterface $mailer
|
||||
*/
|
||||
public function __construct($from, $to, $subject, $view, array $params, BaseMailer $mailer)
|
||||
{
|
||||
$this->from = $from;
|
||||
$this->to = $to;
|
||||
$this->subject = $subject;
|
||||
$this->view = $view;
|
||||
$this->params = $params;
|
||||
$this->mailer = $mailer;
|
||||
$this->mailer->setViewPath($this->viewPath);
|
||||
$this->mailer->getView()->theme = Yii::$app->view->theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setViewParam($name, $value)
|
||||
{
|
||||
$this->params[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
return $this->mailer
|
||||
->compose(['html' => $this->view, 'text' => "text/{$this->view}"], $this->params)
|
||||
->setFrom($this->from)
|
||||
->setTo($this->to)
|
||||
->setSubject($this->subject)
|
||||
->send();
|
||||
}
|
||||
}
|
||||
61
src/User/Service/PasswordRecoveryService.php
Normal file
61
src/User/Service/PasswordRecoveryService.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Da\User\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Model\Token;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Exception;
|
||||
use Yii;
|
||||
use yii\log\Logger;
|
||||
|
||||
class PasswordRecoveryService implements ServiceInterface
|
||||
{
|
||||
protected $query;
|
||||
|
||||
protected $email;
|
||||
protected $mailService;
|
||||
protected $securityHelper;
|
||||
protected $logger;
|
||||
|
||||
public function __construct($email, MailService $mailService, UserQuery $query, Logger $logger)
|
||||
{
|
||||
$this->email = $email;
|
||||
$this->mailService = $mailService;
|
||||
$this->query = $query;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
/** @var User $user */
|
||||
$user = $this->query->whereEmail($this->email)->one();
|
||||
|
||||
$token = TokenFactory::makeRecoveryToken($user->id);
|
||||
|
||||
if (!$token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->mailService->setViewParam('user', $user);
|
||||
$this->mailService->setViewParam('token', $token);
|
||||
if (!$this->mailService->run()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Yii::$app->session->setFlash(
|
||||
'info',
|
||||
Yii::t('usuario', 'An email has been sent with instructions for resetting your password')
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/User/Service/ResendConfirmationService.php
Normal file
43
src/User/Service/ResendConfirmationService.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Model\User;
|
||||
use yii\log\Logger;
|
||||
|
||||
class ResendConfirmationService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $mailService;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(User $model, MailService $mailService, Logger $logger)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->mailService = $mailService;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
if ($this->model && !$this->model->getIsConfirmed()) {
|
||||
$token = TokenFactory::makeConfirmationToken($this->model->id);
|
||||
$this->mailService->setViewParam('token', $token);
|
||||
|
||||
return $this->mailService->run();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
39
src/User/Service/ResetPasswordService.php
Normal file
39
src/User/Service/ResetPasswordService.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
|
||||
class ResetPasswordService implements ServiceInterface
|
||||
{
|
||||
protected $password;
|
||||
protected $model;
|
||||
protected $securityHelper;
|
||||
|
||||
public function __construct($password, User $model, SecurityHelper $securityHelper)
|
||||
{
|
||||
$this->password;
|
||||
$this->model = $model;
|
||||
$this->securityHelper = $securityHelper;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
return $this->model && (bool) $this->model->updateAttributes(
|
||||
[
|
||||
'password_hash' => $this->securityHelper->generatePasswordHash($this->password),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
98
src/User/Service/SocialNetworkAccountConnectService.php
Normal file
98
src/User/Service/SocialNetworkAccountConnectService.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\AuthClientInterface;
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Controller\SecurityController;
|
||||
use Da\User\Event\SocialNetworkAuthEvent;
|
||||
use Da\User\Model\SocialNetworkAccount;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\SocialNetworkAccountQuery;
|
||||
use Da\User\Traits\ContainerAwareTrait;
|
||||
use Yii;
|
||||
|
||||
class SocialNetworkAccountConnectService implements ServiceInterface
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
protected $controller;
|
||||
protected $client;
|
||||
protected $socialNetworkAccountQuery;
|
||||
|
||||
/**
|
||||
* SocialNetworkAccountUserLinkService constructor.
|
||||
*
|
||||
* @param SecurityController $controller
|
||||
* @param AuthClientInterface $client
|
||||
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
|
||||
*/
|
||||
public function __construct(
|
||||
SecurityController $controller,
|
||||
AuthClientInterface $client,
|
||||
SocialNetworkAccountQuery $socialNetworkAccountQuery
|
||||
) {
|
||||
$this->controller = $controller;
|
||||
$this->client = $client;
|
||||
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$account = $this->getSocialNetworkAccount();
|
||||
|
||||
$event = $this->make(SocialNetworkAuthEvent::class, [$account, $this->client]);
|
||||
|
||||
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT, $event);
|
||||
|
||||
if ($account && $account->user === null) {
|
||||
/** @var User $user */
|
||||
$user = Yii::$app->user->identity;
|
||||
$account->link('user', $user);
|
||||
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Your account has been connected'));
|
||||
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_CONNECT, $event);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
Yii::$app->session->setFlash(
|
||||
'danger',
|
||||
Yii::t('usuario', 'This account has already been connected to another user')
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getSocialNetworkAccount()
|
||||
{
|
||||
$account = $this->socialNetworkAccountQuery->whereClient($this->client)->one();
|
||||
|
||||
if (null === $account) {
|
||||
$data = $this->client->getUserAttributes();
|
||||
|
||||
$account = $this->make(
|
||||
SocialNetworkAccount::class,
|
||||
[
|
||||
'provider' => $this->client->getId(),
|
||||
'client_id' => $data['id'],
|
||||
'data' => json_encode($data),
|
||||
]
|
||||
);
|
||||
|
||||
if ($account->save(false)) {
|
||||
return $account;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
139
src/User/Service/SocialNetworkAuthenticateService.php
Normal file
139
src/User/Service/SocialNetworkAuthenticateService.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\AuthClientInterface;
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Controller\SecurityController;
|
||||
use Da\User\Event\SocialNetworkAuthEvent;
|
||||
use Da\User\Factory\MailFactory;
|
||||
use Da\User\Model\SocialNetworkAccount;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Query\SocialNetworkAccountQuery;
|
||||
use Da\User\Query\UserQuery;
|
||||
use Yii;
|
||||
use yii\authclient\AuthAction;
|
||||
use yii\helpers\Url;
|
||||
|
||||
class SocialNetworkAuthenticateService implements ServiceInterface
|
||||
{
|
||||
protected $controller;
|
||||
protected $authAction;
|
||||
protected $client;
|
||||
protected $socialNetworkAccountQuery;
|
||||
protected $userQuery;
|
||||
|
||||
public function __construct(
|
||||
SecurityController $controller,
|
||||
AuthAction $authAction,
|
||||
AuthClientInterface $client,
|
||||
SocialNetworkAccountQuery $socialNetworkAccountQuery,
|
||||
UserQuery $userQuery
|
||||
) {
|
||||
$this->controller = $controller;
|
||||
$this->authAction = $authAction;
|
||||
$this->client = $client;
|
||||
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
|
||||
$this->userQuery = $userQuery;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$account = $this->socialNetworkAccountQuery->whereClient($this->client)->one();
|
||||
if (!$this->controller->module->enableRegistration && ($account === null || $account->user === null)) {
|
||||
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Registration on this website is disabled'));
|
||||
$this->authAction->setSuccessUrl(Url::to(['/usr/security/login']));
|
||||
|
||||
return false;
|
||||
}
|
||||
if ($account === null) {
|
||||
$account = $this->createAccount();
|
||||
if (!$account) {
|
||||
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Unable to create an account.'));
|
||||
$this->authAction->setSuccessUrl(Url::to(['/usr/security/login']));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$event = Yii::createObject(SocialNetworkAuthEvent::class, [$this->client]);
|
||||
|
||||
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_AUTHENTICATE, $event);
|
||||
if ($account->user instanceof User) {
|
||||
if ($account->user->getIsBlocked()) {
|
||||
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your account has been blocked.'));
|
||||
$this->authAction->setSuccessUrl(Url::to(['/user/security/login']));
|
||||
} else {
|
||||
Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifeSpan);
|
||||
$this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl());
|
||||
}
|
||||
} else {
|
||||
$this->authAction->setSuccessUrl($account->getConnectionUrl());
|
||||
}
|
||||
|
||||
$this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_AUTHENTICATE, $event);
|
||||
}
|
||||
|
||||
protected function createAccount()
|
||||
{
|
||||
$data = $this->client->getUserAttributes();
|
||||
|
||||
/** @var SocialNetworkAccount $account */
|
||||
$account = $this->controller->make(
|
||||
SocialNetworkAccount::class,
|
||||
[
|
||||
'provider' => $this->client->getId(),
|
||||
'client_id' => $data['id'],
|
||||
'data' => json_encode($data),
|
||||
'username' => $this->client->getUserName(),
|
||||
'email' => $this->client->getEmail(),
|
||||
]
|
||||
);
|
||||
|
||||
if (($user = $this->getUser($account)) instanceof User) {
|
||||
$account->user_id = $user->id;
|
||||
$account->save(false);
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function getUser(SocialNetworkAccount $account)
|
||||
{
|
||||
$user = $this->userQuery->whereEmail($account->email)->one();
|
||||
if (null !== $user) {
|
||||
return $user;
|
||||
}
|
||||
/** @var User $user */
|
||||
$user = $this->controller->make(
|
||||
User::class,
|
||||
[
|
||||
'scenario' => 'connect',
|
||||
'username' => $account->username,
|
||||
'email' => $account->email,
|
||||
]
|
||||
);
|
||||
|
||||
if (!$user->validate(['email'])) {
|
||||
$user->email = null;
|
||||
}
|
||||
|
||||
if (!$user->validate(['username'])) {
|
||||
$user->username = null;
|
||||
}
|
||||
|
||||
$mailService = MailFactory::makeWelcomeMailerService($user);
|
||||
|
||||
return $this->controller->make(UserCreateService::class, [$user, $mailService])->run() ? $user : false;
|
||||
}
|
||||
}
|
||||
52
src/User/Service/UpdateAuthAssignmentsService.php
Normal file
52
src/User/Service/UpdateAuthAssignmentsService.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Model\Assignment;
|
||||
use Da\User\Traits\AuthManagerAwareTrait;
|
||||
|
||||
class UpdateAuthAssignmentsService implements ServiceInterface
|
||||
{
|
||||
use AuthManagerAwareTrait;
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(Assignment $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
if ($this->model->validate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($this->model->items)) {
|
||||
$this->model->items = [];
|
||||
}
|
||||
|
||||
$assignedItems = $this->getAuthManager()->getItemsByUser($this->model->user_id);
|
||||
$assignedItemsNames = array_keys($assignedItems);
|
||||
|
||||
foreach (array_diff($assignedItemsNames, $this->model->items) as $item) {
|
||||
$this->model->getAuthManager()->revoke($assignedItems[$item], $this->model->user_id);
|
||||
}
|
||||
|
||||
foreach (array_diff($this->model->items, $assignedItemsNames) as $item) {
|
||||
$this->getAuthManager()->assign($this->getAuthManager()->getItem($item), $this->model->user_id);
|
||||
}
|
||||
|
||||
return $this->model->updated = true;
|
||||
}
|
||||
}
|
||||
55
src/User/Service/UserBlockService.php
Normal file
55
src/User/Service/UserBlockService.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Controller\AdminController;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
|
||||
class UserBlockService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $event;
|
||||
protected $controller;
|
||||
protected $securityHelper;
|
||||
|
||||
public function __construct(
|
||||
User $model,
|
||||
UserEvent $event,
|
||||
AdminController $controller,
|
||||
SecurityHelper $securityHelper
|
||||
) {
|
||||
$this->model = $model;
|
||||
$this->event = $event;
|
||||
$this->controller = $controller;
|
||||
$this->securityHelper = $securityHelper;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
if ($this->model->getIsBlocked()) {
|
||||
$this->controller->trigger(UserEvent::EVENT_BEFORE_UNBLOCK, $this->event);
|
||||
$result = (bool) $this->model->updateAttributes(['blocked_at' => null]);
|
||||
$this->controller->trigger(UserEvent::EVENT_AFTER_UNBLOCK, $this->event);
|
||||
} else {
|
||||
$this->controller->trigger(UserEvent::EVENT_BEFORE_BLOCK, $this->event);
|
||||
$result = (bool) $this->model->updateAttributes(
|
||||
['blocked_at' => time(), 'auth_key' => $this->securityHelper->generateRandomString()]
|
||||
);
|
||||
$this->controller->trigger(UserEvent::EVENT_AFTER_BLOCK, $this->event);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
38
src/User/Service/UserConfirmationService.php
Normal file
38
src/User/Service/UserConfirmationService.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Model\User;
|
||||
|
||||
class UserConfirmationService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
|
||||
public function __construct(User $model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$this->model->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION);
|
||||
if ((bool) $this->model->updateAttributes(['confirmed_at' => time()])) {
|
||||
$this->model->trigger(UserEvent::EVENT_AFTER_CONFIRMATION);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
77
src/User/Service/UserCreateService.php
Normal file
77
src/User/Service/UserCreateService.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
use yii\base\InvalidCallException;
|
||||
use Exception;
|
||||
use yii\log\Logger;
|
||||
|
||||
class UserCreateService implements ServiceInterface
|
||||
{
|
||||
protected $model;
|
||||
protected $securityHelper;
|
||||
protected $mailService;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper, Logger $logger)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->mailService = $mailService;
|
||||
$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);
|
||||
|
||||
$model->trigger(UserEvent::EVENT_BEFORE_CREATE);
|
||||
|
||||
if (!$model->save()) {
|
||||
$transaction->rollBack();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$model->trigger(UserEvent::EVENT_AFTER_CREATE);
|
||||
|
||||
$this->mailService->run();
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
$this->logger->log($e->getMessage(), Logger::LEVEL_ERROR);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/User/Service/UserRegisterService.php
Normal file
86
src/User/Service/UserRegisterService.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?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\Service;
|
||||
|
||||
use Da\User\Contracts\ServiceInterface;
|
||||
use Da\User\Event\UserEvent;
|
||||
use Da\User\Factory\TokenFactory;
|
||||
use Da\User\Helper\SecurityHelper;
|
||||
use Da\User\Model\User;
|
||||
use Da\User\Traits\ModuleAwareTrait;
|
||||
use Exception;
|
||||
use yii\base\InvalidCallException;
|
||||
use yii\log\Logger;
|
||||
|
||||
class UserRegisterService implements ServiceInterface
|
||||
{
|
||||
use ModuleAwareTrait;
|
||||
|
||||
protected $model;
|
||||
protected $securityHelper;
|
||||
protected $mailService;
|
||||
protected $logger;
|
||||
|
||||
public function __construct(User $model, MailService $mailService, SecurityHelper $securityHelper, Logger $logger)
|
||||
{
|
||||
$this->model = $model;
|
||||
$this->mailService = $mailService;
|
||||
$this->securityHelper = $securityHelper;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
$model = $this->model;
|
||||
|
||||
if ($model->getIsNewRecord() === false) {
|
||||
throw new InvalidCallException('Cannot register user from an existing one.');
|
||||
}
|
||||
|
||||
$transaction = $model->getDb()->beginTransaction();
|
||||
|
||||
try {
|
||||
$model->confirmed_at = $this->getModule()->enableEmailConfirmation ? null : time();
|
||||
$model->password = $this->getModule()->generatePasswords
|
||||
? $this->securityHelper->generatePassword(8)
|
||||
: $model->password;
|
||||
|
||||
$model->trigger(UserEvent::EVENT_BEFORE_REGISTER);
|
||||
|
||||
if (!$model->save()) {
|
||||
$transaction->rollBack();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->getModule()->enableEmailConfirmation) {
|
||||
$token = TokenFactory::makeConfirmationToken($model->id);
|
||||
}
|
||||
|
||||
if (isset($token)) {
|
||||
$this->mailService->setViewParam('token', $token);
|
||||
}
|
||||
$this->mailService->run();
|
||||
|
||||
$model->trigger(UserEvent::EVENT_AFTER_REGISTER);
|
||||
|
||||
$transaction->commit();
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$transaction->rollBack();
|
||||
$this->logger->log($e->getMessage(), Logger::LEVEL_WARNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user