Skip to content

Commit

Permalink
fix: control hub namesapce by user
Browse files Browse the repository at this point in the history
  • Loading branch information
yongheng2016 committed Jun 19, 2024
1 parent 0f6ba55 commit 0d92807
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { ref } from 'vue';
import { t } from 'src/boot/i18n';
import EnvironmentVariables from 'src/containers/EnvironmentVariables.vue';
import Yaml from 'src/pages/NamespacePods/Yaml.vue';
import MyPage from '@packages/ui/src/containers/MyPage.vue';
import MyContentPage from 'src/components/MyContentPage.vue';
import DetailData from './DetailData.vue';
import MoreSelection from '@packages/ui/src/components/MoreSelection.vue';
Expand Down
30 changes: 7 additions & 23 deletions apps/adminConsole/src/pages/Pods/Overview2.vue

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion apps/server/server/cache/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ function podListFormat(ctx, data) {
};
}

function namespaceFormat(ctx, data) {
const user = getUserInfo(ctx);
console.log('user', user);
const newData = data.items.filter((item) => {
const namespace = get(item, 'metadata.name');
const userTarget = namespace
.split('-')
.find((item) => item === user.username);
const systemTarget = systemNamespaces.find(
(system_namespace) => namespace === system_namespace
);
return user.globalrole === ADMIN_ROLE
? true
: userTarget;
});
return {
...data,
totalItems: newData.length,
items: newData,
total: data.totalItems
};
}

function namespaceListFormat(ctx, data) {
const user = getUserInfo(ctx);
const newData = data.items.filter((item) => {
Expand Down Expand Up @@ -134,5 +157,6 @@ module.exports = {
systemNamespaces,
checkUrl,
isAdmin,
canModify
canModify,
namespaceFormat
};
17 changes: 12 additions & 5 deletions apps/server/server/controllers/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const {
setUserInfo,
getUserInfo,
checkUrl,
isAdmin
isAdmin,
namespaceFormat
} = require('../cache/user.js');

const {
Expand Down Expand Up @@ -215,6 +216,12 @@ function endsWith(str, name) {
const namespaceGroup = async (ctx) => {
const namespaces = await getNamespaces(ctx);
const users = await getUsers(ctx)

const originalData = namespaceFormat(
ctx.req,
namespaces,
);

const SYSTEM = 'System'
const usersData = users.items.map(item => ({ name: item.metadata.name, creation_timestamp: item.metadata.creationTimestamp }));

Expand All @@ -228,22 +235,22 @@ const namespaceGroup = async (ctx) => {

const result = list.map(item => {
if (item.name === SYSTEM) {
const data = namespaces.items.filter(namespace => !usersData.some(str => endsWith(namespace.metadata.name, str.name)))
const data = originalData.items.filter(namespace => !usersData.some(str => endsWith(namespace.metadata.name, str.name)))
return {
title: item.name,
data: data,
}
} else {
const data = namespaces.items.filter(namespace => endsWith(namespace.metadata.name, item.name))
const data = originalData.items.filter(namespace => endsWith(namespace.metadata.name, item.name))
return {
title: item.name,
data: data,
}
}

});

ctx.body = result;
const res = result.filter(item => item.data.length > 0)
ctx.body = res;
}

const cacheUser = async (ctx, next) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/server/server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const router = new Router();
router
.get('/capi/app/detail', userDetail)
.get('/capi/app/myapps', appList)
.get('/capi/namespaces/group', namespaceGroup)

.all('/(k)?api(s)?/(.*)', cacheUser)
.use(proxy('/(k)?api(s)?/(.*)', k8sResourceProxy))
.all('/(c)?api(s)?/(.*)', cacheUser)
.get('/capi/monitoring.kubesphere.io/v1alpha3/:type', monitoringMetric);
.get('/capi/monitoring.kubesphere.io/v1alpha3/:type', monitoringMetric)
.get('/capi/namespaces/group', namespaceGroup)

module.exports = router;

0 comments on commit 0d92807

Please sign in to comment.