简体   繁体   English

YII2如何根据登录用户类型处理条件?

[英]YII2 how to handle condition as per login usertype?

在此处输入图片说明 vendorinfo.php I have to hide and show button as per login usertype.Here if login user is 'Visitor' then only show the rating button ow in any condition like may be user is admin or now any user without login condition is also there. vendorinfo.php我必须根据登录用户类型隐藏并显示按钮。如果登录用户是“访问者”,则仅在任何情况下(例如,如果用户是admin或现在没有登录条件的任何用户)显示评分按钮。 My code is work if usertype is 'Visitor' or 'Admin' but it fails when no one login. 如果用户类型为“ Visitor”或“ Admin”,我的代码将正常工作,但是如果没有人登录,它将失败。 Actually that time this condition can not able to getting the type of user For that please help me I am not able to understand to handle this condition 其实那个时候这种情况无法得到用户的类型。为此,请帮我,我不明白如何处理这种情况。

<?php
if(!\Yii::$app->user->isGuest) {
$user = \Yii::$app->user->identity->id;
  $userType = \Yii::$app->user->identity->type;
  echo ''.$userType;
  echo '<input type="text" value='.$userType.' id="usertype"/>';
} else {
  echo '<div>Div if user is not visitor</div>';
}
?>

Script: script for hide and show the div 脚本:隐藏和显示div的脚本

<script type="text/javascript">
  $(document).ready(function(){
    var userType= document.getElementById('usertype').value;

    if(userType == 'Visitor') {  
      $('#divUserRate').show();
    } else {  
      $('#divUserRate').hide();  
    }
});

</script>

You can use isGuest property from user model. 您可以从用户模型使用isGuest属性。

echo (Yii::$app->user->isGuest) ? '<div>Div if user is visitor</div>' : '<div>Div if user is not visitor</div>';

You can also use Rbac to manage roles and access. 您还可以使用Rbac来管理角色和访问。

if(Yii::$app->authManager->checkAccess(Yii::$app->user->id, "editor"){
    //do something
}

For hide div.I have add class in button after that hide that class instead of div 对于隐藏div。我在按钮中添加了类,然后隐藏该类而不是div

<script type="text/javascript">
 $(document).ready(function(){

    $('.btnRating').hide(); 
    $('#divliveventGallery').hide();
    var userType= document.getElementById('usertype').value;
    if(userType == 'Visitor' || userType == 'FB' || userType == 'GL') {  
      $('.btnRating').show(); 
    } else if(userType == 'Vendor'){ 
      $('.btnRating').hide();  
    } else if(userType == "") {
      $('.btnRating').hide(); 
    }
});
**************Button html**************
 echo '<button type="button" id="btnRate" onClick="rateDetail('.$dp['ven_id'].','.$userId.');" class=" btnRating btn btn-default btn-block" data-toggle="modal" data-target="#rcrate">';
             echo '<strong>Rate</strong>';
             echo '</button> ';

</script>

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

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