-
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.
- Loading branch information
Showing
5 changed files
with
40 additions
and
36 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
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 |
---|---|---|
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
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,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(); | ||
|
@@ -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 | ||
|
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 |
---|---|---|
|
@@ -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; | ||
} |