Skip to content

Commit 6008b75

Browse files
committed
v2.0.0
1 parent bb96742 commit 6008b75

19 files changed

+90
-81
lines changed

.gitignore

100644100755
File mode changed.

Bootstrap.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace filsh\yii2\oauth2server;
4+
5+
class Bootstrap implements \yii\base\BootstrapInterface
6+
{
7+
/**
8+
* @var array Model's map
9+
*/
10+
private $_modelMap = [
11+
'OauthClients' => 'filsh\yii2\oauth2server\models\OauthClients',
12+
'OauthAccessTokens' => 'filsh\yii2\oauth2server\models\OauthAccessTokens',
13+
'OauthAuthorizationCodes' => 'filsh\yii2\oauth2server\models\OauthAuthorizationCodes',
14+
'OauthRefreshTokens' => 'filsh\yii2\oauth2server\models\OauthRefreshTokens',
15+
'OauthScopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
16+
];
17+
18+
/**
19+
* @var array Storage's map
20+
*/
21+
private $_storageMap = [
22+
'access_token' => 'filsh\yii2\oauth2server\storage\Pdo',
23+
'authorization_code' => 'filsh\yii2\oauth2server\storage\Pdo',
24+
'client_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
25+
'client' => 'filsh\yii2\oauth2server\storage\Pdo',
26+
'refresh_token' => 'filsh\yii2\oauth2server\storage\Pdo',
27+
'user_credentials' => 'filsh\yii2\oauth2server\storage\Pdo',
28+
'public_key' => 'filsh\yii2\oauth2server\storage\Pdo',
29+
'jwt_bearer' => 'filsh\yii2\oauth2server\storage\Pdo',
30+
'scope' => 'filsh\yii2\oauth2server\storage\Pdo',
31+
];
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
public function bootstrap($app)
37+
{
38+
/** @var $module Module */
39+
if ($app->hasModule('oauth2') && ($module = $app->getModule('oauth2')) instanceof Module) {
40+
$this->_modelMap = array_merge($this->_modelMap, $module->modelMap);
41+
foreach ($this->_modelMap as $name => $definition) {
42+
\Yii::$container->set("filsh\\yii2\\oauth2server\\models\\" . $name, $definition);
43+
$module->modelMap[$name] = is_array($definition) ? $definition['class'] : $definition;
44+
}
45+
46+
$this->_storageMap = array_merge($this->_storageMap, $module->storageMap);
47+
foreach ($this->_storageMap as $name => $definition) {
48+
\Yii::$container->set($name, $definition);
49+
$module->storageMap[$name] = is_array($definition) ? $definition['class'] : $definition;
50+
}
51+
52+
if ($app instanceof \yii\console\Application) {
53+
$module->controllerNamespace = 'filsh\yii2\oauth2server\commands';
54+
}
55+
}
56+
}
57+
}

LICENSE

100644100755
File mode changed.

Module.php

100644100755
Lines changed: 29 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace filsh\yii2\oauth2server;
44

55
use \Yii;
6+
use yii\i18n\PhpMessageSource;
67

78
/**
89
* For example,
@@ -35,31 +36,35 @@
3536
*/
3637
class Module extends \yii\base\Module
3738
{
38-
public $options = [];
39+
const VERSION = '2.0.0';
3940

41+
/**
42+
* @var array Model's map
43+
*/
44+
public $modelMap = [];
45+
46+
/**
47+
* @var array Storage's map
48+
*/
4049
public $storageMap = [];
4150

42-
public $storageDefault = 'filsh\yii2\oauth2server\storage\Pdo';
4351

44-
public $grantTypes = [];
4552

46-
public $modelClasses = [];
4753

48-
public $i18n;
49-
54+
public $options = [];
55+
56+
public $grantTypes = [];
57+
5058
private $_server;
5159

5260
private $_request;
5361

54-
private $_models = [];
55-
5662
/**
5763
* @inheritdoc
5864
*/
5965
public function init()
6066
{
6167
parent::init();
62-
$this->modelClasses = array_merge($this->getDefaultModelClasses(), $this->modelClasses);
6368
$this->registerTranslations();
6469
}
6570

@@ -71,7 +76,10 @@ public function init()
7176
public function getServer($force = false)
7277
{
7378
if($this->_server === null || $force === true) {
74-
$storages = $this->createStorages();
79+
$storages = [];
80+
foreach($this->storageMap as $name => $value) {
81+
$storages[$name] = \Yii::$container->get($name);
82+
}
7583
$server = new \OAuth2\Server($storages, $this->options);
7684

7785
foreach($this->grantTypes as $name => $options) {
@@ -114,86 +122,28 @@ public function getResponse()
114122
{
115123
return new \OAuth2\Response();
116124
}
117-
118-
/**
119-
* Create storages
120-
* @return type
121-
*/
122-
public function createStorages()
123-
{
124-
$connection = Yii::$app->getDb();
125-
if(!$connection->getIsActive()) {
126-
$connection->open();
127-
}
128-
129-
$storages = [];
130-
foreach($this->storageMap as $name => $storage) {
131-
$storages[$name] = Yii::createObject($storage);
132-
}
133-
134-
$defaults = [
135-
'access_token',
136-
'authorization_code',
137-
'client_credentials',
138-
'client',
139-
'refresh_token',
140-
'user_credentials',
141-
'public_key',
142-
'jwt_bearer',
143-
'scope',
144-
];
145-
foreach($defaults as $name) {
146-
if(!isset($storages[$name])) {
147-
$storages[$name] = Yii::createObject($this->storageDefault);
148-
}
149-
}
150-
151-
return $storages;
152-
}
153125

154-
/**
155-
* Get object instance of model
156-
* @param string $name
157-
* @param array $config
158-
* @return ActiveRecord
159-
*/
160-
public function model($name, $config = [])
161-
{
162-
if(!isset($this->_models[$name])) {
163-
$className = $this->modelClasses[ucfirst($name)];
164-
$this->_models[$name] = Yii::createObject(array_merge(['class' => $className], $config));
165-
}
166-
return $this->_models[$name];
167-
}
126+
127+
128+
129+
168130

169131
/**
170132
* Register translations for this module
171133
* @return array
172134
*/
173135
public function registerTranslations()
174136
{
175-
Yii::setAlias('@oauth2server', dirname(__FILE__));
176-
if (empty($this->i18n)) {
177-
$this->i18n = [
178-
'class' => 'yii\i18n\PhpMessageSource',
179-
'basePath' => '@oauth2server/messages',
137+
if(!isset(Yii::$app->get('i18n')->translations['modules/oauth2/*'])) {
138+
Yii::$app->get('i18n')->translations['modules/oauth2/*'] = [
139+
'class' => PhpMessageSource::className(),
140+
'basePath' => __DIR__ . '/messages',
180141
];
181142
}
182-
Yii::$app->i18n->translations['oauth2server'] = $this->i18n;
183143
}
184-
185-
/**
186-
* Get default model classes
187-
* @return array
188-
*/
189-
protected function getDefaultModelClasses()
144+
145+
public static function t($category, $message, $params = [], $language = null)
190146
{
191-
return [
192-
'Clients' => 'filsh\yii2\oauth2server\models\OauthClients',
193-
'AccessTokens' => 'filsh\yii2\oauth2server\models\OauthAccessTokens',
194-
'AuthorizationCodes' => 'filsh\yii2\oauth2server\models\OauthAuthorizationCodes',
195-
'RefreshTokens' => 'filsh\yii2\oauth2server\models\OauthRefreshTokens',
196-
'Scopes' => 'filsh\yii2\oauth2server\models\OauthScopes',
197-
];
147+
return Yii::t('modules/oauth2/' . $category, $message, $params, $language);
198148
}
199149
}

README.md

100644100755
File mode changed.

commands/empty

Whitespace-only changes.

composer.json

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
}
2626
},
2727
"extra": {
28+
"bootstrap": "filsh\\yii2\\oauth2server\\Bootstrap",
2829
"branch-alias": {
2930
"dev-master": "1.0.x-dev"
3031
}

controllers/DefaultController.php

100644100755
File mode changed.

filters/ErrorToExceptionFilter.php

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Yii;
66
use yii\base\Controller;
7+
use filsh\yii2\oauth2server\Module;
78

89
class ErrorToExceptionFilter extends \yii\base\Behavior
910
{
@@ -28,9 +29,9 @@ public function afterAction($event)
2829
if(!$isValid) {
2930
$status = $response->getStatusCode();
3031
// TODO: необходимо также пробрасывать error_uri
31-
$message = Yii::t('oauth2server', $response->getParameter('error_description'));
32+
$message = Module::t('common', $response->getParameter('error_description'));
3233
if($message === null) {
33-
$message = Yii::t('yii', 'An internal server error occurred.');
34+
$message = Module::t('common', 'An internal server error occurred.');
3435
}
3536
throw new \yii\web\HttpException($status, $message);
3637
}

filters/auth/CompositeAuth.php

100644100755
File mode changed.

grants/UserAuthCredentials.php

100644100755
File mode changed.
File renamed without changes.

migrations/m140501_075311_add_oauth2_server.php

100644100755
File mode changed.

models/OauthAccessTokens.php

100644100755
File mode changed.

models/OauthAuthorizationCodes.php

100644100755
File mode changed.

models/OauthClients.php

100644100755
File mode changed.

models/OauthRefreshTokens.php

100644100755
File mode changed.

models/OauthScopes.php

100644100755
File mode changed.

storage/Pdo.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)