Miguel Nogueira
5f1f92a9ce
This commit fixes some superficial instances of Broken Access Control (https://owasp.org/www-project-top-ten/OWASP_Top_Ten_2017/Top_10-2017_A5-Broken_Access_Control). There may be some more instances of this, as authorization was only done after most of the controllers were done (big mistake). Some refactoring was also performed, where Route Model Binding with DI (dependency injection) was used whenever possible, to increase testability of the codebase. Some reused code was also moved to Helper classes as to enforce DRY; There may be some lines of code that are still copy-pasted from other parts of the codebase for reuse. Non-breaking refactoring changes were made, but the app as a whole still needs full manual testing, and customised responses to HTTP 500 responses. Some errors are also not handled gracefully and this wasn't checked in this commit.
128 lines
3.8 KiB
PHP
128 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\ProfileSave;
|
|
use Illuminate\Support\Facades\Log;
|
|
use App\Profile;
|
|
use App\User;
|
|
use App\Facades\IP;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class ProfileController extends Controller
|
|
{
|
|
|
|
public function index()
|
|
{
|
|
|
|
return view('dashboard.user.directory')
|
|
->with('users', User::with('profile', 'bans')->paginate(9));
|
|
}
|
|
|
|
public function showProfile()
|
|
{
|
|
|
|
$socialLinks = Auth::user()->profile->socialLinks ?? "[]";
|
|
$socialMediaProfiles = json_decode($socialLinks, true);
|
|
|
|
return view('dashboard.user.profile.userprofile')
|
|
->with([
|
|
'profile' => Auth::user()->profile,
|
|
'github' => $socialMediaProfiles['links']['github'] ?? 'UpdateMe',
|
|
'twitter' => $socialMediaProfiles['links']['twitter'] ?? 'UpdateMe',
|
|
'insta' => $socialMediaProfiles['links']['insta'] ?? 'UpdateMe',
|
|
'discord' => $socialMediaProfiles['links']['discord'] ?? 'UpdateMe#12345',
|
|
]);
|
|
|
|
}
|
|
|
|
// Route model binding
|
|
public function showSingleProfile(Request $request, User $user)
|
|
{
|
|
|
|
$socialMediaProfiles = json_decode($user->profile->socialLinks, true);
|
|
$createdDate = Carbon::parse($user->created_at);
|
|
|
|
$systemRoles = Role::all()->pluck('name')->all();
|
|
$userRoles = $user->roles->pluck('name')->all();
|
|
|
|
$roleList = [];
|
|
|
|
|
|
foreach($systemRoles as $role)
|
|
{
|
|
if (in_array($role, $userRoles))
|
|
{
|
|
$roleList[$role] = true;
|
|
}
|
|
else
|
|
{
|
|
$roleList[$role] = false;
|
|
}
|
|
}
|
|
|
|
if (Auth::user()->is($user) || Auth::user()->can('profiles.view.others'))
|
|
{
|
|
return view('dashboard.user.profile.displayprofile')
|
|
->with([
|
|
'profile' => $user->profile,
|
|
'github' => $socialMediaProfiles['links']['github'] ?? 'UpdateMe',
|
|
'twitter' => $socialMediaProfiles['links']['twitter'] ?? 'UpdateMe',
|
|
'insta' => $socialMediaProfiles['links']['insta'] ?? 'UpdateMe',
|
|
'discord' => $socialMediaProfiles['links']['discord'] ?? 'UpdateMe#12345',
|
|
'since' => $createdDate->englishMonth . " " . $createdDate->year,
|
|
'ipInfo' => IP::lookup($user->originalIP),
|
|
'roles' => $roleList
|
|
]);
|
|
}
|
|
else
|
|
{
|
|
abort(403, 'You cannot view someone else\'s profile.');
|
|
}
|
|
|
|
}
|
|
|
|
public function saveProfile(ProfileSave $request)
|
|
{
|
|
$profile = User::find(Auth::user()->id)->profile;
|
|
$social = [];
|
|
|
|
if (!is_null($profile))
|
|
{
|
|
switch ($request->avatarPref)
|
|
{
|
|
case 'MOJANG':
|
|
$avatarPref = 'crafatar';
|
|
|
|
break;
|
|
case 'GRAVATAR':
|
|
$avatarPref = strtolower($request->avatarPref);
|
|
|
|
break;
|
|
}
|
|
|
|
$social['links']['github'] = $request->socialGithub;
|
|
$social['links']['twitter'] = $request->socialTwitter;
|
|
$social['links']['insta'] = $request->socialInsta;
|
|
$social['links']['discord'] = $request->socialDiscord;
|
|
|
|
$profile->profileShortBio = $request->shortBio;
|
|
$profile->profileAboutMe = $request->aboutMe;
|
|
$profile->avatarPreference = $avatarPref;
|
|
$profile->socialLinks = json_encode($social);
|
|
|
|
$newProfile = $profile->save();
|
|
|
|
$request->session()->flash('success', 'Profile settings saved successfully.');
|
|
|
|
}
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
}
|