Skip to content

Commit

Permalink
refactor(php): Create and use a UserFormatter
Browse files Browse the repository at this point in the history
From the FileCSVController, I refactored all functions to get users data for the frontend in the new UserFormatter class.

Signed-off-by: Baptiste Fotia <[email protected]>
  • Loading branch information
zak39 committed Dec 1, 2023
1 parent e2bf971 commit 97767d9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 37 deletions.
55 changes: 18 additions & 37 deletions lib/Controller/FileCSVController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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 OCA\Workspace\Users\UsersExistCheck;
use OCP\AppFramework\Controller;
Expand All @@ -39,6 +38,7 @@
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCA\Workspace\Users\UserFormatter;

/**
* @todo rename to import csv users : ImportCsvUsersController
Expand All @@ -51,7 +51,7 @@ public function __construct(
IRequest $request,
private IUserManager $userManager,
private WorkspaceService $workspaceService,
private UserService $userService,
private UserFormatter $userFormatter,
private UsersExistCheck $userChecker,
// private FileInfo $file,
private IUserSession $userSession,
Expand Down Expand Up @@ -128,14 +128,15 @@ function ($name) {
return new JSONResponse([$errorMessage], Http::STATUS_FORBIDDEN);
}

$existingNames = array_filter($names, function ($user) {
return $this->userManager->userExists($user['name']);
});

// get list of IUser objects
$users = $this->formatUsers($existingNames);
$names = array_map(function($user) {
$user['user'] = $this->userManager->get($user['name']);
return $user;
}, $names);

$data = $this->formatData($users, $space);
$data = array_map(
fn($user) => $this->userFormatter->formatUser($user['user'], $space, $user['role']),
$names
);

return new JSONResponse($data);
}
Expand Down Expand Up @@ -189,39 +190,19 @@ function ($name) {
return new JSONResponse([$errorMessage], Http::STATUS_FORBIDDEN);
}

// filter array to leave only existing users
$existingNames = array_filter($names, function ($user) {
return $this->userManager->userExists($user['name']);
});

$users = $this->formatUsers($existingNames);
$names = array_map(function($user) {
$user['user'] = $this->userManager->get($user['name']);
return $user;
}, $names);

$data = $this->formatData($users, $space);
$data = array_map(
fn($user) => $this->userFormatter->formatUser($user['user'], $space, $user['role']),
$names
);

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';
Expand Down
40 changes: 40 additions & 0 deletions lib/Users/UserFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace OCA\Workspace\Users;

use OCP\IGroupManager;
use OCP\IUser;

class UserFormatter
{
public function __construct(private IGroupManager $groupManager)
{
}

/**
* @since 3.1.0|4.0.0
*
* @param IUser $user
* @param array $space
* @param string $role admin|user
* @return array users array valid for the frontend
*/
public function formatUser(IUser $user, array $space, string $role): array {
// Gets the workspace subgroups the user is member of
$groups = [];
foreach ($this->groupManager->getUserGroups($user) as $group) {
if (in_array($group->getGID(), array_keys($space['groups']))) {
array_push($groups, $group->getGID());
}
}

return array(
'uid' => $user->getUID(),
'name' => $user->getDisplayName(),
'email' => $user->getEmailAddress(),
'subtitle' => $user->getEmailAddress(),
'groups' => $groups,
'role' => $role
);
}
}

0 comments on commit 97767d9

Please sign in to comment.