bolt / users

Bolt users extension.
MIT License
8 stars 7 forks source link

Records for storing additional user data never get created #9

Closed crim3hound closed 3 years ago

crim3hound commented 3 years ago

This is related to Issue #7 and PR #8.

I've taken some time to fiddle with the code in FrontendUsersProfileController.php and found that a bug seems to be in the record creation bit. I have rewritten my getUserRecord() function as follows:

private function getUserRecord(ContentType $contentType): Content
    {
        /** @var User $user */
        $user = $this->getUser();

        // access record if available
        $contentTypeSlug = $this->getExtension()->getExtConfig('contenttype', $user->getRoles()[0]);
        $contentType = $this->getBoltConfig()->getContentType($contentTypeSlug);

        $content = $this->contentRepository->findBy([
            'author' => $user,
            'contentType' => $contentType->getSlug(),
        ]);

        // bug in record creation bit
        if (empty($content)) {
            $content = new Content($contentType);
            $content->setAuthor($user);
            $content->setPublishedAt(new \DateTime());
            $content->setStatus(Statuses::PUBLISHED);
            $contentTypeName = strtolower($contentType->get('name', $contentType->get('slug')));
            $content->setContentType($contentTypeName);
            $this->contentFillListener->fillContent($content);

        } elseif (is_iterable($content)) {
            $content = end($content);
        }

        return $content;
    }

I have found that when I manually create a Member record, rewriting the $content definition as follows fetches content as expected, thereby rendering a profile page.

$content = $this->contentRepository->findBy([
        'contentType' => $contentType->getSlug(),
]);

Adding back the author filter throws an error because $content is empty, indicating that the block of code is working as expected. However, if $content is empty, we are supposed to add the respective user record, but that doesn't happen. I believe the issue is in this bit of the code:

 if (empty($content)) {
            $content = new Content($contentType);
            $content->setAuthor($user);
            $content->setPublishedAt(new \DateTime());
            $content->setStatus(Statuses::PUBLISHED);
            $contentTypeName = strtolower($contentType->get('name', $contentType->get('slug')));
            $content->setContentType($contentTypeName);
            $this->contentFillListener->fillContent($content);

 } elseif (is_iterable($content)) {
            $content = end($content);
 }

        return $content;

I have tried working on a function to generate the record during user registration, but I've been swimming in exceptions. I have also tried using a standalone record creation function in FrontendUsersProfileController.php as follows...

    private function new(ContentType $contentType): Content
    {
        /** @var User $user */
        $user = $this->getUser();

        $contentTypeSlug = $this->getExtension()->getExtConfig('contenttype', $user->getRoles()[0]);
        $contentType = $this->getBoltConfig()->getContentType($contentTypeSlug);

        $content = new Content($contentType);
        $content->setAuthor($user);
        $content->setCreatedAt(new \DateTime());
        $content->setPublishedAt(new \DateTime());
        $content->setStatus(Statuses::PUBLISHED);
        $contentTypeName = strtolower($contentType->get('name', $contentType->get('slug')));
        $content->setContentType($contentTypeName);
        $this->contentFillListener->fillContent($content);

        dump($content);
        return $content;
    }

...called from of the getUserRecord() function as follows:

if (empty($content)) {
       return $this->new($contentType);

} elseif (is_iterable($content)) {
       $content = end($content);
}

Dumping $content outputs the following, but the record is never created, even though it seems as though the new Content() function is working.

_dump

The implication is that the /profile route throws an error that the content requested cannot be found (see screenshot 1 below), and the edit fields in /profile/edit are not generated because there is no record to edit (see screenshot 2 below).

Screenshot 2020-10-20 073236

Screenshot 2020-10-20 073406

Any help will be greatly appreciated. Thanks!

crim3hound commented 3 years ago

I have found the solution to this issue. Will share a PR shortly.