Skip to content

Commit

Permalink
WIP: Allow avatar upload
Browse files Browse the repository at this point in the history
  • Loading branch information
richieri-bps committed Sep 13, 2024
1 parent 5de1b6c commit 88f2d85
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
10 changes: 10 additions & 0 deletions etc/RT_Config.pm.in
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,16 @@ Set($AvatarEngine, 'Initials');

=back

=head2 EnableAvatarUpload

If set to true, users will be allowed to upload their own avatars.

Default is false.

=cut

Set($EnableAvatarUpload, 0);

=item C<%SVG>

C<%SVG> allows you to customize SVG images used in RT.
Expand Down
11 changes: 10 additions & 1 deletion share/html/Elements/ShowAvatar
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ $class => undef
</%ARGS>
<%INIT>
my $engine = RT->Config->Get('AvatarEngine') || 'Initials';

# Check if the user has an uploaded avatar (only if uploads are enabled)
my $has_uploaded_avatar = 0;
if ( RT->Config->Get('EnableAvatarUpload') && $User ) {
my $avatar_attr = $User->Preferences('Avatar');
$has_uploaded_avatar = $avatar_attr ? 1 : 0;
}
</%INIT>

% if ($engine eq 'Gravatar') {
% if ($has_uploaded_avatar) {
<& /Elements/ShowAvatarUploaded, %ARGS &>
% } elsif ($engine eq 'Gravatar') {
<& /Elements/ShowAvatarGravatar, %ARGS &>
% } else {
<& /Elements/ShowAvatarInitials, %ARGS &>
Expand Down
71 changes: 71 additions & 0 deletions share/html/Elements/ShowAvatarUploaded
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2024 Best Practical Solutions, LLC
%# <[email protected]>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<%ARGS>
$User => undef
$Tooltip => undef
$Size => 'sm'
$class => undef
</%ARGS>
<%INIT>
my $avatar_attr = $User->Preferences('Avatar');
return unless $avatar_attr;

my $content = $avatar_attr->{data}; # Adjusted to match 'data' key
my $content_type = $avatar_attr->{type}; # Adjusted to match 'type' key

# Encode content for inline display
my $encoded_content = MIME::Base64::encode_base64($content, '');

my $data_uri = "data:$content_type;base64,$encoded_content";
</%INIT>
<img src="<% $data_uri |n %>" class="avatar avatar-<% $Size %> <% $class %> rounded-circle p-0"
% if ($Tooltip) {
data-bs-toggle="tooltip" data-bs-placement="right"
data-bs-title="<% $Tooltip %>" alt="<% $Tooltip %>"
% }
>
22 changes: 21 additions & 1 deletion share/html/Prefs/AboutMe.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</form>

<%INIT>
use Digest::MD5 'md5_hex';

my $UserObj = RT::User->new( $session{'CurrentUser'} );
$UserObj->Load($id) if $id;
Expand Down Expand Up @@ -102,7 +103,7 @@

push @results, ProcessObjectCustomFieldUpdates( ARGSRef => \%ARGS, Object => $UserObj );

# Deal with special fields: Privileged, Enabled, and Password
# Deal with special fields: Privileged, Enabled, Password and Avatar
if ( $SetPrivileged and $Privileged != $UserObj->Privileged ) {
my ($code, $msg) = $UserObj->SetPrivileged( $Privileged );
push @results, loc('Privileged status: [_1]', loc_fuzzy($msg));
Expand All @@ -117,6 +118,25 @@
);
push @results, loc("Password: [_1]", $msg);
}

if ( RT->Config->Get('EnableAvatarUpload') ) {
if ( my $file_hash = _UploadedFile( 'FormUploadAvatar' ) ) {
my ($status, $msg) = $UserObj->SetPreferences(
'Avatar',
{
type => $file_hash->{ContentType},
data => $file_hash->{LargeContent},
hash => md5_hex($file_hash->{LargeContent}),
},
);
if ($status) {
push @results, loc('Avatar updated');
} else {
push @results, loc('Failed to update avatar: [_1]', $msg);
RT->Logger->error("Failed to update avatar for user " . $UserObj->Name . ": $msg");
}
}
}
}


Expand Down
8 changes: 8 additions & 0 deletions share/html/Prefs/Elements/EditAboutMe
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<&| /Widgets/TitleBox, title => loc('Identity'), id => "user-prefs-identity" &>
<input type="hidden" class="hidden" name="Name" value="<%$UserObj->Name%>" />

<& /Elements/ShowAvatar, User => $UserObj, Size => 'sm' &>

% if ( RT->Config->Get('EnableAvatarUpload') ) {
<&| /Elements/LabeledValue, Label => loc("Avatar") &>
<input type="file" class="form-control" name="FormUploadAvatar" accept="image/*">
</&>
% }

<&| /Elements/LabeledValue, Label => loc("Email") &>
<input class="form-control" type="text" name="EmailAddress" value="<%$UserObj->EmailAddress%>" />
</&>
Expand Down

0 comments on commit 88f2d85

Please sign in to comment.