Skip to content

Commit

Permalink
feat. impl. new ban system -> StanByes
Browse files Browse the repository at this point in the history
  • Loading branch information
nivcoo committed Aug 28, 2021
1 parent 0304cec commit 92ce8b7
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 28 deletions.
11 changes: 11 additions & 0 deletions app/Config/Schema/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ class AppSchema extends CakeSchema
],
'tableParameters' => ['charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB']
];

public $bans = [
'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' => 'string', 'null' => false, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
'indexes' => [
'PRIMARY' => ['column' => 'id', 'unique' => 1]
],
'tableParameters' => ['charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB']
];

public $cake_sessions = [
'id' => ['type' => 'string', 'null' => false, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1', 'key' => 'primary'],
'data' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'],
Expand Down
44 changes: 28 additions & 16 deletions app/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ class AppController extends Controller
public $view = 'Theme';

protected $isConnected = false;
protected $isBanned = false;

public function beforeFilter()
{

// lowercase to avoid errors when the controller is called with uppercase
$this->params['controller'] = strtolower($this->params['controller']);
$this->params['action'] = strtolower($this->params['action']);
// Plugin disabled
if ($this->request->params['plugin']) {
$plugin = $this->EyPlugin->findPlugin('slugLower', $this->request->params['plugin']);
Expand Down Expand Up @@ -89,28 +94,17 @@ public function beforeFilter()
return $event->result;
}
$LoginCondition = ($this->here != "/login") || !$this->EyPlugin->isInstalled('phpierre.signinup');
// Maintenance / Bans
// lowercase to avoid errors when the controller is called with uppercase
$this->params['controller'] = strtolower($this->params['controller']);
$this->params['action'] = strtolower($this->params['action']);
if ($this->isConnected and $this->User->getKey('rank') == 5 and $this->params['controller'] != "maintenance" and $this->params['action'] != "logout" and $this->params['controller'] != "api") {
// Maintenance
if ($this->params['controller'] != "user" && $this->params['controller'] != "maintenance" && $this->Configuration->getKey('maintenance') != '0' && !$this->Permissions->can('BYPASS_MAINTENANCE') && $LoginCondition) {
$this->redirect([
'controller' => 'maintenance',
'action' => 'index/banned',
'action' => 'index',
'plugin' => false,
'admin' => false
]);
} else {
if ($this->params['controller'] != "user" && $this->params['controller'] != "maintenance" && $this->Configuration->getKey('maintenance') != '0' && !$this->Permissions->can('BYPASS_MAINTENANCE') && $LoginCondition) {
$this->redirect([
'controller' => 'maintenance',
'action' => 'index',
'plugin' => false,
'admin' => false
]);
}
}


}

public function __initConfiguration()
Expand Down Expand Up @@ -212,6 +206,19 @@ private function __initUser()
$this->isConnected = $this->User->isConnected();
$this->set('isConnected', $this->isConnected);

if ($this->isConnected) {
$LoginCondition = ($this->here != "/login") || !$this->EyPlugin->isInstalled('phpierre.signinup');
if ($this->params['controller'] != "user" and $this->params['controller'] != "ban" and $this->User->isBanned() != false and $LoginCondition) {
$this->isBanned = $this->User->isBanned();

$this->redirect([
'controller' => 'ban',
'action' => 'index',
'plugin' => false,
'admin' => false
]);
}
}
$user = ($this->isConnected) ? $this->User->getAllFromCurrentUser() : [];
if (!empty($user))
$user['isAdmin'] = $this->User->isAdmin();
Expand Down Expand Up @@ -249,6 +256,11 @@ public function __initAdminNavbar()
'permission' => 'MANAGE_USERS',
'route' => ['controller' => 'user', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'BAN__MEMBERS' => [
'icon' => 'users',
'permission' => 'MANAGE_BAN',
'route' => ['controller' => 'ban', 'action' => 'index', 'admin' => true, 'plugin' => false]
],
'PERMISSIONS__LABEL' => [
'icon' => 'user',
'permission' => 'MANAGE_PERMISSIONS',
Expand Down Expand Up @@ -572,7 +584,7 @@ public function sendGetRequest($url)

public function sendMultipleGetRequests($urls)
{
if(!is_array($urls))
if (!is_array($urls))
$urls = [$urls];
$multi = curl_multi_init();
$channels = [];
Expand Down
130 changes: 130 additions & 0 deletions app/Controller/BanController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

class BanController extends AppController
{
function index() {
if (!$this->isConnected || $this->User->isBanned() == false)
$this->redirect("/");

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

function admin_index()
{
if (!$this->isConnected || !$this->Permissions->can("MANAGE_BAN"))
throw new ForbiddenException();

$this->set('title_for_layout', $this->Lang->get("BAN__HOME"));
$this->layout = 'admin';

$this->loadModel("Ban");
$this->loadModel("User");
$banned_users = $this->Ban->find("all");

$users = $this->User->find("all");

$this->set(compact("banned_users", "users"));
}

function admin_add()
{
if (!$this->isConnected || !$this->Permissions->can("MANAGE_BAN"))
throw new ForbiddenException();

$this->set('title_for_layout', $this->Lang->get("BAN__HOME"));
$this->layout = 'admin';

if ($this->request->is("post")) {
$this->autoRender = false;
$this->response->type('json');

if (empty($this->request->data("reason")))
return $this->response->body(json_encode(['statut' => false, 'msg' => $this->Lang->get('ERROR__FILL_ALL_FIELDS')]));

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

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

$this->response->body(json_encode(['statut' => true, 'msg' => $this->Lang->get('BAN__SUCCESS')]));
}
}

function admin_unban($id = false)
{
if (!$this->isConnected || !$this->Permissions->can("MANAGE_BAN"))
throw new ForbiddenException();

$this->loadModel('Ban');
$this->Ban->delete($id);
$this->Session->setFlash($this->Lang->get('BAN__UNBAN_SUCCESS'), 'default.success');
$this->redirect(['controller' => 'ban', 'action' => 'index', 'admin' => true]);
}

public function admin_get_users_not_ban()
{
if ($this->isConnected and $this->Permissions->can('MANAGE_BAN')) {
$this->autoRender = false;
$this->response->type('json');
if ($this->request->is('ajax')) {
$available_ranks = [
0 => ['label' => 'success', 'name' => $this->Lang->get('USER__RANK_MEMBER')],
2 => ['label' => 'warning', 'name' => $this->Lang->get('USER__RANK_MODERATOR')],
3 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
4 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
5 => ['label' => 'primary', 'name' => $this->Lang->get('USER__RANK_BANNED')]
];
$this->loadModel('Rank');
$custom_ranks = $this->Rank->find('all');
foreach ($custom_ranks as $value) {
$available_ranks[$value['Rank']['rank_id']] = [
'label' => 'info',
'name' => $value['Rank']['name']
];
}
$this->DataTable = $this->Components->load('DataTable');
$this->modelClass = 'User';
$this->DataTable->initialize($this);
$this->paginate = [
'fields' => ['User.id', 'User.pseudo', 'User.rank'],
];
$this->DataTable->mDataProp = true;
$response = $this->DataTable->getResponse();
$users = $response['aaData'];
$data = [];
foreach ($users as $value) {
$checkIsBan = $this->Ban->find('first', ["conditions" => ['user_id' => $value['User']['id']]]);

if ($checkIsBan != null)
continue;

if ($this->Permissions->have($value['User']['rank'], "CAN_BE_BAN"))
continue;

$username = $value['User']['pseudo'];
$rank_label = (isset($available_ranks[$value['User']['rank']])) ? $available_ranks[$value['User']['rank']]['label'] : $available_ranks[0]['label'];
$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'] . ">";
$data[] = [
'User' => [
'pseudo' => $username,
'ban' => $checkbox,
'rank' => $rank
]
];
}
$response['aaData'] = $data;
$this->response->body(json_encode($response));
}
}
}
}
2 changes: 2 additions & 0 deletions app/Controller/Component/PermissionsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PermissionsComponent extends CakeObject
'MANAGE_NAV',
'MANAGE_SEO',
'BYPASS_MAINTENANCE',
'BYPASS_BAN',
'MANAGE_MAINTENANCE',
'MANAGE_CONFIGURATION',
'USE_ADMIN_HELP',
Expand All @@ -29,6 +30,7 @@ class PermissionsComponent extends CakeObject
'VIEW_STATISTICS',
'MANAGE_THEMES',
'MANAGE_USERS',
'MANAGE_BANS',
'VIEW_WEBSITE_HISTORY'
];

Expand Down
13 changes: 5 additions & 8 deletions app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,11 @@ function profile()
0 => $this->Lang->get('USER__RANK_MEMBER'),
2 => $this->Lang->get('USER__RANK_MODERATOR'),
3 => $this->Lang->get('USER__RANK_ADMINISTRATOR'),
4 => $this->Lang->get('USER__RANK_ADMINISTRATOR'),
5 => $this->Lang->get('USER__RANK_BANNED')
4 => $this->Lang->get('USER__RANK_ADMINISTRATOR')
];
$this->loadModel('Rank');
$custom_ranks = $this->Rank->find('all');
foreach ($custom_ranks as $key => $value) {
foreach ($custom_ranks as $value) {
$available_ranks[$value['Rank']['rank_id']] = $value['Rank']['name'];
}
$this->set(compact('available_ranks'));
Expand Down Expand Up @@ -628,7 +627,7 @@ function admin_liveSearch($query = false)
if ($query != false) {
$result = $this->User->find('all', ['conditions' => ['pseudo LIKE' => $query . '%']]);
$users = [];
foreach ($result as $key => $value) {
foreach ($result as $value) {
$users[] = ['pseudo' => $value['User']['pseudo'], 'id' => $value['User']['id']];
}
$response = (empty($result)) ? ['status' => false] : ['status' => true, 'data' => $users];
Expand All @@ -651,8 +650,7 @@ public function admin_get_users()
0 => ['label' => 'success', 'name' => $this->Lang->get('USER__RANK_MEMBER')],
2 => ['label' => 'warning', 'name' => $this->Lang->get('USER__RANK_MODERATOR')],
3 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
4 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')],
5 => ['label' => 'primary', 'name' => $this->Lang->get('USER__RANK_BANNED')]
4 => ['label' => 'danger', 'name' => $this->Lang->get('USER__RANK_ADMINISTRATOR')]
];
$this->loadModel('Rank');
$custom_ranks = $this->Rank->find('all');
Expand Down Expand Up @@ -721,8 +719,7 @@ function admin_edit($search = false)
0 => $this->Lang->get('USER__RANK_MEMBER'),
2 => $this->Lang->get('USER__RANK_MODERATOR'),
3 => $this->Lang->get('USER__RANK_ADMINISTRATOR'),
4 => $this->Lang->get('USER__RANK_SUPER_ADMINISTRATOR'),
5 => $this->Lang->get('USER__RANK_BANNED')
4 => $this->Lang->get('USER__RANK_SUPER_ADMINISTRATOR')
];
$this->loadModel('Rank');
$custom_ranks = $this->Rank->find('all');
Expand Down
5 changes: 5 additions & 0 deletions app/Model/Ban.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class Ban extends AppModel
{
}
10 changes: 10 additions & 0 deletions app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User extends AppModel
private $userData;
private $isConnected = null;
private $isAdmin = null;
private $isBanned = null;

public function validRegister($data, $UtilComponent)
{
Expand Down Expand Up @@ -204,6 +205,15 @@ public function isConnected()
return !empty($user);
}

public function isBanned()
{
$check = ClassRegistry::init("Ban")->find('first', ["conditions" => ['user_id' => $this->getKey("id")]]);
$this->isBanned = $check ? $check["Ban"]["reason"] : false;

return $this->isBanned;

}

private function getDataBySession()
{
if (empty($this->userData))
Expand Down
62 changes: 62 additions & 0 deletions app/View/Ban/admin_add.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header with-border">
<h3 class="card-title"><?= $Lang->get('BAN__HOME') ?></h3>
</div>
<div class="card-body">
<form method="post" data-ajax="true" data-upload-image="true"
data-redirect-url="<?= $this->Html->url(['controller' => 'ban', 'action' => 'index', 'admin' => 'true']) ?>">
<table class="table table-responsive-sm table-bordered"
style="table-layout: fixed;word-wrap: break-word;" id="users">
<thead>
<tr>
<th><?= $Lang->get('BAN__QUESTION') ?></th>
<th><?= $Lang->get('USER__TITLE') ?></th>
<th><?= $Lang->get('USER__RANK') ?></th>
</tr>
</thead>
</table>

<div class="col-sm-6">
<div class="form-group">
<label><?= $Lang->get('BAN__REASON') ?></label>
<input type="text" class="form-control"
value="<?= $page['title'] ?>"
name="reason">
</div>
</div>

<div class="float-right">
<a href="<?= $this->Html->url(['controller' => 'ban', 'action' => 'index', 'admin' => true]) ?>"
class="btn btn-default"><?= $Lang->get('GLOBAL__CANCEL') ?></a>
<button class="btn btn-primary" type="submit"><?= $Lang->get('GLOBAL__SUBMIT') ?></button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$(document).ready(function () {
$('#users').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": false,
"info": false,
"autoWidth": false,
'searching': true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<?= $this->Html->url(['action' => 'get_users_not_ban']) ?>",
"aoColumns": [
{mData: "User.ban", "bSearchable": true},
{mData: "User.pseudo", "bSearchable": true},
{mData: "User.rank", "bSearchable": false}
]
});
});
</script>
Loading

0 comments on commit 92ce8b7

Please sign in to comment.