Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
resurtm committed Dec 21, 2016
1 parent 05566c0 commit 82dfada
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/README.md
@@ -0,0 +1 @@
**[Documentation index is here!](index.md)**
72 changes: 72 additions & 0 deletions docs/enhancing-and-overriding/how-to-enhance-a-user-model.md
@@ -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.
5 changes: 5 additions & 0 deletions docs/index.md
Expand Up @@ -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
--------------

Expand Down
10 changes: 8 additions & 2 deletions lib/User/Controller/SecurityController.php
Expand Up @@ -19,13 +19,14 @@
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
{
Expand Down Expand Up @@ -113,6 +114,11 @@ public function actionLogin()
/** @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()) {
Expand Down
5 changes: 3 additions & 2 deletions lib/User/Model/User.php
Expand Up @@ -28,7 +28,7 @@
*
* @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
Expand Down Expand Up @@ -224,7 +224,8 @@ public function getIsAdmin()
}

/**
* @return bool
* Returns whether user account has been confirmed or not.
* @return bool whether user account has been confirmed or not
*/
public function getIsConfirmed()
{
Expand Down

0 comments on commit 82dfada

Please sign in to comment.