diff options
author | vantezzen <hello@vantezzen.io> | 2019-10-19 15:41:31 +0200 |
---|---|---|
committer | vantezzen <hello@vantezzen.io> | 2019-10-19 15:41:31 +0200 |
commit | 5bf9eb5898f14afc66f690c866637b4815195bad (patch) | |
tree | 08e772531561928f9152a559b43c1e74909886c5 /app/Controllers | |
parent | Bump version number (diff) | |
download | ferdium-server-5bf9eb5898f14afc66f690c866637b4815195bad.tar.gz ferdium-server-5bf9eb5898f14afc66f690c866637b4815195bad.tar.zst ferdium-server-5bf9eb5898f14afc66f690c866637b4815195bad.zip |
Add page to transfer user accounts
Diffstat (limited to 'app/Controllers')
-rw-r--r-- | app/Controllers/Http/DashboardController.js | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/app/Controllers/Http/DashboardController.js b/app/Controllers/Http/DashboardController.js index 49f6cc0..fe179c9 100644 --- a/app/Controllers/Http/DashboardController.js +++ b/app/Controllers/Http/DashboardController.js | |||
@@ -3,7 +3,11 @@ const { | |||
3 | validateAll, | 3 | validateAll, |
4 | } = use('Validator'); | 4 | } = use('Validator'); |
5 | 5 | ||
6 | const Service = use('App/Models/Service'); | ||
7 | const Workspace = use('App/Models/Workspace'); | ||
8 | |||
6 | const crypto = require('crypto'); | 9 | const crypto = require('crypto'); |
10 | const uuid = require('uuid/v4'); | ||
7 | 11 | ||
8 | class DashboardController { | 12 | class DashboardController { |
9 | async login({ | 13 | async login({ |
@@ -135,6 +139,108 @@ class DashboardController { | |||
135 | }); | 139 | }); |
136 | } | 140 | } |
137 | 141 | ||
142 | async export({ | ||
143 | auth, | ||
144 | response, | ||
145 | }) { | ||
146 | const general = auth.user; | ||
147 | const services = (await auth.user.services().fetch()).toJSON(); | ||
148 | const workspaces = (await auth.user.workspaces().fetch()).toJSON(); | ||
149 | |||
150 | const exportData = { | ||
151 | username: general.username, | ||
152 | mail: general.email, | ||
153 | services, | ||
154 | workspaces, | ||
155 | }; | ||
156 | |||
157 | return response | ||
158 | .header('Content-Type', 'application/force-download') | ||
159 | .header('Content-disposition', 'attachment; filename=export.ferdi-data') | ||
160 | .send(exportData); | ||
161 | } | ||
162 | |||
163 | async import({ | ||
164 | auth, | ||
165 | request, | ||
166 | session, | ||
167 | response, | ||
168 | }) { | ||
169 | let validation = await validateAll(request.all(), { | ||
170 | file: 'required', | ||
171 | }); | ||
172 | if (validation.fails()) { | ||
173 | session.withErrors(validation.messages()).flashExcept(['password']); | ||
174 | return response.redirect('back'); | ||
175 | } | ||
176 | |||
177 | let file; | ||
178 | try { | ||
179 | file = JSON.parse(request.input('file')); | ||
180 | } catch(e) { | ||
181 | session.flash({ type: 'danger', message: 'Invalid Ferdi account file' }) | ||
182 | return response.redirect('back'); | ||
183 | } | ||
184 | console.log(file); | ||
185 | |||
186 | if(!file || !file.services || !file.workspaces) { | ||
187 | session.flash({ type: 'danger', message: 'Invalid Ferdi account file (2)' }) | ||
188 | return response.redirect('back'); | ||
189 | } | ||
190 | |||
191 | const serviceIdTranslation = {}; | ||
192 | |||
193 | // Import services | ||
194 | try { | ||
195 | for (const service of file.services) { | ||
196 | // Get new, unused uuid | ||
197 | let serviceId; | ||
198 | do { | ||
199 | serviceId = uuid(); | ||
200 | } while ((await Service.query().where('serviceId', serviceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
201 | |||
202 | await Service.create({ // eslint-disable-line no-await-in-loop | ||
203 | userId: auth.user.id, | ||
204 | serviceId, | ||
205 | name: service.name, | ||
206 | recipeId: service.recipeId, | ||
207 | settings: JSON.stringify(service.settings), | ||
208 | }); | ||
209 | |||
210 | serviceIdTranslation[service.id] = serviceId; | ||
211 | } | ||
212 | } catch (e) { | ||
213 | const errorMessage = `Could not import your services into our system.\nError: ${e}`; | ||
214 | return response.status(401).send(errorMessage); | ||
215 | } | ||
216 | |||
217 | // Import workspaces | ||
218 | try { | ||
219 | for (const workspace of file.workspaces) { | ||
220 | let workspaceId; | ||
221 | do { | ||
222 | workspaceId = uuid(); | ||
223 | } while ((await Workspace.query().where('workspaceId', workspaceId).fetch()).rows.length > 0); // eslint-disable-line no-await-in-loop | ||
224 | |||
225 | const services = workspace.services.map((service) => serviceIdTranslation[service]); | ||
226 | |||
227 | await Workspace.create({ // eslint-disable-line no-await-in-loop | ||
228 | userId: auth.user.id, | ||
229 | workspaceId, | ||
230 | name: workspace.name, | ||
231 | order: workspace.order, | ||
232 | services: JSON.stringify(services), | ||
233 | data: JSON.stringify(workspace.data), | ||
234 | }); | ||
235 | } | ||
236 | } catch (e) { | ||
237 | const errorMessage = `Could not import your workspaces into our system.\nError: ${e}`; | ||
238 | return response.status(401).send(errorMessage); | ||
239 | } | ||
240 | |||
241 | return response.send('Your account has been imported.'); | ||
242 | } | ||
243 | |||
138 | logout({ | 244 | logout({ |
139 | auth, | 245 | auth, |
140 | response, | 246 | response, |