Skip to content

Commit

Permalink
(Cleanup): Apply PSR coding convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Olawale Lawal committed Aug 27, 2019
1 parent 2fc0226 commit 104d3c9
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 37 deletions.
24 changes: 20 additions & 4 deletions migrations/m151005_151946_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
class m151005_151946_install extends Migration
{

public function up()
{
$tableOptions = null;
Expand Down Expand Up @@ -52,11 +51,28 @@ public function up()
], $tableOptions);

// Add Indexes for performance optimization
$this->createIndex('rp_role_id_composite', Constants::TABLE_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', 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');
$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()
Expand Down
1 change: 0 additions & 1 deletion migrations/m170914_110639_alter_roles_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function down()
$this->boolean()->defaultValue(1)
);


$this->dropIndex(
'k_' . Constants::TABLE_PERMISSIONS . '_status',
Constants::TABLE_PERMISSIONS
Expand Down
3 changes: 2 additions & 1 deletion src/BasePermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function canAccess($permissionKey)
}

/**
* @param $roleKey
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleByKey($roleKey)
Expand All @@ -38,9 +39,9 @@ public function setRoleByKey($roleKey)
}

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleId
* @return void
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleById($roleId)
{
Expand Down
21 changes: 12 additions & 9 deletions src/DbPermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,82 @@
use cottacush\rbac\libs\Constants;
use cottacush\rbac\models\Permission;
use cottacush\rbac\models\Role;
use Yii;

class DbPermissionManager extends BasePermissionManager
{
/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getRoles()
{
return Role::find()->asArray()->all();
}

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

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleId
* @return Role
* @author Adegoke Obasa <[email protected]>
*/
public function getRoleById($roleId)
{
return Role::find()->where(['id' => $roleId, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissions()
{
return Permission::find()->asArray()->all();
}

/**
* @author Adegoke Obasa <[email protected]>
* @param $key
* @return Permission
* @author Adegoke Obasa <[email protected]>
*/
public function getPermission($key)
{
return Permission::find()->where(['key' => $key, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
}

/**
* @author Adegoke Obasa <[email protected]>
* @param $permissionId
* @return Permission
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissionById($permissionId)
{
return Permission::find()->where(['id' => $permissionId, 'status' => Constants::STATUS_ACTIVE])->limit(1)->one();
return Permission::find()->where(['id' => $permissionId, 'status' => Constants::STATUS_ACTIVE])
->limit(1)->one();
}

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getUserRole()
{
$role = \Yii::$app->session->get($this->sessionPrefix . '::user_role');
$role = Yii::$app->session->get($this->sessionPrefix . '::user_role');
return $role;
}

/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
* @author Adegoke Obasa <[email protected]>
*/
public function getUserPermissions()
Expand Down
22 changes: 11 additions & 11 deletions src/ManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,75 @@ interface ManagerInterface
{
/**
* Checks if the user has the specified permission.
* @author Adegoke Obasa <[email protected]>
* @param $permissionKey
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function canAccess($permissionKey);

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getRoles();

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getUserRole();

/**
* @author Adegoke Obasa <[email protected]>
* @param $key
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getRole($key);

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleId
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getRoleById($roleId);

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissions();

/**
* @author Adegoke Obasa <[email protected]>
* @param $key
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getPermission($key);

/**
* @author Adegoke Obasa <[email protected]>
* @param $permissionId
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissionById($permissionId);

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleKey
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleByKey($roleKey);

/**
* @author Adegoke Obasa <[email protected]>
* @param $roleId
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleById($roleId);

/**
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getUserPermissions();
}
7 changes: 0 additions & 7 deletions src/libs/Constants.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: Lawale
* Date: 14/09/2017
* Time: 12:07
*/

namespace cottacush\rbac\libs;


class Constants
{
const STATUS_ACTIVE = 'active';
Expand Down
2 changes: 1 addition & 1 deletion src/models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ public function getRolePermissions()
{
return $this->hasMany(RolePermission::className(), ['permission_id' => 'id']);
}
}
}
5 changes: 3 additions & 2 deletions src/models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public function getRolePermissions()

/**
* Get Permissions assigned to role
* @author Adegoke Obasa <[email protected]>
* @return mixed
* @throws \yii\base\InvalidConfigException
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissions()
{
Expand All @@ -79,4 +80,4 @@ public function getPermissions()
->asArray()
->all();
}
}
}
2 changes: 1 addition & 1 deletion src/models/RolePermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ public function getRole()
{
return $this->hasOne(Role::className(), ['id' => 'role_id']);
}
}
}

0 comments on commit 104d3c9

Please sign in to comment.