11454: On /search & /sessions page, check if the current user is
[arvados.git] / apps / workbench / app / assets / javascripts / components / sessions.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 $(document).on('ready', function() {
6     var db = new SessionDB()
7     db.checkForNewToken()
8     db.fillMissingUUIDs()
9     db.migrateNonFederatedSessions()
10     db.autoLoadRemoteHosts()
11 })
12
13 window.SessionsTable = {
14     oninit: function(vnode) {
15         vnode.state.db = new SessionDB()
16         vnode.state.hostToAdd = m.stream('')
17         vnode.state.error = m.stream()
18         vnode.state.checking = m.stream()
19     },
20     view: function(vnode) {
21         var db = vnode.state.db
22         var sessions = db.loadAll()
23         return m('.container', [
24             m('p', [
25                 'You can log in to multiple Arvados sites here, then use the ',
26                 m('a[href="/search"]', 'multi-site search'),
27                 ' page to search collections and projects on all sites at once.',
28             ]),
29             m('table.table.table-condensed.table-hover', [
30                 m('thead', m('tr', [
31                     m('th', 'status'),
32                     m('th', 'cluster ID'),
33                     m('th', 'username'),
34                     m('th', 'email'),
35                     m('th', 'actions'),
36                     m('th'),
37                 ])),
38                 m('tbody', [
39                     Object.keys(sessions).map(function(uuidPrefix) {
40                         var session = sessions[uuidPrefix]
41                         return m('tr', [
42                             session.token && session.user ? [
43                                 m('td', session.user.is_active ?
44                                     m('span.label.label-success', 'logged in') :
45                                     m('span.label.label-warning', 'inactive')),
46                                 m('td', {title: session.baseURL}, [
47                                     m('a', {
48                                         href: db.workbenchBaseURL(session) + '?api_token=' + session.token
49                                     }, uuidPrefix),
50                                 ]),
51                                 m('td', session.user.username),
52                                 m('td', session.user.email),
53                                 m('td', session.isFromRails ? null : m('button.btn.btn-xs.btn-default', {
54                                     uuidPrefix: uuidPrefix,
55                                     onclick: m.withAttr('uuidPrefix', db.logout),
56                                 }, 'Log out ', m('span.glyphicon.glyphicon-log-out'))),
57                             ] : [
58                                 m('td', m('span.label.label-default', 'logged out')),
59                                 m('td', {title: session.baseURL}, uuidPrefix),
60                                 m('td'),
61                                 m('td'),
62                                 m('td', m('a.btn.btn-xs.btn-primary', {
63                                     uuidPrefix: uuidPrefix,
64                                     onclick: db.login.bind(db, session.baseURL),
65                                 }, 'Log in ', m('span.glyphicon.glyphicon-log-in'))),
66                             ],
67                             m('td', session.isFromRails ? null : m('button.btn.btn-xs.btn-default', {
68                                 uuidPrefix: uuidPrefix,
69                                 onclick: m.withAttr('uuidPrefix', db.trash),
70                             }, 'Remove ', m('span.glyphicon.glyphicon-trash'))),
71                         ])
72                     }),
73                 ]),
74             ]),
75             m('.row', m('.col-md-6', [
76                 m('form', {
77                     onsubmit: function() {
78                         vnode.state.error(null)
79                         vnode.state.checking(true)
80                         db.findAPI(vnode.state.hostToAdd())
81                             .then(db.login)
82                             .catch(function() {
83                                 vnode.state.error(true)
84                             })
85                             .then(vnode.state.checking.bind(null, null))
86                         return false
87                     },
88                 }, [
89                     m('p', [
90                         'To add a remote Arvados site, paste the remote site\'s host here (see "ARVADOS_API_HOST" on the "current token" page).',
91                     ]),
92                     m('.input-group', { className: vnode.state.error() && 'has-error' }, [
93                         m('input.form-control[type=text][name=apiHost][placeholder="zzzzz.arvadosapi.com"]', {
94                             oninput: m.withAttr('value', vnode.state.hostToAdd),
95                         }),
96                         m('.input-group-btn', [
97                             m('input.btn.btn-primary[type=submit][value="Log in"]', {
98                                 disabled: !vnode.state.hostToAdd(),
99                             }),
100                         ]),
101                     ]),
102                 ]),
103                 m('p'),
104                 vnode.state.error() && m('p.alert.alert-danger', 'Request failed. Make sure this is a working API server address.'),
105                 vnode.state.checking() && m('p.alert.alert-info', 'Checking...'),
106             ])),
107         ])
108     },
109 }