| 
									
										
										
										
											2018-08-09 08:27:16 -07:00
										 |  |  | import Service from '@ember/service'; | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | import { get, set } from '@ember/object'; | 
					
						
							| 
									
										
										
										
											2020-08-28 10:31:15 -07:00
										 |  |  | import { currentUserDeprecated } from '@datahub/shared/api/user/authentication'; | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | import Session from 'ember-simple-auth/services/session'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import { inject as service } from '@ember/service'; | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  | import DataModelsService from '@datahub/data-models/services/data-models'; | 
					
						
							|  |  |  | import { PersonEntity } from '@datahub/data-models/entity/person/person-entity'; | 
					
						
							| 
									
										
										
										
											2020-08-28 10:31:15 -07:00
										 |  |  | import { IUser } from '@datahub/metadata-types/types/common/user'; | 
					
						
							| 
									
										
										
										
											2017-03-26 00:56:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 16:35:00 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Indicates that the current user has already been tracked in the current session | 
					
						
							|  |  |  |  * @type {boolean} | 
					
						
							|  |  |  |  * @private | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | let _hasUserBeenTracked = false; | 
					
						
							| 
									
										
										
										
											2017-03-26 00:56:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * The current user service can be injected into our various datahub addons to give reference | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |  * whenever necessary to the current logged in user | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  | export default class CurrentUser extends Service { | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Reference to the application session service, implemented with Ember Simple Auth | 
					
						
							|  |  |  |    * @type {ComputedProperty<Session>} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2018-08-09 08:27:16 -07:00
										 |  |  |   @service | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   session!: Session; | 
					
						
							| 
									
										
										
										
											2017-10-23 16:50:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  |    * Reference to the data models service in order to access the proper entity for our current user | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  |   @service | 
					
						
							|  |  |  |   dataModels!: DataModelsService; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Creates a PersonEntity out of the current user so that we can fetch and store information | 
					
						
							|  |  |  |    * about the logged in person | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   entity?: PersonEntity; | 
					
						
							| 
									
										
										
										
											2017-03-26 00:56:19 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Attempt to load the currently logged in user. | 
					
						
							|  |  |  |    *   If the userName is found from a previously retained session, | 
					
						
							|  |  |  |    *   append to service. Request the full user object, and append | 
					
						
							|  |  |  |    *   to service. | 
					
						
							|  |  |  |    * @returns {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   async load(): Promise<void> { | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  |     const { session, dataModels } = this; | 
					
						
							| 
									
										
										
										
											2017-05-22 19:55:07 -07:00
										 |  |  |     // If we have a valid session, get the currently logged in user, and set the currentUser attribute,
 | 
					
						
							|  |  |  |     // otherwise raise an exception
 | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |     if (session.isAuthenticated) { | 
					
						
							| 
									
										
										
										
											2020-08-28 10:31:15 -07:00
										 |  |  |       // NOTE: Open source is currently using an outdated version of the /me endpoint, which requires a separated
 | 
					
						
							|  |  |  |       // logic in the open source version of current-user
 | 
					
						
							|  |  |  |       const userV1: IUser = await currentUserDeprecated(); | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  |       const PersonEntityClass = dataModels.getModel(PersonEntity.displayName); | 
					
						
							| 
									
										
										
										
											2020-08-28 10:31:15 -07:00
										 |  |  |       const urn = PersonEntityClass.urnFromUsername(userV1.userName); | 
					
						
							| 
									
										
										
										
											2020-10-11 11:40:32 -07:00
										 |  |  |       const entity = dataModels.createPartialInstance(PersonEntityClass.displayName, { ...userV1, urn }); | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       set(this, 'entity', entity); | 
					
						
							| 
									
										
										
										
											2017-03-26 00:56:19 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-05-22 19:55:07 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Invalidates the current session if the session is currently valid | 
					
						
							|  |  |  |    * useful if, for example, the server is no able to provide the currently logged in user | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |    * @return {Promise<void>} | 
					
						
							| 
									
										
										
										
											2017-05-22 19:55:07 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   invalidateSession(): Promise<void> { | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  |     const session = get(this, 'session'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return session.isAuthenticated ? session.invalidate() : Promise.resolve(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-06-19 16:35:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Uses the provided tracking function to track the currently logged in user's userId | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    * This is not a self-contained method, since it depends on other values that cannot be enforced as dependency | 
					
						
							|  |  |  |    * of this service as a whole. The dependency on the tracking implementation is also not a concern of this service, | 
					
						
							|  |  |  |    * injecting as a function is the better approach. | 
					
						
							|  |  |  |    * @param {Function} userIdTracker a function that takes the userId and tracks it | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   trackCurrentUser(userIdTracker: (...args: Array<unknown>) => void = (): void => void 0): void { | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  |     const userId: string = this.entity?.username || ''; | 
					
						
							| 
									
										
										
										
											2017-06-19 16:35:00 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // If we have a non-empty userId, the user hasn't already been tracked and the userIdTracker is a valid argument
 | 
					
						
							|  |  |  |     // then track the user and toggle the flag affirmative
 | 
					
						
							|  |  |  |     if (userId && !_hasUserBeenTracked && typeof userIdTracker === 'function') { | 
					
						
							|  |  |  |       userIdTracker(userId); | 
					
						
							|  |  |  |       _hasUserBeenTracked = true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-03-26 00:56:19 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-05-15 13:13:37 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // DO NOT DELETE: this is how TypeScript knows how to look up your services.
 | 
					
						
							|  |  |  | declare module '@ember/service' { | 
					
						
							|  |  |  |   // eslint-disable-next-line
 | 
					
						
							|  |  |  |   interface Registry { | 
					
						
							|  |  |  |     'current-user': CurrentUser; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |