Skip to content

Commit ceb28d2

Browse files
committed
Add automatic linking of fake ckeys to the WI
1 parent 1d8a347 commit ceb28d2

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

app/Http/Controllers/Auth/ServerController.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\User;
88
use App\Services\Server\ServerQuery;
9+
use App\Services\Server\Helpers;
910
use Illuminate\Http\Request;
1011
use Illuminate\Support\Facades\Auth;
1112
use Illuminate\Support\Facades\Log;
@@ -89,6 +90,13 @@ public function endLogin(Request $request)
8990
$client_token = $request->session()->pull('server_client_token');
9091
$byond_key_is_linked = $request->user()->byond_key != null;
9192

93+
$ckey_is_external = False;
94+
if ($byond_key_is_linked) {
95+
$ckey_is_external = ServerPlayer::where('ckey', $request->user()->byond_key)->select('ckey_is_external')->firstOrFail();
96+
} else {
97+
$ckey_is_external = True;
98+
}
99+
92100
$query = new ServerQuery;
93101
try {
94102
Log::debug("server.login - Sending auth_client request to server for ckey and forum account: " . $request->user()->byond_key . ", " . $request->user()->name);
@@ -98,7 +106,7 @@ public function endLogin(Request $request)
98106
'clienttoken' => $client_token,
99107
'key' => $request->user()->byond_key,
100108
'forumuser' => $request->user()->name,
101-
'use-external-key' => $byond_key_is_linked
109+
'use-external-key' => $ckey_is_external
102110
]);
103111
} catch (\Exception $e) {
104112
Log::debug("server.login - Error while sending auth_client request to server: " . $e->getMessage());
@@ -107,6 +115,13 @@ public function endLogin(Request $request)
107115

108116
if ($query->response->statuscode == '200') {
109117
Log::debug("server.login - Ckey or external user succesfully logged in: " . $request->user()->byond_key . ", " . $request->user()->name);
118+
119+
// User was authorized but no BYOND key is linked yet. Ergo, external ckey was used. Link it.
120+
if ($byond_key_is_linked == False) {
121+
$fake_ckey = "GuestF-" . Helpers::sanitize_ckey($request->user()->name);
122+
$request->user()->linkWithCkey($fake_ckey);
123+
}
124+
110125
return view('auth.server.success');
111126
} else {
112127
Log::debug("server.login - Invalid status-code while sending auth_client request to server: " . $query->response->statuscode);

app/Http/Controllers/User/LinkController.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,8 @@ public function index(Request $request)
4949
//Check if the linking request is set to something other than new
5050
if ($linking_request->status == "confirmed") {
5151
//If its confirmed write it to the forum db
52-
$sanitized_key = Helpers::sanitize_ckey($linking_request->player_ckey);
53-
$request->user()->byond_key = $sanitized_key;
54-
$request->user()->save();
55-
56-
//Update the forum
57-
$base_url = config('aurora.forum_url');
58-
if ($base_url) {
59-
$request_url = $base_url . 'api/core/members/' . $request->user()->id .
60-
'?key=' . config('aurora.forum_api_key') .
61-
'&customFields[' . config('aurora.forum_byond_attribute') . ']=' . $sanitized_key;
62-
$client = new \GuzzleHttp\Client();
63-
$client->request('POST', $request_url);
64-
}
6552

53+
$request->user()->linkWithCkey($byond_key);
6654

6755
//Set the status of the linking request to linked and set the deleted_at date
6856
DB::connection('server')->table('player_linking')

app/Models/User.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Foundation\Auth\User as Authenticatable;
99
use App\Models\ServerPlayer;
1010
use App\Models\SiteRole;
11+
use App\Services\Server\Helpers;
1112

1213
class User extends Authenticatable
1314
{
@@ -149,4 +150,33 @@ public function checkPlayerChar($char_id)
149150
return NULL;
150151
}
151152
}
153+
154+
/**
155+
* Linked the user account to a specified ckey. Will also create the link in the forums.
156+
*
157+
* Will throw if the user is already linked with a byond key.
158+
*
159+
* @param $byond_key
160+
*/
161+
public function linkWithCkey($byond_key)
162+
{
163+
if ($this->byond_linked == True) {
164+
throw new \Exception("User already linked with a ckey.");
165+
}
166+
167+
$sanitized_key = Helpers::sanitize_ckey($byond_key);
168+
$this->byond_key = $sanitized_key;
169+
$this->byond_linked = True;
170+
$this->save();
171+
172+
//Update the forum
173+
$base_url = config('aurora.forum_url');
174+
if ($base_url) {
175+
$request_url = $base_url . 'api/core/members/' . $this->id .
176+
'?key=' . config('aurora.forum_api_key') .
177+
'&customFields[' . config('aurora.forum_byond_attribute') . ']=' . $sanitized_key;
178+
$client = new \GuzzleHttp\Client();
179+
$client->request('POST', $request_url);
180+
}
181+
}
152182
}

0 commit comments

Comments
 (0)