Skip to content

Commit f8eca86

Browse files
author
Angel Garcia
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # init
2 parents 0bad60c + 4a461c3 commit f8eca86

File tree

12 files changed

+129
-85
lines changed

12 files changed

+129
-85
lines changed

.idea/yii2-api-template.iml

Lines changed: 1 addition & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ return [
7474
- Check and edit the other files in the `config/` directory to customize your application as required.
7575
- Refer to the README in the `tests` directory for information specific to basic application tests.
7676

77+
### Running in Apache
78+
79+
Edit the Apache configuration file, this can be found in ```/etc/apache2/apache2.conf```
80+
81+
Inside the security model section make sure to change the AllowOverride option from **None** to **All**
82+
so it looks like this
83+
```
84+
<Directory /var/www/>
85+
Options Indexes FollowSymLinks
86+
AllowOverride All
87+
Require all granted
88+
</Directory>
89+
```
90+
7791
DIRECTORY STRUCTURE
7892
-------------------
7993

@@ -111,6 +125,13 @@ To extract your app messages you can do so using the [message command](https://w
111125

112126
```./yii message messages/config-messages.php```
113127

128+
To scan the tree 'sourcePath' to generate files default for labels used in the code execute the following command
129+
```./yii message/extract @app/messages/config-messages.php ```
130+
131+
To change target lenguage for example to spanish use
132+
```Yii::$app->language = 'es';```
133+
134+
114135
### Applying migrations
115136

116137
The migrations can be found in ```@console/migrations``` . The initial migration named ```m130524_201442_init.php``` contains the user table creation script, including the following aditions to
@@ -123,4 +144,9 @@ created_at int(13) - Time of creation
123144
updated_at int(13) - Time of creation
124145
created_by string(13) - id of the user that created the record
125146
updated_by string(13) - id of the last user that updated the record
126-
```
147+
```
148+
To apply the migrations execute the command
149+
```./yii migrate```
150+
151+
To revert the migrations execute the command
152+
```./yii migrate/down```

api/config/urlRules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'pluralize' => false,
66
'extraPatterns' => [
77
'GET status' => 'status',
8+
'POST signup' => 'signup',
89
],
910
'except' => ['create', 'delete', 'update', 'view', 'index'],
1011
],

api/modules/v1/controllers/UserController.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,31 @@
44

55

66
use base\rest\ActiveController;
7+
use yii\filters\auth\CompositeAuth;
8+
use yii\filters\auth\HttpBearerAuth;
9+
use yii\helpers\ArrayHelper;
710

811
class UserController extends ActiveController
912
{
10-
public $modelClass = 'common\models\User';
13+
public $modelClass = 'api\modules\v1\models\User';
14+
15+
public function behaviors()
16+
{
17+
return ArrayHelper::merge(parent::behaviors(), [
18+
[
19+
'class' => CompositeAuth::class,
20+
// Status endpoint is inherited from the ActiveController class
21+
'except' => ['status', 'signup'],
22+
'authMethods' => [
23+
HttpBearerAuth::class,
24+
],
25+
]
26+
]);
27+
}
28+
29+
public function actionSignup()
30+
{
31+
return 'working';
32+
}
33+
1134
}

api/modules/v1/models/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace api\modules\v1\models;
4+
5+
/**
6+
* Class User
7+
* @package api\modules\v1\models
8+
*
9+
* {@inheritdoc}
10+
*/
11+
class User extends \common\models\User
12+
{
13+
14+
}

common/components/base/db/ActiveRecord.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use base\util\UUID;
66
use yii\behaviors\AttributeBehavior;
7+
use yii\behaviors\BlameableBehavior;
78
use yii\behaviors\TimestampBehavior;
89

910
class ActiveRecord extends \yii\db\ActiveRecord

common/components/base/rest/ActiveController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use Yii;
77
use yii\filters\auth\CompositeAuth;
88
use yii\filters\auth\HttpBearerAuth;
9+
use yii\helpers\ArrayHelper;
910
use yii\web\Response;
1011

1112
class ActiveController extends \yii\rest\ActiveController
1213
{
1314
public function behaviors()
1415
{
15-
return array_merge(parent::behaviors(), [
16+
return ArrayHelper::merge(parent::behaviors(), [
1617
'corsFilter' => [
1718
'class' => CustomCors::class,
1819
'cors' => [
@@ -28,21 +29,14 @@ public function behaviors()
2829
'application/json' => Response::FORMAT_JSON,
2930
],
3031
],
31-
[
32-
'class' => CompositeAuth::class,
33-
'except' => ['status'],
34-
'authMethods' => [
35-
HttpBearerAuth::class,
36-
],
37-
]
3832
]);
3933
}
4034

4135
public function actionStatus()
4236
{
4337
$version = `git log -1 --pretty=%h`;
4438
$version = str_replace(array("\r", "\n"), '', $version);
45-
return ['status' => 'online', 'version' => $version];
39+
return ['status' => Yii::t('api', 'online'), 'version' => $version];
4640
}
4741

4842

common/models/User.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Yii;
66
use yii\base\NotSupportedException;
77
use yii\behaviors\TimestampBehavior;
8-
use yii\db\ActiveRecord;
8+
use base\db\ActiveRecord;
99
use yii\web\IdentityInterface;
1010

1111
/**
@@ -17,9 +17,13 @@
1717
* @property string $password_reset_token
1818
* @property string $email
1919
* @property string $auth_key
20+
* @property string $access_token
21+
* @property string $refresh_token
2022
* @property integer $status
2123
* @property integer $created_at
2224
* @property integer $updated_at
25+
* @property string $created_by
26+
* @property string $updated_by
2327
* @property string $password write-only password
2428
*/
2529
class User extends ActiveRecord implements IdentityInterface
@@ -49,7 +53,7 @@ public static function findIdentity($id)
4953
*/
5054
public static function findIdentityByAccessToken($token, $type = null)
5155
{
52-
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
56+
return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
5357
}
5458

5559
/**
@@ -187,4 +191,36 @@ public function removePasswordResetToken()
187191
{
188192
$this->password_reset_token = null;
189193
}
194+
195+
/**
196+
* Generates new access token
197+
*/
198+
public function generateAccessToken()
199+
{
200+
$this->access_token = Yii::$app->security->generateRandomString(32) . '_' . time();
201+
}
202+
203+
/**
204+
* Removes access token
205+
*/
206+
public function removeAccessToken()
207+
{
208+
$this->access_token = null;
209+
}
210+
211+
/**
212+
* Generates new refresh token
213+
*/
214+
public function generateRefreshToken()
215+
{
216+
$this->refresh_token = Yii::$app->security->generateRandomString(32) . '_' . time();
217+
}
218+
219+
/**
220+
* Removes access token
221+
*/
222+
public function removeRefreshToken()
223+
{
224+
$this->refresh_token = null;
225+
}
190226
}

console/migrations/m130524_201442_init.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public function up()
1616
'id' => $this->string(36)->notNull(),
1717
'username' => $this->string()->notNull()->unique(),
1818
'auth_key' => $this->string(32)->notNull(),
19-
'access_token' => $this->string(32)->notNull(),
20-
'refresh_token' => $this->string(32)->notNull(),
19+
'access_token' => $this->string(46)->notNull(),
20+
'refresh_token' => $this->string(46)->notNull(),
2121
'password_hash' => $this->string()->notNull(),
2222
'password_reset_token' => $this->string()->unique(),
2323
'email' => $this->string()->notNull()->unique(),

init

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,8 @@ function formatMessage($message, $styles)
307307
// TODO check .yml files and git attributes and bowerrc
308308
// TODO Set up testing according to business
309309
// TODO Set up automatic API Docs
310-
// TODO Set up i18n accoding to https://www.yiiframework.com/doc/guide/2.0/en/tutorial-i18n
311-
// TODO Set up environments configuration
310+
// TODO Set up and fix environments configuration
312311
// TODO Fix common / models
313312
// TODO Add CORS config to readme
314313
// TODO Add components description and ussage
314+
// TODO Create basic auth endpoints

0 commit comments

Comments
 (0)