一、概述
根據官方解釋的話,apollo-portal-1.2.0-github.zip 默認是只有apollo是有權限的,所以我們只能采取 build的方式去打包編譯,官方搭建步驟:https://github.com/ctripcorp/apollo/wiki。雖然官方說,我只需要在數據庫的ApolloPortalDB中的ServerConfig 表中去添加,但是還是只能看到 DEV 的環境的 配置文件,所以我們怎么來控制吶。今天我來寫一下。
- 前端js修改
- 數據庫中添加
二、前端js修改
首先我們應該到官網上去下載源碼,apollo-xxxx.tar.gz,版本自己定,我這邊用xxx表示,然后在我們的windows機器上去解壓。然后我們進入到 apollo-portal中,路徑如下:
C:\apollo-xxxx\apollo-portal\src\main\resources\static\scripts\controller\config\ConfigBaseInfoController.js
修改ConfigBaseInfoController.js 文件:
function loadNavTree() { //172行,修改的函數名
.....
EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE); //178行
//登錄人賬號
var loginUserId=$rootScope.pageContext.userId;
var adminUserIdList=['admin','xxx','apollo'] //新增賬號
nodes.forEach(function (env) {
.......
}
node.nodes = clusterNodes;
/**
* xxx 項目管理員權限才能查看其它環境配置,默認只能看dev配置
* 管理員名單
* $rootScope.pageContext.userId
*/
if(node.text=='DEV'){ //權限控制
navTree.push(node);
}else{
$.each(adminUserIdList,function(i,v){
if(v===loginUserId)
navTree.push(node);
});
}
});
//init treeview
$('#treeview').treeview({
.....})
}
然后再編譯 build.bat 文件,當然我們是在 windows下,如果在Linux下就是./build.sh了
C:\apollo-1.2.0\scripts\build.bat
從而獲取最新的apollo-portal-xxx.jar,然后你再改名為 apollo-portal.jar 上傳到服務器上重啟服務就可以了。修改后完整的源碼文件:
application_module.controller("ConfigBaseInfoController",
['$rootScope', '$scope', '$window', '$location', 'toastr', 'EventManager', 'UserService',
'AppService',
'FavoriteService',
'PermissionService',
'AppUtil', ConfigBaseInfoController]);
function ConfigBaseInfoController($rootScope, $scope, $window, $location, toastr, EventManager, UserService, AppService,
FavoriteService,
PermissionService,
AppUtil) {
var urlParams = AppUtil.parseParams($location.$$url);
var appId = urlParams.appid;
if (!appId) {
$window.location.href = '/index.html';
return;
}
initPage();
function initPage() {
$rootScope.hideTip = JSON.parse(localStorage.getItem("hideTip"));
//load session storage to recovery scene
var scene = JSON.parse(sessionStorage.getItem(appId));
$rootScope.pageContext = {
appId: appId,
env: urlParams.env ? urlParams.env : (scene ? scene.env : ''),
clusterName: urlParams.cluster ? urlParams.cluster : (scene ? scene.cluster : 'default')
};
//storage page context to session storage
sessionStorage.setItem(
$rootScope.pageContext.appId,
JSON.stringify({
env: $rootScope.pageContext.env,
cluster: $rootScope.pageContext.clusterName
}));
UserService.load_user().then(function (result) {
$rootScope.pageContext.userId = result.userId;
loadAppInfo();
handleFavorite();
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "獲取用戶登錄信息失敗");
});
handlePermission();
}
function loadAppInfo() {
$scope.notFoundApp = true;
AppService.load($rootScope.pageContext.appId).then(function (result) {
$scope.notFoundApp = false;
$scope.appBaseInfo = result;
$scope.appBaseInfo.orgInfo = result.orgName + '(' + result.orgId + ')';
loadNavTree();
recordVisitApp();
findMissEnvs();
$(".J_appFound").removeClass("hidden");
}, function (result) {
$(".J_appNotFound").removeClass("hidden");
});
}
$scope.createAppInMissEnv = function () {
var count = 0;
$scope.missEnvs.forEach(function (env) {
AppService.create_remote(env, $scope.appBaseInfo).then(function (result) {
toastr.success(env, '創建成功');
count++;
if (count == $scope.missEnvs.length) {
location.reload(true);
}
}, function (result) {
toastr.error(AppUtil.errorMsg(result), '創建失敗:' + env);
count++;
if (count == $scope.missEnvs.length) {
location.reload(true);
}
});
});
};
$scope.createMissingNamespaces = function () {
AppService.create_missing_namespaces($rootScope.pageContext.appId, $rootScope.pageContext.env,
$rootScope.pageContext.clusterName).then(function (result) {
toastr.success("創建成功");
location.reload(true);
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "創建失敗");
}
);
};
function findMissEnvs() {
$scope.missEnvs = [];
AppService.find_miss_envs($rootScope.pageContext.appId).then(function (result) {
$scope.missEnvs = AppUtil.collectData(result);
if ($scope.missEnvs.length > 0) {
toastr.warning("當前項目有環境缺失,請點擊頁面左側『補缺環境』補齊數據");
}
$scope.findMissingNamespaces();
});
}
EventManager.subscribe(EventManager.EventType.CHANGE_ENV_CLUSTER, function () {
$scope.findMissingNamespaces();
});
$scope.findMissingNamespaces = function () {
$scope.missingNamespaces = [];
// only check missing private namespaces when app exists in current env
if ($rootScope.pageContext.env && $scope.missEnvs.indexOf($rootScope.pageContext.env) === -1) {
AppService.find_missing_namespaces($rootScope.pageContext.appId, $rootScope.pageContext.env,
$rootScope.pageContext.clusterName).then(function (result) {
$scope.missingNamespaces = AppUtil.collectData(result);
if ($scope.missingNamespaces.length > 0) {
toastr.warning("當前環境有Namespace缺失,請點擊頁面左側『補缺Namespace』補齊數據");
}
});
}
};
function recordVisitApp() {
//save user recent visited apps
var VISITED_APPS_STORAGE_KEY = "VisitedAppsV2";
var visitedAppsObject = JSON.parse(localStorage.getItem(VISITED_APPS_STORAGE_KEY));
var hasSaved = false;
if (!visitedAppsObject) {
visitedAppsObject = {};
}
if (!visitedAppsObject[$rootScope.pageContext.userId]) {
visitedAppsObject[$rootScope.pageContext.userId] = [];
}
var visitedApps = visitedAppsObject[$rootScope.pageContext.userId];
if (visitedApps && visitedApps.length > 0) {
visitedApps.forEach(function (app) {
if (app == appId) {
hasSaved = true;
return;
}
});
}
var currentUserVisitedApps = visitedAppsObject[$rootScope.pageContext.userId];
if (!hasSaved) {
//if queue's length bigger than 6 will remove oldest app
if (currentUserVisitedApps.length >= 6) {
currentUserVisitedApps.splice(0, 1);
}
currentUserVisitedApps.push($rootScope.pageContext.appId);
localStorage.setItem(VISITED_APPS_STORAGE_KEY,
JSON.stringify(visitedAppsObject));
}
}
function loadNavTree() {
AppService.load_nav_tree($rootScope.pageContext.appId).then(function (result) {
var navTree = [];
var nodes = AppUtil.collectData(result);
if (!nodes || nodes.length == 0) {
toastr.error("系統出錯,請重試或聯系系統負責人");
return;
}
//default first env if session storage is empty
if (!$rootScope.pageContext.env) {
$rootScope.pageContext.env = nodes[0].env;
}
EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE);
//登錄人賬號
var loginUserId=$rootScope.pageContext.userId;
var adminUserIdList=['admin','zhangqg','apollo','maoxy']
nodes.forEach(function (env) {
if (!env.clusters || env.clusters.length == 0) {
return;
}
var node = {};
node.text = env.env;
var clusterNodes = [];
//如果env下面只有一個default集群則不顯示集群列表
if (env.clusters && env.clusters.length == 1 && env.clusters[0].name
== 'default') {
if ($rootScope.pageContext.env == env.env) {
node.state = {};
node.state.selected = true;
}
node.selectable = true;
} else {
node.selectable = false;
//cluster list
env.clusters.forEach(function (cluster) {
var clusterNode = {},
parentNode = [];
//default selection from session storage or first env & first cluster
if ($rootScope.pageContext.env == env.env && $rootScope.pageContext.clusterName
== cluster.name) {
clusterNode.state = {};
clusterNode.state.selected = true;
}
clusterNode.text = cluster.name;
parentNode.push(node.text);
clusterNode.tags = ['集群'];
clusterNode.parentNode = parentNode;
clusterNodes.push(clusterNode);
});
}
node.nodes = clusterNodes;
/**
* rsp 項目管理員權限才能查看其它環境配置,默認只能看dev配置
* 管理員名單
* $rootScope.pageContext.userId
*/
if(node.text=='DEV'){
navTree.push(node);
}else{
$.each(adminUserIdList,function(i,v){
if(v===loginUserId)
navTree.push(node);
});
}
});
//init treeview
$('#treeview').treeview({
color: "#797979",
showBorder: true,
data: navTree,
levels: 99,
expandIcon: '',
collapseIcon: '',
showTags: true,
onNodeSelected: function (event, data) {
if (!data.parentNode) {//first nav node
$rootScope.pageContext.env = data.text;
$rootScope.pageContext.clusterName =
'default';
} else {//second cluster node
$rootScope.pageContext.env =
data.parentNode[0];
$rootScope.pageContext.clusterName =
data.text;
}
//storage scene
sessionStorage.setItem(
$rootScope.pageContext.appId,
JSON.stringify({
env: $rootScope.pageContext.env,
cluster: $rootScope.pageContext.clusterName
}));
$window.location.href = "/config.html#/appid="
+ $rootScope.pageContext.appId
+ "&env=" + $rootScope.pageContext.env
+ "&cluster=" + $rootScope.pageContext.clusterName;
EventManager.emit(EventManager.EventType.REFRESH_NAMESPACE);
EventManager.emit(EventManager.EventType.CHANGE_ENV_CLUSTER);
$rootScope.showSideBar = false;
}
});
var envMapClusters = {};
navTree.forEach(function (node) {
if (node.nodes && node.nodes.length > 0) {
var clusterNames = [];
node.nodes.forEach(function (cluster) {
if (cluster.text != 'default') {
clusterNames.push(cluster.text);
}
});
envMapClusters[node.text] = clusterNames.join(",");
}
});
$rootScope.envMapClusters = envMapClusters;
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "系統出錯,請重試或聯系系統負責人");
});
}
function handleFavorite() {
FavoriteService.findFavorites($rootScope.pageContext.userId,
$rootScope.pageContext.appId)
.then(function (result) {
if (result && result.length) {
$scope.favoriteId = result[0].id;
}
});
$scope.addFavorite = function () {
var favorite = {
userId: $rootScope.pageContext.userId,
appId: $rootScope.pageContext.appId
};
FavoriteService.addFavorite(favorite)
.then(function (result) {
$scope.favoriteId = result.id;
toastr.success("收藏成功");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "收藏失敗");
})
};
$scope.deleteFavorite = function () {
FavoriteService.deleteFavorite($scope.favoriteId)
.then(function (result) {
$scope.favoriteId = 0;
toastr.success("取消收藏成功");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "取消收藏失敗");
})
};
}
function handlePermission() {
//permission
PermissionService.has_create_namespace_permission(appId).then(function (result) {
$scope.hasCreateNamespacePermission = result.hasPermission;
}, function (result) {
});
PermissionService.has_create_cluster_permission(appId).then(function (result) {
$scope.hasCreateClusterPermission = result.hasPermission;
}, function (result) {
});
PermissionService.has_assign_user_permission(appId).then(function (result) {
$scope.hasAssignUserPermission = result.hasPermission;
}, function (result) {
});
$scope.showMasterPermissionTips = function () {
$("#masterNoPermissionDialog").modal('show');
};
}
var VIEW_MODE_SWITCH_WIDTH = 1156;
if (window.innerWidth <= VIEW_MODE_SWITCH_WIDTH) {
$rootScope.viewMode = 2;
$rootScope.showSideBar = false;
} else {
$rootScope.viewMode = 1;
}
$rootScope.adaptScreenSize = function () {
if (window.innerWidth <= VIEW_MODE_SWITCH_WIDTH) {
$rootScope.viewMode = 2;
} else {
$rootScope.viewMode = 1;
$rootScope.showSideBar = false;
}
};
$(window).resize(function () {
$scope.$apply(function () {
$rootScope.adaptScreenSize();
});
});
}
三、修改數據庫
如果光上面修改的話,還是不能是 管理員權限,但是 如果賦權限的話,能看到 DEV,FAT,UAT,PRO等4個環境了,但是還是不能達到管理員的權限,所以我們要在 數據庫的ApolloPortalDB中的ServerConfig 表中去添加相關權限:

只能在key中的 superAdmin 的 Value中添加相關賬號,並且用 逗號 隔開。
權限設置完成之后,效果如下:

