Skip to content

Commit 6e20c6c

Browse files
fix: πŸ› properly show username for oidc auth method in user menu
βœ… Closes: https://hashicorp.atlassian.net/browse/ICU-17231
1 parent d2b8805 commit 6e20c6c

File tree

10 files changed

+159
-47
lines changed

10 files changed

+159
-47
lines changed

β€Žaddons/auth/addon/authenticators/base.jsβ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,14 @@ export default class BaseAuthenticator extends SimpleAuthBaseAuthenticator {
101101
* @param {string} username
102102
* @return {object}
103103
*/
104-
normalizeData(data, username) {
104+
normalizeData(data) {
105105
// Pull fields up from `data.attributes` for easier access in JavaScript.
106106
// The `attributes` field exists on the Go side for its convenience but is
107107
// unnecessary here.
108108
Object.assign(data, data.attributes);
109109
// Add booleans indicated the scope type
110110
data.isGlobal = data?.scope?.type === 'global';
111111
data.isOrg = data?.scope?.type === 'org';
112-
if (username) data.username = username;
113112
return data;
114113
}
115114

β€Žaddons/auth/addon/authenticators/password.jsβ€Ž

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ export default class PasswordAuthenticator extends BaseAuthenticator {
7272
fetch(authEndpointURL, { method: 'post', body }),
7373
);
7474
const json = await response.json();
75-
return response.status < 400
76-
? resolve(this.normalizeData(json, login_name))
77-
: reject();
75+
return response.status < 400 ? resolve(this.normalizeData(json)) : reject();
7876
}
7977
}

β€Žaddons/core/translations/en-us.yamlβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ titles:
4949
project-actions: Project Actions
5050
user-menu: User Menu
5151
back-link: 'Back to {scope}'
52+
authenticated: Signed In
5253
descriptions:
5354
empty-set: There are no items to display yet. You may be able to add items or try back later.
5455
cluster-url-initialization: To get started, please enter your cluster URL

β€Žui/admin/app/routes/application.jsβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default class ApplicationRoute extends Route {
2323
@service featureEdition;
2424
@service sqlite;
2525
@service('browser/window') window;
26+
@service store;
2627

2728
// =attributes
2829

@@ -75,6 +76,19 @@ export default class ApplicationRoute extends Route {
7576
if (userId && hostUrl) {
7677
await this.sqlite.setup(formatDbName(userId, hostUrl));
7778
}
79+
80+
if (this.session.data?.authenticated?.account_id) {
81+
const account = await this.store.findRecord(
82+
'account',
83+
this.session.data.authenticated.account_id,
84+
);
85+
const username =
86+
account.login_name ||
87+
account.subject ||
88+
account.email ||
89+
account.full_name;
90+
this.session.set('data.authenticated.username', username);
91+
}
7892
}
7993
}
8094

β€Žui/admin/app/services/session.jsβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
import BaseSessionService from 'ember-simple-auth/services/session';
77
import { service } from '@ember/service';
8+
import { set } from '@ember/object';
89
import { formatDbName } from 'api/services/sqlite';
910

1011
export default class SessionService extends BaseSessionService {
1112
@service sqlite;
1213
@service('browser/window') window;
14+
@service store;
1315

1416
/**
1517
* Extend ember simple auth's handleAuthentication method
@@ -21,6 +23,18 @@ export default class SessionService extends BaseSessionService {
2123
if (userId && hostUrl) {
2224
await this.sqlite.setup(formatDbName(userId, hostUrl));
2325
}
26+
if (this.data?.authenticated?.account_id) {
27+
const account = await this.store.findRecord(
28+
'account',
29+
this.data.authenticated.account_id,
30+
);
31+
const username =
32+
account.login_name ||
33+
account.subject ||
34+
account.email ||
35+
account.full_name;
36+
set(this, 'data.authenticated.username', username);
37+
}
2438

2539
// We let ember-simple-auth handle transitioning back to the index after authentication.
2640
// This route can be configured in our environment config.

β€Žui/admin/app/styles/app.scssβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,3 +1025,18 @@
10251025
.injected-app-credential-alert {
10261026
margin-bottom: 1rem;
10271027
}
1028+
1029+
// User menu dropdown in global side nav
1030+
.user-menu-dropdown {
1031+
max-width: 125px;
1032+
1033+
&__toggle-button {
1034+
width: 100%;
1035+
1036+
.hds-dropdown-toggle-button__text {
1037+
white-space: nowrap;
1038+
overflow: hidden;
1039+
text-overflow: ellipsis;
1040+
}
1041+
}
1042+
}

β€Žui/admin/app/templates/application.hbsβ€Ž

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,76 @@
7070
</dd.Interactive>
7171
</Hds::Dropdown>
7272
{{/if}}
73-
74-
<Hds::Dropdown
75-
@enableCollisionDetection={{true}}
76-
data-test-side-nav-user-dropdown
77-
as |dd|
78-
>
79-
{{#if this.session.data.authenticated.username}}
73+
{{#if this.session.data.authenticated.username}}
74+
<Hds::Dropdown
75+
@enableCollisionDetection={{true}}
76+
class='user-menu-dropdown'
77+
data-test-side-nav-user-dropdown
78+
as |dd|
79+
>
8080
<dd.ToggleButton
8181
@icon='user'
8282
@text={{this.session.data.authenticated.username}}
83+
{{hds-tooltip this.session.data.authenticated.username}}
84+
class='user-menu-dropdown__toggle-button'
85+
/>
86+
<dd.Title @text={{t 'titles.authenticated'}} />
87+
<dd.Description
88+
@text={{this.session.data.authenticated.username}}
8389
/>
84-
{{else}}
90+
<dd.Separator />
91+
92+
<dd.Interactive @route='account.change-password'>{{t
93+
'actions.change-password'
94+
}}</dd.Interactive>
95+
<dd.Separator />
96+
97+
<dd.Interactive {{on 'click' this.invalidateSession}}>{{t
98+
'actions.deauthenticate'
99+
}}</dd.Interactive>
100+
<dd.Separator />
101+
102+
<dd.Title @text={{t 'actions.toggle-color-theme'}} />
103+
{{#each this.themes as |theme|}}
104+
<dd.Radio
105+
@value={{theme.value}}
106+
checked={{eq this.session.data.theme theme.value}}
107+
{{on 'change' (fn this.toggleTheme theme.value)}}
108+
>
109+
{{t (concat 'themes.' theme.label)}}
110+
</dd.Radio>
111+
{{/each}}
112+
</Hds::Dropdown>
113+
{{else}}
114+
<Hds::Dropdown
115+
@enableCollisionDetection={{true}}
116+
data-test-side-nav-user-dropdown
117+
as |dd|
118+
>
85119
<dd.ToggleIcon @icon='user' @text={{t 'titles.user-menu'}} />
86-
{{/if}}
87-
88-
<dd.Interactive @route='account.change-password'>{{t
89-
'actions.change-password'
90-
}}</dd.Interactive>
91-
<dd.Separator />
92-
93-
<dd.Interactive {{on 'click' this.invalidateSession}}>{{t
94-
'actions.deauthenticate'
95-
}}</dd.Interactive>
96-
<dd.Separator />
97-
98-
<dd.Title @text={{t 'actions.toggle-color-theme'}} />
99-
{{#each this.themes as |theme|}}
100-
<dd.Radio
101-
@value={{theme.value}}
102-
checked={{eq this.session.data.theme theme.value}}
103-
{{on 'change' (fn this.toggleTheme theme.value)}}
104-
>
105-
{{t (concat 'themes.' theme.label)}}
106-
</dd.Radio>
107-
{{/each}}
108-
</Hds::Dropdown>
120+
121+
<dd.Interactive @route='account.change-password'>{{t
122+
'actions.change-password'
123+
}}</dd.Interactive>
124+
<dd.Separator />
125+
126+
<dd.Interactive {{on 'click' this.invalidateSession}}>{{t
127+
'actions.deauthenticate'
128+
}}</dd.Interactive>
129+
<dd.Separator />
130+
131+
<dd.Title @text={{t 'actions.toggle-color-theme'}} />
132+
{{#each this.themes as |theme|}}
133+
<dd.Radio
134+
@value={{theme.value}}
135+
checked={{eq this.session.data.theme theme.value}}
136+
{{on 'change' (fn this.toggleTheme theme.value)}}
137+
>
138+
{{t (concat 'themes.' theme.label)}}
139+
</dd.Radio>
140+
{{/each}}
141+
</Hds::Dropdown>
142+
{{/if}}
109143
</:actions>
110144
</Hds::SideNav::Header>
111145
</:header>

β€Žui/desktop/app/routes/application.jsβ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class ApplicationRoute extends Route {
1616
@service clusterUrl;
1717
@service ipc;
1818
@service intl;
19+
@service store;
1920

2021
// =attributes
2122

@@ -49,6 +50,19 @@ export default class ApplicationRoute extends Route {
4950
tokenId: sessionData?.id,
5051
token: sessionData?.token,
5152
});
53+
54+
if (sessionData?.account_id) {
55+
const account = await this.store.findRecord(
56+
'account',
57+
sessionData.account_id,
58+
);
59+
const username =
60+
account.login_name ||
61+
account.subject ||
62+
account.email ||
63+
account.full_name;
64+
this.session.set('data.authenticated.username', username);
65+
}
5266
}
5367
}
5468

β€Žui/desktop/app/services/session.jsβ€Ž

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import { service } from '@ember/service';
77
import BaseSessionService from 'ember-simple-auth/services/session';
88
import { notifyError } from 'core/decorators/notify';
9+
import { set } from '@ember/object';
910

1011
export default class SessionService extends BaseSessionService {
1112
@service ipc;
13+
@service store;
1214

1315
/**
1416
* Extend ember simple auth's handleAuthentication method
@@ -24,6 +26,19 @@ export default class SessionService extends BaseSessionService {
2426
tokenId: sessionData?.id,
2527
token: sessionData?.token,
2628
});
29+
30+
if (sessionData?.account_id) {
31+
const account = await this.store.findRecord(
32+
'account',
33+
sessionData.account_id,
34+
);
35+
const username =
36+
account.login_name ||
37+
account.subject ||
38+
account.email ||
39+
account.full_name;
40+
set(this, 'data.authenticated.username', username);
41+
}
2742
}
2843
}
2944
}

β€Žui/desktop/app/templates/application.hbsβ€Ž

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,33 @@
5050
{{/if}}
5151

5252
{{#if this.session.isAuthenticated}}
53-
<Hds::Dropdown class='header-dropdown-button-override' as |dd|>
54-
{{#if this.session.data.authenticated.username}}
53+
{{#if this.session.data.authenticated.username}}
54+
<Hds::Dropdown class='header-dropdown-button-override' as |dd|>
5555
<dd.ToggleButton
5656
@icon='user-circle'
5757
@text={{this.session.data.authenticated.username}}
5858
/>
59-
{{else}}
59+
<dd.Interactive
60+
data-test-nav-signout-btn
61+
{{on 'click' this.showModalOrLogout}}
62+
>
63+
{{t 'actions.deauthenticate'}}
64+
</dd.Interactive>
65+
</Hds::Dropdown>
66+
{{else}}
67+
<Hds::Dropdown class='header-dropdown-button-override' as |dd|>
6068
<dd.ToggleIcon
6169
@icon='user-circle'
6270
@text={{t 'titles.user-menu'}}
6371
/>
64-
{{/if}}
65-
<dd.Interactive
66-
data-test-nav-signout-btn
67-
{{on 'click' this.showModalOrLogout}}
68-
>
69-
{{t 'actions.deauthenticate'}}
70-
</dd.Interactive>
71-
</Hds::Dropdown>
72+
<dd.Interactive
73+
data-test-nav-signout-btn
74+
{{on 'click' this.showModalOrLogout}}
75+
>
76+
{{t 'actions.deauthenticate'}}
77+
</dd.Interactive>
78+
</Hds::Dropdown>
79+
{{/if}}
7280
{{/if}}
7381

7482
</header.utilities>

0 commit comments

Comments
Β (0)