Skip to content
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

Added #15015 - ability for admins to select default avatar #15027

Merged
merged 15 commits into from
Jul 4, 2024
66 changes: 45 additions & 21 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ public function postSettings(Request $request)
}

$setting->default_eula_text = $request->input('default_eula_text');
$setting->load_remote = $request->input('load_remote', 0);
$setting->thumbnail_max_h = $request->input('thumbnail_max_h');
$setting->privacy_policy_link = $request->input('privacy_policy_link');
$setting->depreciation_method = $request->input('depreciation_method');
Expand Down Expand Up @@ -393,10 +392,11 @@ public function getBranding()
*
* @since [v1.0]
*
* @return View
* @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse
*/
public function postBranding(ImageUploadRequest $request)
{
// Something has gone horribly wrong - no settings record exists!
if (is_null($setting = Setting::getSettings())) {
return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error'));
}
Expand All @@ -407,51 +407,75 @@ public function postBranding(ImageUploadRequest $request)
$setting->version_footer = $request->input('version_footer');
$setting->footer_text = $request->input('footer_text');
$setting->skin = $request->input('skin');
$setting->allow_user_skin = $request->input('allow_user_skin');
$setting->allow_user_skin = $request->input('allow_user_skin', '0');
$setting->show_url_in_emails = $request->input('show_url_in_emails', '0');
$setting->logo_print_assets = $request->input('logo_print_assets', '0');
$setting->load_remote = $request->input('load_remote', 0);

// Only allow the site name and CSS to be changed if lock_passwords is false
// Only allow the site name, images, and CSS to be changed if lock_passwords is false
// Because public demos make people act like dicks

if (! config('app.lock_passwords')) {
$request->validate(['site_name' => 'required']);
$setting->site_name = $request->input('site_name');
if (!config('app.lock_passwords')) {

if ($request->has('site_name')) {
$request->validate(['site_name' => 'required']);
}

$setting->site_name = $request->input('site_name', 'Snipe-IT');
$setting->custom_css = $request->input('custom_css');

// Logo upload
$setting = $request->handleImages($setting, 600, 'logo', '', 'logo');

if ('1' == $request->input('clear_logo')) {
Storage::disk('public')->delete($setting->logo);
if ($request->input('clear_logo') == '1') {

if (($setting->logo) && (Storage::exists($setting->logo))) {
Storage::disk('public')->delete($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 ('1' == $request->input('clear_email_logo')) {
Storage::disk('public')->delete($setting->email_logo);
if (($setting->email_logo) && (Storage::exists($setting->email_logo))) {
Storage::disk('public')->delete($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 ('1' == $request->input('clear_label_logo')) {
Storage::disk('public')->delete($setting->label_logo);
if (($setting->label_logo) && (Storage::exists($setting->label_logo))) {
Storage::disk('public')->delete($setting->label_logo);
}
$setting->label_logo = null;
}


$setting = $request->handleImages($setting, 600, 'favicon', '', 'favicon');

// If the user wants to clear the favicon...
// Favicon upload
$setting = $request->handleImages($setting, 100, 'favicon', '', 'favicon');
if ('1' == $request->input('clear_favicon')) {
Storage::disk('public')->delete($setting->favicon);

if (($setting->favicon) && (Storage::exists($setting->favicon))) {
Storage::disk('public')->delete($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);
}
$setting->default_avatar = null;
}
}

if ($setting->save()) {
Expand Down
5 changes: 1 addition & 4 deletions app/Http/Requests/ImageUploadRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ public function handleImages($item, $w = 600, $form_fieldname = 'image', $path =

$ext = $image->guessExtension();
$file_name = $type.'-'.$form_fieldname.'-'.$item->id.'-'.str_random(10).'.'.$ext;

Log::info('File name will be: '.$file_name);
Log::debug('File extension is: '.$ext);


if (($image->getMimeType() == 'image/vnd.microsoft.icon') || ($image->getMimeType() == 'image/x-icon') || ($image->getMimeType() == 'image/avif') || ($image->getMimeType() == 'image/webp')) {
// If the file is an icon, webp or avif, we need to just move it since gd doesn't support resizing
// icons or avif, and webp support and needs to be compiled into gd for resizing to be available
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Transformers/UsersTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function transformUser(User $user)

$array = [
'id' => (int) $user->id,
'avatar' => e($user->present()->gravatar),
'avatar' => e($user->present()->gravatar) ?? null,
'name' => e($user->getFullNameAttribute()),
'first_name' => e($user->first_name),
'last_name' => e($user->last_name),
Expand Down
12 changes: 10 additions & 2 deletions app/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ public function name()
*/
public function gravatar()
{

// User's specific avatar
if ($this->avatar) {

// Check if it's a google avatar or some external avatar
Expand All @@ -443,6 +445,12 @@ public function gravatar()
return Storage::disk('public')->url('avatars/'.e($this->avatar));
}

// If there is a 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 != '') {

Expand All @@ -456,8 +464,8 @@ public function gravatar()
}
}

// Set a fun, gender-neutral default icon
return config('app.url').'/img/default-sm.png';

return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->string('default_avatar')->after('favicon')->default('default.png')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->dropColumn('default_avatar');
});
}
};
5 changes: 3 additions & 2 deletions resources/lang/en-US/admin/settings/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
'ldap_test' => 'Test LDAP',
'ldap_test_sync' => 'Test LDAP Synchronization',
'license' => 'Software License',
'load_remote' => 'Use Gravatar',
'load_remote_help_text' => 'Uncheck this box if your install cannot load scripts from the outside internet. This will prevent Snipe-IT from trying load images from Gravatar.',
'load_remote' => 'Load Remote Avatars',
'load_remote_help_text' => 'Uncheck this box if your install cannot load scripts from the outside internet. This will prevent Snipe-IT from trying load avatars from Gravatar or other outside sources.',
'login' => 'Login Attempts',
'login_attempt' => 'Login Attempt',
'login_ip' => 'IP Address',
Expand Down Expand Up @@ -375,5 +375,6 @@
'timezone' => 'Timezone',
'profile_edit' => 'Edit Profile',
'profile_edit_help' => 'Allow users to edit their own profiles.',
'default_avatar' => 'Upload default avatar',

];
2 changes: 1 addition & 1 deletion resources/views/layouts/default.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@
<img src="{{ Auth::user()->present()->gravatar() }}" class="user-image"
alt="">
@else
<i class="fas fa-users" aria-hidden="true"></i>
<i class="fas fa-user" aria-hidden="true"></i>
@endif

<span class="hidden-xs">{{ Auth::user()->getFullNameAttribute() }} <strong
Expand Down
17 changes: 8 additions & 9 deletions resources/views/partials/forms/edit/uploadLogo.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="col-md-9">
<label class="btn btn-default{{ (config('app.lock_passwords')) ? ' disabled' : '' }}">
{{ trans('button.select_file') }}
<input type="file" name="{{ $logoVariable }}" class="js-uploadFile" id="{{ $logoId }}" accept="{{ (isset($allowedTypes) ? $allowedTypes : "image/gif,image/jpeg,image/webp,image/png,image/svg,image/svg+xml") }}" data-maxsize="{{ $maxSize ?? Helper::file_upload_max_size() }}"
<input type="file" name="{{ $logoVariable }}" class="js-uploadFile" id="{{ $logoId }}" accept="{{ $allowedTypes ?? "image/gif,image/jpeg,image/webp,image/png,image/svg,image/svg+xml" }}" data-maxsize="{{ $maxSize ?? Helper::file_upload_max_size() }}"
style="display:none; max-width: 90%"{{ (config('app.lock_passwords')) ? ' disabled' : '' }}>
</label>

Expand All @@ -28,13 +28,12 @@
</div>

<div class="col-md-9 col-md-offset-3">

@if (($setting->$logoVariable!='') && (Storage::disk('public')->exists(e($snipeSettings->$logoVariable))))
<div class="pull-left" style="padding-right: 20px;">
<a href="{{ Storage::disk('public')->url(e($snipeSettings->$logoVariable)) }}"{!! ($logoVariable!='favicon') ? ' data-toggle="lightbox"' : '' !!}>
<img id="{{ $logoId }}-imagePreview" style="height: 80px; padding-bottom: 5px;" alt="" src="{{ Storage::disk('public')->url(e($snipeSettings->$logoVariable)) }}">
</a>
</div>
@if (($setting->$logoVariable!='') && (Storage::disk('public')->exists(($logoPath ?? ''). $snipeSettings->$logoVariable)))
<div class="pull-left" style="padding-right: 20px;">
<a href="{{ Storage::disk('public')->url(e(($logoPath ?? '').$snipeSettings->$logoVariable)) }}"{!! ($logoVariable!='favicon') ? ' data-toggle="lightbox"' : '' !!}>
<img id="{{ $logoId }}-imagePreview" style="height: 80px; padding-bottom: 5px;" alt="" src="{{ Storage::disk('public')->url(e(($logoPath ?? ''). $snipeSettings->$logoVariable)) }}">
</a>
</div>
@endif

<div id="{{ $logoId }}-previewContainer" style="display: none;">
Expand All @@ -44,7 +43,7 @@


</div>
@if (($setting->$logoVariable!='') && (Storage::disk('public')->exists(e($snipeSettings->$logoVariable))))
@if (($setting->$logoVariable!='') && (Storage::disk('public')->exists(($logoPath ?? '').$snipeSettings->$logoVariable)))

<div class="col-md-9 col-md-offset-3">
<label id="{{ $logoId }}-deleteCheckbox" for="{{ $logoClearVariable }}" style="font-weight: normal" class="form-control">
Expand Down
32 changes: 31 additions & 1 deletion resources/views/settings/branding.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,37 @@
"maxSize" => 20000
])

<!-- Include logo in print assets -->
<!-- Default Avatar -->
@include('partials/forms/edit/uploadLogo', [
"logoVariable" => "default_avatar",
"logoId" => "defaultAvatar",
"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()]),
])

<!-- Load gravatar -->
<div class="form-group {{ $errors->has('load_remote') ? 'error' : '' }}">
<div class="col-md-3">
<strong>{{ trans('admin/settings/general.load_remote') }}</strong>
</div>
<div class="col-md-9">
<label class="form-control">
{{ Form::checkbox('load_remote', '1', old('load_remote', $setting->load_remote)) }}
{{ trans('general.yes') }}
{!! $errors->first('load_remote', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</label>

<p class="help-block">
{{ trans('admin/settings/general.load_remote_help_text') }}
</p>

</div>
</div>


<!-- Include logo in print assets -->
<div class="form-group">
<div class="col-md-3">
<strong>{{ trans('admin/settings/general.logo_print_assets') }}</strong>
Expand Down
18 changes: 0 additions & 18 deletions resources/views/settings/general.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,6 @@
</div>
</div>

<!-- Load gravatar -->
<div class="form-group {{ $errors->has('load_remote') ? 'error' : '' }}">
<div class="col-md-3">
<strong>{{ trans('admin/settings/general.load_remote') }}</strong>
</div>
<div class="col-md-9">
<label class="form-control">
{{ Form::checkbox('load_remote', '1', old('load_remote', $setting->load_remote)) }}
{{ trans('general.yes') }}
{!! $errors->first('load_remote', '<span class="alert-msg" aria-hidden="true">:message</span>') !!}
</label>

<p class="help-block">
{{ trans('admin/settings/general.load_remote_help_text') }}
</p>

</div>
</div>

<!-- unique serial -->
<div class="form-group">
Expand Down
Loading
Loading