How to view data only made by the user?

2019-08-29 01:24发布

问题:

I created a link and what it's supposed to do is take me to the page where it displays all the announcements that I posted but instead it shows me all of the announcements inside the database.

here is my link:

  <a class="more" href="<?php echo Yii::app()->createUrl('announcement')?>" ><?php switch_lang('View Announcements', '查看更多', FALSE)?></a>

This is the controller for announcement for the actionView() :

public function actionView() {

    $post=$this->loadModel();
    if(Persons::model()->compare_country(explode("|",$post->country)))
    {
        $post->view_count = $post->view_count + 1;
        Yii::app()->db->createCommand("UPDATE content SET view_count = {$post->view_count} WHERE id = {$post->id}")->execute();
        //$post->save();
        $comment=$this->newComment($post, 'view');

        if (!empty(Yii::app()->session['announcement_message']))
        {
            Yii::app()->user->setFlash('message',Yii::app()->session['announcement_message']);
            Yii::app()->session['announcement_message'] = null;
        }

        $this->render('view',array(
            'model'=>$post,
            'comment'=>$comment,
            'view'=>'view',
        ));
    }
    else
    {
        $this->redirect(Yii::app()->createAbsoluteUrl('news/index',array('page'=>'1')));
    }
}

回答1:

Yii supports the concept of the data owner in its access control implementation.

The first step to implementing this in your own application is to instruct the controller to enable this rule. This is done by overwriting the filters() function.

class ContentController extends Controller {

    public function filters() {
        return array(
            'accessControl'
        );
    }

    public function accessRules() {
    }
}

The 'accessControl' flag specifies that access control is applied for data management. The actual business rules are defined in the accessRules() function, and specifying the access control expression that will be evaluated to provide the desired control. And example of the function implementation is.

public function accessRules() {
    return array(
        array('allow', // allow all users to perform 'index' and 'view' actions
            'actions' => array('view'),
            'users' => array('*'),
        ),
        array('allow', // allow authenticated user to perform 'add' action
            'actions' => array('add'),
            'users' => array('@'),
        ),
        array('allow', // allow only the owner to perform 'modify' 'delete' actions
            'actions' => array('modify', 'delete'),
            'expression' => array('ContentController','isMyRecord')
        ),
        array('deny', // deny all users
            'users' => array('*'),
        ),
    );
}

The isMyRecord is a method that will be run that returns true or false to indicate if the action should be allowed.

public function isMyRecord(){
    $content_id = $_GET["content_id"];
    $person = Example::model()->findByPk($content_id); 
    if ($example->owner_id === Yii::app()->user->id)
       return true;
    else
       return false;
}


标签: php yii