wip
This commit is contained in:
@ -0,0 +1 @@
|
||||
**[Documentation index is here!](index.md)**
|
||||
|
||||
72
docs/enhancing-and-overriding/how-to-enhance-a-user-model.md
Normal file
72
docs/enhancing-and-overriding/how-to-enhance-a-user-model.md
Normal file
@ -0,0 +1,72 @@
|
||||
How to Enhance a User Model
|
||||
===========================
|
||||
|
||||
It is obvious that if you started your project development on Yii2 Framework, then your business and domain needs
|
||||
are going to be very customized, and more or less unique. While our extension (and of course Yii2 Framework itself!)
|
||||
provide sensible defaults where it's possible, we encourage and keep in mind user will extend classes.
|
||||
|
||||
Very often you have to override and add your own domain (or application specific code) to your user model. With this
|
||||
extension this is very easy and can be done in a few minutes!
|
||||
|
||||
For the case if you're using [Sidekit Application Template](../installation/sidekit-application-template.md) or
|
||||
[Advanced Application Template](../installation/advanced-application-template.md) create the following class file
|
||||
at the `%PROJECT_DIR%/common/models/User.php` path:
|
||||
|
||||
```php
|
||||
namespace common\models;
|
||||
|
||||
use Da\User\Model\User as BaseUser;
|
||||
|
||||
class User extends BaseUser
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
Then adjust configuration of `yii2-usuario` extension module as follows:
|
||||
|
||||
```php
|
||||
'modules' => [
|
||||
'user' => [
|
||||
'class' => Da\User\Module::class,
|
||||
'classMap' => [
|
||||
'User' => common\models\User::class,
|
||||
],
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
Another way to do that is to use Yii2 Dependency Injection configuration as we extensively use service container
|
||||
feature. This is good approach too:
|
||||
|
||||
```php
|
||||
'container' => [
|
||||
'definitions' => [
|
||||
Da\User\Model\User::class => common\models\User::class,
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
Finally you can now add new methods, properties, and other things to your new `User` model class:
|
||||
|
||||
```php
|
||||
// model
|
||||
class User extends BaseUser
|
||||
{
|
||||
public function addFriend(User $friend)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
// somewhere in controller
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
public function actionAddFriend(int $id)
|
||||
{
|
||||
Yii::$app->user->identity->addFriend(User::findOne($id));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
> This is absolutely good way to extend almost any class of the extension. For more information you could
|
||||
> check `Da\User\Bootstrap` class file to see what you have in your control.
|
||||
@ -22,6 +22,11 @@ Installation
|
||||
- [Advanced Application Template](installation/advanced-application-template.md)
|
||||
- [Basic Application Template](installation/basic-application-template.md)
|
||||
|
||||
Enhancing and Overriding
|
||||
------------------------
|
||||
|
||||
- [How to Enhance a User Model](enhancing-and-overriding/how-to-enhance-a-user-model.md)
|
||||
|
||||
Helpful Guides
|
||||
--------------
|
||||
|
||||
|
||||
@ -19,13 +19,14 @@ 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;
|
||||
use \yii\web\Response;
|
||||
use yii\web\Response;
|
||||
use yii\widgets\ActiveForm;
|
||||
|
||||
class SecurityController extends Controller
|
||||
{
|
||||
@ -113,6 +114,11 @@ class SecurityController extends Controller
|
||||
/** @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()) {
|
||||
|
||||
@ -28,7 +28,7 @@ use yii\web\IdentityInterface;
|
||||
*
|
||||
* @property bool $isAdmin
|
||||
* @property bool $isBlocked
|
||||
* @property bool $isConfirmed
|
||||
* @property bool $isConfirmed whether user account has been confirmed or not
|
||||
*
|
||||
* Database fields:
|
||||
* @property int $id
|
||||
@ -224,7 +224,8 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* Returns whether user account has been confirmed or not.
|
||||
* @return bool whether user account has been confirmed or not
|
||||
*/
|
||||
public function getIsConfirmed()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user