3030use OCA \Richdocuments \Helper ;
3131use OCA \Richdocuments \TemplateManager ;
3232use OCP \AppFramework \Http ;
33+ use OCP \AppFramework \Http \Attribute \BruteForceProtection ;
34+ use OCP \AppFramework \Http \Attribute \NoAdminRequired ;
35+ use OCP \AppFramework \Http \Attribute \PublicPage ;
3336use OCP \AppFramework \Http \DataResponse ;
3437use OCP \AppFramework \Http \JSONResponse ;
3538use OCP \Files \Folder ;
4043use OCP \Files \Lock \NoLockProviderException ;
4144use OCP \IL10N ;
4245use OCP \IRequest ;
46+ use OCP \ISession ;
4347use OCP \PreConditionNotMetException ;
4448use OCP \Share \IManager ;
4549use Psr \Log \LoggerInterface ;
4650use Throwable ;
4751
4852class DocumentAPIController extends \OCP \AppFramework \OCSController {
49- private $ rootFolder ;
50- private $ shareManager ;
51- private $ templateManager ;
52- private $ l10n ;
53- private $ logger ;
54- private $ lockManager ;
55- private $ userId ;
56-
57- public function __construct (IRequest $ request , IRootFolder $ rootFolder , IManager $ shareManager , TemplateManager $ templateManager , IL10N $ l10n , LoggerInterface $ logger , ILockManager $ lockManager , $ userId ) {
53+ public function __construct (
54+ IRequest $ request ,
55+ private IRootFolder $ rootFolder ,
56+ private IManager $ shareManager ,
57+ private TemplateManager $ templateManager ,
58+ private IL10N $ l10n ,
59+ private LoggerInterface $ logger ,
60+ private ILockManager $ lockManager ,
61+ private ISession $ session ,
62+ private ?string $ userId
63+ ) {
5864 parent ::__construct (Application::APPNAME , $ request );
59- $ this ->rootFolder = $ rootFolder ;
60- $ this ->shareManager = $ shareManager ;
61- $ this ->templateManager = $ templateManager ;
62- $ this ->l10n = $ l10n ;
63- $ this ->logger = $ logger ;
64- $ this ->lockManager = $ lockManager ;
65- $ this ->userId = $ userId ;
6665 }
6766
6867 /**
@@ -71,14 +70,26 @@ public function __construct(IRequest $request, IRootFolder $rootFolder, IManager
7170 * As the server template API for file creation is not available there, we need a dedicated API
7271 * in order to properly create files as public page visitors. This is being called in the new file
7372 * actions in src/view/NewFileMenu.js
74- *
75- * @NoAdminRequired
76- * @PublicPage
7773 */
74+ #[NoAdminRequired]
75+ #[PublicPage]
76+ #[BruteForceProtection(action: 'richdocumentsCreatePublic ' )]
7877 public function create (string $ mimeType , string $ fileName , string $ directoryPath = '/ ' , ?string $ shareToken = null , ?int $ templateId = null ): JSONResponse {
7978 try {
8079 if ($ shareToken !== null ) {
8180 $ share = $ this ->shareManager ->getShareByToken ($ shareToken );
81+
82+ if ($ share ->getPassword ()) {
83+ if (!$ this ->session ->exists ('public_link_authenticated ' )
84+ || $ this ->session ->get ('public_link_authenticated ' ) !== (string )$ share ->getId ()
85+ ) {
86+ throw new Exception ('Invalid password ' );
87+ }
88+ }
89+
90+ if (!($ share ->getPermissions () & \OCP \Constants::PERMISSION_CREATE )) {
91+ throw new Exception ('No create permissions ' );
92+ }
8293 }
8394
8495 $ rootFolder = $ shareToken !== null ? $ share ->getNode () : $ this ->rootFolder ->getUserFolder ($ this ->userId );
@@ -156,7 +167,7 @@ public function create(string $mimeType, string $fileName, string $directoryPath
156167 ]);
157168 }
158169
159- #[Http \ Attribute \ NoAdminRequired]
170+ #[NoAdminRequired]
160171 public function openLocal (int $ fileId ): DataResponse {
161172 try {
162173 $ files = $ this ->rootFolder ->getUserFolder ($ this ->userId )->getById ($ fileId );
0 commit comments