Created
October 9, 2016 11:49
-
-
Save ydatech/4b0743e1eb8357ff38623f60aa8eee30 to your computer and use it in GitHub Desktop.
KontakController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace app\controllers; | |
| use Yii; | |
| use yii\data\ActiveDataProvider; | |
| use yii\web\ForbiddenHttpException; | |
| class KontakController extends ActiveController{ | |
| public $modelClass = "app\models\Kontak"; | |
| public function actions() | |
| { | |
| $actions = parent::actions(); | |
| // customize the data provider preparation with the "prepareDataProvider()" method | |
| $actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider']; | |
| return $actions; | |
| } | |
| public function prepareDataProvider() | |
| { | |
| $modelClass = $this->modelClass; | |
| // tampilkan data kontak yang hanya dibuat oleh user | |
| $query = $modelClass::find() | |
| ->where(['user_id'=>Yii::$app->user->id]); | |
| $dataprovider = new ActiveDataProvider([ | |
| 'query'=>$query, | |
| 'pagination'=>false | |
| ]); | |
| return $dataprovider; | |
| } | |
| /* | |
| * Lakukan autorisasi akses | |
| * | |
| */ | |
| public function checkAccess($action, $model = null, $params = []){ | |
| if(!$model == null && $model->user_id !== Yii::$app->user->id){ | |
| throw new ForbiddenHttpException(Yii::t('yii','Kamu hanya bisa mengakses kontak yang kamu buat')); | |
| } | |
| else{ | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment