Skip to content

Commit

Permalink
[FEATURE] Add API endpoint to return the user's image
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexvaar committed Mar 26, 2018
1 parent 3fab8dc commit 2c5b39f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions Classes/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Server
'user/state' => [UserRepository::class, 'getUserState'],
'user/auth' => [SecurityService::class, 'authUser'],
'user/get' => [UserRepository::class, 'getUser'],
'user/image' => [UserRepository::class, 'getUserImage'],
'encryption/getKey' => [SecurityService::class, 'createEncryptionKey'],
];

Expand Down
52 changes: 52 additions & 0 deletions Classes/UserRepository.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
<?php
namespace In2code\T3AM\Server;

/*
* Copyright (C) 2018 Oliver Eglseder <php@vxvr.de>, in2code GmbH
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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.
*/

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\DatabaseConnection;
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;

/**
* Class UserRepository
*/
class UserRepository
{
/**
Expand Down Expand Up @@ -72,4 +92,36 @@ public function getUser($user)
$where = 'username = ' . $this->connection->fullQuoteStr($user, 'be_users');
return $this->connection->exec_SELECTgetSingleRow(implode(',', $this->fields), 'be_users', $where);
}

/**
* @param string $user
*
* @return null|array
*/
public function getUserImage($user)
{
$sql = '
SELECT sys_file.* FROM sys_file
RIGHT JOIN sys_file_reference ON sys_file.uid = sys_file_reference.uid_local
RIGHT JOIN be_users ON sys_file_reference.uid_foreign = be_users.uid
WHERE be_users.username = ' . $this->connection->fullQuoteStr($user, 'be_users') . '
AND sys_file_reference.deleted = 0
AND sys_file_reference.tablenames = "be_users"
AND sys_file_reference.fieldname = "avatar"';

$file = $this->connection->admin_query($sql)->fetch_assoc();
if (!empty($file['uid'])) {
try {
$resource = ResourceFactory::getInstance()->getFileObject($file['uid'], $file);
if ($resource instanceof File && $resource->exists()) {
return [
'identifier' => $resource->getName(),
'b64content' => base64_encode($resource->getContents()),
];
}
} catch (FileDoesNotExistException $e) {
}
}
return null;
}
}
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'author' => 'Oliver Eglseder',
'author_email' => 'php@vxvr.de',
'author_company' => 'in2code GmbH',
'version' => '1.0.1',
'version' => '1.1.0',
'constraints' => [
'depends' => [
'typo3' => '7.6.0-8.7.99',
Expand Down

0 comments on commit 2c5b39f

Please sign in to comment.