Skip to content

Commit d659657

Browse files
author
Zane
committed
Editor: getCurrentUserAsAuthor
1 parent f1dc97b commit d659657

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

‎src/Controller/Editor/ArticleEditBaseController.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Service\Cms\ArticleEditor;
77
use App\Service\Factory;
88
use App\Service\FrontendHelper;
9+
use App\Service\User;
910
use Symfony\Component\HttpFoundation\JsonResponse;
1011
use Symfony\Component\HttpFoundation\RequestStack;
1112
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -15,6 +16,7 @@
1516

1617
abstract class ArticleEditBaseController extends BaseController
1718
{
19+
protected User $currentUserAsAuthor;
1820

1921
public function __construct(
2022
protected Factory $factory,
@@ -31,9 +33,8 @@ protected function loadArticleEditor(int $articleId) : ArticleEditor
3133
{
3234
$this->ajaxOnly();
3335

34-
if( empty($this->getUser()) ) {
35-
throw $this->createAccessDeniedException('Non sei loggato!');
36-
}
36+
// login check
37+
$this->getCurrentUserAsAuthor();
3738

3839
$this->articleEditor->load($articleId);
3940

@@ -45,6 +46,29 @@ protected function loadArticleEditor(int $articleId) : ArticleEditor
4546
}
4647

4748

49+
protected function getCurrentUserAsAuthor() : User
50+
{
51+
/**
52+
* $currentUser is unknown to Doctrine: if we try to set it as Author directly:
53+
*
54+
* A new entity was found through the relationship 'App\Entity\Cms\ArticleAuthor#user' that was not configured
55+
* to cascade persist operations for entity: App\Entity\PhpBB\User@--
56+
*/
57+
58+
if( !empty($this->currentUserAsAuthor) ) {
59+
return $this->currentUserAsAuthor;
60+
}
61+
62+
$currentUserId = $this->getUser()?->getId();
63+
64+
if( empty($currentUserId) ) {
65+
throw $this->createAccessDeniedException('Non sei loggato!');
66+
}
67+
68+
return $this->factory->createUser()->load($currentUserId);
69+
}
70+
71+
4872
protected function jsonOKResponse(string $okMessage) : JsonResponse
4973
{
5074
return $this->json([

‎src/Controller/Editor/ArticleNewController.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,7 @@ public function new() : Response
6868
#[Route('/scrivi/salva', name: 'app_article_new_submit', methods: ['POST'])]
6969
public function submit() : Response
7070
{
71-
$currentUser = $this->factory->getCurrentUser();
72-
73-
if( empty($currentUser) ) {
74-
75-
throw $this->createAccessDeniedException(
76-
'Non sei loggato! Solo gli utenti registrati possono creare nuovi articoli.'
77-
);
78-
}
71+
$currentUserAsAuthor = $this->getCurrentUserAsAuthor();
7972

8073
$this->validateCsrfToken();
8174

@@ -96,17 +89,10 @@ public function submit() : Response
9689

9790
$newArticleFormat = $this->request->get(static::FORMAT_FIELD_NAME);
9891

99-
/*
100-
* $currentUser is unknown to Doctrine: if we try to set it as Author directly:
101-
* A new entity was found through the relationship 'App\Entity\Cms\ArticleAuthor#user' that was not configured to cascade persist operations for entity: App\Entity\PhpBB\User@--
102-
*/
103-
$currentUserId = $currentUser->getId();
104-
$author = $this->factory->createUser()->load($currentUserId);
105-
10692
$this->articleEditor
10793
->setFormat($newArticleFormat)
108-
->addAuthor($author)
109-
->autotag($author)
94+
->addAuthor($currentUserAsAuthor)
95+
->autotag($currentUserAsAuthor)
11096
->save();
11197

11298
return $this->redirect( $this->articleEditor->getUrl() );

0 commit comments

Comments
 (0)