Sfoglia il codice sorgente

api module implementation

Oleg K 4 anni fa
parent
commit
e76ed08b53

BIN
.docs/antares_cab.docx


BIN
.docs/download (1).xls


+ 14 - 14
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "dde014b3cd8dec0e9afd236a4262892b",
+    "content-hash": "6e6516f7396bafc74d186140ab56dc91",
     "packages": [
         {
             "name": "almasaeed2010/adminlte",
@@ -75,7 +75,7 @@
             "version": "3.3.11",
             "source": {
                 "type": "git",
-                "url": "https://github.com/RobinHerbots/Inputmask.git",
+                "url": "git@github.com:RobinHerbots/Inputmask.git",
                 "reference": "5e670ad62f50c738388d4dcec78d2888505ad77b"
             },
             "dist": {
@@ -672,12 +672,12 @@
             "source": {
                 "type": "git",
                 "url": "https://github.com/kartik-v/yii2-widget-select2.git",
-                "reference": "05ed365ea5ed3555646db27dfb9beea79757a054"
+                "reference": "b30936d556639cdecd98bdd9733509a6be5dcc1c"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/kartik-v/yii2-widget-select2/zipball/05ed365ea5ed3555646db27dfb9beea79757a054",
-                "reference": "05ed365ea5ed3555646db27dfb9beea79757a054",
+                "url": "https://api.github.com/repos/kartik-v/yii2-widget-select2/zipball/b30936d556639cdecd98bdd9733509a6be5dcc1c",
+                "reference": "b30936d556639cdecd98bdd9733509a6be5dcc1c",
                 "shasum": ""
             },
             "require": {
@@ -721,7 +721,7 @@
             ],
             "support": {
                 "issues": "https://github.com/kartik-v/yii2-widget-select2/issues",
-                "source": "https://github.com/kartik-v/yii2-widget-select2/tree/master"
+                "source": "https://github.com/kartik-v/yii2-widget-select2/tree/v2.2.1"
             },
             "funding": [
                 {
@@ -729,7 +729,7 @@
                     "type": "open_collective"
                 }
             ],
-            "time": "2020-12-15T14:53:54+00:00"
+            "time": "2021-06-09T07:29:31+00:00"
         },
         {
             "name": "npm-asset/bootstrap",
@@ -3852,16 +3852,16 @@
         },
         {
             "name": "phpunit/phpunit",
-            "version": "8.5.15",
+            "version": "8.5.16",
             "source": {
                 "type": "git",
                 "url": "https://github.com/sebastianbergmann/phpunit.git",
-                "reference": "038d4196d8e8cb405cd5e82cedfe413ad6eef9ef"
+                "reference": "cc66f2fc61296be66c99931a862200e7456b9a01"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/038d4196d8e8cb405cd5e82cedfe413ad6eef9ef",
-                "reference": "038d4196d8e8cb405cd5e82cedfe413ad6eef9ef",
+                "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/cc66f2fc61296be66c99931a862200e7456b9a01",
+                "reference": "cc66f2fc61296be66c99931a862200e7456b9a01",
                 "shasum": ""
             },
             "require": {
@@ -3933,7 +3933,7 @@
             ],
             "support": {
                 "issues": "https://github.com/sebastianbergmann/phpunit/issues",
-                "source": "https://github.com/sebastianbergmann/phpunit/tree/8.5.15"
+                "source": "https://github.com/sebastianbergmann/phpunit/tree/8.5.16"
             },
             "funding": [
                 {
@@ -3945,7 +3945,7 @@
                     "type": "github"
                 }
             ],
-            "time": "2021-03-17T07:27:54+00:00"
+            "time": "2021-06-05T04:46:20+00:00"
         },
         {
             "name": "psr/container",
@@ -6203,7 +6203,7 @@
     "prefer-stable": false,
     "prefer-lowest": false,
     "platform": {
-        "php": ">=7.3"
+        "php": ">=7.2"
     },
     "platform-dev": [],
     "plugin-api-version": "2.0.0"

+ 25 - 1
config/web.php

@@ -18,6 +18,9 @@ $config = [
         'request' => [
             // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
             'cookieValidationKey' => '9F0UJBZKPPoUsJAKuAh9hpvyWdaF1IIb',
+            'parsers' => [
+                'application/json' => 'yii\web\JsonParser',
+            ]
         ],
         'cache' => [
             'class' => 'yii\caching\FileCache',
@@ -58,9 +61,18 @@ $config = [
         'db' => $db,
         'urlManager' => [
             'enablePrettyUrl' => true,
-            'showScriptName' => false,
             'enableStrictParsing' => true,
+            'showScriptName' => false,
             'rules' => [
+                [
+                    'class' => 'yii\rest\UrlRule',
+                    'controller' => ['api/user'],
+                    'except' => [ 'index', 'view', 'delete' ],
+                    'extraPatterns' => [
+                        'POST add' => 'bulk-add',
+                    ],
+                ],
+
                 '/' => 'site/index',
                 'pricing/<action:[\w\-]+>' => 'pricing/<action>',
                 //'user/admin/<action:\w+>' => 'user/admin/<action>',
@@ -72,6 +84,10 @@ $config = [
                 '<alias:logout|login>' => 'user/security/<alias>',
                 //'<alias:logout|login>' => 'user/security/<alias>',
 
+                //'api/<action:\w+>' => 'api/<action>',
+                //'api/<action:[\w\-]+>/<id:\d+>' => 'api/<action>'
+
+
             ],
         ],
         'i18n' => [
@@ -128,6 +144,9 @@ $config = [
                 //'SettingsForm' => 'app\models\user\SettingsForm',
             ],*/
         ],
+        'api' => [
+            'class' => 'app\modules\api\Module',
+        ],
     ],
     'params' => $params,
     'on beforeAction' => function ($event) {
@@ -152,6 +171,11 @@ $config = [
                 'roles' => ['@'],
             ],
             [
+                'controllers' => ['api/user'],
+                'allow' => true,
+                'roles' => ['?'],
+            ],
+            [
                 'controllers' => ['user/admin'],
                 'allow' => true,
                 'roles' => ['admin'],

+ 5 - 1
messages/ru/user.php

@@ -1,4 +1,8 @@
 <?php
 return [
-
+    'Nomer Dogovora' => 'Номер договора',
+    'Fio' => 'ФИО',
+    'Company name' => 'Название компании',
+    'Phone' => 'Телефон',
+    'Address' => 'Адрес',
 ];

+ 34 - 0
migrations/m210624_065654_user_table_fields.php

@@ -0,0 +1,34 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Class m210624_065654_user_table_fields
+ */
+class m210624_065654_user_table_fields extends Migration
+{
+    public function safeUp()
+    {
+        $this->dropColumn('{{%user}}', 'type');
+
+        $this->addColumn('{{%user}}', 'uuid', $this->integer(11)->notNull()->unique()->after('id'));
+        $this->addColumn('{{%user}}', 'fio', $this->string(255)->notNull()->after('username'));
+        $this->addColumn('{{%user}}', 'cname', $this->string(255)->null()->after('fio'));
+        $this->addColumn('{{%user}}', 'type', $this->string(32)->notNull()->after('cname'));
+        $this->addColumn('{{%user}}', 'phone', $this->string(32)->notNull()->after('email'));
+        $this->addColumn('{{%user}}', 'address', $this->text()->null()->after('phone'));
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $this->dropColumn('{{%user}}', 'uuid');
+        $this->dropColumn('{{%user}}', 'fio');
+        $this->dropColumn('{{%user}}', 'cname');
+        $this->dropColumn('{{%user}}', 'type');
+        $this->dropColumn('{{%user}}', 'phone');
+        $this->dropColumn('{{%user}}', 'address');
+    }
+}

+ 68 - 0
models/user/User.php

@@ -21,6 +21,69 @@ use dektrium\user\models\User as BaseUser;
  */
 class User extends BaseUser
 {
+    /** @inheritdoc */
+    public function attributeLabels()
+    {
+        return [
+            'uuid'          => \Yii::t('user', 'Nomer Dogovora'),
+            'username'          => \Yii::t('user', 'Username'),
+            'fio'          => \Yii::t('user', 'Fio'),
+            'cname'          => \Yii::t('user', 'Company name'),
+            'type'          => \Yii::t('user', 'Role'),
+            'email'             => \Yii::t('user', 'Email'),
+            'phone'             => \Yii::t('user', 'Phone'),
+            'address'             => \Yii::t('user', 'Address'),
+            'registration_ip'   => \Yii::t('user', 'Registration ip'),
+            'unconfirmed_email' => \Yii::t('user', 'New email'),
+            'password'          => \Yii::t('user', 'Password'),
+            'created_at'        => \Yii::t('user', 'Registration time'),
+            'last_login_at'     => \Yii::t('user', 'Last login'),
+            'confirmed_at'      => \Yii::t('user', 'Confirmation time'),
+        ];
+    }
+
+    /** @inheritdoc */
+    public function rules()
+    {
+        return [
+            // username rules
+            'usernameTrim'     => ['username', 'trim'],
+            'usernameRequired' => ['username', 'required', 'on' => ['register', 'create', 'connect', 'update']],
+            'usernameMatch'    => ['username', 'match', 'pattern' => static::$usernameRegexp],
+            'usernameLength'   => ['username', 'string', 'min' => 3, 'max' => 255],
+            'usernameUnique'   => [
+                'username',
+                'unique',
+                'message' => \Yii::t('user', 'This username has already been taken')
+            ],
+
+            // uuid rules
+            'uuidInt'     => ['uuid', 'integer'],
+            'uuidRequired' => ['uuid', 'required', 'on' => ['register', 'create', 'connect', 'update']],
+            'uuidUnique'   => [
+                'username',
+                'unique',
+                'message' => \Yii::t('user', 'This username has already been taken')
+            ],
+
+
+            // email rules
+            'emailTrim'     => ['email', 'trim'],
+            'emailRequired' => ['email', 'required', 'on' => ['register', 'connect', 'create', 'update']],
+            'emailPattern'  => ['email', 'email'],
+            'emailLength'   => ['email', 'string', 'max' => 255],
+            'emailUnique'   => [
+                'email',
+                'unique',
+                'message' => \Yii::t('user', 'This email address has already been taken')
+            ],
+
+            // password rules
+            'passwordRequired' => ['password', 'required', 'on' => ['register']],
+            'passwordLength'   => ['password', 'string', 'min' => 6, 'max' => 72, 'on' => ['register', 'create']],
+        ];
+    }
+
     public static function typeNames()
     {
         return [
@@ -28,5 +91,10 @@ class User extends BaseUser
         ];
     }
 
+    public static function findIdentityByAccessToken($token, $type = null)
+    {
+        return static::findOne(['auth_key' => $token]);
+    }
+
 }
 

+ 56 - 0
modules/api/Module.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace app\modules\api;
+
+use app\models\user\User;
+use yii\filters\auth\CompositeAuth;
+use yii\filters\auth\HttpBasicAuth;
+use yii\filters\auth\HttpBearerAuth;
+use yii\filters\auth\QueryParamAuth;
+
+/**
+ * api module definition class
+ */
+class Module extends \yii\base\Module
+{
+    /**
+     * {@inheritdoc}
+     */
+    public $controllerNamespace = 'app\modules\api\controllers';
+
+    public function behaviors()
+    {
+        $behaviors = parent::behaviors();
+        /*$behaviors['authenticator'] = [
+            'class' => CompositeAuth::class,
+            'authMethods' => [
+
+                [
+                    'class' =>HttpBasicAuth::class,
+                    'auth' => function ($username, $password) {
+                        $user = User::find()->where(['username' => $username])->one();
+                        if ($user->verifyPassword($password)) {
+                            return $user;
+                        }
+                        return null;
+                    },
+                ],
+                HttpBearerAuth::class,
+                QueryParamAuth::class,
+            ],
+        ];*/
+        $behaviors['authenticator'] = [
+            'class' => HttpBearerAuth::class,
+        ];
+        return $behaviors;
+    }
+    /**
+     * {@inheritdoc}
+     */
+    public function init()
+    {
+        parent::init();
+        \Yii::$app->user->enableSession = false;
+        // custom initialization code goes here
+    }
+}

+ 20 - 0
modules/api/controllers/DefaultController.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace app\modules\api\controllers;
+
+use yii\web\Controller;
+
+/**
+ * Default controller for the `api` module
+ */
+class DefaultController extends Controller
+{
+    /**
+     * Renders the index view for the module
+     * @return string
+     */
+    public function actionIndex()
+    {
+        return $this->render('index');
+    }
+}

+ 36 - 0
modules/api/controllers/UserController.php

@@ -0,0 +1,36 @@
+<?php
+namespace app\modules\api\controllers;
+
+use app\models\user\User;
+use yii\rest\ActiveController;
+
+/**
+ * Country Controller API
+ *
+ * @author Budi Irawan <deerawan@gmail.com>
+ */
+class UserController extends ActiveController
+{
+    public $modelClass = 'app\models\user\User';
+
+    public function actionBulkAdd(){
+        $requestParams = \Yii::$app->getRequest()->getBodyParams();
+
+        $errs['err'] = array();
+
+        foreach ($requestParams as $param){
+            $user = new User();
+            $user->uuid = $param['uuid'];
+            $user->email = $param['email'];
+            $user->username = $param['username'];
+            $user->password = $param['password'];
+
+            $user->save(false);
+        }
+
+
+        $result = $this->modelClass::find()->all();
+        return $errs;
+    }
+
+}

+ 12 - 0
modules/api/views/default/index.php

@@ -0,0 +1,12 @@
+<div class="api-default-index">
+    <h1><?= $this->context->action->uniqueId ?></h1>
+    <p>
+        This is the view content for action "<?= $this->context->action->id ?>".
+        The action belongs to the controller "<?= get_class($this->context) ?>"
+        in the "<?= $this->context->module->id ?>" module.
+    </p>
+    <p>
+        You may customize this page by editing the following file:<br>
+        <code><?= __FILE__ ?></code>
+    </p>
+</div>