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

Add the profile's PID to child records of a profile #9

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions Classes/Command/CreateProfilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,12 @@ public function __construct(FrontendUserProvider $frontendUserProvider, EventDis

protected function configure(): void
{
$this
->setHelp('This command create profiles for all frontend users that do not have a profile yet but should have one.')
->addOption(
'storage-pid',
's',
InputOption::VALUE_REQUIRED,
'Storage PID for the fe_user records.',
0
);
$this->setHelp('This command create profiles for all frontend users that do not have a profile yet but should have one.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$storagePid = (int)$input->getOption('storage-pid');

if ($storagePid <= 0) {
$output->writeln('<error>StoragePid needs to be a positive integer.</error>');
return Command::FAILURE;
}

$frontendUsers = $this->frontendUserProvider->getUsersWithoutProfile($storagePid);
$frontendUsers = $this->frontendUserProvider->getUsersWithoutProfile();

foreach ($frontendUsers as $frontendUser) {
$frontendUserAuthentication = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
Expand Down
19 changes: 10 additions & 9 deletions Classes/Profile/AbstractProfileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,26 @@ public function shouldCreateProfileForUser(FrontendUserAuthentication $frontendU

try {
$configuration = $this->extensionConfiguration->get('academic_persons_edit');
$shouldCreateProfile = (bool)($configuration['profile']['autoCreateProfiles'] ?? false);
$userGroupListToCreateProfileFor = (string)($configuration['profile']['createProfileForUserGroups'] ?? '');
} catch (ExtensionConfigurationExtensionNotConfiguredException) {
$shouldCreateProfile = false;
$userGroupListToCreateProfileFor = '';
} catch (ExtensionConfigurationPathDoesNotExistException) {
} catch (ExtensionConfigurationExtensionNotConfiguredException | ExtensionConfigurationPathDoesNotExistException) {
return false;
}

// Check if the profile should be created for the user
$shouldCreateProfile = (bool)($configuration['profile']['autoCreateProfiles'] ?? false);
if ($shouldCreateProfile === false) {
return false;
}

// Check if the user is in a user group that should have a profile created
$userGroupListToCreateProfileFor = (string)($configuration['profile']['createProfileForUserGroups'] ?? '');
if (empty($userGroupListToCreateProfileFor)) {
return $shouldCreateProfile;
}

$userGroupId = $userAspect->getGroupIds();
$userGroupIdsToCreateProfileFor = GeneralUtility::intExplode(',', $userGroupListToCreateProfileFor);
$userIsInUserGroup = count(array_intersect($userGroupId, $userGroupIdsToCreateProfileFor)) > 0;

return $shouldCreateProfile && $userIsInUserGroup;
return $userIsInUserGroup;
}

public function createProfileForUser(FrontendUserAuthentication $frontendUserAuthentication): ?int
Expand All @@ -82,7 +84,6 @@ public function createProfileForUser(FrontendUserAuthentication $frontendUserAut
}

$profileForDefaultLanguage = $this->createProfileFromFrontendUser($userData);
$profileForDefaultLanguage->setPid((int)$userData['pid']);
$profileForDefaultLanguage->getFrontendUsers()->attach($frontendUser);
$this->persistenceManager->add($profileForDefaultLanguage);

Expand Down
8 changes: 8 additions & 0 deletions Classes/Profile/ProfileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ final class ProfileFactory extends AbstractProfileFactory
*/
protected function createProfileFromFrontendUser(array $frontendUserData): Profile
{
$pid = (int)$frontendUserData['pid'];

$profile = new Profile();
$profile->setPid($pid);
$profile->setTitle((string)($frontendUserData['title'] ?? ''));
$profile->setFirstName((string)($frontendUserData['first_name'] ?? ''));
$profile->setMiddleName((string)($frontendUserData['middle_name'] ?? ''));
$profile->setLastName((string)($frontendUserData['last_name'] ?? ''));
$profile->setWebsite((string)($frontendUserData['www'] ?? ''));

$contract = new Contract();
$contract->setPid($pid);
$profile->getContracts()->attach($contract);

$address = new Address();
$address->setPid($pid);
$address->setStreet((string)($frontendUserData['address'] ?? ''));
$address->setZip((string)($frontendUserData['zip'] ?? ''));
$address->setCity((string)($frontendUserData['city'] ?? ''));
Expand All @@ -43,18 +48,21 @@ protected function createProfileFromFrontendUser(array $frontendUserData): Profi

if (!empty($frontendUserData['email'])) {
$email = new Email();
$email->setPid($pid);
$email->setEmail((string)($frontendUserData['email']));
$contract->getEmailAddresses()->attach($email);
}

if (!empty($frontendUserData['telephone'])) {
$phoneNumber = new PhoneNumber();
$phoneNumber->setPid($pid);
$phoneNumber->setPhoneNumber((string)($frontendUserData['telephone']));
$phoneNumber->setType('phone');
$contract->getPhoneNumbers()->attach($phoneNumber);
}
if (!empty($frontendUserData['fax'])) {
$phoneNumber = new PhoneNumber();
$phoneNumber->setPid($pid);
$phoneNumber->setPhoneNumber((string)($frontendUserData['fax']));
$phoneNumber->setType('fax');
$contract->getPhoneNumbers()->attach($phoneNumber);
Expand Down
6 changes: 3 additions & 3 deletions Classes/Provider/FrontendUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(ConnectionPool $connectionPool)
/**
* @return array<int, array<string, mixed>>
*/
public function getUsersWithoutProfile(int $storagePid): array
public function getUsersWithoutProfile(): array
{
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('fe_users');
return $queryBuilder
Expand All @@ -44,8 +44,8 @@ public function getUsersWithoutProfile(int $storagePid): array
->where(
$queryBuilder->expr()->isNull('tx_academicpersons_feuser_mm.uid_local'),
$queryBuilder->expr()->eq(
'fe_users.pid',
$queryBuilder->createNamedParameter($storagePid, Connection::PARAM_INT)
'fe_users.tx_extbase_type',
$queryBuilder->createNamedParameter('Tx_Academicpersonsedit_Domain_Model_FrontendUser', Connection::PARAM_STR)
)
)
->groupBy('fe_users.uid')
Expand Down
Loading