Skip to content

Commit

Permalink
Merge pull request #15127 from snipe/fixes/small_default_avatar_tweaks
Browse files Browse the repository at this point in the history
Fixes #15076 - Removes ability to remove the default avatar from disk
  • Loading branch information
snipe authored Jul 20, 2024
2 parents e9f9d3c + 8fc5c0b commit 9d890b3
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 141 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ _ide_helper_models.php
/.phplint-cache
storage/ldap_client_tls.cert
storage/ldap_client_tls.key
/storage/framework/testing
1 change: 1 addition & 0 deletions app/Console/Commands/ResetDemoSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function handle()
$settings->saml_forcelogin = '0';
$settings->saml_slo = null;
$settings->saml_custom_settings = null;
$settings->default_avatar = 'default.png';


$settings->save();
Expand Down
34 changes: 13 additions & 21 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,54 +414,46 @@ public function postBranding(ImageUploadRequest $request) : RedirectResponse
$setting = $request->handleImages($setting, 600, 'logo', '', 'logo');

if ($request->input('clear_logo') == '1') {

if (($setting->logo) && (Storage::exists($setting->logo))) {
Storage::disk('public')->delete($setting->logo);
}
$setting = $request->deleteExistingImage($setting, '', 'logo');
$setting->logo = null;
$setting->brand = 1;
}

// Email logo upload
$setting = $request->handleImages($setting, 600, 'email_logo', '', 'email_logo');
if ($request->input('clear_email_logo') == '1') {

if (($setting->email_logo) && (Storage::exists($setting->email_logo))) {
Storage::disk('public')->delete($setting->email_logo);
}
$setting = $request->deleteExistingImage($setting, '', 'email_logo');
$setting->email_logo = null;
// If they are uploading an image, validate it and upload it
}

// Label logo upload
$setting = $request->handleImages($setting, 600, 'label_logo', '', 'label_logo');
if ($request->input('clear_label_logo') == '1') {

if (($setting->label_logo) && (Storage::exists($setting->label_logo))) {
Storage::disk('public')->delete($setting->label_logo);
}
if ($request->input('clear_label_logo') == '1') {
$setting = $request->deleteExistingImage($setting, '', 'label_logo');
$setting->label_logo = null;
}

// Favicon upload
$setting = $request->handleImages($setting, 100, 'favicon', '', 'favicon');
if ('1' == $request->input('clear_favicon')) {

if (($setting->favicon) && (Storage::exists($setting->favicon))) {
Storage::disk('public')->delete($setting->favicon);
}
$setting = $request->deleteExistingImage($setting, '', 'favicon');
$setting->favicon = null;
}

// Default avatar upload
$setting = $request->handleImages($setting, 500, 'default_avatar', 'avatars', 'default_avatar');
if ($request->input('clear_default_avatar') == '1') {

if (($setting->default_avatar) && (Storage::exists('avatars/'.$setting->default_avatar))) {
Storage::disk('public')->delete('avatars/'.$setting->default_avatar);
if ($request->input('clear_default_avatar') == '1') {
// Don't delete the file, just update the field if this is the default
if ($setting->default_avatar!='default.png') {
$setting = $request->deleteExistingImage($setting, 'avatars', 'default_avatar');
}
$setting->default_avatar = null;
}

if ($request->input('restore_default_avatar') == '1') {
$setting->default_avatar = 'default.png';
}
}

if ($setting->save()) {
Expand Down
25 changes: 13 additions & 12 deletions app/Http/Requests/ImageUploadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Exception\NotReadableException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class ImageUploadRequest extends Request
{
Expand Down Expand Up @@ -123,7 +124,7 @@ public function handleImages($item, $w = 600, $form_fieldname = 'image', $path =

} catch(NotReadableException $e) {
Log::debug($e);
$validator = \Validator::make([], []);
$validator = Validator::make([], []);
$validator->errors()->add($form_fieldname, trans('general.unaccepted_image_type', ['mimetype' => $image->getClientMimeType()]));

throw new \Illuminate\Validation\ValidationException($validator);
Expand All @@ -135,28 +136,28 @@ public function handleImages($item, $w = 600, $form_fieldname = 'image', $path =
}

// Remove Current image if exists
if (($item->{$form_fieldname}!='') && (Storage::disk('public')->exists($path.'/'.$item->{$db_fieldname}))) {
try {
Storage::disk('public')->delete($path.'/'.$item->{$form_fieldname});
} catch (\Exception $e) {
Log::debug('Could not delete old file. '.$path.'/'.$file_name.' does not exist?');
}
}

$item = $this->deleteExistingImage($item, $path, $db_fieldname);
$item->{$db_fieldname} = $file_name;
}


// If the user isn't uploading anything new but wants to delete their old image, do so
} elseif ($this->input('image_delete') == '1') {
Log::debug('Deleting image');
$item = $this->deleteExistingImage($item, $path, $db_fieldname);
}

return $item;
}

public function deleteExistingImage($item, $path = null, $db_fieldname = 'image') {

if ($item->{$db_fieldname}!='') {
try {
Storage::disk('public')->delete($path.'/'.$item->{$db_fieldname});
$item->{$db_fieldname} = null;
$item->{$db_fieldname} = null;
} catch (\Exception $e) {
Log::debug($e);
}

}

return $item;
Expand Down
1 change: 1 addition & 0 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use App\Helpers\Helper;
use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
use Illuminate\Support\Facades\Log;

Expand Down
18 changes: 11 additions & 7 deletions app/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,26 +445,30 @@ public function gravatar()
return Storage::disk('public')->url('avatars/'.e($this->avatar));
}

// If there is a default avatar
if (Setting::getSettings()->default_avatar!= '') {

// If the default is system default
if (Setting::getSettings()->default_avatar == 'default.png') {
return Storage::disk('public')->url('default.png');
}

// If there is a custom default avatar
if (Setting::getSettings()->default_avatar != '') {
return Storage::disk('public')->url('avatars/'.e(Setting::getSettings()->default_avatar));
}

// Fall back to Gravatar if the settings allow loading remote scripts
if (Setting::getSettings()->load_remote == '1') {
if ($this->model->gravatar != '') {
// If there is no default and no custom avatar, check for gravatar
if ((Setting::getSettings()->load_remote == '1') && (Setting::getSettings()->default_avatar == '')) {

if ($this->model->gravatar != '') {
$gravatar = md5(strtolower(trim($this->model->gravatar)));
return '//gravatar.com/avatar/'.$gravatar;

} elseif ($this->email != '') {

$gravatar = md5(strtolower(trim($this->email)));
return '//gravatar.com/avatar/'.$gravatar;
}
}


return false;
}

Expand Down
1 change: 1 addition & 0 deletions database/seeders/SettingsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function run()
$settings->version_footer = 'on';
$settings->support_footer = 'on';
$settings->pwd_secure_min = '8';
$settings->default_avatar = 'default.png';
$settings->save();

if ($user = User::where('username', '=', 'admin')->first()) {
Expand Down
Binary file removed public/img/default-sm.png
Binary file not shown.
File renamed without changes
Binary file removed public/uploads/avatars/default.png
Binary file not shown.
5 changes: 4 additions & 1 deletion resources/lang/en-US/admin/settings/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@
'timezone' => 'Timezone',
'profile_edit' => 'Edit Profile',
'profile_edit_help' => 'Allow users to edit their own profiles.',
'default_avatar' => 'Upload default avatar',
'default_avatar' => 'Upload custom default avatar',
'default_avatar_help' => 'This image will be displayed as a profile if a user does not have a profile photo.',
'restore_default_avatar' => 'Restore <a href=":default_avatar" data-toggle="lightbox" data-type="image">original system default avatar</a>',
'restore_default_avatar_help' => '',

];
18 changes: 17 additions & 1 deletion resources/views/settings/branding.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,25 @@
"logoLabel" => trans('admin/settings/general.default_avatar'),
"logoClearVariable" => "clear_default_avatar",
"logoPath" => "avatars/",
"helpBlock" => trans('general.image_filetypes_help', ['size' => Helper::file_upload_max_size_readable()]),
"helpBlock" => trans('admin/settings/general.default_avatar_help').' '.trans('general.image_filetypes_help', ['size' => Helper::file_upload_max_size_readable()]),
])

@if (($setting->default_avatar == '') || (($setting->default_avatar == 'default.png') && (Storage::disk('public')->missing('default.png'))))
<!-- Restore Default Avatar -->
<div class="form-group">

<div class="col-md-9 col-md-offset-3">
<label class="form-control">
{{ Form::checkbox('restore_default_avatar', '1', old('restore_default_avatar', $setting->restore_default_avatar)) }}
<span>{!! trans('admin/settings/general.restore_default_avatar', ['default_avatar'=> Storage::disk('public')->url('default.png')]) !!}</span>
</label>
<p class="help-block">
{{ trans('admin/settings/general.restore_default_avatar_help') }}
</p>
</div>
</div>
@endif

<!-- Load gravatar -->
<div class="form-group {{ $errors->has('load_remote') ? 'error' : '' }}">
<div class="col-md-3">
Expand Down
Loading

0 comments on commit 9d890b3

Please sign in to comment.