|
| 1 | +const express = require('express'); |
| 2 | +const { Endpoint } = require('../util/expressutil'); |
| 3 | + |
| 4 | +const validator = require('validator'); |
| 5 | +const APIError = require('../api/APIError'); |
| 6 | +const { get_user } = require('../helpers'); |
| 7 | +const { Context } = require('../util/context'); |
| 8 | +const auth2 = require('../middleware/auth2'); |
| 9 | +const config = require('../config'); |
| 10 | + |
| 11 | +const { PermissionUtil } = require('../services/auth/PermissionService'); |
| 12 | + |
| 13 | +const uuidv4 = require('uuid').v4; |
| 14 | + |
| 15 | +const router = express.Router(); |
| 16 | +router.use(auth2); |
| 17 | + |
| 18 | +Endpoint({ |
| 19 | + route: '/file-by-username', |
| 20 | + methods: ['POST'], |
| 21 | + handler: async (req, res) => { |
| 22 | + const svc_token = req.services.get('token'); |
| 23 | + const svc_email = req.services.get('email'); |
| 24 | + const svc_permission = req.services.get('permission'); |
| 25 | + |
| 26 | + console.log('which actor exists?', |
| 27 | + req.actor, |
| 28 | + Context.get('actor')) |
| 29 | + const actor = Context.get('actor'); |
| 30 | + |
| 31 | + const username = req.body.username; |
| 32 | + if ( ! (typeof username === 'string') ) { |
| 33 | + throw APIError.create('field_invalid', null, { |
| 34 | + key: 'username', |
| 35 | + expected: 'string', |
| 36 | + got: typeof username, |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + let path = req.body.path; |
| 41 | + if ( ! (typeof path === 'string') ) { |
| 42 | + throw APIError.create('field_invalid', null, { |
| 43 | + key: 'path', |
| 44 | + expected: 'string', |
| 45 | + got: typeof path, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const access_level = req.body.access_level || 'write'; |
| 50 | + if ( ! ['read','write'].includes(access_level) ) { |
| 51 | + throw APIError.create('field_invalid', null, { |
| 52 | + key: 'access_level', |
| 53 | + expected: '"read" or "write"', |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + const recipient = await get_user({ username }); |
| 58 | + if ( ! recipient ) { |
| 59 | + throw APIError.create('user_does_not_exist', null, { |
| 60 | + username |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + await svc_permission.grant_user_user_permission( |
| 65 | + actor, username, |
| 66 | + PermissionUtil.join('fs', path, access_level), |
| 67 | + {}, {}, |
| 68 | + ); |
| 69 | + |
| 70 | + if ( path.startsWith('/') ) path = path.slice(1); |
| 71 | + const link = `${config.origin}/show/${path}`; |
| 72 | + |
| 73 | + const email_values = { |
| 74 | + link, |
| 75 | + susername: req.user.username, |
| 76 | + ...(recipient ? { |
| 77 | + rusername: recipient.username, |
| 78 | + } : {}), |
| 79 | + }; |
| 80 | + |
| 81 | + const email_tmpl = recipient ? |
| 82 | + 'share_existing_user' : 'share_new_user'; |
| 83 | + |
| 84 | + await svc_email.send_email({ email: recipient.email }, email_tmpl, email_values); |
| 85 | + |
| 86 | + res.send({}); |
| 87 | + }, |
| 88 | +}).attach(router); |
| 89 | + |
| 90 | +module.exports = router; |
0 commit comments