import Ember from 'ember'; import C from 'ui/utils/constants'; import Service from 'ui/models/service'; import Subscribe from 'ui/mixins/subscribe'; const CHECK_AUTH_TIMER = 600000; export default Ember.Route.extend(Subscribe, { prefs : Ember.inject.service(), projects : Ember.inject.service(), k8s : Ember.inject.service(), access : Ember.inject.service(), userTheme : Ember.inject.service('user-theme'), beforeModel(transition) { this._super.apply(this,arguments); if ( this.get('access.enabled') ) { if ( this.get('access.isLoggedIn') ) { this.testAuthToken(); } else { transition.send('logout', transition, false); return Ember.RSVP.reject('Not logged in'); } } }, testAuthToken: function() { Ember.run.later(() => { this.get('access').testAuth().then((/* res */) => { this.testAuthToken(); }, (/* err */) => { this.send('logout',null,true); }); }, CHECK_AUTH_TIMER); }, model(params, transition) { // Save whether the user is admin var type = this.get(`session.${C.SESSION.USER_TYPE}`); var isAdmin = (type === C.USER.TYPE_ADMIN) || !this.get('access.enabled'); this.set('access.admin', isAdmin); return Ember.RSVP.hash({ schemas: this.loadUserSchemas(), projects: this.loadProjects(), preferences: this.loadPreferences(), settings: this.loadPublicSettings(), }).then((hash) => { var projectId = null; if ( transition.params && transition.params['authenticated.project'] && transition.params['authenticated.project'].project_id ) { projectId = transition.params['authenticated.project'].project_id; } // Make sure a valid project is selected return this.get('projects').selectDefault(projectId).then((project) => { // Load stuff that is needed to draw the header hash.project = project; return Ember.RSVP.hash({ orchestrationState: project.updateOrchestrationState(), hosts: this.get('store').findAllUnremoved('host'), machines: this.get('store').findAllUnremoved('machine'), stacks: this.get('store').findAllUnremoved('environment'), }).then((moreHash) => { Ember.merge(hash, moreHash); if ( hash.orchestrationState.kubernetesReady ) { return this.loadKubernetes().then((k8sHash) => { Ember.merge(hash, k8sHash); return Ember.Object.create(hash); }); } else { return Ember.Object.create(hash); } }); }); }).catch((err) => { return this.loadingError(err, transition, Ember.Object.create({ projects: [], project: null, })); }); }, activate() { this._super(); this.connectSubscribe(); }, deactivate() { this._super(); this.disconnectSubscribe(); // Forget all the things this.reset(); }, loadingError(err, transition, ret) { var isAuthEnabled = this.get('access.enabled'); if ( err && err.status && [401,403].indexOf(err.status) >= 0 && isAuthEnabled ) { this.send('logout',transition, (transition.targetName !== 'authenticated.index')); return; } this.replaceWith('settings.projects'); return ret; }, loadPreferences() { return this.get('userStore').find('userpreference', null, {url: 'userpreferences', forceReload: true}).then((res) => { // Save the account ID from the response headers into session if ( res && res.xhr ) { this.set(`session.${C.SESSION.ACCOUNT_ID}`, res.xhr.getResponseHeader(C.HEADER.ACCOUNT_ID)); } this.get('userTheme').setupTheme(); return res; }); }, loadKubernetes() { return this.get('k8s').allNamespaces().then((all) => { return this.get('k8s').selectNamespace().then((ns) => { return { namespaces: all, namespace: ns, }; }); }).catch(() => { return { namespaces: null, namespace: null, }; }); }, loadUserSchemas() { // @TODO Inline me into releases var userStore = this.get('userStore'); return userStore.rawRequest({url:'schema', dataType: 'json'}).then((res) => { userStore._bulkAdd('schema', res.xhr.responseJSON.data); }); }, loadProjects() { var svc = this.get('projects'); return svc.getAll().then((all) => { svc.set('all', all); return all; }); }, loadPublicSettings() { return this.get('userStore').find('setting', null, {url: 'setting', forceReload: true, filter: {all: 'false'}}); }, reset() { // Forget all the things this.get('userStore').reset(); this.get('store').reset(); // Service has extra special hackery to cache relationships Service.reset(); }, actions: { error(err,transition) { // Unauthorized error, send back to login screen if ( err.status === 401 ) { this.send('logout',transition,true); return false; } else { // Bubble up return true; } }, showAbout() { this.controllerFor('application').set('showAbout', true); }, switchProject(projectId, transition=true) { this.reset(); if ( transition ) { this.intermediateTransitionTo('authenticated'); } this.set(`tab-session.${C.TABSESSION.PROJECT}`, projectId); this.refresh(); }, refreshKubernetes() { var model = this.get('controller.model'); if ( model.get('project') ) { this.loadKubernetes(model.get('project')).then((hash) => { model.setProperties(hash); }); } }, switchNamespace(namespaceId) { var route = this.get('app.currentRouteName'); if ( route !== 'k8s-tab.namespaces' && !route.match(/^k8s-tab\.namespace\.[^.]+.index$/) ) { route = 'k8s-tab.namespace'; } // This will return a different one if the one you ask for doesn't exist this.get('k8s').selectNamespace(namespaceId).then((ns) => { this.transitionTo(route, ns.get('id')); }); }, }, });