-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(Chore): Alter data type of
status
in permissions
and roles
(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
Showing
14 changed files
with
221 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
/composer.lock | ||
/vendor | ||
/runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ | |
{ | ||
"name": "Adegoke Obasa", | ||
"email": "[email protected]" | ||
}, | ||
{ | ||
"name": "Olawale Lawal", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
} | ||
|
@@ -30,8 +30,6 @@ public function canAccess($permissionKey) | |
|
||
/** | ||
* @author Adegoke Obasa <[email protected]> | ||
* @param $roleKey | ||
* @return mixed | ||
*/ | ||
public function setRoleByKey($roleKey) | ||
{ | ||
|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -92,4 +92,4 @@ public function getUserPermissions() | |
|
||
return $role->getPermissions(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.