-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into style/fix-user-highlight
- Loading branch information
Showing
20 changed files
with
572 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2017 Arawa | ||
* | ||
* @author 2021 Baptiste Fotia <[email protected]> | ||
* @author 2021 Cyrille Bollu <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Workspace\Controller; | ||
|
||
use OCA\Workspace\Files\Csv; | ||
use OCA\Workspace\Files\InternalFile; | ||
use OCA\Workspace\Files\LocalFile; | ||
use OCA\Workspace\Service\UserService; | ||
use OCA\Workspace\Service\WorkspaceService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\Node; | ||
use OCP\IRequest; | ||
use OCP\IUserManager; | ||
use OCP\IUserSession; | ||
|
||
class FileCSVController extends Controller { | ||
private $currentUser; | ||
|
||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
private IUserManager $userManager, | ||
private WorkspaceService $workspaceService, | ||
private UserService $userService, | ||
// private FileInfo $file, | ||
private IUserSession $userSession, | ||
private IRootFolder $rootFolder, | ||
) { | ||
parent::__construct($appName, $request); | ||
$this->currentUser = $userSession->getUser(); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @SpaceAdminRequired | ||
* Returns formatted list of existing users of the instance. | ||
* | ||
*/ | ||
public function import(): JSONResponse { | ||
$params = $this->request->getParams(); | ||
$spaceObj = $params['space']; | ||
$space = json_decode($spaceObj, true); | ||
$file = $this->request->getUploadedFile('file'); | ||
|
||
if ($this->isCSVMimeType($file)) { | ||
return new JSONResponse( | ||
[ | ||
'Wrong file extension. Must be <b>.csv</b>.' | ||
], | ||
Http::STATUS_FORBIDDEN | ||
); | ||
} | ||
|
||
$csv = new Csv(); | ||
|
||
$localFile = new LocalFile($file['tmp_name']); | ||
|
||
if (!$csv->hasProperHeader($localFile)) { | ||
return new JSONResponse( | ||
[ | ||
'Invalid file format. Table header doesn\'t contain any of the following values:<br>', | ||
[ | ||
...$csv::DISPLAY_NAME, | ||
...$csv::ROLE | ||
] | ||
], | ||
Http::STATUS_FORBIDDEN | ||
); | ||
} else { | ||
new JSONResponse( | ||
[ | ||
'Something went wrong. Couldn\'t open a file.' | ||
], | ||
Http::STATUS_FORBIDDEN | ||
); | ||
} | ||
|
||
$names = $csv->parser($localFile); | ||
|
||
$existingNames = array_filter($names, function ($user) { | ||
return $this->userManager->userExists($user['name']); | ||
}); | ||
|
||
// get list of IUser objects | ||
$users = $this->formatUsers($existingNames); | ||
|
||
$data = $this->formatData($users, $space); | ||
|
||
return new JSONResponse($data); | ||
} | ||
|
||
/** | ||
* @NoAdminRequired | ||
* @SpaceAdminRequired | ||
* Returns formatted list of existing users of the instance. | ||
* | ||
*/ | ||
public function getFromFiles():JSONResponse { | ||
$params = $this->request->getParams(); | ||
$path = $params['path']; | ||
$spaceObj = $params['space']; | ||
$space = json_decode($spaceObj, true); | ||
$uid = $this->currentUser->getUID(); | ||
$folder = $this->rootFolder->getUserFolder($uid); | ||
$file = $folder->get($path); | ||
|
||
if ($this->isCSVMimeType($file)) { | ||
return new JSONResponse(['Wrong file extension. Must be <b>.csv</b>.'], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$fullPath = $file->getInternalPath(); | ||
$csv = new Csv(); | ||
$internalFile = new InternalFile($fullPath, $file->getStorage()); | ||
|
||
if (!$csv->hasProperHeader($internalFile)) { | ||
return new JSONResponse(['Invalid file format. Table header doesn\'t contain any of the following values:<br>', [...$csv::DISPLAY_NAME, ...$csv::ROLE]], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$names = $csv->parser($internalFile); | ||
|
||
// filter array to leave only existing users | ||
$existingNames = array_filter($names, function ($user) { | ||
return $this->userManager->userExists($user['name']); | ||
}); | ||
|
||
$users = $this->formatUsers($existingNames); | ||
|
||
$data = $this->formatData($users, $space); | ||
|
||
return new JSONResponse($data); | ||
} | ||
|
||
private function formatData(array $users, array $space): array { | ||
$data = []; | ||
foreach ($users as $user) { | ||
$role = $user['role'] == "admin" ? "admin" : "user"; | ||
$data[] = $this->userService->formatUser($user['user'], $space, $role); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
private function formatUsers(array $data): array { | ||
$users = []; | ||
foreach($data as $user) { | ||
$users[] = [ | ||
'user' => $this->userManager->get($user['name']), | ||
'role' => $user['role'] | ||
]; | ||
} | ||
return $users; | ||
} | ||
|
||
private function isCSVMimeType(Node|array $file): bool { | ||
if($file instanceof Node) { | ||
return $file->getMimetype() !== 'text/csv'; | ||
} | ||
|
||
return $file['type'] !== 'text/csv'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2017 Arawa | ||
* | ||
* @author 2021 Baptiste Fotia <[email protected]> | ||
* @author 2021 Cyrille Bollu <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\Workspace\Files; | ||
|
||
use Exception; | ||
|
||
class Csv { | ||
|
||
public const DISPLAY_NAME = ["username", "displayname", "name"]; | ||
public const ROLE = ["role", "status", "userrole"]; | ||
|
||
private function getIndex(array $haystack, array $needles): int|bool { | ||
$index = null; | ||
foreach($haystack as $key => $value) { | ||
$index = array_search($value, $needles); | ||
if ($index !== false) { | ||
return $index; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function parser(ManagerConnectionFileInterface $file) { | ||
$handle = $file->open(); | ||
|
||
if ($handle === false) { | ||
throw new Exception("Imposible to open the $file->getPath() file."); | ||
} | ||
|
||
$users = []; | ||
// rewind($handle); | ||
$tableHeader = fgetcsv($handle, 1000, ","); // ignore the first line | ||
$tableHeader = array_map('strtolower', $tableHeader); | ||
|
||
$nameIndex = $this->getIndex($this::DISPLAY_NAME, $tableHeader); | ||
$roleIndex = $this->getIndex($this::ROLE, $tableHeader); | ||
|
||
while (($data = fgetcsv($handle, 1000, ",")) !== false) { | ||
$users[] = ['name' => $data[$nameIndex], 'role' => $data[$roleIndex]]; | ||
} | ||
$file->close(); | ||
|
||
return $users; | ||
} | ||
|
||
public function hasProperHeader(ManagerConnectionFileInterface $file): bool { | ||
// var_dump($path); | ||
// die(); | ||
$res = false; | ||
if (($handle = $file->open()) !== false) { | ||
$tableHeader = fgetcsv($handle, 1000, ","); | ||
$tableHeader = array_map('strtolower', $tableHeader); | ||
// var_dump($tableHeader); | ||
// die(); | ||
|
||
$nameIndex = $this->getIndex(self::DISPLAY_NAME, $tableHeader); | ||
$roleIndex = $this->getIndex(self::ROLE, $tableHeader); | ||
|
||
$res = ($nameIndex !== false) && ($roleIndex !== false); | ||
} | ||
|
||
$file->close(); | ||
|
||
return $res; | ||
} | ||
} |
Oops, something went wrong.