| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import { ApiStatus } from '@datahub/utils/api/shared'; | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | import { Response, faker } from 'ember-cli-mirage'; | 
					
						
							|  |  |  | import ApprovedAclUser from 'wherehows-web/mirage/fixtures/dataset-acl-users'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const textContentHeader = { 'Content-Type': 'text/plain; charset=utf-8' }; | 
					
						
							|  |  |  | //TODO: Build a dummy server to simulate ACL authentication process
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Build response body property | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |  * @param {string} ldap | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  */ | 
					
						
							|  |  |  | const approvedUserInfo = (ldap: string | undefined) => { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     tableItem: { | 
					
						
							|  |  |  |       userName: ldap, | 
					
						
							|  |  |  |       name: faker.name.findName(), | 
					
						
							|  |  |  |       idType: 'USER', | 
					
						
							|  |  |  |       source: `${faker.random.alphaNumeric()}${faker.random.alphaNumeric()}`.toUpperCase(), | 
					
						
							|  |  |  |       modifiedTime: faker.date.past(), | 
					
						
							|  |  |  |       ownerShip: 'DataOwner' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Build ACL authentication server POST logic | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |  * @param {any}_schema | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |  * @param {string} requestBody | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const aclAuth = (_schema: any, { requestBody }: { requestBody: string }) => { | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   const { principal, businessJustification } = < | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       principal: string; | 
					
						
							|  |  |  |       businessJustification: string; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   >JSON.parse(requestBody); | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const isApproved = businessJustification.toLowerCase().includes('read'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (businessJustification && isApproved) { | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     const principalLdap = principal.split(':').pop(); | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |     const responseBody = { | 
					
						
							|  |  |  |       principal, | 
					
						
							|  |  |  |       businessJustification, | 
					
						
							|  |  |  |       accessTypes: 'READ', | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |       ...approvedUserInfo(principalLdap) | 
					
						
							| 
									
										
										
										
											2018-02-07 12:32:03 -08:00
										 |  |  |     }; | 
					
						
							|  |  |  |     _schema.db.datasetAclUsers.insert(responseBody); | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       status: ApiStatus.OK, | 
					
						
							|  |  |  |       ...responseBody | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return new Response(401, textContentHeader, { status: ApiStatus.FAILED, isApproved: false }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  *  For testing purpose, defined a mocked response when user is granted ACL permission | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const approvedResponseTesting = ApprovedAclUser[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * For testing purpose, defined a method to mock ACL server response | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const accessInfoTesting = (permmision: boolean) => { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     isAccess: permmision, | 
					
						
							|  |  |  |     body: [approvedResponseTesting] | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export { aclAuth, accessInfoTesting, approvedResponseTesting }; |