Yii Framework Forum: Yii Accessrules Delete Option Only For Authenticated User. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii Accessrules Delete Option Only For Authenticated User. Rate Topic: -----

#1 User is offline   Selvakumar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 14-August 13

Posted 17 January 2014 - 06:39 AM

Hi,
I have designed my view as, every logged user can post their own post. and when the user don't want or delete the particular post, that has been posted by the same user. I want to restrict all other users to delete other users post.. Only i want delete option in the particular post of particular users...
accessRules:
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 'create' and 'update' actions
		'actions' => array('create','index', 'update'),
		'users' => array('@'), ),
	array('allow', // allow admin user to perform 'admin' and 'delete' actions
		'actions' => array('admin', 'delete'),
		'users' => array('admin'), ),
	array('deny', // deny all users
		'users' => array('*'), ), );
}

Action:
public function actionDeletePost($id) {
	$this -> loadModel($id) -> delete();
}

View:
<img src="<?php echo Yii::app()->request->baseUrl;?>/images/site/delete-icon.png" alt="Delete this post" onclick="javascript: deletePost(this, <?php echo ($data->id) ? $data->id : '\'null\'' ?>);">

Script:
function deletePost(row,postId) {
    if (confirm('Are you sure want to delete this post?')) {
        if (postId != 'null') {
            //call an ajax request to delete the row...
            $.ajax({
                'type' : 'post',
                'url'  : '?r=forumPost/deletePost&id='+ postId,
                'dataType' : 'data',
                'beforeSend' : function() {},
                'success' : function(data) {
                            jQuery("#list-of-post").load("<?php echo Yii::app()->createUrl('//forumPost/forumPostDisplay'); ?>");
                    },
                  'error': function(data){
                  	alert("Error Occurred... Please try again later...!");
                  }
            });
        }
        // Remove UI component
        // $(row).parent().parent().parent().remove();
    }
}

Please help me to delete only the particular user post in the view page.. :(
0

#2 User is offline   hemc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 09-July 12
  • Location:New Delhi

Posted 18 January 2014 - 01:03 AM

I hope you are storing author_id for every post.

When you delete the post, just if author_id is equal to current logged in user id.

public function actionDeletePost($id) {
        $this -> loadModel($id) -> delete();
}


public function actionDeletePost($id) {
       $model= $this -> loadModel($id) ;
       if($model->author_id==Yii::app()->user->id){
        // delete here
        $model->delete();
       }else{
     // throw exception or error message for ajax call.
}
}

Please don't add a reply to say thanks, There is already a +1 button to do same.
1

#3 User is offline   Selvakumar 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 14-August 13

Posted 19 January 2014 - 07:54 AM

thanks hemc.. it's working fine now... :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users