/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = BannedIP::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         return $dataProvider;
     }
     if (isset($this->ip_address) && trim($this->ip_address) != '') {
         $ipValue = ip2long($this->ip_address);
         $query->andFilterWhere(['or', ['start_ip_num' => $ipValue], ['and', ['<=', 'start_ip_num', $ipValue], ['>=', 'end_ip_num', $ipValue]]]);
     }
     $dataProvider->sort->attributes['ip_address'] = ['asc' => ['start_ip_num' => SORT_ASC], 'desc' => ['start_ip_num' => SORT_DESC]];
     $query->andFilterWhere(['id' => $this->id, 'is_active' => $this->is_active, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'start_ip_num_value', $this->start_ip_num_value])->andFilterWhere(['like', 'end_ip_num_value', $this->end_ip_num_value]);
     return $dataProvider;
 }
 /**
  * Finds the BannedIP model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return BannedIP the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = BannedIP::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #3
0
 /**
  * Return true if current user has banned ip
  * @return boolean 
  */
 public static function hasBannedIP()
 {
     $userIP = Yii::$app->getRequest()->getUserIP();
     $userIPlong = ip2long($userIP);
     if ($userIPlong == -1 || $userIPlong === false) {
         return false;
     }
     $bannedIPs = \common\models\BannedIP::find()->where(['is_active' => 1])->all();
     foreach ($bannedIPs as $bannedIP) {
         if (!isset($bannedIP->end_ip_num) || empty($bannedIP->end_ip_num)) {
             if ($userIPlong == $bannedIP->start_ip_num) {
                 return true;
             }
         } else {
             if ($userIPlong >= $bannedIP->start_ip_num && $userIPlong <= $bannedIP->end_ip_num) {
                 return true;
             }
         }
     }
     return false;
 }