Skip to content

Commit

Permalink
(Chore): Alter data type of status in permissions and roles
Browse files Browse the repository at this point in the history
(Chore): Index for `status` columns
(Cleanup): Migration constants to a `Constants`
(Refactor): Refactor classes to use string constants for statuses
  • Loading branch information
Olawale Lawal committed Sep 14, 2017
1 parent c9b1e6e commit 8ee46f2
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Created by .ignore support plugin (hsz.mobi)
/composer.lock
/vendor
/runtime
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Changelog

All Notable changes to `yii2-permission-ext` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 1.0.0 - 2017-09-14

### Added
- Alter data type of `status` in `permissions` and `roles`
- Index for `status` columns
- Migration constants to a `Constants`
- Refactor classes to use string constants for statuses

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ If you discover any security related issues, please email <developers@cottacush.
## Credits

- Adegoke Obasa [email protected]
- Olawale Lawal <[email protected]>
- [All Contributors][link-contributors]

## License
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
{
"name": "Adegoke Obasa",
"email": "[email protected]"
},
{
"name": "Olawale Lawal",
"email": "[email protected]"
}
],
"require": {
Expand Down
31 changes: 31 additions & 0 deletions config/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [
'id' => 'yii2_permission_extension',
'name' => 'Custom Role Based Access Control Implementation',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'app\commands',
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=' . getenv('DB_HOST') . ';dbname=' . getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
]
],
'params' => [],
'vendorPath' => dirname(__DIR__) . "/../vendor",
];
36 changes: 13 additions & 23 deletions migrations/m151005_151946_install.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use yii\db\Schema;
use cottacush\rbac\libs\Constants;
use yii\db\Migration;

/**
Expand All @@ -9,6 +9,7 @@
*/
class m151005_151946_install extends Migration
{

public function up()
{
$tableOptions = null;
Expand All @@ -17,7 +18,7 @@ public function up()
}

// Create permissions table
$this->createTable('permissions', [
$this->createTable(Constants::TABLE_PERMISSIONS, [
'id' => $this->primaryKey(),
'key' => $this->string(150)->notNull(),
'label' => $this->string(255)->notNull(),
Expand All @@ -27,10 +28,10 @@ public function up()
], $tableOptions);

// Add Indexes for performance optimization
$this->createIndex('permissions_unique_key', 'permissions', 'key', true);
$this->createIndex('permissions_unique_key', Constants::TABLE_PERMISSIONS, 'key', true);

// Create Roles Table
$this->createTable('roles', [
$this->createTable(Constants::TABLE_ROLES, [
'id' => $this->primaryKey(),
'key' => $this->string(150)->notNull(),
'label' => $this->string(255)->notNull(),
Expand All @@ -40,39 +41,28 @@ public function up()
], $tableOptions);

// Add Indexes for performance optimization
$this->createIndex('roles_unique_key', 'roles', 'key', true);
$this->createIndex('roles_unique_key', Constants::TABLE_ROLES, 'key', true);

// Create role permissions mapping table
$this->createTable('role_permissions', [
$this->createTable(Constants::TABLE_ROLE_PERMISSIONS, [
'id' => $this->primaryKey(),
'role_id' => $this->integer()->notNull(),
'permission_id' => $this->integer()->notNull(),
'created_at' => $this->dateTime()->notNull()
], $tableOptions);

// Add Indexes for performance optimization
$this->createIndex('rp_role_id_composite', 'role_permissions', ['role_id', 'permission_id'], true);
$this->createIndex('rp_role_id_composite', Constants::TABLE_ROLE_PERMISSIONS, ['role_id', 'permission_id'], true);

// Add Foreign Keys
$this->addForeignKey('rp_roles_role_id', 'role_permissions', 'role_id', 'roles', 'id');
$this->addForeignKey('rp_roles_permission_id', 'role_permissions', 'permission_id', 'permissions', 'id');
$this->addForeignKey('rp_roles_role_id', Constants::TABLE_ROLE_PERMISSIONS, 'role_id', Constants::TABLE_ROLES, 'id');
$this->addForeignKey('rp_roles_permission_id', Constants::TABLE_ROLE_PERMISSIONS, 'permission_id', Constants::TABLE_PERMISSIONS, 'id');
}

public function down()
{
$this->dropTable("role_permissions");
$this->dropTable("permissions");
$this->dropTable("roles");
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
$this->dropTable(Constants::TABLE_ROLE_PERMISSIONS);
$this->dropTable(Constants::TABLE_PERMISSIONS);
$this->dropTable(Constants::TABLE_ROLES);
}
*/
}
66 changes: 66 additions & 0 deletions migrations/m170914_110639_alter_roles_permissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use cottacush\rbac\libs\Constants;
use yii\db\Migration;

/**
* Class m170914_110639_alter_user_credentials
* @author Olawale Lawal <[email protected]>
*/
class m170914_110639_alter_roles_permissions extends Migration
{
public function up()
{
//PERMISSIONS
$this->alterColumn(
Constants::TABLE_PERMISSIONS,
'status',
$this->string(100)->notNull()->defaultValue(Constants::STATUS_ACTIVE)
);

$this->createIndex(
'k_' . Constants::TABLE_PERMISSIONS . '_status',
Constants::TABLE_PERMISSIONS,
'status'
);

//ROLES
$this->alterColumn(
Constants::TABLE_ROLES,
'status',
$this->string(100)->notNull()->defaultValue(Constants::STATUS_ACTIVE)
);

$this->createIndex(
'k_' . Constants::TABLE_ROLES . '_status',
Constants::TABLE_ROLES,
'status'
);
}

public function down()
{
$this->dropIndex(
'k_' . Constants::TABLE_ROLES . '_status',
Constants::TABLE_ROLES
);

$this->alterColumn(
Constants::TABLE_ROLES,
'status',
$this->boolean()->defaultValue(1)
);


$this->dropIndex(
'k_' . Constants::TABLE_PERMISSIONS . '_status',
Constants::TABLE_PERMISSIONS
);

$this->alterColumn(
Constants::TABLE_PERMISSIONS,
'status',
$this->boolean()->defaultValue(1)
);
}
}
12 changes: 5 additions & 7 deletions src/BasePermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

namespace cottacush\rbac;

use cottacush\rbac\models\Role;
use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;

abstract class BasePermissionManager extends Component implements ManagerInterface
{
protected $sessionPrefix = 'cottacush_rbac';

/**
* @author Adegoke Obasa <[email protected]>
* @inheritdoc
*/
public function canAccess($permissionKey)
{
$permissions = $this->getPermissions();
foreach($permissions as $permission) {
if(ArrayHelper::getValue($permission, 'key') === $permissionKey) {
foreach ($permissions as $permission) {
if (ArrayHelper::getValue($permission, 'key') === $permissionKey) {
return true;
}
}
Expand All @@ -30,8 +30,6 @@ public function canAccess($permissionKey)

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleKey
* @return mixed
*/
public function setRoleByKey($roleKey)
{
Expand All @@ -42,11 +40,11 @@ public function setRoleByKey($roleKey)
/**
* @author Adegoke Obasa <[email protected]>
* @param $roleId
* @return mixed
* @return void
*/
public function setRoleById($roleId)
{
$role = $this->getRoleById($roleId);
Yii::$app->session->set($this->sessionPrefix . '::user_role', $role);
}
}
}
12 changes: 6 additions & 6 deletions src/DbPermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace cottacush\rbac;

use cottacush\rbac\libs\Constants;
use cottacush\rbac\models\Permission;
use cottacush\rbac\models\Role;

class DbPermissionManager extends BasePermissionManager
{

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
Expand All @@ -27,7 +27,7 @@ public function getRoles()
*/
public function getRole($key)
{
return Role::find()->where(['key' => $key, 'status' => 1])->one();
return Role::find()->where(['key' => $key, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
Expand All @@ -37,7 +37,7 @@ public function getRole($key)
*/
public function getRoleById($roleId)
{
return Role::find()->where(['id' => $roleId, 'status' => 1])->one();
return Role::find()->where(['id' => $roleId, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
Expand All @@ -56,7 +56,7 @@ public function getPermissions()
*/
public function getPermission($key)
{
return Permission::find()->where(['key' => $key, 'status' => 1])->one();
return Permission::find()->where(['key' => $key, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
Expand All @@ -66,7 +66,7 @@ public function getPermission($key)
*/
public function getPermissionById($permissionId)
{
return Permission::find()->where(['id' => $permissionId, 'status' => 1])->one();
return Permission::find()->where(['id' => $permissionId, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
Expand All @@ -92,4 +92,4 @@ public function getUserPermissions()

return $role->getPermissions();
}
}
}
20 changes: 20 additions & 0 deletions src/libs/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Created by PhpStorm.
* User: Lawale
* Date: 14/09/2017
* Time: 12:07
*/

namespace cottacush\rbac\libs;


class Constants
{
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';

const TABLE_PERMISSIONS = "permissions";
const TABLE_ROLE_PERMISSIONS = "role_permissions";
const TABLE_ROLES = "roles";
}
6 changes: 3 additions & 3 deletions src/models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace cottacush\rbac\models;

use Yii;
use yii\db\ActiveRecord;

/**
* This is the model class for table "permissions".
Expand All @@ -17,7 +17,7 @@
*
* @property RolePermission[] $rolePermissions
*/
class Permission extends \yii\db\ActiveRecord
class Permission extends ActiveRecord
{
/**
* @inheritdoc
Expand All @@ -35,7 +35,7 @@ public function rules()
return [
[['key', 'label', 'created_at', 'updated_at'], 'required'],
[['created_at', 'updated_at'], 'safe'],
[['status'], 'integer'],
[['status'], 'string', 'max' => 100],
[['key'], 'string', 'max' => 150],
[['label'], 'string', 'max' => 255],
[['key'], 'unique']
Expand Down
Loading

0 comments on commit 8ee46f2

Please sign in to comment.