fix: change auth/V3user to auth/user (#824)

* fix: change auth/V3user to auth/user

Signed-off-by: Andy Lee <andy.lee@suse.com>

* refactor: extract to utils/auth.js

Signed-off-by: Andy Lee <andy.lee@suse.com>

---------

Signed-off-by: Andy Lee <andy.lee@suse.com>
This commit is contained in:
Andy Lee 2026-04-22 15:39:24 +08:00 committed by GitHub
parent 7d0f33f31d
commit 6fdd1e3954
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 41 additions and 37 deletions

View File

@ -4,6 +4,7 @@ import { Card } from '@components/Card';
import AsyncButton from '@shell/components/AsyncButton';
import { escapeHtml } from '@shell/utils/string';
import { HCI } from '../types';
import { getHarvesterUserName } from '../utils/auth';
export default {
name: 'HarvesterEnablePciPassthrough',
@ -34,16 +35,7 @@ export default {
},
async save(buttonCb) {
// isSingleProduct == this is a standalone Harvester cluster
const isSingleProduct = this.$store.getters['isSingleProduct'];
let userName = 'admin';
// if this is imported Harvester, there may be users other than 'admin
if (!isSingleProduct) {
const user = this.$store.getters['auth/v3User'];
userName = user?.username || user?.id;
}
const userName = getHarvesterUserName(this.$store.getters);
for (let i = 0; i < this.resources.length; i++) {
const actionResource = this.resources[i];

View File

@ -4,6 +4,7 @@ import { Card } from '@components/Card';
import AsyncButton from '@shell/components/AsyncButton';
import { escapeHtml } from '@shell/utils/string';
import { HCI } from '../types';
import { getHarvesterUserName } from '../utils/auth';
export default {
name: 'HarvesterEnableUSBPassthrough',
@ -34,16 +35,7 @@ export default {
},
async save(buttonCb) {
// isSingleProduct == this is a standalone Harvester cluster
const isSingleProduct = this.$store.getters['isSingleProduct'];
let userName = 'admin';
// if this is imported Harvester, there may be users other than 'admin
if (!isSingleProduct) {
const user = this.$store.getters['auth/v3User'];
userName = user?.username || user?.id;
}
const userName = getHarvesterUserName(this.$store.getters);
for (let i = 0; i < this.resources.length; i++) {
const actionResource = this.resources[i];

View File

@ -1,6 +1,7 @@
<script>
import { NORMAN } from '@shell/config/types';
import { HCI } from '../types';
import { getHarvesterUser } from '../utils/auth';
export default {
props: {
@ -25,7 +26,7 @@ export default {
},
data() {
const user = this.$store.getters['auth/v3User'];
const user = getHarvesterUser(this.$store.getters);
return {
harvesterSettings: [],

View File

@ -2,6 +2,7 @@ import SteveModel from '@shell/plugins/steve/steve-class';
import { escapeHtml } from '@shell/utils/string';
import { HCI } from '../types';
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
import { getHarvesterUserName } from '../utils/auth';
const STATUS_DISPLAY = {
enabled: {
@ -96,15 +97,8 @@ export default class PCIDevice extends SteveModel {
if (!this.passthroughClaim) {
return false;
}
const isSingleProduct = this.$rootGetters['isSingleProduct'];
let userName = 'admin';
// if this is imported Harvester, there may be users other than admin
if (!isSingleProduct) {
const user = this.$rootGetters['auth/v3User'];
userName = user?.username || user?.id;
}
const userName = getHarvesterUserName(this.$rootGetters);
return this.claimedBy === userName;
}

View File

@ -1,6 +1,7 @@
import SteveModel from '@shell/plugins/steve/steve-class';
import { escapeHtml } from '@shell/utils/string';
import { HCI } from '../types';
import { getHarvesterUserName } from '../utils/auth';
const STATUS_DISPLAY = {
enabled: {
@ -87,15 +88,8 @@ export default class USBDevice extends SteveModel {
if (!this.passthroughClaim) {
return false;
}
const isSingleProduct = this.$rootGetters['isSingleProduct'];
let userName = 'admin';
// if this is imported Harvester, there may be users other than admin
if (!isSingleProduct) {
const user = this.$rootGetters['auth/v3User'];
userName = user?.username || user?.id;
}
const userName = getHarvesterUserName(this.$rootGetters);
return this.claimedBy === userName;
}

View File

@ -0,0 +1,31 @@
/**
* Resolve the Harvester username from Vuex getters.
*
* Works with both `this.$store.getters` (in components) and
* `this.$rootGetters` (in Steve models).
*
* - In single-product (standalone Harvester) mode, always returns the
* default username (`admin`).
* - Otherwise, falls back to the authenticated user's `username` or `id`.
*/
export function getHarvesterUserName(getters, defaultUserName = 'admin') {
const isSingleProduct = getters?.['isSingleProduct'];
if (isSingleProduct) {
return defaultUserName;
}
const user = getHarvesterUser(getters);
return user?.username || user?.id || defaultUserName;
}
/**
* Return the authenticated user object from Vuex getters.
*
* Works with both `this.$store.getters` (in components) and
* `this.$rootGetters` (in Steve models).
*/
export function getHarvesterUser(getters) {
return getters?.['auth/user'];
}