forked from jameslkingsley/ARCOMM
-
Notifications
You must be signed in to change notification settings - Fork 3
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
#82 - Authenticate with discord #77
Merged
BorderKeeper
merged 58 commits into
ARCOMM:master
from
TomBurch:authenticate-with-discord
Jun 22, 2021
Merged
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
f8662ac
Update Mission.php
TomBurch 1371ec6
Implement ValidateMissionContents
TomBurch 3ea61ca
Merge branch 'master' of https://github.com/TomBurch/ARCOMM
TomBurch 70de06f
Fix + translate PBOMission errors
TomBurch 3f2fcc4
Remove PBOMission changes from master
TomBurch f11a3e8
Merge branch 'master' of https://github.com/ARCOMM/ARCHUB
TomBurch 06de1ea
Merge pull request #2 from ARCOMM/master
TomBurch 8c316bb
Merge branch 'ARCOMM:master' into master
TomBurch 1c72564
Merge branch 'ARCOMM:master' into master
TomBurch f0febc1
Merge branch 'ARCOMM:master' into master
TomBurch ec94996
Merge branch 'ARCOMM:master' into master
TomBurch 5e7e930
Upgrade laravel/framework 5.8 -> 6 and upgrade/remove other dependencies
TomBurch 2064c84
Update Mission.php
TomBurch dc76412
Create 2021_06_01_202948_medialibrary_v8_update_media_table.php
TomBurch b6f9727
Update media-library.php
TomBurch 86d8f24
Merge branch 'master' into 55-upgrade_laravel_framework
BorderKeeper 2a3cd3c
Scrap old login page and countdown
TomBurch 034e137
Basic discord authentication
TomBurch 54eb0a2
Update discord, remove permissions/steam/username
TomBurch b294178
Permission comments
TomBurch 42b35ab
Remove attendance
TomBurch 4e1c468
Start adding permissions
TomBurch 233bfff
More permissions
TomBurch 1354933
Add admin and senior tester roles
TomBurch 26f3c45
Change login button
TomBurch 008ab31
Remove absences
TomBurch 28e927f
Remove extra absences
TomBurch 031309b
name -> username
TomBurch 3857677
Missed instances
TomBurch 442e22c
Remove nonMembers
TomBurch c1a3d0b
Add avatarSync
TomBurch abcd87d
Update User.php
TomBurch 775aded
Remove brackets
TomBurch 3e1e7a7
Update Mission.php
TomBurch 5d0ff46
Update PageController.php
TomBurch 4e4e418
Merge branch 'ARCOMM:master' into master
TomBurch 999dc4d
Merge branch 'ARCOMM:master' into master
TomBurch 1226e7b
Merge branch 'master' into authenticate-with-discord
TomBurch 1425adb
Merge conflict
TomBurch 61ece02
Merge branch 'ARCOMM:master' into master
TomBurch 7d840c5
Merge branch 'master' into authenticate-with-discord
TomBurch 5626cfc
Merge branch 'ARCOMM:master' into master
TomBurch c79dc5e
Upgrade laravel/framework to v8 (latest)
TomBurch 724ca76
Merge branch 'master' into authenticate-with-discord
TomBurch 327ff07
Merge branch '55-upgrade_laravel_framework_to_v8' into authenticate-w…
TomBurch 87b1040
Use HTTP facade
TomBurch 7c2f283
Add method for migration
TomBurch 60e11ad
Restore migration files
TomBurch c88719a
Remove line
TomBurch d68906e
Update PageController.php
TomBurch d768816
Redirect on failed getRoles
TomBurch ceaf662
Fix exception, change verify-missions to Tester
TomBurch 3ce755a
Merge branch 'ARCOMM:master' into master
TomBurch 1858bb7
Merge remote-tracking branch 'origin/master' into authenticate-with-d…
TomBurch 1f64185
Update AuthServiceProvider.php
TomBurch 9dd6fb8
Update AuthServiceProvider.php
TomBurch 2b6c527
Fix missionUpdate ping
TomBurch b6ef1bc
Update AuthServiceProvider.php
TomBurch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
class ChannelEnum | ||
{ | ||
const Archub = 0; | ||
const Staff = 1; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use App\ChannelEnum; | ||
use App\RoleEnum; | ||
use App\Models\Portal\User; | ||
use App\Models\Missions\Mission; | ||
use Illuminate\Auth\Access\AuthorizationException; | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Support\Facades\Http; | ||
|
||
class Discord | ||
{ | ||
public static function missionUpdate(string $content, Mission $mission, bool $tagAuthor = false, string $url = null) | ||
{ | ||
if ($tagAuthor && ($mission->user->id != auth()->user()->id)) { | ||
$content = "{$content} <@{$mission->user->discord_id}>"; | ||
} | ||
|
||
if (!is_null($url)) { | ||
$content = "{$content}\n{$url}"; | ||
} | ||
|
||
self::notifyChannel(ChannelEnum::Archub, $content); | ||
} | ||
|
||
public static function notifyChannel(string $channel, string $content) | ||
{ | ||
$webhook = self::getWebhookFromChannel($channel); | ||
$response = HTTP::post($webhook, [ | ||
'content' => $content, | ||
]); | ||
|
||
return $response; | ||
} | ||
|
||
private static function getWebhookFromChannel(int $channel) | ||
{ | ||
if ($channel == ChannelEnum::Archub) | ||
{ | ||
return config('services.discord.archub_webhook'); | ||
} | ||
else if ($channel == ChannelEnum::Staff) | ||
{ | ||
return config('services.discord.staff_webhook'); | ||
} | ||
else | ||
{ | ||
throw new Exception("Webhook not found"); | ||
} | ||
} | ||
|
||
private static function getUser(int $discord_id) | ||
{ | ||
return Cache::remember($discord_id, 10, function() use ($discord_id) { | ||
$url = "https://discord.com/api/v8/guilds/".config('services.discord.server_id')."/members/{$discord_id}"; | ||
$response = HTTP::withHeaders([ | ||
'Authorization' => "Bot ".config('services.discord.token') | ||
])->get($url); | ||
|
||
if ($response->successful()) { | ||
return (array)$response->json(); | ||
} | ||
|
||
throw new AuthorizationException("Error getting user from discord ". $response->status()); | ||
}); | ||
} | ||
|
||
public static function getAvatar(int $discord_id) | ||
{ | ||
$avatarHash = ((array)self::getUser($discord_id)["user"])["avatar"]; | ||
return "https://cdn.discordapp.com/avatars/{$discord_id}/{$avatarHash}.jpg"; | ||
} | ||
|
||
private static function getRoles(int $discord_id) | ||
{ | ||
return self::getUser($discord_id)["roles"]; | ||
} | ||
|
||
public static function hasRole(User $user, int $role) | ||
{ | ||
if (!auth()->guest() && is_null(auth()->user()->discord_id)) { | ||
throw new AuthorizationException; | ||
} | ||
$roleId = self::getRoleIdFromRole($role); | ||
$roles = self::getRoles($user->discord_id); | ||
|
||
return in_array($roleId, $roles); | ||
} | ||
|
||
public static function isMember(int $discord_id) | ||
{ | ||
$roleId = self::getRoleIdFromRole(RoleEnum::Member); | ||
$roles = self::getRoles($discord_id); | ||
|
||
return in_array($roleId, $roles); | ||
} | ||
|
||
private static function getRoleIdFromRole(int $role) | ||
{ | ||
if ($role == RoleEnum::Member) | ||
{ | ||
return config('services.discord.member_role'); | ||
} | ||
else if ($role == RoleEnum::Tester) | ||
{ | ||
return config('services.discord.tester_role'); | ||
} | ||
else if ($role == RoleEnum::SeniorTester) | ||
{ | ||
return config('services.discord.senior_tester_role'); | ||
} | ||
else if ($role == RoleEnum::Staff) | ||
{ | ||
return config('services.discord.staff_role'); | ||
} | ||
else if ($role == RoleEnum::Admin) | ||
{ | ||
return config('services.discord.admin_role'); | ||
} | ||
else | ||
{ | ||
throw new Exception("RoleId not found"); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Discord; | ||
use App\RoleEnum; | ||
use App\Models\Portal\User; | ||
use App\Http\Controllers\Controller; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Auth\Events\Registered; | ||
use Laravel\Socialite\Facades\Socialite; | ||
|
||
class DiscordController extends Controller | ||
{ | ||
/** | ||
* Redirect the user to the Discord login page. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function redirect() | ||
{ | ||
return Socialite::driver('discord')->redirect(); | ||
} | ||
|
||
/** | ||
* Handles the response from Discord. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function callback(Request $request) | ||
{ | ||
$user = Socialite::driver('discord')->user(); | ||
|
||
if (!auth()->guest()) { | ||
if (is_null(auth()->user()->discord_id)) { | ||
auth()->user()->discord_id = $user->id; | ||
auth()->user()->save(); | ||
return redirect('/hub'); | ||
} | ||
} | ||
|
||
if (Discord::isMember($user->id)) { | ||
return $this->create($user); | ||
} | ||
|
||
return [ | ||
'error' => 'You are not a member.' | ||
]; | ||
} | ||
|
||
/** | ||
* Creates the user account and redirects with the access token. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function create($data) | ||
{ | ||
$user = User::where('discord_id', $data->id)->first(); | ||
|
||
if (is_null($user)) { | ||
$user = User::create([ | ||
'discord_id' => $data->id, | ||
'username' => $data->name, | ||
'email' => $data->email, | ||
'avatar' => $data->avatar, | ||
]); | ||
} | ||
|
||
auth()->login($user, true); | ||
|
||
if ($user->can('access-hub')) { | ||
return redirect('/hub'); | ||
} | ||
return redirect('/'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For migrating existing users