Переглянути джерело

Fix edit document using document repository (with flysystem)

Julio Montoya 5 роки тому
батько
коміт
a67c7c6442

+ 0 - 3
.env

@@ -1,11 +1,8 @@
-# This file is a "template" of which env vars needs to be defined in your configuration or in an .env file
 # Set variables here that may be different on each deployment target of the app, e.g. development, staging, production.
 # https://symfony.com/doc/current/best_practices/configuration.html#infrastructure-related-configuration
 
 ###> doctrine/doctrine-bundle ###
 # Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
-# For a sqlite database, use: "sqlite:///%kernel.project_dir%/var/data.db"
-# Set "serverVersion" to your server version to avoid edge-case exceptions and extra database calls
 DATABASE_HOST="{{DATABASE_HOST}}"
 DATABASE_PORT="{{DATABASE_PORT}}"
 DATABASE_NAME="{{DATABASE_NAME}}"

+ 37 - 37
config/packages/hwi_oauth.yaml

@@ -1,38 +1,38 @@
 hwi_oauth:
-  connect:
-    account_connector: chamilo_user.security.user_provider
-  firewall_names:
-    - admin
-  resource_owners:
-    github:
-      type: github
-      client_id: '%env(GITHUB_ID)%'
-      client_secret: '%env(GITHUB_SECRET)%'
-    facebook:
-      type: facebook
-      client_id: '%env(FB_ID)%'
-      client_secret: '%env(FB_SECRET)%'
-    google:
-      type: google
-      client_id: '%env(GOOGLE_ID)%'
-      client_secret: '%env(GOOGLE_SECRET)%'
-      scope: "email profile"
-    my_custom_oauth2:
-      type: oauth2
-      client_id: '<client_id>'
-      client_secret: '<client_secret>'
-      access_token_url: 'https://path.to/oauth/v2/token'
-      authorization_url: 'https://path.to/oauth/v2/authorize'
-      infos_url: 'https://path.to/api/user'
-      scope: user_details
-      user_response_class: HWI\Bundle\OAuthBundle\OAuth\Response\PathUserResponse
-      paths: {identifier: id, nickname: username, realname: fullname}
-  fosub:
-    # try 30 times to check if a username is available (foo, foo1, foo2 etc)
-    username_iterations: 30
-    # mapping between resource owners (see below) and properties
-    properties:
-      github: githubId
-      google: googleId
-      facebook: facebookId
-      my_custom_provider: customId
+    connect:
+        account_connector: chamilo_user.security.user_provider
+    firewall_names:
+        - admin
+    resource_owners:
+        github:
+            type: github
+            client_id: '%env(GITHUB_ID)%'
+            client_secret: '%env(GITHUB_SECRET)%'
+        facebook:
+            type: facebook
+            client_id: '%env(FB_ID)%'
+            client_secret: '%env(FB_SECRET)%'
+        google:
+            type: google
+            client_id: '%env(GOOGLE_ID)%'
+            client_secret: '%env(GOOGLE_SECRET)%'
+            scope: "email profile"
+        my_custom_oauth2:
+            type: oauth2
+            client_id: '<client_id>'
+            client_secret: '<client_secret>'
+            access_token_url: 'https://path.to/oauth/v2/token'
+            authorization_url: 'https://path.to/oauth/v2/authorize'
+            infos_url: 'https://path.to/api/user'
+            scope: user_details
+            user_response_class: HWI\Bundle\OAuthBundle\OAuth\Response\PathUserResponse
+            paths: {identifier: id, nickname: username, realname: fullname}
+    fosub:
+        # try 30 times to check if a username is available (foo, foo1, foo2 etc)
+        username_iterations: 30
+        # mapping between resource owners (see below) and properties
+        properties:
+            github: githubId
+            google: googleId
+            facebook: facebookId
+            my_custom_provider: customId

+ 2 - 2
config/packages/security.yaml

@@ -81,8 +81,8 @@ security:
                     facebook: "/login/check-facebook"
                     google: "/login/check-google"
                     github: "/login/check-github"
-                login_path: /login_oauth
-                failure_path: /login_oauth
+                login_path: /login
+                failure_path: /login
                 use_forward: false
                 oauth_user_provider:
                     service: chamilo_user.security.user_provider

+ 5 - 6
main/document/edit_document.php

@@ -281,10 +281,8 @@ if ($is_allowed_to_edit) {
         $read_only_flag = isset($_POST['readonly']) ? $_POST['readonly'] : null;
         $read_only_flag = empty($read_only_flag) ? 0 : 1;
 
-        if ($read_only_flag == 0) {
-            if (!empty($content)) {
-                $documentRepository->updateDocumentContent($document, $content);
-            }
+        if ($read_only_flag == 0 && !empty($content)) {
+            $documentRepository->updateDocumentContent($document, $content);
         }
 
         header('Location: document.php?id='.$document_data['parent_id'].'&'.api_get_cidreq().($is_certificate_mode ? '&curdirpath=/certificates&selectcat=1' : ''));
@@ -303,10 +301,11 @@ $extension = $path_info['extension'] ?? '';
 
 $em = Database::getManager();
 /** @var \Chamilo\CoreBundle\Entity\Resource\ResourceNode $node */
-$node = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($document_data['resource_node_id']);
+//$node = $em->getRepository('ChamiloCoreBundle:Resource\ResourceNode')->find($document_data['resource_node_id']);
+$node = $document->getResourceNode();
 
 if (in_array($extension, ['html', 'htm'])) {
-    $file = $node->getResourceFile();
+    $file = $document->getResourceNode()->getResourceFile();
 
     if ($file) {
         $content = $documentRepository->getDocumentContent($document_data['id']);

+ 4 - 3
src/CoreBundle/Controller/ResourceController.php

@@ -17,6 +17,7 @@ use Chamilo\CourseBundle\Controller\CourseControllerTrait;
 use Chamilo\CourseBundle\Entity\CDocument;
 use Chamilo\CourseBundle\Repository\CDocumentRepository;
 use FOS\RestBundle\View\View;
+use League\Flysystem\MountManager;
 use Liip\ImagineBundle\Service\FilterService;
 use Sonata\MediaBundle\Provider\ImageProvider;
 use Sonata\MediaBundle\Provider\MediaProviderInterface;
@@ -415,7 +416,8 @@ class ResourceController extends BaseController implements CourseControllerInter
     public function showAction(
         Request $request,
         CDocumentRepository $documentRepo,
-        FilterService $filterService
+        FilterService $filterService,
+        MountManager $mountManager
     ): Response {
         $file = $request->get('file');
         $type = $request->get('type');
@@ -453,11 +455,10 @@ class ResourceController extends BaseController implements CourseControllerInter
 
         //$media = $resourceFile->getMedia();
         //$format = MediaProviderInterface::FORMAT_REFERENCE;
-
         $fileName = $resourceNode->getName();
         $filePath = $resourceFile->getFile()->getPathname();
         $mimeType = $resourceFile->getMimeType();
-
+        // @todo use $documentRepo
         $fs = $this->get('oneup_flysystem.mount_manager')->getFilesystem('resources_fs');
 
         $stream = $fs->readStream($filePath);

+ 2 - 3
src/CoreBundle/Entity/Resource/ResourceFile.php

@@ -27,12 +27,11 @@ class ResourceFile
     protected $id;
 
     /**
-     *
      * @Assert\NotBlank()
      *
-     * @ORM\Column(type="string", length=255)
-     *
      * @var string
+     *
+     * @ORM\Column(type="string", length=255)
      */
     protected $name;
 

+ 11 - 2
src/CourseBundle/Repository/CDocumentRepository.php

@@ -91,6 +91,11 @@ class CDocumentRepository extends ResourceRepository
         }
     }
 
+    /**
+     * @param int $id
+     *
+     * @return string
+     */
     public function getDocumentContent($id): string
     {
         try {
@@ -105,10 +110,15 @@ class CDocumentRepository extends ResourceRepository
         }
     }
 
+    /**
+     * @param CDocument $document
+     * @param string    $content
+     *
+     * @return bool
+     */
     public function updateDocumentContent(CDocument $document, $content)
     {
         try {
-            //$document = $this->find($id);
             $resourceNode = $document->getResourceNode();
             $resourceFile = $resourceNode->getResourceFile();
             $fileName = $resourceFile->getFile()->getPathname();
@@ -120,7 +130,6 @@ class CDocumentRepository extends ResourceRepository
 
             return true;
         } catch (\Throwable $exception) {
-            throw new $exception;
         }
     }