Skip to content

Commit 77cecb8

Browse files
committed
enabledForAuthenticatedUser
1 parent 4c19cd8 commit 77cecb8

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

spec/ParseFile.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,5 +958,32 @@ describe('Parse.File testing', () => {
958958
fail('should have allowed file to save.');
959959
}
960960
});
961+
962+
it('enable for anonymous but not authenticated', async () => {
963+
await reconfigureServer({
964+
fileUpload: {
965+
enabled: true,
966+
enabledForPublic: false,
967+
enabledForAnonymousUser: true,
968+
enabledForAuthenticatedUser: false,
969+
},
970+
});
971+
try {
972+
const user = await Parse.AnonymousUtils.logIn();
973+
const file = new Parse.File('hello.txt', data, 'text/plain');
974+
await file.save({ sessionToken: user.getSessionToken() });
975+
} catch (e) {
976+
fail('should have allowed file to save.');
977+
}
978+
try {
979+
const user = await Parse.User.signUp('myUser', 'password');
980+
const file = new Parse.File('hello.txt', data, 'text/plain');
981+
await file.save({ sessionToken: user.getSessionToken() });
982+
fail('should have not allowed file to save.');
983+
} catch (e) {
984+
expect(e.code).toBe(130);
985+
expect(e.message).toBe('Authenticated file upload is not enabled.');
986+
}
987+
});
961988
});
962989
});

src/Options/Definitions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ module.exports.FileUploadOptions = {
564564
action: parsers.booleanParser,
565565
default: false,
566566
},
567+
enabledForAuthenticatedUser: {
568+
env: 'PARSE_SERVER_PARSE_SERVER_FILE_UPLOAD_ENABLED_FOR_AUTHENTICATED_USER',
569+
help: 'File upload is enabled for authenticated users.',
570+
action: parsers.booleanParser,
571+
default: true,
572+
},
567573
enabledForPublic: {
568574
env: 'PARSE_SERVER_PARSE_SERVER_FILE_UPLOAD_ENABLED_FOR_PUBLIC',
569575
help:

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,6 @@
125125
* @interface FileUploadOptions
126126
* @property {Boolean} enabled Files can be uploaded with Parse Server.
127127
* @property {Boolean} enabledForAnonymousUser File upload is enabled for Anonymous Users.
128+
* @property {Boolean} enabledForAuthenticatedUser File upload is enabled for authenticated users.
128129
* @property {Boolean} enabledForPublic File upload is enabled for anyone with access to the Parse Server file upload endpoint, regardless of user authentication.
129130
*/

src/Options/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,7 @@ export interface FileUploadOptions {
302302
/* File upload is enabled for anyone with access to the Parse Server file upload endpoint, regardless of user authentication.
303303
:DEFAULT: false */
304304
enabledForPublic: ?boolean;
305+
/* File upload is enabled for authenticated users.
306+
:DEFAULT: true */
307+
enabledForAuthenticatedUser: ?boolean;
305308
}

src/Routers/FilesRouter.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ export class FilesRouter {
106106
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Anonymous file upload is not enabled.'));
107107
return;
108108
}
109+
if (
110+
!req.config.fileUpload.enabledForAuthenticatedUser &&
111+
req.config.fileUpload.enabledForAuthenticatedUser != null &&
112+
req.auth.user &&
113+
!Parse.AnonymousUtils.isLinked(req.auth.user)
114+
) {
115+
next(
116+
new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Authenticated file upload is not enabled.')
117+
);
118+
return;
119+
}
109120
if (!req.config.fileUpload.enabledForPublic && !req.auth.user) {
110121
next(new Parse.Error(Parse.Error.FILE_SAVE_ERROR, 'Public file upload is not enabled.'));
111122
return;

0 commit comments

Comments
 (0)