经理如何使用RBAC在yii2中看到自己的客户

经理如何使用RBAC在yii2中看到自己的客户,yii2,rbac,Yii2,Rbac,我有一个customer表,其中有customername、address和salesmanagername。 发票数据存储在另一个表-invoice中,其中我有发票详细信息customername。 我在customer.customername和invoice.customername之间有关系。 我有一个用户表,用户通过它登录。SalesManager的全名存储在用户表中。 我的问题是SalesManager只有在登录时才能查看与客户相关的数据。 我使用RBAC和DbManager&Yii

我有一个customer表,其中有customername、address和salesmanagername。 发票数据存储在另一个表-invoice中,其中我有发票详细信息customername。 我在customer.customername和invoice.customername之间有关系。 我有一个用户表,用户通过它登录。SalesManager的全名存储在用户表中。 我的问题是SalesManager只有在登录时才能查看与客户相关的数据。 我使用RBAC和DbManager&Yii框架。我创建了一个模块、模型和crud应用程序- 它显示所有客户的发票详细信息以及相应的salesmanager名称。 我希望SalesManager只查看与他相关的数据。 如果需要更多信息,请告诉我。 我读过了,但看不懂。 请帮忙

这是我的账单模型

<?php

namespace frontend\modules\salebills\models;

use Yii;

/**
 * This is the model class for table "bills".
 *
 * @property string $billid
 * @property integer $bills_ebillid
 * @property string $bills_year
 * @property string $console
 * @property string $billno
 * @property string $billdate
 * @property string $bills_partyname
 * @property integer $billamount
 * @property string $pdate1
 * @property integer $payment1
 * @property string $details1
 * @property string $pdate2
 * @property integer $payment2
 * @property string $details2
 * @property string $pdate3
 * @property integer $payment3
 * @property string $details3
 * @property string $pdate4
 * @property integer $payment4
 * @property string $details4
 * @property integer $totalpayment
 * @property integer $bills_tc
 * @property integer $bills_tc_approval
 * @property integer $doctorsgift
 * @property integer $mrcommision
 * @property string $mrname
 * @property integer $bills_other
 * @property string $bills_specify_other
 * @property integer $bills_other_approval
 * @property integer $overdue
 * @property string $cst
 * @property string $wbst
 * @property integer $caseno
 * @property string $amount
 * @property string $discount
 * @property string $tot
 * @property string $surcharge
 * @property string $total
 * @property string $tax
 * @property string $mrpvalue
 * @property string $cstpercent
 * @property string $wbstpercent
 * @property string $surpercent
 * @property string $totpercent
 * @property string $transport
 *
 * @property Parties $billsPartyname
 * @property Productsales[] $productsales
 */
class Bills extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'bills';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['bills_ebillid'], 'required'],
            [['bills_ebillid', 'billamount', 'payment1', 'payment2', 'payment3', 'payment4', 'totalpayment', 'bills_tc', 'bills_tc_approval', 'doctorsgift', 'mrcommision', 'bills_other', 'bills_other_approval', 'overdue', 'caseno'], 'integer'],
            [['billdate', 'pdate1', 'pdate2', 'pdate3', 'pdate4'], 'safe'],
            [['mrname', 'bills_specify_other'], 'string'],
            [['bills_year'], 'string', 'max' => 8],
            [['console', 'cstpercent', 'wbstpercent', 'surpercent', 'totpercent'], 'string', 'max' => 6],
            [['billno'], 'string', 'max' => 10],
            [['bills_partyname'], 'string', 'max' => 60],
            [['details1', 'details2', 'details3', 'details4'], 'string', 'max' => 50],
            [['cst', 'wbst', 'amount', 'discount', 'tot', 'surcharge', 'total', 'tax', 'mrpvalue'], 'string', 'max' => 15],
            [['transport'], 'string', 'max' => 30],
            [['bills_ebillid'], 'unique']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'billid' => 'Billid',
            'bills_ebillid' => 'Bills Ebillid',
            'bills_year' => 'Bills Year',
            'console' => 'Console',
            'billno' => 'Billno',
            'billdate' => 'Billdate',
            'bills_partyname' => 'Bills Partyname',
            'billamount' => 'Billamount',
            'pdate1' => 'Pdate1',
            'payment1' => 'Payment1',
            'details1' => 'Details1',
            'pdate2' => 'Pdate2',
            'payment2' => 'Payment2',
            'details2' => 'Details2',
            'pdate3' => 'Pdate3',
            'payment3' => 'Payment3',
            'details3' => 'Details3',
            'pdate4' => 'Pdate4',
            'payment4' => 'Payment4',
            'details4' => 'Details4',
            'totalpayment' => 'Totalpayment',
            'bills_tc' => 'Bills Tc',
            'bills_tc_approval' => 'Bills Tc Approval',
            'doctorsgift' => 'Doctorsgift',
            'mrcommision' => 'Mrcommision',
            'mrname' => 'Mrname',
            'bills_other' => 'Bills Other',
            'bills_specify_other' => 'Bills Specify Other',
            'bills_other_approval' => 'Bills Other Approval',
            'overdue' => 'Overdue',
            'cst' => 'Cst',
            'wbst' => 'Wbst',
            'caseno' => 'Caseno',
            'amount' => 'Amount',
            'discount' => 'Discount',
            'tot' => 'Tot',
            'surcharge' => 'Surcharge',
            'total' => 'Total',
            'tax' => 'Tax',
            'mrpvalue' => 'Mrpvalue',
            'cstpercent' => 'Cstpercent',
            'wbstpercent' => 'Wbstpercent',
            'surpercent' => 'Surpercent',
            'totpercent' => 'Totpercent',
            'transport' => 'Transport',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBillsPartyname()
    {
        return $this->hasOne(Parties::className(), ['parties_partyname' => 'bills_partyname']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProductsales()
    {
        return $this->hasMany(Productsales::className(), ['productsales_ebillid' => 'bills_ebillid']);
    }
}
这是我的索引文件

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\salebills\models\BillsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Bills';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="bills-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <!-- <p>
        <?= Html::a('Create Bills', ['create'], ['class' => 'btn btn-success']) ?>
    </p> -->

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            //'billid',
            //'bills_ebillid',
            //'bills_year',
            //'console',
            'billno',
            'billdate',
            'bills_partyname',
            'billamount',
            'billsPartyname.name_manager'
            // 'pdate1',
            // 'payment1',
            // 'details1',
            // 'pdate2',
            // 'payment2',
            // 'details2',
            // 'pdate3',
            // 'payment3',
            // 'details3',
            // 'pdate4',
            // 'payment4',
            // 'details4',
            // 'totalpayment',
            // 'bills_tc',
            // 'bills_tc_approval',
            // 'doctorsgift',
            // 'mrcommision',
            // 'mrname:ntext',
            // 'bills_other',
            // 'bills_specify_other:ntext',
            // 'bills_other_approval',
            // 'overdue',
            // 'cst',
            // 'wbst',
            // 'caseno',
            // 'amount',
            // 'discount',
            // 'tot',
            // 'surcharge',
            // 'total',
            // 'tax',
            // 'mrpvalue',
            // 'cstpercent',
            // 'wbstpercent',
            // 'surpercent',
            // 'totpercent',
            // 'transport',

            //['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

</div>

要允许SalesManager仅查看与其相关的数据,您可以访问基于数据提供程序的正确视图(gridView),该数据提供程序在SalesManager代码上固定了一个过滤器

您可以将RBAC功能(Yii::$app->User->can('SalesManager')与ModelSearch中的适当搜索功能进行耦合。。 或者简单地向数据提供程序添加一个适当的where条件来检索销售

在你的行动中。。。像这样的事情

    $searchModel = new YourSalesSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    if ( Yii::$app->User->can('SalesManager') ){
          $dataProvider->query->
              andWhere(['your_sales_manager_key'=> $actual_sales_manager_code]);
         return $this->render('your_sales_manager_view', [
        'dataProvider' => $dataProvider,
     ]);
    }

    if ( Yii::$app->User->can('GeneralManager') ){
          // the general manager see all the sales of all the sales manager
         return $this->render('your_general_manager_view', [
        'dataProvider' => $dataProvider,
     ]);
    }
在你的情况下,你的指数可以是

  public function actionIndex()
  {

      // You need the proper name for matching the name_manager 
      // If is the same of username you can use 
      $salesManager  = Yii::$app->user->identity->username
      // otherwhise you can find ........ whit proper
      // $salesManagerModel =  YourUserModel::find()->where(['your_column_name' => Yii::$app->user->identity->username])->one();
      // $salesManager  = $salesManagerModel->name;
      $searchModel = new BillsSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if ( Yii::$app->User->can('s_viewownsalebills')){
       $dataProvider->query->
           andWhere(['billsPartyname.name_manager'=>  $salesManager  ]);
      return $this->render('index', [
     'dataProvider' => $dataProvider,
  ]);
  }
对于未定义的searchModel,您可以尝试在index.php的gridView中进行注释

   <?= GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
您没有加入BillsSearch for BillsPattyName 我认为你应该改变这种方式

  .......

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1'); 
        $query->joinWith(['billsPartyname']);
        return $dataProvider;
    }

    .......

未获取$dataProvider->query->andWhere(['your\u sales\u manager\u key'=>$actual\u sales\u manager\u code]);部分我有客户表,其中有managername和cutomername。我有发票表,其中有客户名称和发票详细信息。customer.customername和invoice.customername之间存在关系。如果要呈现grdiview,通常使用数据提供程序。。相反,您希望显示模型中的数据?您所问的问题与RBAC无关,但与RBAC的关系如何。。在模型/表格之间。。主要似乎是一个应用程序设计问题。一旦定义了关系的函数,您是否可以应用RBAC来使用适当的函数。我已经在上面描述了我的关系。我添加了控制器的图像。你可以看到我正在使用你给我的代码。我已经在图像中添加了我的表的关系。当事人是指客户。但是销售经理和客户之间通过账单的关系是如何建立的??
    $searchModel = new YourSalesSearch(); 
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    if ( Yii::$app->User->can('SalesManager') ){
          $dataProvider->query->
              andWhere(['your_sales_manager_key'=> $actual_sales_manager_code]);
         return $this->render('your_sales_manager_view', [
        'dataProvider' => $dataProvider,
     ]);
    }

    if ( Yii::$app->User->can('GeneralManager') ){
          // the general manager see all the sales of all the sales manager
         return $this->render('your_general_manager_view', [
        'dataProvider' => $dataProvider,
     ]);
    }
  public function actionIndex()
  {

      // You need the proper name for matching the name_manager 
      // If is the same of username you can use 
      $salesManager  = Yii::$app->user->identity->username
      // otherwhise you can find ........ whit proper
      // $salesManagerModel =  YourUserModel::find()->where(['your_column_name' => Yii::$app->user->identity->username])->one();
      // $salesManager  = $salesManagerModel->name;
      $searchModel = new BillsSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if ( Yii::$app->User->can('s_viewownsalebills')){
       $dataProvider->query->
           andWhere(['billsPartyname.name_manager'=>  $salesManager  ]);
      return $this->render('index', [
     'dataProvider' => $dataProvider,
  ]);
  }
   <?= GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
           return $this->render('index', [
     'dataProvider' => $dataProvider,
     'searchModel' = $searchModel,
  ]);
  .......

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1'); 
        $query->joinWith(['billsPartyname']);
        return $dataProvider;
    }

    .......