diff --git a/migrations/m151005_151946_install.php b/migrations/m151005_151946_install.php index 5b58110..9757985 100644 --- a/migrations/m151005_151946_install.php +++ b/migrations/m151005_151946_install.php @@ -9,7 +9,7 @@ */ class m151005_151946_install extends Migration { - public function up() + public function up(): void { $tableOptions = null; if ($this->db->driverName === 'mysql') { @@ -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); diff --git a/migrations/m170914_110639_alter_roles_permissions.php b/migrations/m170914_110639_alter_roles_permissions.php index cf1ce67..c875aa2 100644 --- a/migrations/m170914_110639_alter_roles_permissions.php +++ b/migrations/m170914_110639_alter_roles_permissions.php @@ -9,7 +9,7 @@ */ class m170914_110639_alter_roles_permissions extends Migration { - public function up() + public function up(): void { //PERMISSIONS $this->alterColumn( @@ -38,7 +38,7 @@ public function up() ); } - public function down() + public function down(): void { $this->dropIndex( 'k_' . Constants::TABLE_ROLES . '_status', diff --git a/src/BasePermissionManager.php b/src/BasePermissionManager.php index 14f7dca..cd617ca 100644 --- a/src/BasePermissionManager.php +++ b/src/BasePermissionManager.php @@ -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 * @inheritdoc */ - public function canAccess($permissionKey) + public function canAccess($permissionKey): bool { $permissions = $this->getPermissions(); foreach ($permissions as $permission) { @@ -30,9 +32,10 @@ public function canAccess($permissionKey) /** * @param $roleKey + * @return mixed * @author Adegoke Obasa */ - public function setRoleByKey($roleKey) + public function setRoleByKey($roleKey): void { $role = $this->getRole($roleKey); Yii::$app->session->set($this->sessionPrefix . '::user_role', $role); @@ -43,7 +46,7 @@ public function setRoleByKey($roleKey) * @return void * @author Adegoke Obasa */ - public function setRoleById($roleId) + public function setRoleById($roleId): void { $role = $this->getRoleById($roleId); Yii::$app->session->set($this->sessionPrefix . '::user_role', $role); diff --git a/src/DbPermissionManager.php b/src/DbPermissionManager.php index 688f3d5..bfd024f 100644 --- a/src/DbPermissionManager.php +++ b/src/DbPermissionManager.php @@ -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 */ - public function getRoles() + public function getRoles(): array { return Role::find()->asArray()->all(); } /** * @param $key - * @return Role + * @return array|Role|ActiveRecord * @author Adegoke Obasa */ - 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 */ - 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 */ - public function getPermissions() + public function getPermissions(): array { return Permission::find()->asArray()->all(); } /** * @param $key - * @return Permission + * @return array|Permission|ActiveRecord * @author Adegoke Obasa */ - 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 */ - public function getPermissionById($permissionId) + public function getPermissionById($permissionId): array|ActiveRecord|Permission { return Permission::find()->where(['id' => $permissionId, 'status' => Constants::STATUS_ACTIVE]) ->limit(1)->one(); @@ -75,18 +77,17 @@ public function getPermissionById($permissionId) * @return mixed * @author Adegoke Obasa */ - 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 */ - public function getUserPermissions() + public function getUserPermissions(): mixed { /** * @var Role $role diff --git a/src/ManagerInterface.php b/src/ManagerInterface.php index 0e594dc..c78b3e8 100644 --- a/src/ManagerInterface.php +++ b/src/ManagerInterface.php @@ -14,71 +14,71 @@ interface ManagerInterface * @return mixed * @author Adegoke Obasa */ - public function canAccess($permissionKey); + public function canAccess($permissionKey): mixed; /** * @return mixed * @author Adegoke Obasa */ - public function getRoles(); + public function getRoles(): mixed; /** * @return mixed * @author Adegoke Obasa */ - public function getUserRole(); + public function getUserRole(): mixed; /** * @param $key * @return mixed * @author Adegoke Obasa */ - public function getRole($key); + public function getRole($key): mixed; /** * @param $roleId * @return mixed * @author Adegoke Obasa */ - public function getRoleById($roleId); + public function getRoleById($roleId): mixed; /** * @return mixed * @author Adegoke Obasa */ - public function getPermissions(); + public function getPermissions(): mixed; /** * @param $key * @return mixed * @author Adegoke Obasa */ - public function getPermission($key); + public function getPermission($key): mixed; /** * @param $permissionId * @return mixed * @author Adegoke Obasa */ - public function getPermissionById($permissionId); + public function getPermissionById($permissionId): mixed; /** * @param $roleKey * @return mixed * @author Adegoke Obasa */ - public function setRoleByKey($roleKey); + public function setRoleByKey($roleKey): void; /** * @param $roleId * @return mixed * @author Adegoke Obasa */ - public function setRoleById($roleId); + public function setRoleById($roleId): void; /** * @return mixed * @author Adegoke Obasa */ - public function getUserPermissions(); + public function getUserPermissions(): mixed; } \ No newline at end of file