You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

26 lines
750 B

<?php
namespace backend\widgets\grid;
use core\access\Rbac;
use Yii;
use yii\grid\DataColumn;
use yii\helpers\Html;
use yii\rbac\Item;
class RoleColumn extends DataColumn
{
protected function renderDataCellContent($model, $key, $index): string
{
$roles = Yii::$app->authManager->getRolesByUser($model->id);
return $roles === [] ? $this->grid->emptyCell : implode(', ', array_map(function (Item $role) {
return $this->getRoleLabel($role);
}, $roles));
}
private function getRoleLabel(Item $role): string
{
$class = $role->name == Rbac::ROLE_USER ? 'primary' : 'danger';
return Html::tag('span', Html::encode($role->description), ['class' => 'label label-' . $class]);
}
}