Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit b37d7e1

Browse files
committed
feat: api key generation
1 parent c925683 commit b37d7e1

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

Bootstrap.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace app;
44

5+
use dektrium\user\controllers\RegistrationController;
56
use dektrium\user\events\FormEvent;
67
use yii\base\Application;
78
use yii\base\BootstrapInterface;
8-
use dektrium\user\controllers\RegistrationController;
99
use yii\base\Event;
1010

11-
class Bootstrap implements BootstrapInterface{
11+
class Bootstrap implements BootstrapInterface
12+
{
1213

1314
/**
1415
* Bootstrap method to be called during application bootstrap stage.
@@ -19,9 +20,11 @@ public function bootstrap($app)
1920
Event::on(
2021
RegistrationController::className(),
2122
RegistrationController::EVENT_BEFORE_REGISTER,
22-
function ($event) {
23+
function (FormEvent $event) use ($app) {
2324
// generating api key
24-
25+
$form = $event->getForm();
26+
$form->api_key = $app->security->generateRandomString(16);
27+
$event->setForm($form);
2528
}
2629
);
2730

config/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'class' => 'dektrium\user\Module',
2020
'modelMap' => [
2121
'User' => 'app\models\User',
22+
'RegistrationForm' => 'app\models\user\RegistrationForm',
2223
],
2324
],
2425
'rbac' => 'dektrium\rbac\RbacWebModule',

models/user/RegistrationForm.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace app\models\user;
4+
5+
use dektrium\user\models\RegistrationForm as BaseRegistrationForm;
6+
7+
class RegistrationForm extends BaseRegistrationForm
8+
{
9+
/**
10+
* @var string
11+
*/
12+
public $api_key;
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
public function rules()
18+
{
19+
$rules = parent::rules();
20+
$rules[] = ['api_key', 'required'];
21+
$rules[] = ['api_key', 'string', 'max' => 16];
22+
return $rules;
23+
}
24+
}

0 commit comments

Comments
 (0)