update folder location

This commit is contained in:
Antonio Ramirez
2017-01-11 21:31:42 +01:00
parent 1cb60f0740
commit 13255a5117
167 changed files with 4770 additions and 8 deletions

View File

@ -0,0 +1,174 @@
<?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\Controller;
use Da\User\Filter\AccessRuleFilter;
use Da\User\Helper\AuthHelper;
use Da\User\Model\AbstractAuthItem;
use Da\User\Module;
use Da\User\Service\AuthItemEditionService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
abstract class AbstractAuthItemController extends Controller
{
use ContainerAwareTrait;
protected $modelClass;
protected $searchModelClass;
protected $authHelper;
/**
* AbstractAuthItemController constructor.
*
* @param string $id
* @param Module $module
* @param AuthHelper $authHelper
* @param array $config
*/
public function __construct($id, Module $module, AuthHelper $authHelper, array $config = [])
{
$this->authHelper = $authHelper;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'ruleConfig' => [
'class' => AccessRuleFilter::className(),
],
'rules' => [
[
'allow' => true,
'roles' => ['admin'],
],
],
],
];
}
public function actionIndex()
{
$searchModel = $this->make($this->getSearchModelClass());
return $this->render(
'index',
[
'searchModel' => $searchModel,
'dataProvider' => $searchModel->search(Yii::$app->request->get()),
]
);
}
public function actionCreate()
{
/** @var AbstractAuthItem $model */
$model = $this->make($this->getModelClass(), [], ['scenario' => 'create']);
$this->make(AjaxRequestModelValidator::class, [$model])->validate();
if ($model->load(Yii::$app->request->post())) {
if ($this->make(AuthItemEditionService::class, [$model])->run()) {
Yii::$app
->getSession()
->setFlash('success', Yii::t('usuario', 'Authorization item successfully created.'));
return $this->redirect(['index']);
} else {
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to create authorization item.'));
}
}
return $this->render(
'create',
[
'model' => $model,
'unassignedItems' => $this->authHelper->getUnassignedItems($model),
]
);
}
public function actionUpdate($name)
{
$authItem = $this->getItem($name);
/** @var AbstractAuthItem $model */
$model = $this->make($this->getModelClass(), [], ['scenario' => 'update', 'item' => $authItem]);
$this->make(AjaxRequestModelValidator::class, [$model])->validate();
if ($model->load(Yii::$app->request->post())) {
if ($this->make(AuthItemEditionService::class, [$model])->run()) {
Yii::$app
->getSession()
->setFlash('success', Yii::t('usuario', 'Authorization item successfully updated.'));
return $this->redirect(['index']);
} else {
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to update authorization item.'));
}
}
return $this->render(
'update',
[
'model' => $model,
'unassignedItems' => $this->authHelper->getUnassignedItems($model),
]
);
}
public function actionDelete($name)
{
$item = $this->getItem($name);
if ($this->authHelper->remove($item)) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Authorization item successfully removed.'));
} else {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Unable to remove authorization item.'));
}
return $this->redirect(['index']);
}
/**
* The fully qualified class name of the model.
*
* @return string
*/
abstract protected function getModelClass();
/**
* The fully qualified class name of the search model.
*
* @return string
*/
abstract protected function getSearchModelClass();
/**
* Returns the an auth item.
*
* @param string $name
*
* @return \yii\rbac\Role|\yii\rbac\Permission|\yii\rbac\Rule
*/
abstract protected function getItem($name);
}

View File

@ -0,0 +1,286 @@
<?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\Controller;
use Da\User\Event\UserEvent;
use Da\User\Factory\MailFactory;
use Da\User\Filter\AccessRuleFilter;
use Da\User\Model\Profile;
use Da\User\Model\User;
use Da\User\Query\UserQuery;
use Da\User\Search\UserSearch;
use Da\User\Service\UserBlockService;
use Da\User\Service\UserConfirmationService;
use Da\User\Service\UserCreateService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use yii\base\Module;
use yii\db\ActiveRecord;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
class AdminController extends Controller
{
use ContainerAwareTrait;
/**
* @var UserQuery
*/
protected $userQuery;
/**
* AdminController constructor.
*
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param array $config
*/
public function __construct($id, Module $module, UserQuery $userQuery, array $config = [])
{
$this->userQuery = $userQuery;
parent::__construct($id, $module, $config);
}
/**
* @param \yii\base\Action $action
*
* @return bool
*/
public function beforeAction($action)
{
if (in_array($action->id, ['index', 'update', 'update-profile', 'info', 'assignments'], true)) {
Url::remember('', 'actions-redirect');
}
return parent::beforeAction($action);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
'delete' => ['post'],
'confirm' => ['post'],
'block' => ['post'],
],
],
'access' => [
'class' => AccessControl::class,
'ruleConfig' => [
'class' => AccessRuleFilter::class,
],
'rules' => [
[
'allow' => true,
'roles' => ['admin'],
],
],
],
];
}
public function actionIndex()
{
$searchModel = $this->make(UserSearch::class);
$dataProvider = $searchModel->search(Yii::$app->request->get());
return $this->render(
'index',
[
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
]
);
}
public function actionCreate()
{
/** @var User $user */
$user = $this->make(User::class, [], ['scenario' => 'create']);
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post())) {
$this->trigger(UserEvent::EVENT_BEFORE_CREATE, $event);
$mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User has been created'));
$this->trigger(UserEvent::EVENT_AFTER_CREATE, $event);
return $this->redirect(['update', 'id' => $user->id]);
}
}
return $this->render('create', ['user' => $user]);
}
public function actionUpdate($id)
{
$user = $this->userQuery->where(['id' => $id])->one();
$user->setScenario('update');
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post())) {
$this->trigger(ActiveRecord::EVENT_BEFORE_UPDATE, $event);
if ($user->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Account details have been updated'));
$this->trigger(ActiveRecord::EVENT_AFTER_UPDATE, $event);
return $this->refresh();
}
}
return $this->render('_account', ['user' => $user]);
}
public function actionUpdateProfile($id)
{
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
$profile = $user->profile;
if ($profile === null) {
$profile = $this->make(Profile::class);
$profile->link($user);
}
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($profile->load(Yii::$app->request->post())) {
if ($profile->save()) {
$this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Profile details have been updated'));
$this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);
return $this->refresh();
}
}
return $this->render(
'_profile',
[
'user' => $user,
'profile' => $profile,
]
);
}
public function actionInfo($id)
{
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
return $this->render(
'_info',
[
'user' => $user,
]
);
}
public function actionAssignments($id)
{
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
return $this->render(
'_assignments',
[
'user' => $user,
'params' => Yii::$app->request->post(),
]
);
}
public function actionConfirm($id)
{
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
if ($this->make(UserConfirmationService::class, [$user])->run()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User has been confirmed'));
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
} else {
Yii::$app->getSession()->setFlash('warning', Yii::t('usuario', 'Unable to confirm user. Please, try again.'));
}
return $this->redirect(Url::previous('actions-redirect'));
}
public function actionDelete($id)
{
if ($id === Yii::$app->user->getId()) {
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'You cannot remove your own account'));
} else {
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$this->trigger(ActiveRecord::EVENT_BEFORE_DELETE, $event);
if ($user->delete()) {
Yii::$app->getSession()->setFlash('success', \Yii::t('usuario', 'User has been deleted'));
$this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event);
} else {
Yii::$app->getSession()->setFlash(
'warning',
Yii::t('usuario', 'Unable to delete user. Please, try again later.')
);
}
}
return $this->redirect(['index']);
}
public function actionBlock($id)
{
if ($id === Yii::$app->user->getId()) {
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'You cannot remove your own account'));
} else {
/** @var User $user */
$user = $this->userQuery->where(['id' => $id])->one();
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
if ($this->make(UserBlockService::class, [$user, $event, $this])->run()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'User block status has been updated.'));
} else {
Yii::$app->getSession()->setFlash('danger', Yii::t('usuario', 'Unable to update block status.'));
}
}
return $this->redirect(Url::previous('actions-redirect'));
}
}

View File

@ -0,0 +1,49 @@
<?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\Controller;
use Da\User\Model\Permission;
use Da\User\Search\PermissionSearch;
use yii\web\NotFoundHttpException;
class PermissionController extends AbstractAuthItemController
{
/**
* {@inheritdoc}
*/
protected function getModelClass()
{
return Permission::class;
}
/**
* {@inheritdoc}
*/
protected function getSearchModelClass()
{
return PermissionSearch::class;
}
/**
* {@inheritdoc}
*/
protected function getItem($name)
{
$authItem = $this->authHelper->getPermission($name);
if ($authItem !== null) {
return $authItem;
}
throw new NotFoundHttpException();
}
}

View File

@ -0,0 +1,83 @@
<?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\Controller;
use Da\User\Query\ProfileQuery;
use yii\base\Module;
use yii\filters\AccessControl;
use yii\web\Controller;
use Yii;
use yii\web\NotFoundHttpException;
class ProfileController extends Controller
{
protected $profileQuery;
/**
* ProfileController constructor.
*
* @param string $id
* @param Module $module
* @param ProfileQuery $profileQuery
* @param array $config
*/
public function __construct($id, Module $module, ProfileQuery $profileQuery, array $config = [])
{
$this->profileQuery = $profileQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index'],
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['show'],
'roles' => ['?', '@'],
],
],
],
];
}
public function actionIndex()
{
return $this->redirect(['show', 'id' => Yii::$app->user->getId()]);
}
public function actionShow($id)
{
$profile = $this->profileQuery->whereId($id)->one();
if ($profile === null) {
throw new NotFoundHttpException();
}
return $this->render(
'show',
[
'profile' => $profile,
]
);
}
}

View File

@ -0,0 +1,173 @@
<?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\Controller;
use Da\User\Event\FormEvent;
use Da\User\Event\ResetPasswordEvent;
use Da\User\Factory\MailFactory;
use Da\User\Form\RecoveryForm;
use Da\User\Model\Token;
use Da\User\Query\TokenQuery;
use Da\User\Query\UserQuery;
use Da\User\Service\PasswordRecoveryService;
use Da\User\Service\ResetPasswordService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use Da\User\Module;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class RecoveryController extends Controller
{
use ContainerAwareTrait;
protected $userQuery;
protected $tokenQuery;
/**
* RecoveryController constructor.
*
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param TokenQuery $tokenQuery
* @param array $config
*/
public function __construct($id, Module $module, UserQuery $userQuery, TokenQuery $tokenQuery, array $config = [])
{
$this->userQuery = $userQuery;
$this->tokenQuery = $tokenQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['request', 'reset'],
'roles' => ['?'],
],
],
],
];
}
/**
* Displays / handles user password recovery request.
*
* @return string
*
* @throws NotFoundHttpException
*/
public function actionRequest()
{
if (!$this->module->allowPasswordRecovery) {
throw new NotFoundHttpException();
}
/** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_REQUEST]);
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, $form)->validate();
if ($form->load(Yii::$app->request->post())) {
$this->trigger(FormEvent::EVENT_BEFORE_REQUEST, $event);
$mailService = MailFactory::makeRecoveryMailerService($form->email);
if ($this->make(PasswordRecoveryService::class, [$form->email, $mailService])->run()) {
$this->trigger(FormEvent::EVENT_AFTER_REQUEST, $event);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Recovery message sent'),
'module' => $this->module,
]
);
}
}
return $this->render('request', ['model' => $form]);
}
/**
* Displays / handles user password reset.
*
* @param $id
* @param $code
*
* @return string
*
* @throws NotFoundHttpException
*/
public function actionReset($id, $code)
{
if (!$this->module->allowPasswordRecovery) {
throw new NotFoundHttpException();
}
/** @var Token $token */
$token = $this->tokenQuery->whereUserId($id)->whereCode($code)->whereIsRecoveryType()->one();
/** @var ResetPasswordEvent $event */
$event = $this->make(ResetPasswordEvent::class, [$token]);
$this->trigger(ResetPasswordEvent::EVENT_BEFORE_TOKEN_VALIDATE, $event);
if ($token === null || $token->getIsExpired() || $token->user === null) {
Yii::$app->session->setFlash(
'danger',
Yii::t('usuario', 'Recovery link is invalid or expired. Please try requesting a new one.')
);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Invalid or expired link'),
'module' => $this->module,
]
);
}
/** @var RecoveryForm $form */
$form = $this->make(RecoveryForm::class, [], ['scenario' => RecoveryForm::SCENARIO_RESET]);
$event = $event->updateForm($form);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->getRequest()->post())) {
if ($this->make(ResetPasswordService::class, [$form->password, $token->user])->run()) {
$this->trigger(ResetPasswordEvent::EVENT_AFTER_RESET, $event);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Password has been changed'),
'module' => $this->module,
]
);
}
}
return $this->render('reset', ['model' => $form]);
}
}

View File

@ -0,0 +1,267 @@
<?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\Controller;
use Da\User\Event\FormEvent;
use Da\User\Event\SocialNetworkConnectEvent;
use Da\User\Event\UserEvent;
use Da\User\Factory\MailFactory;
use Da\User\Form\RegistrationForm;
use Da\User\Form\ResendForm;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery;
use Da\User\Service\AccountConfirmationService;
use Da\User\Service\ResendConfirmationService;
use Da\User\Service\UserConfirmationService;
use Da\User\Service\UserCreateService;
use Da\User\Service\UserRegisterService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use Yii;
use yii\base\Module;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
class RegistrationController extends Controller
{
use ContainerAwareTrait;
protected $userQuery;
protected $socialNetworkAccountQuery;
/**
* RegistrationController constructor.
*
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
*/
public function __construct(
$id,
Module $module,
UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config = []
) {
$this->userQuery = $userQuery;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['register', 'connect'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['confirm', 'resend'],
'roles' => ['?', '@'],
],
],
],
];
}
public function actionRegister()
{
if (!$this->module->enableRegistration) {
throw new NotFoundHttpException();
}
/** @var RegistrationForm $form */
$form = $this->make(RegistrationForm::class);
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
$this->trigger(UserEvent::EVENT_BEFORE_REGISTER, $event);
$user = $this->make(User::class, [], $form->attributes);
$user->setScenario('register');
$mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserRegisterService::class, [$user, $mailService])->run()) {
Yii::$app->session->setFlash(
'info',
Yii::t(
'usuario',
'Your account has been created and a message with further instructions has been sent to your email'
)
);
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Your account has been created'),
'module' => $this->module,
]
);
}
}
return $this->render(
'register',
[
'model' => $form,
'module' => $this->module,
]
);
}
public function actionConnect($code)
{
/** @var SocialNetworkAccount $account */
$account = $this->socialNetworkAccountQuery->whereCode($code)->one();
if ($account === null || $account->getIsConnected()) {
throw new NotFoundHttpException();
}
/** @var User $user */
$user = $this->make(
User::class,
['scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]
);
$event = $this->make(SocialNetworkConnectEvent::class, [$user, $account]);
$this->make(AjaxRequestModelValidator::class, [$user])->validate();
if ($user->load(Yii::$app->request->post())) {
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_CONNECT, $event);
$mailService = MailFactory::makeWelcomeMailerService($user);
if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
$account->connect($user);
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_CONNECT, $event);
Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
return $this->goBack();
}
}
return $this->render(
'connect',
[
'model' => $user,
'account' => $account,
]
);
}
public function actionConfirm($id, $code)
{
$user = $this->userQuery->whereId($id)->one();
if ($user === null || $this->module->enableEmailConfirmation === false) {
throw new NotFoundHttpException();
}
/** @var UserEvent $event */
$event = $this->make(UserEvent::class, [$user]);
$userConfirmationService = $this->make(UserConfirmationService::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
if ($this->make(AccountConfirmationService::class, [$code, $user, $userConfirmationService])->run()) {
Yii::$app->user->login($user, $this->module->rememberLoginLifespan);
Yii::$app->session->setFlash('success', Yii::t('usuario', 'Thank you, registration is now complete.'));
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
} else {
Yii::$app->session->setFlash(
'danger',
Yii::t('usuario', 'The confirmation link is invalid or expired. Please try requesting a new one.')
);
}
return $this->render(
'/shared/message',
[
'title' => Yii::t('usuario', 'Account confirmation'),
'module' => $this->module,
]
);
}
public function actionResend()
{
if ($this->module->enableEmailConfirmation === false) {
throw new NotFoundHttpException();
}
/** @var ResendForm $form */
$form = $this->make(ResendForm::class);
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
/** @var User $user */
$user = $this->userQuery->whereEmail($form->email)->one();
$success = true;
if ($user !== null) {
$this->trigger(FormEvent::EVENT_BEFORE_RESEND, $event);
$mailService = MailFactory::makeConfirmationMailerService($user);
if ($success = $this->make(ResendConfirmationService::class, [$user, $mailService])->run()) {
$this->trigger(FormEvent::EVENT_AFTER_RESEND, $event);
Yii::$app->session->setFlash(
'info',
Yii::t(
'usuario',
'A message has been sent to your email address. '.
'It contains a confirmation link that you must click to complete registration.'
)
);
}
}
if ($user === null || $success === false) {
Yii::$app->session->setFlash(
'danger',
Yii::t(
'usuario',
'We couldn\'t re-send the mail to confirm your address. '.
'Please, verify is the correct email or if it has been confirmed already.'
)
);
}
return $this->render('/shared/message', [
'title' => $success
? Yii::t('usuario', 'A new confirmation link has been sent')
: Yii::t('usuario', 'Unable to send confirmation link'),
'module' => $this->module,
]);
}
return $this->render(
'resend',
[
'model' => $form,
]
);
}
}

View File

@ -0,0 +1,49 @@
<?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\Controller;
use Da\User\Model\Role;
use Da\User\Search\RoleSearch;
use yii\web\NotFoundHttpException;
class RoleController extends AbstractAuthItemController
{
/**
* {@inheritdoc}
*/
protected function getModelClass()
{
return Role::class;
}
/**
* {@inheritdoc}
*/
protected function getSearchModelClass()
{
return RoleSearch::class;
}
/**
* {@inheritdoc}
*/
protected function getItem($name)
{
$authItem = $this->authHelper->getRole($name);
if ($authItem !== null) {
return $authItem;
}
throw new NotFoundHttpException();
}
}

View 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\Controller;
use Da\User\Contracts\AuthClientInterface;
use Da\User\Event\FormEvent;
use Da\User\Event\UserEvent;
use Da\User\Form\LoginForm;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Service\SocialNetworkAccountConnectService;
use Da\User\Service\SocialNetworkAuthenticateService;
use Da\User\Traits\ContainerAwareTrait;
use Yii;
use yii\authclient\AuthAction;
use yii\base\Module;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\Response;
use yii\widgets\ActiveForm;
class SecurityController extends Controller
{
use ContainerAwareTrait;
protected $socialNetworkAccountQuery;
/**
* SecurityController constructor.
*
* @param string $id
* @param Module $module
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
*/
public function __construct(
$id,
Module $module,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config = []
) {
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['login', 'auth', 'blocked'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['login', 'auth', 'logout'],
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* {@inheritdoc}
*/
public function actions()
{
return [
'auth' => [
'class' => AuthAction::className(),
// if user is not logged in, will try to log him in, otherwise
// will try to connect social account to user.
'successCallback' => Yii::$app->user->isGuest
? [$this, 'authenticate']
: [$this, 'connect'],
],
];
}
/**
* Controller action responsible for handling login page and actions.
* @return string|Response
*/
public function actionLogin()
{
if (!Yii::$app->user->getIsGuest()) {
return $this->goHome();
}
/** @var LoginForm $form */
$form = $this->make(LoginForm::class);
/** @var FormEvent $event */
$event = $this->make(FormEvent::class, [$form]);
if (Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($form);
}
if ($form->load(Yii::$app->request->post())) {
$this->trigger(FormEvent::EVENT_BEFORE_LOGIN, $event);
if ($form->login()) {
$this->trigger(FormEvent::EVENT_AFTER_LOGIN, $event);
return $this->goBack();
}
}
return $this->render(
'login',
[
'model' => $form,
'module' => $this->module,
]
);
}
public function actionLogout()
{
$event = $this->make(UserEvent::class, [Yii::$app->getUser()->getIdentity()]);
$this->trigger(UserEvent::EVENT_BEFORE_LOGOUT, $event);
if (Yii::$app->getUser()->logout()) {
$this->trigger(UserEvent::EVENT_AFTER_LOGOUT, $event);
}
return $this->goHome();
}
public function authenticate(AuthClientInterface $client)
{
$this->make(SocialNetworkAuthenticateService::class, [$this, $this->action, $client])->run();
}
public function connect(AuthClientInterface $client)
{
if (Yii::$app->user->isGuest) {
Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Something went wrong'));
return;
}
$this->make(SocialNetworkAccountConnectService::class, [$this, $client])->run();
}
}

View File

@ -0,0 +1,229 @@
<?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\Controller;
use Da\User\Contracts\MailChangeStrategyInterface;
use Da\User\Event\FormEvent;
use Da\User\Event\ProfileEvent;
use Da\User\Event\SocialNetworkConnectEvent;
use Da\User\Event\UserEvent;
use Da\User\Form\SettingsForm;
use Da\User\Model\Profile;
use Da\User\Model\SocialNetworkAccount;
use Da\User\Model\User;
use Da\User\Module;
use Da\User\Query\ProfileQuery;
use Da\User\Query\SocialNetworkAccountQuery;
use Da\User\Query\UserQuery;
use Da\User\Service\EmailChangeService;
use Da\User\Traits\ContainerAwareTrait;
use Da\User\Validator\AjaxRequestModelValidator;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
class SettingsController extends Controller
{
use ContainerAwareTrait;
protected $profileQuery;
protected $userQuery;
protected $socialNetworkAccountQuery;
/**
* {@inheritdoc}
*/
public $defaultAction = 'profile';
/**
* SettingsController constructor.
*
* @param string $id
* @param Module $module
* @param ProfileQuery $profileQuery
* @param UserQuery $userQuery
* @param SocialNetworkAccountQuery $socialNetworkAccountQuery
* @param array $config
*/
public function __construct(
$id,
Module $module,
ProfileQuery $profileQuery,
UserQuery $userQuery,
SocialNetworkAccountQuery $socialNetworkAccountQuery,
array $config = []
) {
$this->profileQuery = $profileQuery;
$this->userQuery = $userQuery;
$this->socialNetworkAccountQuery = $socialNetworkAccountQuery;
parent::__construct($id, $module, $config);
}
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'disconnect' => ['post'],
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['profile', 'account', 'networks', 'disconnect', 'delete'],
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['confirm'],
'roles' => ['?', '@'],
],
],
],
];
}
public function actionProfile()
{
$profile = $this->profileQuery->whereId(Yii::$app->user->identity->getId())->one();
if ($profile === null) {
$profile = $this->make(Profile::class);
$profile->link('user', Yii::$app->user->identity);
}
$event = $this->make(ProfileEvent::class, [$profile]);
$this->make(AjaxRequestModelValidator::class, [$profile])->validate();
if ($profile->load(Yii::$app->request->post())) {
$this->trigger(UserEvent::EVENT_BEFORE_PROFILE_UPDATE, $event);
if ($profile->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Your profile has been updated'));
$this->trigger(UserEvent::EVENT_AFTER_PROFILE_UPDATE, $event);
return $this->refresh();
}
}
return $this->render(
'profile',
[
'model' => $profile,
]
);
}
public function actionAccount()
{
/** @var SettingsForm $form */
$form = $this->make(SettingsForm::class);
$event = $this->make(FormEvent::class, [$form]);
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
if ($form->load(Yii::$app->request->post())) {
$this->trigger(UserEvent::EVENT_BEFORE_ACCOUNT_UPDATE, $event);
if ($form->save()) {
Yii::$app->getSession()->setFlash('success', Yii::t('usuario', 'Your account details have been updated'));
$this->trigger(UserEvent::EVENT_AFTER_ACCOUNT_UPDATE, $event);
return $this->refresh();
}
}
return $this->render(
'account',
[
'model' => $form,
]
);
}
public function actionConfirm($id, $code)
{
$user = $this->userQuery->whereId($id)->one();
if ($user === null || $this->module->emailChangeStrategy == MailChangeStrategyInterface::TYPE_INSECURE) {
throw new NotFoundHttpException();
}
$event = $this->make(UserEvent::class, [$user]);
$this->trigger(UserEvent::EVENT_BEFORE_CONFIRMATION, $event);
if ($this->make(EmailChangeService::class, [$code, $user])->run()) {
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
}
return $this->redirect(['account']);
}
public function actionNetworks()
{
return $this->render(
'networks',
[
'user' => Yii::$app->user->identity,
]
);
}
public function actionDisconnect($id)
{
/** @var SocialNetworkAccount $account */
$account = $this->socialNetworkAccountQuery->whereId($id)->one();
if ($account === null) {
throw new NotFoundHttpException();
}
if ($account->user_id != Yii::$app->user->id) {
throw new ForbiddenHttpException();
}
$event = $this->make(SocialNetworkConnectEvent::class, [Yii::$app->user->identity, $account]);
$this->trigger(SocialNetworkConnectEvent::EVENT_BEFORE_DISCONNECT, $event);
$account->delete();
$this->trigger(SocialNetworkConnectEvent::EVENT_AFTER_DISCONNECT, $event);
return $this->redirect(['networks']);
}
public function actionDelete()
{
if (!$this->module->allowAccountDelete) {
throw new NotFoundHttpException(\Yii::t('usuario', 'Not found'));
}
/** @var User $user */
$user = Yii::$app->user->identity;
$event = $this->make(UserEvent::class, [$user]);
Yii::$app->user->logout();
$this->trigger(UserEvent::EVENT_BEFORE_DELETE, $event);
$user->delete();
$this->trigger(UserEvent::EVENT_AFTER_DELETE, $event);
Yii::$app->session->setFlash('info', Yii::t('usuario', 'Your account has been completely deleted'));
return $this->goHome();
}
}