Skip to content

Commit

Permalink
Merge pull request #15 from yii2mod/added_pageSize_property
Browse files Browse the repository at this point in the history
close #14
  • Loading branch information
Igor Chepurnoy authored Feb 7, 2017
2 parents b136aa9 + cdb00cc commit aedffec
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 39 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1

# faster builds on new travis setup not using sudo
sudo: false
Expand Down
4 changes: 2 additions & 2 deletions ConsoleModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Use [[\yii\base\Module::$controllerMap]] to change property of controller.
*
* ~~~
* ```php
* 'controllerMap' => [
* 'migrate' => [
* 'class' => 'yii2mod\rbac\commands\MigrateController',
Expand All @@ -16,7 +16,7 @@
* 'templateFile' => 'your own template file'
* ]
* ]
* ~~~
* ```
*/
class ConsoleModule extends Module
{
Expand Down
13 changes: 8 additions & 5 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
*
* Use [[\yii\base\Module::$controllerMap]] to change property of controller.
*
* ~~~
* ```php
* 'controllerMap' => [
* 'assignment' => [
* 'class' => 'yii2mod\rbac\controllers\AssignmentController',
* 'userIdentityClass' => 'app\models\User',
* 'searchClass' => 'Your own search model'
* 'searchClass' => [
* 'class' => 'yii2mod\rbac\models\search\AssignmentSearch',
* 'pageSize' => 10,
* ],
* 'idField' => 'id',
* 'usernameField' => 'username'
* 'gridViewColumns' => [
* 'id',
* 'username',
* 'email'
* ]
* ]
* ],
* ],
* ],
* ~~~
* ```php
*/
class Module extends \yii\base\Module
{
Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,37 @@ return [
'modules' => [
'rbac' => [
'class' => 'yii2mod\rbac\Module',
// Some controller property maybe need to change.
// Some controller property maybe need to change.
'controllerMap' => [
'assignment' => [
'class' => 'yii2mod\rbac\controllers\AssignmentController',
'userIdentityClass' => 'app\models\User',
'searchClass' => 'Your own search model class',
'searchClass' => [
'class' => 'yii2mod\rbac\models\search\AssignmentSearch',
'pageSize' => 10,
],
'idField' => 'id',
'usernameField' => 'username',
'gridViewColumns' => [
'id',
'username',
'email'
]
]
],
'role' => [
'class' => 'yii2mod\rbac\controllers\RoleController',
'searchClass' => [
'class' => 'yii2mod\rbac\models\search\AuthItemSearch',
'pageSize' => 10,
],
],
'rule' => [
'class' => 'yii2mod\rbac\controllers\RuleController',
'searchClass' => [
'class' => 'yii2mod\rbac\models\search\BizRuleSearch',
'pageSize' => 10
],
],
]
],
]
Expand Down
12 changes: 10 additions & 2 deletions base/ItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
*/
class ItemController extends Controller
{
/**
* @var string search class name for auth items search
*/
public $searchClass = [
'class' => AuthItemSearch::class,
];

/**
* @var int Type of Auth Item
*/
Expand All @@ -35,7 +42,7 @@ public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'index' => ['get'],
'view' => ['get'],
Expand Down Expand Up @@ -63,7 +70,8 @@ public function behaviors()
*/
public function actionIndex()
{
$searchModel = new AuthItemSearch(['type' => $this->type]);
$searchModel = Yii::createObject($this->searchClass);
$searchModel->type = $this->type;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
Expand Down
2 changes: 1 addition & 1 deletion commands/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MigrateController extends BaseMigrateController
*/
public function init()
{
$this->db = Instance::ensure($this->db, Connection::className());
$this->db = Instance::ensure($this->db, Connection::class);
parent::init();
}

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
],
"require": {
"php": ">=5.6",
"yiisoft/yii2": "^2.0.8",
"2amigos/yii2-arrayquery-component": "*",
"yiisoft/yii2-jui": "*"
Expand Down
23 changes: 12 additions & 11 deletions controllers/AssignmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class AssignmentController extends Controller
/**
* @var string search class name for assignments search
*/
public $searchClass;
public $searchClass = [
'class' => AssignmentSearch::class,
];

/**
* @var string id column name
Expand All @@ -42,11 +44,13 @@ class AssignmentController extends Controller
public $gridViewColumns = [];

/**
* Initializes the object.
* @inheritdoc
*/
public function init()
{
if (empty($this->userIdentityClass)) {
parent::init();

if ($this->userIdentityClass === null) {
$this->userIdentityClass = Yii::$app->user->identityClass;
}

Expand All @@ -56,14 +60,10 @@ public function init()
$this->usernameField,
];
}

parent::init();
}

/**
* Returns a list of behaviors that this component should behave as.
*
* @return array
* @inheritdoc
*/
public function behaviors()
{
Expand Down Expand Up @@ -94,11 +94,12 @@ public function behaviors()
*/
public function actionIndex()
{
if (empty($this->searchClass)) {
$searchModel = Yii::createObject(AssignmentSearch::className());
/* @var AssignmentSearch */
$searchModel = Yii::createObject($this->searchClass);

if ($searchModel instanceof AssignmentSearch) {
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->userIdentityClass, $this->idField, $this->usernameField);
} else {
$searchModel = Yii::createObject($this->searchClass);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/RouteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'index' => ['get', 'post'],
'create' => ['post'],
Expand Down
11 changes: 9 additions & 2 deletions controllers/RuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
class RuleController extends Controller
{
/**
* @var string search class name for rules search
*/
public $searchClass = [
'class' => BizRuleSearch::class,
];

/**
* Returns a list of behaviors that this component should behave as.
*
Expand All @@ -25,7 +32,7 @@ public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'class' => VerbFilter::class,
'actions' => [
'index' => ['get'],
'view' => ['get'],
Expand All @@ -44,7 +51,7 @@ public function behaviors()
*/
public function actionIndex()
{
$searchModel = new BizRuleSearch();
$searchModel = Yii::createObject($this->searchClass);
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

return $this->render('index', [
Expand Down
2 changes: 1 addition & 1 deletion migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Migration extends Component implements MigrationInterface
*/
public function init()
{
$this->authManager = Instance::ensure($this->authManager, DbManager::className());
$this->authManager = Instance::ensure($this->authManager, DbManager::class);
parent::init();
}

Expand Down
2 changes: 1 addition & 1 deletion models/BizRuleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function classExists()
return;
}

if (!is_subclass_of($this->className, Rule::className())) {
if (!is_subclass_of($this->className, Rule::class)) {
$message = Yii::t('yii2mod.rbac', "'{class}' must extend from 'yii\\rbac\\Rule' or its child class", [
'class' => $this->className, ]);
$this->addError('className', $message);
Expand Down
11 changes: 9 additions & 2 deletions models/search/AssignmentSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class AssignmentSearch extends Model
*/
public $username;

/**
* @var int the default page size
*/
public $pageSize = 25;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -49,11 +54,13 @@ public function search($params, $class, $idField, $usernameField)
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 20,
'pageSize' => $this->pageSize,
],
]);

if (!($this->load($params) && $this->validate())) {
$this->load($params);

if (!$this->validate()) {
return $dataProvider;
}

Expand Down
7 changes: 6 additions & 1 deletion models/search/AuthItemSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class AuthItemSearch extends Model
*/
public $ruleName;

/**
* @var int the default page size
*/
public $pageSize = 25;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -96,7 +101,7 @@ public function search($params)
'attributes' => ['name'],
],
'pagination' => [
'pageSize' => 25,
'pageSize' => $this->pageSize,
],
]);
}
Expand Down
7 changes: 6 additions & 1 deletion models/search/BizRuleSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class BizRuleSearch extends Model
*/
public $name;

/**
* @var int the default page size
*/
public $pageSize = 25;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -51,7 +56,7 @@ public function search($params)
'attributes' => ['name'],
],
'pagination' => [
'pageSize' => 25,
'pageSize' => $this->pageSize,
],
]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/models/BizRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public function testCreateRule()
{
$model = new BizRuleModel();
$model->name = 'guest';
$model->className = GuestRule::className();
$model->className = GuestRule::class;

$this->assertTrue($model->save());

$rule = Yii::$app->authManager->getRule($model->name);
$this->assertInstanceOf(Rule::className(), $rule);
$this->assertInstanceOf(Rule::class, $rule);

return $rule;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/models/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function testCreateRole()
$model->description = 'admin role';

$this->assertTrue($model->save());
$this->assertInstanceOf(Role::className(), Yii::$app->authManager->getRole('admin'));
$this->assertInstanceOf(Role::class, Yii::$app->authManager->getRole('admin'));

return Yii::$app->authManager->getRole('admin');
}
Expand Down

0 comments on commit aedffec

Please sign in to comment.