mirror of
https://github.com/harvester/harvester-ui-extension.git
synced 2026-05-14 06:51:46 +00:00
* fix: change auth/V3user to auth/user * refactor: extract to utils/auth.js --------- (cherry picked from commit 6fdd1e3954ca730a082c72e558fd79a54b65084f) Signed-off-by: Andy Lee <andy.lee@suse.com> Co-authored-by: Andy Lee <andy.lee@suse.com>
This commit is contained in:
parent
3f80a71303
commit
8803b79524
@ -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];
|
||||
|
||||
@ -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];
|
||||
|
||||
@ -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: [],
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
31
pkg/harvester/utils/auth.js
Normal file
31
pkg/harvester/utils/auth.js
Normal 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'];
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user