简体   繁体   English

Yii2-验证main.php文件中模型的字段

[英]Yii2 - validate field from model in main.php file

I have a table called user, and inside it a field called estatuto. 我有一个名为user的表,并且在其中有一个名为estatuto的字段。

Only users with estatuto == admin can see Admin hyperlink in my NavBar widget located in main.php file. 只有具有estatuto == admin的用户才能在main.php文件中的NavBar小部件中看到Admin超链接。

My problem is to check in: 我的问题是签入:

'label' => 'Admin', 'visible' => !Yii::$app->user->isGuest,

if a user is logged with estatuto == admin, and only in that condition show the admin hyperlink. 如果用户使用estatuto == admin登录,并且仅在这种情况下显示admin超链接。

I can only check if a user is Guest or not Guest. 我只能检查用户是否为访客。

I need something like: 我需要类似的东西:

'label' => 'Admin', 'visible' => !Yii::$app->user->isGuest && User::estatuto = 'admin',

I searched a lot and the only thing i saw and couldn't understand was through: 我搜索了很多东西,但唯一看不到的是通过:

$this->view->params['customParam'] = 'customValue';

But maybe there's another aproach. 但是也许还有另外一种方法。

Anyone can help me solve this problem? 有人可以帮助我解决这个问题吗?

Your task is not about validating model field, it is classical permission management task. 您的任务不是关于验证模型字段,而是经典的权限管理任务。 So you should use Yii2 RBAC feature. 因此,您应该使用Yii2 RBAC功能。 in that case you can check permission like that: 在这种情况下,您可以像这样检查权限:

\Yii::$app->user->can('viewAdminMenu')

Store the value of estatuto in session when the user logs in. 用户登录时,将estatuto的值存储在会话中。

Then at any point just call that value from session. 然后,只要在会话中调用该值即可。

Also don't set the visible tag for access. 也不要设置可见标签进行访问。 You need to not show the entire link if not admin. 如果不是管理员,则无需显示整个链接。 Also hiding just links is not enough, you need to check for access within the controller as well. 另外,仅隐藏链接是不够的,您还需要检查控制器内的访问权限。

A better way to do this is use Role Based Access Control or RBAC. 更好的方法是使用基于角色的访问控制或RBAC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM