| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { validateProfileUpdateInput } = require('../validation/user'); | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  | const { getService } = require('../utils'); | 
					
						
							| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |   async getMe(ctx) { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const userInfo = getService('user').sanitizeUser(ctx.state.user); | 
					
						
							| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							|  |  |  |       data: userInfo, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async updateMe(ctx) { | 
					
						
							|  |  |  |     const input = ctx.request.body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await validateProfileUpdateInput(input); | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							|  |  |  |       return ctx.badRequest('ValidationError', err); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const userService = getService('user'); | 
					
						
							| 
									
										
										
										
											2021-10-21 13:20:57 +02:00
										 |  |  |     const authServer = getService('auth'); | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-21 15:31:05 +02:00
										 |  |  |     const { currentPassword, ...userInfo } = input; | 
					
						
							| 
									
										
										
										
											2021-10-21 13:20:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (currentPassword && userInfo.password) { | 
					
						
							|  |  |  |       const isValid = await authServer.validatePassword(currentPassword, ctx.state.user.password); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!isValid) { | 
					
						
							|  |  |  |         return ctx.badRequest('ValidationError', { | 
					
						
							|  |  |  |           currentPassword: ['Invalid credentials'], | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const updatedUser = await userService.updateById(ctx.state.user.id, userInfo); | 
					
						
							| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |       data: userService.sanitizeUser(updatedUser), | 
					
						
							| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-06-11 10:54:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async getOwnPermissions(ctx) { | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const { findUserPermissions, sanitizePermission } = getService('permission'); | 
					
						
							|  |  |  |     const { user } = ctx.state; | 
					
						
							| 
									
										
										
										
											2020-06-11 10:54:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-25 14:59:44 +01:00
										 |  |  |     const userPermissions = await findUserPermissions(user); | 
					
						
							| 
									
										
										
										
											2020-06-11 10:54:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { | 
					
						
							|  |  |  |       data: userPermissions.map(sanitizePermission), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-05-18 19:54:43 +02:00
										 |  |  | }; |