Update settings sidebar

This commit is contained in:
Daniel Supernault 2024-01-11 02:34:43 -07:00
parent 319a20b473
commit 5f6ed85770
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
3 changed files with 262 additions and 288 deletions

View file

@ -22,189 +22,189 @@ use App\Services\PronounService;
trait HomeSettings trait HomeSettings
{ {
public function home() public function home()
{ {
$id = Auth::user()->profile->id; $id = Auth::user()->profile->id;
$storage = []; $storage = [];
$used = Media::whereProfileId($id)->sum('size'); $used = Media::whereProfileId($id)->sum('size');
$storage['limit'] = config_cache('pixelfed.max_account_size') * 1024; $storage['limit'] = config_cache('pixelfed.max_account_size') * 1024;
$storage['used'] = $used; $storage['used'] = $used;
$storage['percentUsed'] = ceil($storage['used'] / $storage['limit'] * 100); $storage['percentUsed'] = ceil($storage['used'] / $storage['limit'] * 100);
$storage['limitPretty'] = PrettyNumber::size($storage['limit']); $storage['limitPretty'] = PrettyNumber::size($storage['limit']);
$storage['usedPretty'] = PrettyNumber::size($storage['used']); $storage['usedPretty'] = PrettyNumber::size($storage['used']);
$pronouns = PronounService::get($id); $pronouns = PronounService::get($id);
return view('settings.home', compact('storage', 'pronouns')); return view('settings.home', compact('storage', 'pronouns'));
} }
public function homeUpdate(Request $request) public function homeUpdate(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'), 'name' => 'nullable|string|max:'.config('pixelfed.max_name_length'),
'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'), 'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'),
'website' => 'nullable|url', 'website' => 'nullable|url',
'language' => 'nullable|string|min:2|max:5', 'language' => 'nullable|string|min:2|max:5',
'pronouns' => 'nullable|array|max:4' 'pronouns' => 'nullable|array|max:4'
]); ]);
$changes = false; $changes = false;
$name = strip_tags(Purify::clean($request->input('name'))); $name = strip_tags(Purify::clean($request->input('name')));
$bio = $request->filled('bio') ? strip_tags(Purify::clean($request->input('bio'))) : null; $bio = $request->filled('bio') ? strip_tags(Purify::clean($request->input('bio'))) : null;
$website = $request->input('website'); $website = $request->input('website');
$language = $request->input('language'); $language = $request->input('language');
$user = Auth::user(); $user = Auth::user();
$profile = $user->profile; $profile = $user->profile;
$pronouns = $request->input('pronouns'); $pronouns = $request->input('pronouns');
$existingPronouns = PronounService::get($profile->id); $existingPronouns = PronounService::get($profile->id);
$layout = $request->input('profile_layout'); $layout = $request->input('profile_layout');
if($layout) { if($layout) {
$layout = !in_array($layout, ['metro', 'moment']) ? 'metro' : $layout; $layout = !in_array($layout, ['metro', 'moment']) ? 'metro' : $layout;
} }
$enforceEmailVerification = config_cache('pixelfed.enforce_email_verification'); $enforceEmailVerification = config_cache('pixelfed.enforce_email_verification');
// Only allow email to be updated if not yet verified // Only allow email to be updated if not yet verified
if (!$enforceEmailVerification || !$changes && $user->email_verified_at) { if (!$enforceEmailVerification || !$changes && $user->email_verified_at) {
if ($profile->name != $name) { if ($profile->name != $name) {
$changes = true; $changes = true;
$user->name = $name; $user->name = $name;
$profile->name = $name; $profile->name = $name;
} }
if ($profile->website != $website) { if ($profile->website != $website) {
$changes = true; $changes = true;
$profile->website = $website; $profile->website = $website;
} }
if (strip_tags($profile->bio) != $bio) { if (strip_tags($profile->bio) != $bio) {
$changes = true; $changes = true;
$profile->bio = Autolink::create()->autolink($bio); $profile->bio = Autolink::create()->autolink($bio);
} }
if($user->language != $language && if($user->language != $language &&
in_array($language, \App\Util\Localization\Localization::languages()) in_array($language, \App\Util\Localization\Localization::languages())
) { ) {
$changes = true; $changes = true;
$user->language = $language; $user->language = $language;
session()->put('locale', $language); session()->put('locale', $language);
} }
if($existingPronouns != $pronouns) { if($existingPronouns != $pronouns) {
if($pronouns && in_array('Select Pronoun(s)', $pronouns)) { if($pronouns && in_array('Select Pronoun(s)', $pronouns)) {
PronounService::clear($profile->id); PronounService::clear($profile->id);
} else { } else {
PronounService::put($profile->id, $pronouns); PronounService::put($profile->id, $pronouns);
} }
} }
} }
if ($changes === true) { if ($changes === true) {
$user->save(); $user->save();
$profile->save(); $profile->save();
Cache::forget('user:account:id:'.$user->id); Cache::forget('user:account:id:'.$user->id);
AccountService::del($profile->id); AccountService::del($profile->id);
return redirect('/settings/home')->with('status', 'Profile successfully updated!'); return redirect('/settings/home')->with('status', 'Profile successfully updated!');
} }
return redirect('/settings/home'); return redirect('/settings/home');
} }
public function password() public function password()
{ {
return view('settings.password'); return view('settings.password');
} }
public function passwordUpdate(Request $request) public function passwordUpdate(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'current' => 'required|string', 'current' => 'required|string',
'password' => 'required|string', 'password' => 'required|string',
'password_confirmation' => 'required|string', 'password_confirmation' => 'required|string',
]); ]);
$current = $request->input('current'); $current = $request->input('current');
$new = $request->input('password'); $new = $request->input('password');
$confirm = $request->input('password_confirmation'); $confirm = $request->input('password_confirmation');
$user = Auth::user(); $user = Auth::user();
if (password_verify($current, $user->password) && $new === $confirm) { if (password_verify($current, $user->password) && $new === $confirm) {
$user->password = bcrypt($new); $user->password = bcrypt($new);
$user->save(); $user->save();
$log = new AccountLog(); $log = new AccountLog();
$log->user_id = $user->id; $log->user_id = $user->id;
$log->item_id = $user->id; $log->item_id = $user->id;
$log->item_type = 'App\User'; $log->item_type = 'App\User';
$log->action = 'account.edit.password'; $log->action = 'account.edit.password';
$log->message = 'Password changed'; $log->message = 'Password changed';
$log->link = null; $log->link = null;
$log->ip_address = $request->ip(); $log->ip_address = $request->ip();
$log->user_agent = $request->userAgent(); $log->user_agent = $request->userAgent();
$log->save(); $log->save();
Mail::to($request->user())->send(new PasswordChange($user)); Mail::to($request->user())->send(new PasswordChange($user));
return redirect('/settings/home')->with('status', 'Password successfully updated!'); return redirect('/settings/home')->with('status', 'Password successfully updated!');
} else { } else {
return redirect()->back()->with('error', 'There was an error with your request! Please try again.'); return redirect()->back()->with('error', 'There was an error with your request! Please try again.');
} }
} }
public function email() public function email()
{ {
return view('settings.email'); return view('settings.email');
} }
public function emailUpdate(Request $request) public function emailUpdate(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'email' => 'required|email|unique:users,email', 'email' => 'required|email|unique:users,email',
]); ]);
$changes = false; $changes = false;
$email = $request->input('email'); $email = $request->input('email');
$user = Auth::user(); $user = Auth::user();
$profile = $user->profile; $profile = $user->profile;
$validate = config_cache('pixelfed.enforce_email_verification'); $validate = config_cache('pixelfed.enforce_email_verification');
if ($user->email != $email) { if ($user->email != $email) {
$changes = true; $changes = true;
$user->email = $email; $user->email = $email;
if ($validate) { if ($validate) {
$user->email_verified_at = null; // auto verify admin email addresses
// Prevent old verifications from working $user->email_verified_at = $user->is_admin == true ? now() : null;
EmailVerification::whereUserId($user->id)->delete(); // Prevent old verifications from working
} EmailVerification::whereUserId($user->id)->delete();
}
$log = new AccountLog(); $log = new AccountLog();
$log->user_id = $user->id; $log->user_id = $user->id;
$log->item_id = $user->id; $log->item_id = $user->id;
$log->item_type = 'App\User'; $log->item_type = 'App\User';
$log->action = 'account.edit.email'; $log->action = 'account.edit.email';
$log->message = 'Email changed'; $log->message = 'Email changed';
$log->link = null; $log->link = null;
$log->ip_address = $request->ip(); $log->ip_address = $request->ip();
$log->user_agent = $request->userAgent(); $log->user_agent = $request->userAgent();
$log->save(); $log->save();
} }
if ($changes === true) { if ($changes === true) {
Cache::forget('user:account:id:'.$user->id); Cache::forget('user:account:id:'.$user->id);
$user->save(); $user->save();
$profile->save(); $profile->save();
return redirect('/settings/home')->with('status', 'Email successfully updated!'); return redirect('/settings/email')->with('status', 'Email successfully updated!');
} else { } else {
return redirect('/settings/email'); return redirect('/settings/email');
} }
} }
public function avatar()
{
return view('settings.avatar');
}
public function avatar()
{
return view('settings.avatar');
}
} }

View file

@ -1,63 +1,36 @@
@extends('layouts.app') @extends('settings.template')
@section('content') @section('section')
@if (session('status'))
<div class="alert alert-primary px-3 h6 text-center">
{{ session('status') }}
</div>
@endif
@if ($errors->any())
<div class="alert alert-danger px-3 h6 text-center">
@foreach($errors->all() as $error)
<p class="font-weight-bold mb-1">{{ $error }}</p>
@endforeach
</div>
@endif
@if (session('error'))
<div class="alert alert-danger px-3 h6 text-center">
{{ session('error') }}
</div>
@endif
<div class="container"> <div class="d-flex justify-content-between align-items-center">
<div class="col-12"> <div class="title d-flex align-items-center" style="gap: 1rem;">
<div class="card shadow-none border mt-5"> <p class="mb-0"><a href="/settings/home"><i class="far fa-chevron-left fa-lg"></i></a></p>
<div class="card-body"> <h3 class="font-weight-bold mb-0">Email Settings</h3>
<div class="row">
<div class="col-12 p-3 p-md-5">
<div class="title">
<h3 class="font-weight-bold">Email Settings</h3>
</div>
<hr>
<form method="post" action="{{route('settings.email')}}">
@csrf
<input type="hidden" class="form-control" name="name" value="{{Auth::user()->profile->name}}">
<input type="hidden" class="form-control" name="username" value="{{Auth::user()->profile->username}}">
<input type="hidden" class="form-control" name="website" value="{{Auth::user()->profile->website}}">
<div class="form-group">
<label for="email" class="font-weight-bold">Email Address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address" value="{{Auth::user()->email}}">
<p class="help-text small text-muted font-weight-bold">
@if(Auth::user()->email_verified_at)
<span class="text-success">Verified</span> {{Auth::user()->email_verified_at->diffForHumans()}}
@else
<span class="text-danger">Unverified</span> You need to <a href="/i/verify-email">verify your email</a>.
@endif
</p>
</div>
<div class="form-group row">
<div class="col-12 text-right">
<button type="submit" class="btn btn-primary font-weight-bold py-0 px-5">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div> </div>
</div>
</div> </div>
<hr>
<form method="post" action="{{route('settings.email')}}">
@csrf
<input type="hidden" class="form-control" name="name" value="{{Auth::user()->profile->name}}">
<input type="hidden" class="form-control" name="username" value="{{Auth::user()->profile->username}}">
<input type="hidden" class="form-control" name="website" value="{{Auth::user()->profile->website}}">
<div class="form-group">
<label for="email" class="font-weight-bold">Email Address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address" value="{{Auth::user()->email}}">
<p class="help-text small text-muted font-weight-bold">
@if(Auth::user()->email_verified_at)
<span class="text-success">Verified</span> {{Auth::user()->email_verified_at->diffForHumans()}}
@else
<span class="text-danger">Unverified</span> You need to <a href="/i/verify-email">verify your email</a>.
@endif
</p>
</div>
<div class="form-group row">
<div class="col-12 text-right">
<button type="submit" class="btn btn-primary font-weight-bold py-0 px-5">Submit</button>
</div>
</div>
</form>
@endsection @endsection

View file

@ -1,79 +1,80 @@
<div class="col-12 col-md-3"> <div class="col-12 col-md-3">
<ul class="nav flex-column settings-nav py-3"> <ul class="nav flex-column settings-nav py-3">
<li class="nav-item pl-3 {{request()->is('settings/home')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/home')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings')}}">Account</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings')}}">Account</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/accessibility')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/accessibility')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.accessibility')}}">Accessibility</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.accessibility')}}">Accessibility</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/email')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/email')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.email')}}">Email</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.email')}}">Email</a>
</li> </li>
@if(config('pixelfed.user_invites.enabled')) {{-- @if(config('pixelfed.user_invites.enabled'))
<li class="nav-item pl-3 {{request()->is('settings/invites*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/invites*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.invites')}}">Invites</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.invites')}}">Invites</a>
</li> </li>
@endif @endif --}}
<li class="nav-item pl-3 {{request()->is('settings/media*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/media*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.media')}}">Media</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.media')}}">Media</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/notifications')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/notifications')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.notifications')}}">Notifications</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.notifications')}}">Notifications</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/password')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/password')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.password')}}">Password</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.password')}}">Password</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/privacy*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/privacy*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.privacy')}}">Privacy</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.privacy')}}">Privacy</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/relationships*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/relationships*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.relationships')}}">Relationships</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.relationships')}}">Relationships</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/security*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/security*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.security')}}">Security</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.security')}}">Security</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/timeline*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/timeline*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.timeline')}}">Timelines</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.timeline')}}">Timelines</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<hr> <hr>
</li> </li>
<li class="nav-item pl-3 {{request()->is('*import*')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.import')}}">Import</a>
</li>
<li class="nav-item pl-3 {{request()->is('settings/data-export')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.dataexport')}}">Export</a>
</li>
@if(config_cache('pixelfed.oauth_enabled') == true) @if(config_cache('pixelfed.oauth_enabled') == true)
<li class="nav-item"> <li class="nav-item pl-3 {{request()->is('settings/applications')?'active':''}}">
<hr> <a class="nav-link font-weight-light text-muted" href="{{route('settings.applications')}}">Applications</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/applications')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/developers')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.applications')}}">Applications</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.developers')}}">Developers</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/developers')?'active':''}}"> @endif
<a class="nav-link font-weight-light text-muted" href="{{route('settings.developers')}}">Developers</a>
</li>
@endif
<li class="nav-item"> <li class="nav-item pl-3 {{request()->is('*import*')?'active':''}}">
<hr> <a class="nav-link font-weight-light text-muted" href="{{route('settings.import')}}">Import</a>
</li> </li>
<li class="nav-item pl-3 {{request()->is('settings/labs*')?'active':''}}"> <li class="nav-item pl-3 {{request()->is('settings/data-export')?'active':''}}">
<a class="nav-link font-weight-light text-muted" href="{{route('settings.labs')}}">Labs</a> <a class="nav-link font-weight-light text-muted" href="{{route('settings.dataexport')}}">Export</a>
</li> </li>
</ul>
</div>
@push('styles') <li class="nav-item pl-3 {{request()->is('settings/labs*')?'active':''}}">
<style type="text/css"> <a class="nav-link font-weight-light text-muted" href="{{route('settings.labs')}}">Labs</a>
.settings-nav { </li>
@media only screen and (min-width: 768px) {
border-right: 1px solid #dee2e6 !important @if(config('instance.parental_controls.enabled'))
} <li class="nav-item pl-3 {{request()->is('settings/parental-controls*')?'active':''}}">
height: 100%; <a class="nav-link font-weight-light text-muted" href="{{route('settings.parental-controls')}}">Parental Controls</a>
flex-grow: 1; </li>
} @endif
</style> </ul>
@endpush </div>
@push('styles')
<style type="text/css">
.settings-nav {
@media only screen and (min-width: 768px) {
border-right: 1px solid #dee2e6 !important
}
height: 100%;
flex-grow: 1;
}
</style>
@endpush