Class luya\testsuite\scopes\PermissionScope
Inheritance | luya\testsuite\scopes\PermissionScope » luya\testsuite\scopes\BaseScope |
---|---|
Uses Traits | luya\testsuite\traits\AdminDatabaseTableTrait |
Available since version | 1.0.20 |
Source Code | https://github.com/luyadev/luya-testsuite/blob/master/src/scopes/PermissionScope.php |
Generate a permission Scope for a Rest Call.
Its recommend to have the application with the following components config:
'modules' => [
'admin' => ['class' => 'luya\admin\Module']
]
An example of how to test Rest Controllers with route permissions:
class MyUnitTest extends WebApplicationTestCase
{
public function testRestController()
{
$controller = new RestController('id', $this->app);
$assert = PermissionScope::run($this->app, function(PermissionScope $scope) use ($controller) {
// there is now a permission for this route
$scope->createRoute('module/controller/action');
// now you can either allow this route or deny:
$scope->allowRoute('module/controller/action');
// which is equals to: $scope->createAndAllowRoute('module/controller/action');
// now the user has permission to this route.
return $scope->runControllerAction($controller, 'action');
});
$this->assertSame('foobar', $assert);
}
}
An example in how to test an RestActive Controllers with api permissions:
public function testRestActiveController()
{
$api = new RestActiveController('id', $this->app);
PermissionScope::run($this->app, function(PermissionScope $scope) use ($api) {
$scope->createApiAndAllow('api-test-action', true, true, false); // create the route and also allocate the permission create and update but not delete.
$this->expectException('yii\web\ForbiddenHttpRequest');
$scope->runControllerAction($api, 'delete', ['id' => 1], 'DELETE');
});
}
In order to configure the scope use the second argument in run():
PermissionScop::run($this->app, function(PermissionScope $scope) {
// do stuff
}, function(PermissionScope $setupScope) {
// the setup scope allows you to configre scoped details before the call scope runs.
$setupScope->userId = 1000;
$scopeScope->userFixture = [
'firstname' => 'Jane',
'is_api_user' => false,
]
});
An example of how to test a POST (save) action of an {{luya\admin\ngrest\base\Api}} controller with JWT enabled:
class MyNgRestTest extends NgRestTestCase
{
public $modelClass = MyTestModel::class;
public $apiClass = MyTestModelController::class;
public $controllerClass = MyTestModelController::class;
public function getConfigArray()
{
return [
'id' => 'MyTestModel',
'basePath' => dirname(__FILE__),
'modules' => [
'admin' => [
'class' => 'luya\admin\Module',
'cors' => true
],
'myadminmodel' => 'myadminmodel\admin\Module',
],
'components' => [
'jwt' => [
'class' => 'luya\admin\components\Jwt',
'key' => '123',
'apiUserEmail' => 'unknown',
'identityClass' => 'myadminmodel\models\User',
],
]
];
}
public function testSaveEntry()
{
$user = new NgRestModelFixture([
'modelClass' => User::class,
]);
$model = $user->newModel;
$model->id = 1;
// set the jwt identity model
$this->app->jwt->identity = $model;
PermissionScope::run($this->app, function(PermissionScope $scope) {
$fixture = new NgRestModelFixture([
'modelClass' => MyTestModel::class,
]);
$scope->createAndAllowApi('MyTestModel');
$ctrl = new MyTestModelController('MyTestModel', $this->app);
$this->app->request->setBodyParams([
'field' => 'value',
// ... other post vlaues
]);
$response = $scope->runControllerAction($ctrl, 'create', [], 'POST');
// assert response content
});
}
}
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$groupId | integer | The value which should be taken to generate the group. | luya\testsuite\scopes\PermissionScope |
$userFixtureData | array | An array which allows you to override user data for example to make user deleted: ` php 'userFixtureData' => [ 'is_deleted' => 1, ] ` > the id attribute should never be provided, as its determed by {{$userId}} property. |
luya\testsuite\scopes\PermissionScope |
$userId | integer | The value which should be taken to generate the user. | luya\testsuite\scopes\PermissionScope |
Protected Properties
Public Methods
Property Details
The value which should be taken to generate the group.
An array which allows you to override user data for example to make user deleted:
'userFixtureData' => [
'is_deleted' => 1,
]
the
id
attribute should never be provided, as its determed by {{$userId}} property.
The value which should be taken to generate the user.
Method Details
Defined in: luya\testsuite\scopes\BaseScope::__construct()
Permission Scope contstructor.
public void __construct ( yii\base\Application $app, callable $fn, callable $invoke = null ) | ||
$app | yii\base\Application | |
$fn | callable | |
$invoke | callable |
public function __construct(Application $app, callable $fn, callable $invoke = null)
{
$this->_app = $app;
$this->_invoke = $invoke;
$this->_fn = $fn;
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::addPermissionApi()
Add permission api entry
public integer addPermissionApi ( $id, $api, $isCrud = true ) | ||
$id | ||
$api | string | |
$isCrud | boolean | |
return | integer |
Returns the "fake" id fro the given api |
---|
public function addPermissionApi($id, $api, $isCrud = true)
{
$this->insertRow('admin_auth', [
'id' => $id,
'module_name' => '@app',
'alias_name' => $api,
'is_crud' => (int) $isCrud,
'api' => $api,
]);
return $id;
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::addPermissionRoute()
Add permission route.
public integer addPermissionRoute ( $id, $route, $moduleName = '@app' ) | ||
$id | ||
$route | string | |
$moduleName | ||
return | integer |
Returns the fake id for the auth entry. |
---|
public function addPermissionRoute($id, $route, $moduleName = '@app')
{
$this->insertRow('admin_auth', [
'id' => $id,
'module_name' => $moduleName,
'alias_name' => $route,
'route' => $route,
]);
return $id;
}
Assign the Api permission to the current user and group with given permissions.
public void allowApi ( $api, $canCreate = true, $canUpdate = true, $canDelete = true ) | ||
$api | string | |
$canCreate | boolean | |
$canUpdate | boolean | |
$canDelete | boolean |
public function allowApi($api, $canCreate = true, $canUpdate = true, $canDelete = true)
{
return $this->assignGroupAuth($this->groupId, strlen($api), $canCreate, $canUpdate, $canDelete);
}
Allow the route for the current user and group.
public void allowRoute ( $route ) | ||
$route | string |
public function allowRoute($route)
{
return $this->assignGroupAuth($this->groupId, strlen($route));
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::assignGroupAuth()
Assigne a group to an auth entry.
public integer assignGroupAuth ( $groupId, $authId, $canCreate = true, $canUpdate = true, $canDelete = true ) | ||
$groupId | integer | |
$authId | integer | |
$canCreate | boolean | |
$canUpdate | boolean | |
$canDelete | boolean | |
return | integer |
The number of affected rows. |
---|
public function assignGroupAuth($groupId, $authId, $canCreate = true, $canUpdate = true, $canDelete = true)
{
return $this->insertRow('admin_group_auth', [
'group_id' => $groupId,
'auth_id' => $authId,
'crud_create' => (int) $canCreate,
'crud_update' => (int) $canUpdate,
'crud_delete' => (int) $canDelete,
]);
}
Clean up tables and fixtures.
public void cleanup ( ) |
public function cleanup()
{
$this->userFixture->cleanup();
$this->groupFixture->cleanup();
$this->userOnlineFixture->cleanup();
$this->ngRestLogFixture->cleanup();
$this->langFixture->cleanup();
$this->dropAdminAuthTable();
$this->dropAdminGroupAuthTable();
$this->dropAdminUserGroupTable();
$this->dropAdminUserAuthNotificationTable();
}
Defined in: luya\testsuite\scopes\BaseScope::cleanupFixture()
A helper method to cleanup fixtures.
If the given fixture property is not an instance of ActiveRecordFixture nothing happens, otherwise the cleanup() will be run.
public void cleanupFixture ( $fixture ) | ||
$fixture | mixed |
public function cleanupFixture($fixture)
{
if ($fixture instanceof ActiveRecordFixture) {
$fixture->cleanup();
}
}
Defined in: luya\testsuite\scopes\BaseScope::configure()
Configured is used before prepare which allows you to configure the current scope.
public void configure ( ) |
public function configure()
{
if ($this->_invoke) {
call_user_func_array($this->_invoke, [$this]);
}
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminAuthTable()
Create the admin auth table.
public void createAdminAuthTable ( ) |
public function createAdminAuthTable()
{
$this->createTableIfNotExists('admin_auth', [
'id' => 'INT(11) PRIMARY KEY',
'alias_name' => 'text',
'module_name' => 'text',
'is_crud' => 'int(11)',
'route' => 'text',
'api' => 'text',
'pool' => 'text',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminGroupAuthTable()
Create the admin group auth table.
public void createAdminGroupAuthTable ( ) |
public function createAdminGroupAuthTable()
{
$this->createTableIfNotExists('admin_group_auth', [
'id' => 'INT(11) PRIMARY KEY',
'group_id' => 'int(11)',
'auth_id' => 'int(11)',
'crud_create' => 'int(11)',
'crud_update' => 'int(11)',
'crud_delete' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminGroupFixture()
Create the Group Fixture with given ID.
public luya\testsuite\fixtures\NgRestModelFixture createAdminGroupFixture ( $id ) | ||
$id | integer |
public function createAdminGroupFixture($id)
{
return new NgRestModelFixture([
'modelClass' => Group::class,
'fixtureData' => [
'tester' => [
'id' => $id,
'name' => 'Test Group',
],
],
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminLangFixture()
Create admin language fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminLangFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminLangFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => Lang::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminNgRestLogFixture()
Create the NgRest Log Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createAdminNgRestLogFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createAdminNgRestLogFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => NgrestLog::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueLogErrorFixture()
Queue Log Error
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogErrorFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminQueueLogErrorFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => QueueLogError::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueLogFixture()
Queue Log
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminQueueLogFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => QueueLog::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueTable()
Create the Admin Queue Table
public void createAdminQueueTable ( ) |
public function createAdminQueueTable()
{
return $this->createTableIfNotExists('admin_queue', [
'id' => 'INT(11) PRIMARY KEY',
'channel' => 'text',
'job' => 'blog',
'pushed_at' => 'int(11)',
'ttr' => 'int(11)',
'delay' => 'int(11)',
'priority' => 'int(11)',
'reserved_at' => 'int(11)',
'attempt' => 'int(11)',
'done_at' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFileFixture()
Storage File
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFileFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFileFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFile::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFilterChainFixture()
Storage Filter Chain
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterChainFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFilterChainFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFilterChain::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFilterFixture()
Storage Filter
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFilterFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFilter::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageImageFixture()
Storage Image
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageImageFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageImageFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageImage::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminTagFixture()
Create admin tag fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminTagFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminTagFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => Tag::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminTagRelationFixture()
Create admin tag relation fixture
public luya\testsuite\fixtures\ActiveRecordFixture createAdminTagRelationFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminTagRelationFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => TagRelation::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserAuthNotificationTable()
Create the admin user auth notification table.
public void createAdminUserAuthNotificationTable ( ) |
public function createAdminUserAuthNotificationTable()
{
$this->createTableIfNotExists('admin_user_auth_notification', [
'id' => 'INT(11) PRIMARY KEY',
'user_id' => 'int(11)',
'auth_id' => 'int(11)',
'is_muted' => 'int(11)',
'model_latest_pk_value' => 'text',
'model_class' => 'text',
'created_at' => 'int(11)',
'updated_at' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserDeviceFixture()
User Device
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserDeviceFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserDeviceFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserDevice::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserFixture()
Create the User Fixture with given fixture Data.
public luya\testsuite\fixtures\NgRestModelFixture createAdminUserFixture ( array $fixtureData = [], $defaultSchema = true ) | ||
$fixtureData | array | |
$defaultSchema |
public function createAdminUserFixture(array $fixtureData = [], $defaultSchema = true)
{
return new NgRestModelFixture([
'modelClass' => User::class,
'schema' => $defaultSchema ? [
'title' => 'int(11)',
'firstname' => 'text',
'lastname' => 'text',
'email' => 'text',
'is_deleted' => 'int(11)',
'is_api_user' => 'boolean',
'api_last_activity' => 'int(11)',
'auth_token' => 'text',
'is_request_logger_enabled' => 'boolean',
'email_verification_token_timestamp' => 'int(11)',
'login_attempt_lock_expiration' => 'int(11)',
'login_attempt' => 'int(11)',
'email_verification_token' => 'text',
'api_allowed_ips' => 'text',
'api_rate_limit' => 'int(11)',
'cookie_token' => 'text',
'settings' => 'text',
'force_reload' => 'int(11)',
'secure_token_timestamp' => 'int(11)',
'secure_token' => 'text',
'password' => 'text',
'password_salt' => 'text',
'login_2fa_enabled' => 'int(11)',
'login_2fa_secret' => 'text',
'login_2fa_backup_key' => 'text',
'password_verification_token' => 'text',
'password_verification_token_timestamp' => 'int(11)',
] : null,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserGroupTable()
Create the admin user group table.
public void createAdminUserGroupTable ( ) |
public function createAdminUserGroupTable()
{
$this->createTableIfNotExists('admin_user_group', [
'id' => 'INT(11) PRIMARY KEY',
'user_id' => 'int(11)',
'group_id' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserLoginFixture()
Admin User Fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminUserLoginFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserLoginFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => UserLogin::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserLoginLockoutFixture()
User Login Lockout
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserLoginLockoutFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserLoginLockoutFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserLoginLockout::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserOnlineFixture()
Create the User Online Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserOnlineFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createAdminUserOnlineFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserOnline::class,
'fixtureData' => $fixtureData,
]);
}
Create the Api in the permission system and directly allow the api with given permissions.
public void createAndAllowApi ( $api, $canCreate = true, $canUpdate = true, $canDelete = true ) | ||
$api | string | |
$canCreate | boolean | |
$canUpdate | boolean | |
$canDelete | boolean |
public function createAndAllowApi($api, $canCreate = true, $canUpdate = true, $canDelete = true)
{
$this->createApi($api);
$this->allowApi($api, $canCreate, $canUpdate, $canDelete);
}
Create a route in permission system and directly allow the route.
Read more about details of the route and how its build in {{createRoute()}} method.
public void createAndAllowRoute ( $route ) | ||
$route | string |
public function createAndAllowRoute($route)
{
$this->createRoute($route);
$this->allowRoute($route);
}
Create an Api in permission system. (ActiveRestController).
public integer createApi ( $api ) | ||
$api | string | |
return | integer |
Returns the id of the admin_auth table entry. |
---|
public function createApi($api)
{
return $this->addPermissionApi(strlen($api), $api, true);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createGroupFixture()
Create the Group Fixture with given ID.
public luya\testsuite\fixtures\NgRestModelFixture createGroupFixture ( $id ) | ||
$id | integer |
public function createGroupFixture($id)
{
trigger_error('use createAdminGroupFixture() instead', E_USER_DEPRECATED);
return $this->createAdminGroupFixture($id);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createNgRestLogFixture()
Create the NgRest Log Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createNgRestLogFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createNgRestLogFixture(array $fixtureData = [])
{
trigger_error('use createAdminNgRestLogFixture() instead', E_USER_DEPRECATED);
return $this->createAdminNgRestLogFixture($fixtureData);
}
Create a route in permission system.
Assuming the application has id "myapp" (['id' => 'myapp']
) and controller defined is mycontroller and the action is helloworld
the route would be myapp/mycontroller/helloworld
.
$app = new Application(['id' => 'myapp']);
PermissionScope::run($app, function($scope) use ($app) {
$controller = new MyController('mycontroller', $app);
$scope->runControllerAction($controller, 'helloworld');
});
The above example would nee to set:
$scope->createRoute('myapp/mycontroller/helloworld');
public integer createRoute ( $route ) | ||
$route | string | |
return | integer |
Returns the id of the admin_auth table entry. |
---|
public function createRoute($route)
{
return $this->addPermissionRoute(strlen($route), $route);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createUserFixture()
Create the User Fixture with given fixture Data.
public luya\testsuite\fixtures\NgRestModelFixture createUserFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createUserFixture(array $fixtureData = [])
{
trigger_error('use createAdminUserFixture() instead', E_USER_DEPRECATED);
return $this->createAdminUserFixture($fixtureData);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createUserOnlineFixture()
Create the User Online Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createUserOnlineFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createUserOnlineFixture(array $fixtureData = [])
{
trigger_error('use createAdminUserOnlineFixture() instead', E_USER_DEPRECATED);
return $this->createAdminUserOnlineFixture($fixtureData);
}
Deny the Api, which deletes the api permission entry.
public void denyApi ( $api ) | ||
$api | string |
public function denyApi($api)
{
return $this->unAssignGroupAuth($this->groupId, strlen($api));
}
Deny the route for the given user and group (removes the group assigment).
public void denyRoute ( $route ) | ||
$route | string |
public function denyRoute($route)
{
return $this->unAssignGroupAuth($this->groupId, strlen($route));
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminAuthTable()
Drop the admin auth table.
public void dropAdminAuthTable ( ) |
public function dropAdminAuthTable()
{
$this->dropTableIfExists('admin_auth');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminGroupAuthTable()
Drop the admin group auth table.
public void dropAdminGroupAuthTable ( ) |
public function dropAdminGroupAuthTable()
{
$this->dropTableIfExists('admin_group_auth');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminUserAuthNotificationTable()
Drop the admin user auth notification table.
public void dropAdminUserAuthNotificationTable ( ) |
public function dropAdminUserAuthNotificationTable()
{
$this->dropTableIfExists('admin_user_auth_notification');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminUserGroupTable()
Drop the admin user group table.
public void dropAdminUserGroupTable ( ) |
public function dropAdminUserGroupTable()
{
$this->dropTableIfExists('admin_user_group');
}
Defined in: luya\testsuite\scopes\BaseScope::getApp()
Returns the Application instance.
Getter method to access the private object.
public yii\base\Application getApp ( ) |
public function getApp()
{
return $this->_app;
}
Returns the application database componenet.
public yii\db\Connection getDatabaseComponent ( ) |
public function getDatabaseComponent()
{
return $this->getApp()->db;
}
Login the the given user into the admin user system.
this is only used for WEB controller request with session based auth.
public boolean loginUser ( ) | ||
return | boolean |
Whether login was successfull or not. |
---|
public function loginUser()
{
return $this->getApp()->adminuser->login($this->userFixture->getModel('user'));
}
This method is called before the callback runs in order to prepare and setup the permission scope.
public void prepare ( ) |
public function prepare()
{
// ensure the given and required application components are available.
$this->updateApplicationConfig();
$this->userFixture = $this->createAdminUserFixture([
'user' => array_merge([
'id' => $this->userId,
'firstname' => 'John',
'lastname' => 'Doe',
'email' => '[email protected]',
'is_deleted' => 0,
'is_api_user' => 1,
'api_last_activity' => 12345678,
'auth_token' => 'TestAuthToken',
'is_request_logger_enabled' => false,
], $this->userFixtureData),
]);
$this->groupFixture = $this->createAdminGroupFixture($this->groupId);
$this->userOnlineFixture = $this->createAdminUserOnlineFixture();
$this->ngRestLogFixture = $this->createAdminNgRestLogFixture();
$this->langFixture = $this->createAdminLangFixture([
1 => [
'id' => 1,
'short_code' => 'en',
'name' => 'English',
'is_default' => 1,
'is_deleted' => 0,
]
]);
$this->createAdminUserGroupTable();
$this->createAdminGroupAuthTable();
$this->createAdminAuthTable();
$this->createAdminUserAuthNotificationTable();
$this->createAdminUserLoginLockoutFixture();
$this->userGroupId = $this->insertRow('admin_user_group', [
'user_id' => $this->userId,
'group_id' => $this->groupId,
]);
}
Remove the api from permission system.
public void removeApi ( $api ) | ||
$api | string |
public function removeApi($api)
{
return $this->removePermissionApi($api);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::removePermissionApi()
Remove permission api entry.
public integer removePermissionApi ( $api ) | ||
$api | string | |
return | integer |
The number of affected rows. |
---|
public function removePermissionApi($api)
{
return $this->deleteRow('admin_auth', [
'api' => $api,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::removePermissionRoute()
Remove permission route
public integer removePermissionRoute ( $route ) | ||
$route | string | |
return | integer |
The number of affected rows. |
---|
public function removePermissionRoute($route)
{
return $this->deleteRow('admin_auth', [
'route' => $route,
]);
}
Remove the route from the permission system.
public void removeRoute ( $route ) | ||
$route | string |
public function removeRoute($route)
{
return $this->removePermissionRoute($route);
}
Defined in: luya\testsuite\scopes\BaseScope::run()
Run a given function inside a permission scope.
public static mixed run ( yii\base\Application $app, callable $fn, callable $invoke = null ) | ||
$app | \luya\testsuite\scopes\yii\base\Application | |
$fn | callable |
The function to run. |
$invoke | callable |
The function to configure the scope. |
public static function run(Application $app, callable $fn, callable $invoke = null)
{
$scope = new static($app, $fn, $invoke);
$scope->configure();
$scope->prepare();
$response = $scope->runCallable($scope);
$scope->cleanup();
return $response;
}
Defined in: luya\testsuite\scopes\BaseScope::runCallable()
Run the provided callable function
public mixed runCallable ( luya\testsuite\scopes\BaseScope $scope ) | ||
$scope | luya\testsuite\scopes\PermissionScope |
public function runCallable(BaseScope $scope)
{
return call_user_func_array($this->_fn, [$scope]);
}
Make a call to a controllers action with params and request method defintion.
public mixed runControllerAction ( yii\base\Controller $controller, $action, array $params = [], $method = 'GET' ) | ||
$controller | yii\base\Controller | |
$action | string | |
$params | array | |
$method | string |
GET, POST, HEAD, PUT, PATCH, DELETE |
public function runControllerAction(Controller $controller, $action, array $params = [], $method = 'GET')
{
$_SERVER['REQUEST_METHOD'] = strtoupper($method);
$this->getApp()->controller = $controller;
if ($controller instanceof RestController) {
$this->setQueryAuthToken();
} else {
$this->loginUser();
$controller->enableCsrfValidation = false;
}
return $controller->runAction($action, $params);
}
Set authentification query.
public void setQueryAuthToken ( $value = true, $token = null ) | ||
$value | boolean | |
$token | string |
public function setQueryAuthToken($value = true, $token = null)
{
if ($value) {
$accessToken = $token ? $token : $this->userFixture->getModel('user')->auth_token;
$this->getApp()->request->setQueryParams(array_merge(['access-token' => $accessToken], $this->getApp()->request->getQueryParams()));
} else {
$this->getApp()->request->setQueryParams(array_merge(['access-token' => null], $this->getApp()->request->getQueryParams()));
}
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::unAssignGroupAuth()
Unassigne a group from auth
public integer unAssignGroupAuth ( $groupId, $authId ) | ||
$groupId | integer | |
$authId | integer | |
return | integer |
The number of affected rows. |
---|
public function unAssignGroupAuth($groupId, $authId)
{
return $this->deleteRow('admin_group_auth', [
'group_id' => $groupId,
'auth_id' => $authId,
]);
}
Method to update the application config with requrired componenets.
public void updateApplicationConfig ( ) |
public function updateApplicationConfig()
{
$this->getApp()->set('session',['class' => 'luya\testsuite\components\DummySession']);
$this->getApp()->set('cache', ['class' => 'yii\caching\DummyCache']);
$this->getApp()->set('adminuser', ['class' => 'luya\admin\components\AdminUser', 'enableSession' => false]);
$this->getApp()->set('db', ['class' => 'yii\db\Connection', 'dsn' => 'sqlite::memory:']);
}