Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Irc bans #306

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cncnet-api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ APP_URL=http://localhost
ASSET_URL=http://localhost
URL=http://localhost

# Laravel mysql config
# Mysql config
DB_HOST="mysql"
DB_DATABASE="cncnet_api"
DB_USERNAME="cncnet"
DB_PASSWORD="cncnet"

# Mysql config
# IRC Mysql config
DB_IRC_DATABASE="cncnet_irc"

# Main Ladder Mysql config
MYSQL_DATABASE="cncnet_api"
MYSQL_USER="cncnet"
MYSQL_PASSWORD="cncnet"
MYSQL_ALLOW_EMPTY_PASSWORD="false"
MYSQL_ROOT_PASSWORD="yourRandomRootPass"

# IRC Mysql config
MYSQL_IRC_HOST=mysql
MYSQL_IRC_DATABASE=cncnet_irc

CACHE_DRIVER="file"
SESSION_DRIVER="cookie"
QUEUE_DRIVER="database"
Expand Down
108 changes: 108 additions & 0 deletions cncnet-api/app/Http/Controllers/Admin/IrcBanController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Irc\CreateIrcBanRequest;
use App\Http\Services\IrcBanService;
use App\Models\IrcBan;
use App\Models\IrcWarning;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class IrcBanController extends Controller
{
protected IrcBanService $ircBanService;

public function __construct(
IrcBanService $ircBanService,
)
{
$this->ircBanService = $ircBanService;
}

public function getBanIndex()
{
$bans = IrcBan::limit(20)->orderBy("created_at", "desc")->get();
$warnings = IrcWarning::limit(20)->orderBy("created_at", "desc")->get();
return view('admin.irc.index', compact('bans', 'warnings'));
}

public function getAllBans()
{
$bans = IrcBan::orderBy("created_at", "desc")->paginate(20);
return view('admin.irc.bans', compact('bans'));
}

public function getCreateBan(Request $request)
{
return view('admin.irc.create');
}

public function createBan(CreateIrcBanRequest $request)
{
// Safety checks
if ($request->username == null && $request->ident == null && $request->host == null)
{
return redirect()->back()->withErrors(["You just banned everyone on the CnCNet server. Just kidding. Specify at least one value in the fields: user, ident or host"])->withInput();
}

if ($request->channel == null && $request->global_ban != "on")
{
return redirect()->back()->withErrors(["Which channel(s) are we banning this user from?"])->withInput();
}

$ircBan = $this->ircBanService->saveBan(
banReason: $request->ban_reason,
adminId: Auth::user()->id,
channel: $request->channel,
globalBan: $request->global_ban == "on",
username: $request->username,
ident: $request->ident,
host: $request->host,
expiresAt: $request->expires_at
);

return redirect()->route('admin.irc.bans.edit', ['id' => $ircBan->id])->with('status', 'Ban created');
}

public function getEditBan(Request $request)
{
$ban = IrcBan::findOrFail($request->id);
return view('admin.irc.edit', compact('ban'));
}

public function updateBan(Request $request)
{
$ban = IrcBan::findOrFail($request->ban_id);

// Safety checks
if ($request->username == null && $request->ident == null && $request->host == null)
{
return redirect()->back()->withErrors(["You just banned everyone on the CnCNet server. Just kidding. Specify at least one value in the fields: username, ident or host"])->withInput();
}

if ($request->channel == null && $request->global_ban != "on")
{
return redirect()->back()->withErrors(["Which channel(s) are we banning this user from?"])->withInput();
}

$this->ircBanService->updateBan(
banId: $ban->id,
banReason: $request->ban_reason,
adminId: Auth::user()->id,
channel: $request->channel,
globalBan: $request->global_ban == "on",
expiresAt: $request->expires_at
);

return redirect()->back()->with('status', 'Ban updated');
}

public function expireBan(Request $request)
{
$ban = IrcBan::findOrFail($request->ban_id);
$this->ircBanService->expireBan($ban, Auth::user()->id);
return redirect()->back()->with('status', 'Ban expired');
}
}
95 changes: 95 additions & 0 deletions cncnet-api/app/Http/Controllers/Admin/IrcWarningController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Irc\CreateIrcBanRequest;
use App\Http\Services\IrcBanService;
use App\Http\Services\IrcWarningService;
use App\Models\IrcBan;
use App\Models\IrcWarning;
use App\Rules\AtLeastOneField;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class IrcWarningController extends Controller
{
protected IrcWarningService $ircWarningService;

public function __construct(IrcWarningService $ircWarningService)
{
$this->ircWarningService = $ircWarningService;
}

public function getAllWarnings()
{
$warnings = IrcWarning::orderBy("created_at", "desc")->paginate(20);
return view('admin.irc.warnings', compact('warnings'));
}

public function getCreateWarning(Request $request)
{
return view('admin.irc.warning-create');
}

public function getEditWarning(Request $request)
{
$warning = IrcWarning::findOrFail($request->id);
return view('admin.irc.warning-edit', compact('warning'));
}

public function createWarning(Request $request)
{
// Safety checks
if ($request->username == null && $request->ident == null)
{
return redirect()->back()->withErrors(["Specify at least one value in the fields: user or ident"])->withInput();
}

if ($request->channel == null)
{
return redirect()->back()->withErrors(["Specify a channel this user will receive this message"])->withInput();
}

$this->ircWarningService->issueWarning(
adminId: Auth::user()->id,
username: $request->username,
ident: $request->ident,
warningMessage: $request->warning_message,
channel: $request->channel
);

return redirect()->route('admin.irc.warnings')->with('status', 'Warning created');
}

public function expireWarning(Request $request)
{
$this->ircWarningService->expireWarning($request->id);
return redirect()->route('admin.irc.warnings')->with('status', 'Warning expired');
}

public function updateWarning(Request $request)
{
// Safety checks
if ($request->username == null && $request->ident == null)
{
return redirect()->back()->withErrors(["Specify at least one value in the fields: user or ident"])->withInput();
}

if ($request->channel == null)
{
return redirect()->back()->withErrors(["Specify a channel this user will receive this message"])->withInput();
}

$this->ircWarningService->updateWarning(
warningId: $request->warning_id,
adminId: Auth::user()->id,
username: $request->username,
ident: $request->ident,
warningMessage: $request->warning_message,
channel: $request->channel
);

return redirect()->route('admin.irc.warnings.edit', ['id' => $request->warning_id])->with('status', 'Warning updated');
}
}
15 changes: 9 additions & 6 deletions cncnet-api/app/Http/Controllers/Admin/PodiumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admi\Podium\ComputePodiumRequest;
use App\Http\Requests\Admin\Podium\ComputePodiumRequest;
use App\Models\Ladder;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
Expand All @@ -26,11 +26,13 @@ public function getPodiumForm()
return view('admin.podium', compact('ladders', 'fromDate', 'toDate'));
}

public function computePodium(ComputePodiumRequest $request) {
public function computePodium(ComputePodiumRequest $request)
{

if (RateLimiter::tooManyAttempts($this->getRateLimiterKey(), 1)) {
if (RateLimiter::tooManyAttempts($this->getRateLimiterKey(), 1))
{
$seconds = RateLimiter::availableIn($this->getRateLimiterKey());
$message = 'Don\'t submit this form this too often... You may try again in '.$seconds.' seconds.';
$message = 'Don\'t submit this form this too often... You may try again in ' . $seconds . ' seconds.';
return view('admin.podium.result-too-many-attempts', compact('message'));
}

Expand Down Expand Up @@ -63,7 +65,8 @@ public function computePodium(ComputePodiumRequest $request) {
return view('admin.podium.result', compact('players', 'from', 'to', 'ladder'));
}

private function getRateLimiterKey() {
return 'compute-podium-win-count:'.auth()->id();
private function getRateLimiterKey()
{
return 'compute-podium-win-count:' . auth()->id();
}
}
57 changes: 19 additions & 38 deletions cncnet-api/app/Http/Controllers/Api/V2/Bans/ApiBanController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,34 @@
namespace App\Http\Controllers\Api\V2\Bans;

use App\Http\Controllers\Controller;
use App\Http\Services\AuthService;
use App\Http\Services\LadderService;
use App\Http\Services\PlayerService;
use App\Models\PlayerActiveHandle;
use Carbon\Carbon;
use App\Http\Services\IrcBanService;
use App\Http\Services\IrcWarningService;
use Illuminate\Http\Request;

class ApiBanController extends Controller
{
private $authService;
private $ladderService;
private $playerService;
protected IrcBanService $ircBanService;
protected IrcWarningService $ircWarningService;

public function __construct()
public function __construct(IrcBanService $ircBanService, IrcWarningService $ircWarningService)
{
$this->authService = new AuthService();
$this->playerService = new PlayerService;
$this->ladderService = new LadderService;
$this->ircBanService = $ircBanService;
$this->ircWarningService = $ircWarningService;
}

public function getBans()
{
return [
[
"user" => "cncnet",
"ident" => "a964ad",
"host" => "gamesurge-d3a0cd5b.res.spectrum.com",
"kickBan" => true
],
[
"user" => null,
"ident" => "t364ad",
"host" => null,
"kickBan" => false
],
[
"user" => null,
"ident" => "",
"host" => "*.res.spectrum.com",
"kickBan" => false
],
[
"user" => "cncnet-moderator",
"ident" => null,
"host" => null,
"kickBan" => true
],
];
return $this->ircBanService->getActiveBans();
}

public function getWarnings()
{
return $this->ircWarningService->getActiveWarnings();
}

public function receiveWarningAcknowledgments(Request $request)
{
$this->ircWarningService->acknowledgeWarningsByUser($request->usernames);
return response()->json(["success" => true], 200);
}
}
29 changes: 29 additions & 0 deletions cncnet-api/app/Http/Requests/Admin/Irc/CreateIrcBanRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Admin\Irc;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class CreateIrcBanRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'ban_reason' => 'required',
];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Requests\Admi\Podium;
namespace App\Http\Requests\Admin\Podium;

use Illuminate\Foundation\Http\FormRequest;

Expand Down
Loading