Skip to content

Commit

Permalink
feat. add ip ban system (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
StanByes authored Dec 11, 2021
1 parent 878ab9d commit 902871b
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 14 deletions.
1 change: 1 addition & 0 deletions app/Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AppSchema extends CakeSchema
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 20, 'unsigned' => false, 'key' => 'primary'],
'user_id' => ['type' => 'integer', 'null' => false, 'default' => null, 'length' => 20, 'unsigned' => false],
'reason' => ['type' => 'text', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
'ip' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
'indexes' => [
'PRIMARY' => ['column' => 'id', 'unique' => 1]
],
Expand Down
39 changes: 31 additions & 8 deletions app/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,25 @@ public function beforeFilter()

$LoginCondition = $this->here != "/login" || !$this->EyPlugin->isInstalled('phpierre.signinup');

$this->loadModel("Maintenance");
if ($this->params['controller'] != "user" and $this->params['controller'] != "maintenance" and !$this->Permissions->can("BYPASS_MAINTENANCE") and $maintenance = $this->Maintenance->checkMaintenance($this->here, $this->Util) and $LoginCondition) {
$this->redirect([
'controller' => 'maintenance',
'action' => $maintenance['url'],
'plugin' => false,
'admin' => false
]);
if ($this->params['controller'] != "user" and $LoginCondition) {
if ($this->isIPBan($this->Util->getIP()) and $this->params['controller'] != "ban" and !$this->Permissions->can("BYPASS_BAN")) {
$this->redirect([
'controller' => 'ban',
'action' => 'ip',
'plugin' => false,
'admin' => false
]);
}

$this->loadModel("Maintenance");
if ($this->params['controller'] != "maintenance" and !$this->Permissions->can("BYPASS_MAINTENANCE") and $maintenance = $this->Maintenance->checkMaintenance($this->here, $this->Util)) {
$this->redirect([
'controller' => 'maintenance',
'action' => $maintenance['url'],
'plugin' => false,
'admin' => false
]);
}
}

// Plugin disabled
Expand Down Expand Up @@ -671,4 +682,16 @@ public function sendJSON($data)
$this->autoRender = false;
return $this->response->body(json_encode($data));
}

public function isIPBan($ip) {
$this->loadModel("Ban");
$ipIsBan = $this->Ban->find('first', ['conditions' => ['ip' => $ip]]);

if (isset($ipIsBan["Ban"])) {
$this->isBanned = $ipIsBan["Ban"]["reason"];
return $this->isBanned;
} else {
return false;
}
}
}
28 changes: 23 additions & 5 deletions app/Controller/BanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ function index() {
$this->set('reason', $this->User->isBanned());
}

function ip() {
if (!$this->isIPBan($this->Util->getIP()))
$this->redirect("/");

$this->set('title_for_layout', $this->Lang->get("BAN__BAN"));
$this->set('reason', $this->isBanned);
}

function admin_index()
{
if (!$this->isConnected || !$this->Permissions->can("MANAGE_BAN"))
Expand Down Expand Up @@ -42,15 +50,22 @@ function admin_add()
if (empty($this->request->data("reason")))
return $this->response->body(json_encode(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]));

$this->loadModel("User");
foreach ($this->request->data as $key => $v) {
if ($v != "on" || $key == "name")
if ($v != "on" || $key == "name" || strpos($key, "-ip"))
continue;

$this->Ban->create();
$this->Ban->set([
"user_id" => $key,
"reason" => $this->request->data("reason")
"user_id" => $key,
"reason" => $this->request->data("reason")
]);

if ($this->request->data($key . "-ip") == "on")
$this->Ban->set([
"ip" => $this->User->find("first", ["conditions" => ['id' => $key]])['User']['ip']
]);

$this->Ban->save();
}

Expand Down Expand Up @@ -93,7 +108,7 @@ public function admin_get_users_not_ban()
$this->modelClass = 'User';
$this->DataTable->initialize($this);
$this->paginate = [
'fields' => ['User.id', 'User.pseudo', 'User.rank'],
'fields' => ['User.id', 'User.pseudo', 'User.rank', 'User.ip'],
];
$this->DataTable->mDataProp = true;
$response = $this->DataTable->getResponse();
Expand All @@ -113,11 +128,14 @@ public function admin_get_users_not_ban()
$rank_name = (isset($available_ranks[$value['User']['rank']])) ? $available_ranks[$value['User']['rank']]['name'] : $available_ranks[0]['name'];
$rank = '<span class="label label-' . $rank_label . '">' . $rank_name . '</span>';
$checkbox = "<input type='checkbox' name=" . $value['User']['id'] . ">";
$banIpCheckbox = "<input type='checkbox' name=" . $value['User']['id'] . "-ip>";
$data[] = [
'User' => [
'pseudo' => $username,
'ban' => $checkbox,
'rank' => $rank
'banIp' => $banIpCheckbox,
'rank' => $rank,
'ip' => $value['User']['ip']
]
];
}
Expand Down
6 changes: 5 additions & 1 deletion app/View/Ban/admin_add.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<th><?= $Lang->get('BAN__QUESTION') ?></th>
<th><?= $Lang->get('USER__TITLE') ?></th>
<th><?= $Lang->get('USER__RANK') ?></th>
<th>IP</th>
<th><?= $Lang->get('BAN__IP_QUESTION') ?></th>
</tr>
</thead>
</table>
Expand Down Expand Up @@ -55,7 +57,9 @@
"aoColumns": [
{mData: "User.ban", "bSearchable": false},
{mData: "User.pseudo", "bSearchable": true},
{mData: "User.rank", "bSearchable": false}
{mData: "User.rank", "bSearchable": false},
{mData: "User.ip", "bSearchable": true},
{mData: "User.banIp", "bSearchable": false}
]
});
});
Expand Down
2 changes: 2 additions & 0 deletions app/View/Ban/admin_index.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<tr>
<th><?= $Lang->get("USER__USERNAME") ?></th>
<th><?= $Lang->get("BAN__REASON") ?></th>
<th><?= $Lang->get("BAN__IS_BAN_IP") ?></th>
<th><?= $Lang->get("GLOBAL__ACTIONS")?></th>
</tr>
</thead>
Expand All @@ -27,6 +28,7 @@
}
} ?>
<td><?= $v["Ban"]["reason"] ?></td>
<td><?= $v["Ban"]["ip"] != null ? $v["Ban"]["ip"] : $Lang->get("BAN__NOT_BAN_IP") ?></td>
<td>
<a onClick="confirmDel('<?= $this->Html->url(['action' => 'unban', $v["Ban"]['id']]) ?>')"
class="btn btn-danger"><?= $Lang->get('BAN__UNBAN') ?></a>
Expand Down
10 changes: 10 additions & 0 deletions app/View/Ban/ip.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<br><br><br>
<div class="container">
<div class="row">
<div class="panel panel-default">
<div class="panel-body">
<?=$Lang->get("BAN__IP_EXPLICATION") . $reason ?>
</div>
</div>
</div>
</div>
4 changes: 4 additions & 0 deletions lang/en_UK.json
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@
"BAN__MEMBERS": "Banned members",
"BAN__HOME": "Bans",
"BAN__QUESTION": "Ban?",
"BAN__IP_QUESTION": "IP's ban ?",
"BAN__REASON": "Reason for ban",
"BAN__ADD": "Ban one or more members",
"BAN__SUCCESS": "Banned member(s)",
Expand All @@ -694,6 +695,9 @@
"BAN__UNBAN_SUCCESS": "Ban revoked",
"BAN__BAN": "Banned",
"BAN__EXPLICATION": "You have been banned for ",
"BAN__IP_EXPLICATION": "Your IP have been banned for ",
"BAN__IS_BAN_IP" : "The IP have been banned ?",
"BAN__NOT_BAN_IP" : "The IP is not banned",

"SOCIAL__TITLE": "Manage social networks",
"SOCIAL__HOME": "Social networks",
Expand Down
4 changes: 4 additions & 0 deletions lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@
"BAN__MEMBERS": "Banned members",
"BAN__HOME": "Bans",
"BAN__QUESTION": "Ban?",
"BAN__IP_QUESTION": "IP's ban ?",
"BAN__REASON": "Reason for ban",
"BAN__ADD": "Ban one or more members",
"BAN__SUCCESS": "Banned member(s)",
Expand All @@ -699,6 +700,9 @@
"BAN__UNBAN_SUCCESS": "Ban revoked",
"BAN__BAN": "Banned",
"BAN__EXPLICATION": "You have been banned for ",
"BAN__IP_EXPLICATION": "Your IP have been banned for ",
"BAN__IS_BAN_IP" : "The IP have been banned ?",
"BAN__NOT_BAN_IP" : "The IP is not banned",

"SOCIAL__TITLE": "Manage social networks",
"SOCIAL__HOME": "Social networks",
Expand Down
4 changes: 4 additions & 0 deletions lang/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,7 @@
"BAN__MEMBERS": "Membres bannis",
"BAN__HOME": "Bannissements",
"BAN__QUESTION": "Bannir ?",
"BAN__IP_QUESTION": "Bannir l'IP ?",
"BAN__REASON": "Raison du bannissement",
"BAN__ADD": "Bannir un ou des membres",
"BAN__SUCCESS": "Membre(s) banni(s)",
Expand All @@ -692,6 +693,9 @@
"BAN__UNBAN_SUCCESS": "Bannissement révoqué",
"BAN__BAN": "Bannis",
"BAN__EXPLICATION": "Vous avez été banni pour ",
"BAN__IP_EXPLICATION": "Votre IP a été bannie pour ",
"BAN__IS_BAN_IP" : "Bannissement d'IP ?",
"BAN__NOT_BAN_IP" : "L'IP n'est pas bannie",

"SOCIAL__TITLE": "Gérer les réseaux sociaux",
"SOCIAL__HOME": "Réseaux sociaux",
Expand Down
4 changes: 4 additions & 0 deletions lang/ru_RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@
"BAN__MEMBERS": "Запрещенные участники",
"BAN__HOME": "Баны",
"BAN__QUESTION": "Запретить?",
"BAN__IP_QUESTION": "Запретить IP?",
"BAN__REASON": "Причина бана",
"BAN__ADD": "Забанить одного или нескольких участников",
"BAN__SUCCESS": "Забаненный участник(ы)",
Expand All @@ -702,6 +703,9 @@
"BAN__UNBAN_SUCCESS": "Бан отменен",
"BAN__BAN": "Запрещено",
"BAN__EXPLICATION": "Вас забанили за ",
"BAN__IP_EXPLICATION": "Ваш IP заблокирован на ",
"BAN__IS_BAN_IP" : "Бан по IP?",
"BAN__NOT_BAN_IP" : "IP не забанен",

"SOCIAL__TITLE": "Управление социальными сетями",
"SOCIAL__HOME": "Социальные сети",
Expand Down

0 comments on commit 902871b

Please sign in to comment.