Class yii\debug\panels\UserPanel
Inheritance | yii\debug\panels\UserPanel » yii\debug\Panel » yii\base\Component |
---|---|
Available since extension's version | 2.0.8 |
Source Code | https://github.com/yiisoft/yii2-debug/blob/master/src/panels/UserPanel.php |
Debugger panel that collects and displays user data.
Public Properties
Public Methods
Method | Description | Defined By |
---|---|---|
canSearchUsers() | Check is available search of users | yii\debug\panels\UserPanel |
canSwitchUser() | Check can main user switch identity. | yii\debug\panels\UserPanel |
getDetail() | yii\debug\panels\UserPanel | |
getError() | yii\debug\Panel | |
getName() | yii\debug\panels\UserPanel | |
getSummary() | yii\debug\panels\UserPanel | |
getTraceLine() | Returns a trace line | yii\debug\Panel |
getUrl() | yii\debug\Panel | |
getUser() | yii\debug\panels\UserPanel | |
getUserDataProvider() | Get model for GridView -> DataProvider | yii\debug\panels\UserPanel |
getUsersFilterModel() | Get model for GridView -> FilterModel | yii\debug\panels\UserPanel |
hasError() | yii\debug\Panel | |
init() | yii\debug\panels\UserPanel | |
isEnabled() | Checks whether this panel is enabled. | yii\debug\panels\UserPanel |
load() | Loads data into the panel | yii\debug\Panel |
save() | Saves data to be later used in debugger detail view. | yii\debug\panels\UserPanel |
setError() | yii\debug\Panel |
Protected Methods
Method | Description | Defined By |
---|---|---|
dataToString() | Converts mixed data to string | yii\debug\panels\UserPanel |
getLogMessages() | Gets messages from log target and filters according to their categories and levels. | yii\debug\Panel |
identityData() | Returns the array that should be set on \yii\widgets\DetailView::model | yii\debug\panels\UserPanel |
Property Details
Display Name of the debug panel.
Allowed columns for GridView.
See also https://www.yiiframework.com/doc-2.0/yii-grid-gridview.html#$columns-detail.
Implements of User model with search method.
The rule which defines who allowed to switch user identity. Access Control Filter single rule. Ignore: actions, controllers, verbs. Settable: allow, roles, ips, matchCallback, denyCallback. By default deny for everyone. Recommendation: can allow for administrator or developer (if implement) role: ['allow' => true, 'roles' => ['admin']]
See also https://www.yiiframework.com/doc-2.0/guide-security-authorization.html.
ID of the user component or a user object
Object of switching users
Method Details
Check is available search of users
public boolean canSearchUsers ( ) |
public function canSearchUsers()
{
return (isset($this->filterModel) &&
$this->filterModel instanceof Model &&
$this->filterModel->hasMethod('search')
);
}
Check can main user switch identity.
public boolean canSwitchUser ( ) | ||
throws | \yii\base\InvalidConfigException |
---|
public function canSwitchUser()
{
if ($this->getUser()->isGuest) {
return false;
}
$allowSwitchUser = false;
$rule = new AccessRule($this->ruleUserSwitch);
/** @var Controller $userController */
$userController = null;
$controller = $this->module->createController('user');
if (isset($controller[0]) && $controller[0] instanceof UserController) {
$userController = $controller[0];
}
//check by rule
if ($userController) {
$action = $userController->createAction('set-identity');
$user = $this->userSwitch->getMainUser();
$request = Yii::$app->request;
$allowSwitchUser = $rule->allows($action, $user, $request) ?: false;
}
return $allowSwitchUser;
}
Converts mixed data to string
protected string dataToString ( $data ) | ||
$data | mixed |
protected function dataToString($data)
{
if (is_string($data)) {
return $data;
}
return VarDumper::export($data);
}
public string getDetail ( ) | ||
return | string |
Content that is displayed in debugger detail view |
---|
public function getDetail()
{
return Yii::$app->view->render('panels/user/detail', ['panel' => $this]);
}
Defined in: yii\debug\Panel::getError()
public yii\debug\FlattenException|null getError ( ) |
public function getError()
{
return $this->error;
}
Defined in: yii\debug\Panel::getLogMessages()
Gets messages from log target and filters according to their categories and levels.
See also \yii\log\Target::filterMessages().
protected array getLogMessages ( $levels = 0, $categories = [], $except = [], $stringify = false ) | ||
$levels | integer |
The message levels to filter by. This is a bitmap of level values. Value 0 means allowing all levels. |
$categories | array |
The message categories to filter by. If empty, it means all categories are allowed. |
$except | array |
The message categories to exclude. If empty, it means all categories are allowed. |
$stringify | boolean |
Convert non-string (such as closures) to strings |
return | array |
The filtered messages. |
---|
protected function getLogMessages($levels = 0, $categories = [], $except = [], $stringify = false)
{
$target = $this->module->logTarget;
$messages = $target->filterMessages($target->messages, $levels, $categories, $except);
if (!$stringify) {
return $messages;
}
foreach ($messages as &$message) {
if (!isset($message[0]) || is_string($message[0])) {
continue;
}
// exceptions may not be serializable if in the call stack somewhere is a Closure
if ($message[0] instanceof \Throwable || $message[0] instanceof \Exception) {
$message[0] = (string) $message[0];
} else {
$message[0] = VarDumper::export($message[0]);
}
}
return $messages;
}
public string getName ( ) | ||
return | string |
Name of the panel |
---|
public function getName()
{
return $this->displayName;
}
public string getSummary ( ) | ||
return | string |
Content that is displayed at debug toolbar |
---|
public function getSummary()
{
return Yii::$app->view->render('panels/user/summary', ['panel' => $this]);
}
Defined in: yii\debug\Panel::getTraceLine()
Returns a trace line
public string getTraceLine ( $options ) | ||
$options | array |
The array with trace |
return | string |
The trace line |
---|
public function getTraceLine($options)
{
/**
* If an internal PHP function, such as `call_user_func`, in the backtrace, the 'file' and 'line' not be available.
* @see https://www.php.net/manual/en/function.debug-backtrace.php#59713
*/
if (!isset($options['file'])) {
return VarDumper::dumpAsString($options);
}
if (!isset($options['text'])) {
$options['text'] = "{$options['file']}:{$options['line']}";
}
$traceLine = $this->module->traceLine;
if ($traceLine === false) {
return $options['text'];
}
$options['file'] = str_replace('\\', '/', $options['file']);
foreach ($this->module->tracePathMappings as $old => $new) {
$old = rtrim(str_replace('\\', '/', $old), '/') . '/';
if (StringHelper::startsWith($options['file'], $old)) {
$new = rtrim(str_replace('\\', '/', $new), '/') . '/';
$options['file'] = $new . substr($options['file'], strlen($old));
break;
}
}
$rawLink = $traceLine instanceof \Closure ? $traceLine($options, $this) : $traceLine;
return strtr($rawLink, ['{file}' => $options['file'], '{line}' => $options['line'], '{text}' => $options['text']]);
}
Defined in: yii\debug\Panel::getUrl()
public string getUrl ( $additionalParams = null ) | ||
$additionalParams | null|array |
Optional additional parameters to add to the route |
return | string |
URL pointing to panel detail view |
---|
public function getUrl($additionalParams = null)
{
$route = [
'/' . $this->module->getUniqueId() . '/default/view',
'panel' => $this->id,
'tag' => $this->tag,
];
if (is_array($additionalParams)) {
$route = ArrayHelper::merge($route, $additionalParams);
}
return Url::toRoute($route);
}
public \yii\web\User|null getUser ( ) | ||
throws | \yii\base\InvalidConfigException |
---|
public function getUser()
{
/* @var $user User */
return is_string($this->userComponent) ? Yii::$app->get($this->userComponent, false) : $this->userComponent;
}
Get model for GridView -> DataProvider
public \yii\data\DataProviderInterface getUserDataProvider ( ) |
public function getUserDataProvider()
{
return $this->getUsersFilterModel()->search(Yii::$app->request->queryParams);
}
Get model for GridView -> FilterModel
public \yii\base\Model|yii\debug\models\search\UserSearchInterface getUsersFilterModel ( ) |
public function getUsersFilterModel()
{
return $this->filterModel;
}
Defined in: yii\debug\Panel::hasError()
public boolean hasError ( ) |
public function hasError()
{
return $this->error !== null;
}
Returns the array that should be set on \yii\widgets\DetailView::model
protected array identityData ( $identity ) | ||
$identity | \yii\web\IdentityInterface |
protected function identityData($identity)
{
if ($identity instanceof Model) {
return $identity->getAttributes();
}
return get_object_vars($identity);
}
public void init ( ) | ||
throws | \yii\base\InvalidConfigException |
---|
public function init()
{
if (!$this->isEnabled() || $this->getUser()->isGuest) {
return;
}
$this->userSwitch = new UserSwitch(['userComponent' => $this->userComponent]);
$this->addAccessRules();
if (is_string($this->filterModel)
&& class_exists($this->filterModel)
&& in_array('yii\debug\models\search\UserSearchInterface', class_implements($this->filterModel), true)
) {
$this->filterModel = new $this->filterModel;
} elseif ($this->getUser() && $this->getUser()->identityClass) {
if (is_subclass_of($this->getUser()->identityClass, 'yii\db\ActiveRecord')) {
$this->filterModel = new \yii\debug\models\search\User();
}
}
}
Checks whether this panel is enabled.
public boolean isEnabled ( ) | ||
return | boolean |
Whether this panel is enabled. |
---|
public function isEnabled()
{
try {
$this->getUser();
} catch (InvalidConfigException $exception) {
return false;
}
return true;
}
Defined in: yii\debug\Panel::load()
Loads data into the panel
public void load ( $data ) | ||
$data | mixed |
public function load($data)
{
$this->data = $data;
}
Saves data to be later used in debugger detail view.
This method is called on every page where debugger is enabled.
public mixed save ( ) | ||
return | mixed |
Data to be saved |
---|
public function save()
{
$identity = Yii::$app->{$this->userComponent}->identity;
if (!isset($identity)) {
return null;
}
$rolesProvider = null;
$permissionsProvider = null;
try {
$authManager = Yii::$app->getAuthManager();
if ($authManager instanceof \yii\rbac\ManagerInterface) {
$roles = ArrayHelper::toArray($authManager->getRolesByUser($this->getUser()->id));
foreach ($roles as &$role) {
$role['data'] = $this->dataToString($role['data']);
}
unset($role);
$rolesProvider = new ArrayDataProvider([
'allModels' => $roles,
]);
$permissions = ArrayHelper::toArray($authManager->getPermissionsByUser($this->getUser()->id));
foreach ($permissions as &$permission) {
$permission['data'] = $this->dataToString($permission['data']);
}
unset($permission);
$permissionsProvider = new ArrayDataProvider([
'allModels' => $permissions,
]);
}
} catch (\Exception $e) {
// ignore auth manager misconfiguration
}
$identityData = $this->identityData($identity);
foreach ($identityData as $key => $value) {
$identityData[$key] = VarDumper::dumpAsString($value);
}
// If the identity is a model, let it specify the attribute labels
if ($identity instanceof Model) {
$attributes = [];
foreach (array_keys($identityData) as $attribute) {
$attributes[] = [
'attribute' => $attribute,
'label' => $identity->getAttributeLabel($attribute),
];
}
} else {
// Let the DetailView widget figure the labels out
$attributes = null;
}
return [
'id' => $identity->getId(),
'identity' => $identityData,
'attributes' => $attributes,
'rolesProvider' => $rolesProvider,
'permissionsProvider' => $permissionsProvider,
];
}
Defined in: yii\debug\Panel::setError()
public void setError ( yii\debug\FlattenException $error ) | ||
$error | yii\debug\FlattenException |
public function setError(FlattenException $error)
{
$this->error = $error;
}