Skip to content

Commit

Permalink
Code Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
adaugochi committed Feb 19, 2021
1 parent ab037de commit b75915f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
4 changes: 2 additions & 2 deletions migrations/m151005_151946_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class m151005_151946_install extends Migration
{
public function up()
public function up(): void
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
Expand Down Expand Up @@ -75,7 +75,7 @@ public function up()
);
}

public function down()
public function down(): void
{
$this->dropTable(Constants::TABLE_ROLE_PERMISSIONS);
$this->dropTable(Constants::TABLE_PERMISSIONS);
Expand Down
4 changes: 2 additions & 2 deletions migrations/m170914_110639_alter_roles_permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
class m170914_110639_alter_roles_permissions extends Migration
{
public function up()
public function up(): void
{
//PERMISSIONS
$this->alterColumn(
Expand Down Expand Up @@ -38,7 +38,7 @@ public function up()
);
}

public function down()
public function down(): void
{
$this->dropIndex(
'k_' . Constants::TABLE_ROLES . '_status',
Expand Down
11 changes: 7 additions & 4 deletions src/BasePermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@

namespace cottacush\rbac;

use Exception;
use Yii;
use yii\base\Component;
use yii\helpers\ArrayHelper;

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

/**
* @throws Exception
* @author Adegoke Obasa <[email protected]>
* @inheritdoc
*/
public function canAccess($permissionKey)
public function canAccess($permissionKey): bool
{
$permissions = $this->getPermissions();
foreach ($permissions as $permission) {
Expand All @@ -30,9 +32,10 @@ public function canAccess($permissionKey)

/**
* @param $roleKey
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleByKey($roleKey)
public function setRoleByKey($roleKey): void
{
$role = $this->getRole($roleKey);
Yii::$app->session->set($this->sessionPrefix . '::user_role', $role);
Expand All @@ -43,7 +46,7 @@ public function setRoleByKey($roleKey)
* @return void
* @author Adegoke Obasa <[email protected]>
*/
public function setRoleById($roleId)
public function setRoleById($roleId): void
{
$role = $this->getRoleById($roleId);
Yii::$app->session->set($this->sessionPrefix . '::user_role', $role);
Expand Down
35 changes: 18 additions & 17 deletions src/DbPermissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,65 @@
use cottacush\rbac\models\Permission;
use cottacush\rbac\models\Role;
use Yii;
use yii\base\InvalidConfigException;
use yii\db\ActiveRecord;

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

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

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

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

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

/**
* @param $permissionId
* @return Permission
* @return array|Permission|ActiveRecord
* @author Adegoke Obasa <[email protected]>
*/
public function getPermissionById($permissionId)
public function getPermissionById($permissionId): array|ActiveRecord|Permission
{
return Permission::find()->where(['id' => $permissionId, 'status' => Constants::STATUS_ACTIVE])
->limit(1)->one();
Expand All @@ -75,18 +77,17 @@ public function getPermissionById($permissionId)
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function getUserRole()
public function getUserRole(): mixed
{
$role = Yii::$app->session->get($this->sessionPrefix . '::user_role');
return $role;
return Yii::$app->session->get($this->sessionPrefix . '::user_role');
}

/**
* @inheritdoc
* @throws \yii\base\InvalidConfigException
* @throws InvalidConfigException
* @author Adegoke Obasa <[email protected]>
*/
public function getUserPermissions()
public function getUserPermissions(): mixed
{
/**
* @var Role $role
Expand Down
22 changes: 11 additions & 11 deletions src/ManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,71 @@ interface ManagerInterface
* @return mixed
* @author Adegoke Obasa <[email protected]>
*/
public function canAccess($permissionKey);
public function canAccess($permissionKey): mixed;

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

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

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

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

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

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

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

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

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

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

0 comments on commit b75915f

Please sign in to comment.