Skip to content

Commit

Permalink
feat(auth): finish front-end logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackBear2003 committed Dec 26, 2024
1 parent 3c0ff3b commit 200e108
Show file tree
Hide file tree
Showing 6 changed files with 493 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public ResponseEntity<Void> initAppPermission(@PathVariable String appId, @Reque
return ResponseEntity.ok().build();
}

@PostMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/initPermission")
public ResponseEntity<Void> initClusterPermission(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName) {
roleInitializationService.initClusterRoles(appId, env, clusterName, userInfoHolder.getUser().getUserId());
return ResponseEntity.ok().build();
}

@GetMapping("/apps/{appId}/permissions/{permissionType}")
public ResponseEntity<PermissionCondition> hasPermission(@PathVariable String appId, @PathVariable String permissionType) {
PermissionCondition permissionCondition = new PermissionCondition();
Expand Down
173 changes: 173 additions & 0 deletions apollo-portal/src/main/resources/static/cluster/role.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<!--
~ Copyright 2024 Apollo Authors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<!doctype html>
<html ng-app="role">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="icon" href="../img/config.png">
<!-- styles -->
<link rel="stylesheet" type="text/css" href="../vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../vendor/angular/angular-toastr-1.4.1.min.css">
<link rel="stylesheet" type="text/css" media='all' href="../vendor/angular/loading-bar.min.css">
<link rel="stylesheet" type="text/css" href="../styles/common-style.css">
<link rel="stylesheet" type="text/css" href="../vendor/select2/select2.min.css">
<title>{{'Cluster.Role.Title' | translate }}</title>
</head>

<body>

<apollonav></apollonav>

<div class="container-fluid apollo-container">
<section class="panel col-md-offset-1 col-md-10" ng-controller="ClusterRoleController">
<header class="panel-heading">
<div class="row">
<div class="col-md-9">
<h4 class="modal-title">
{{'Cluster.Role.Title' | translate }}
<small>
(
{{'Common.AppId' | translate }}:
<label ng-bind="pageContext.appId"></label>
{{'Common.Env' | translate }}:
<label ng-bind="pageContext.env"></label>
{{'Common.ClusterName' | translate }}:
<label ng-bind="pageContext.clusterName"></label>
)
</small>
</h4>
</div>
<div class="col-md-3 text-right">
<a type="button" class="btn btn-info" data-dismiss="modal"
href="{{ '/config.html' | prefixPath }}?#appid={{pageContext.appId}}">{{'Common.ReturnToIndex' | translate }}
</a>
</div>
</div>
</header>
<div class="panel-body" ng-show="hasAssignUserPermission">
<div class="row">
<div class="form-horizontal">
<div class="form-group">
<label
class="col-sm-2 control-label">{{'Cluster.Role.GrantModifyTo' | translate }}<br><small>{{'Cluster.Role.GrantModifyTo2' | translate }}</small></label>
<div class="col-sm-8">
<form class="form-inline" ng-submit="assignRoleToUser('ModifyCluster')">
<div class="form-group">
<apollouserselector apollo-id="modifyRoleWidgetId"></apollouserselector>
</div>
<button type="submit" class="btn btn-default" style="margin-left: 20px;"
ng-disabled="modifyRoleSubmitBtnDisabled">{{'Cluster.Role.Add' | translate }}</button>
</form>
<!-- Split button -->
<div class="item-container">
<div class="btn-group item-info"
ng-repeat="user in rolesAssignedUsers.modifyRoleUsers">
<button type="button" class="btn btn-default" ng-bind="user.userId"></button>
<button type="button" class="btn btn-default dropdown-toggle"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
ng-click="removeUserRole('ModifyCluster', user.userId)">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
</div>
</div>
</div>
</div>
<hr>

<div class="row" style="margin-top: 10px;">
<div class="form-horizontal">
<div class="col-sm-2 text-right">
<label
class="control-label">{{'Cluster.Role.GrantPublishTo' | translate }}<br><small>{{'Cluster.Role.GrantPublishTo2' | translate }}</small></label>
</div>
<div class="col-sm-8">
<form class="form-inline" ng-submit="assignRoleToUser('ReleaseCluster')">
<div class="form-group">
<apollouserselector apollo-id="releaseRoleWidgetId"></apollouserselector>
</div>
<button type="submit" class="btn btn-default" style="margin-left: 20px;"
ng-disabled="ReleaseRoleSubmitBtnDisabled">{{'Cluster.Role.Add' | translate }}</button>
</form>
<!-- Split button -->
<div class="item-container">
<div class="btn-group item-info"
ng-repeat="user in rolesAssignedUsers.releaseRoleUsers">
<button type="button" class="btn btn-default" ng-bind="user.userId"></button>
<button type="button" class="btn btn-default dropdown-toggle"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"
ng-click="removeUserRole('ReleaseCluster', user.userId)">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
</div>
</div>
</div>

</div>

</div>


</div>
<div class="panel-body text-center" ng-show="!hasAssignUserPermission">
<h2>{{'Cluster.Role.NoPermission' | translate }}</h2>
</div>

</section>
</div>

<div ng-include="'../views/common/footer.html'"></div>

<!-- jquery.js -->
<script src="../vendor/jquery.min.js" type="text/javascript"></script>

<!--angular-->
<script src="../vendor/angular/angular.min.js"></script>
<script src="../vendor/angular/angular-resource.min.js"></script>
<script src="../vendor/angular/angular-toastr-1.4.1.tpls.min.js"></script>
<script src="../vendor/angular/loading-bar.min.js"></script>
<script src="../vendor/angular/angular-cookies.min.js"></script>

<script src="../vendor/angular/angular-translate.2.18.1/angular-translate.min.js"></script>
<script src="../vendor/angular/angular-translate.2.18.1/angular-translate-loader-static-files.min.js"></script>
<script src="../vendor/angular/angular-translate.2.18.1/angular-translate-storage-cookie.min.js"></script>

<!-- bootstrap.js -->
<script src="../vendor/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>

<script src="../vendor/select2/select2.min.js" type="text/javascript"></script>

<!--biz-->
<!--must import-->
<script type="application/javascript" src="../scripts/app.js"></script>
<script type="application/javascript" src="../scripts/services/AppService.js"></script>
<script type="application/javascript" src="../scripts/services/EnvService.js"></script>
<script type="application/javascript" src="../scripts/services/UserService.js"></script>
<script type="application/javascript" src="../scripts/services/CommonService.js"></script>
<script type="application/javascript" src="../scripts/services/PermissionService.js"></script>

<script type="application/javascript" src="../scripts/AppUtils.js"></script>

<script type="application/javascript" src="../scripts/PageCommon.js"></script>
<script type="application/javascript" src="../scripts/directive/directive.js"></script>

<script type="application/javascript" src="../scripts/controller/role/ClusterRoleController.js"></script>
</body>

</html>
25 changes: 21 additions & 4 deletions apollo-portal/src/main/resources/static/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,27 @@
<div class="config-item-container hide" ng-class="{'view-mode-1': viewMode == 1, 'view-mode-2': viewMode == 2}"
ng-controller="ConfigNamespaceController">

<h4 class="text-center" ng-show="viewMode == 2">
{{'Config.CurrentlyOperatorEnv' | translate }}:{{pageContext.env}},
{{'Common.Cluster' | translate }}:{{pageContext.clusterName}}
</h4>
<section class="panel cluster-info-panel">
<header class="panel-heading">
<div class="row">
<div class="col-md-6 col-sm-6 header-namespace">
<b class="namespace-name" data-tooltip="tooltip" data-placement="bottom">
{{'Config.CurrentlyOperatorEnv' | translate }}:{{pageContext.env}},
{{'Common.Cluster' | translate }}:{{pageContext.clusterName}}
</b>
</div>
<div class="col-md-6 col-sm-6 text-right header-buttons">
<a type="button" class="btn btn-default btn-sm" data-tooltip="tooltip" data-placement="bottom"
title="{{'Component.Cluster.GrantTips' | translate }}"
href="{{ '/cluster/role.html' | prefixPath }}?#/appid={{pageContext.appId}}&env={{pageContext.env}}&clusterName={{pageContext.clusterName}}">
<img src="img/assign.png">
{{'Component.Cluster.Grant' | translate }}
</a>
</div>
</div>
</header>
</section>

<div class="alert alert-info alert-dismissible" role="alert"
ng-show="(!hideTip || !hideTip[pageContext.appId][pageContext.clusterName]) && envMapClusters[pageContext.env]">

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2024 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
role_module.controller('ClusterRoleController',
['$scope', '$location', '$window', '$translate', 'toastr', 'AppService', 'UserService', 'AppUtil', 'EnvService',
'PermissionService',
function ($scope, $location, $window, $translate, toastr, AppService, UserService, AppUtil, EnvService,
PermissionService) {

var params = AppUtil.parseParams($location.$$url);
$scope.pageContext = {
appId: params.appid,
env: params.env,
clusterName: params.clusterName
};

$scope.modifyRoleSubmitBtnDisabled = false;
$scope.ReleaseRoleSubmitBtnDisabled = false;

$scope.releaseRoleWidgetId = 'releaseRoleWidgetId';
$scope.modifyRoleWidgetId = 'modifyRoleWidgetId';

PermissionService.init_cluster_permission($scope.pageContext.appId, $scope.pageContext.env, $scope.pageContext.clusterName)
.then(function (result) {

}, function (result) {
toastr.warning(AppUtil.errorMsg(result), $translate.instant('Cluster.Role.InitClusterPermissionError'));
});

PermissionService.has_assign_user_permission($scope.pageContext.appId)
.then(function (result) {
$scope.hasAssignUserPermission = result.hasPermission;
}, function (result) {

});

PermissionService.get_cluster_role_users($scope.pageContext.appId,
$scope.pageContext.env, $scope.pageContext.clusterName)
.then(function (result) {
$scope.rolesAssignedUsers = result;
}, function (result) {
toastr.error(AppUtil.errorMsg(result), $translate.instant('Cluster.Role.GetGrantUserError'));
});

$scope.assignRoleToUser = function (roleType) {
if ("ReleaseCluster" === roleType) {
var user = $('.' + $scope.releaseRoleWidgetId).select2('data')[0];
if (!user) {
toastr.warning($translate.instant('Cluster.Role.PleaseChooseUser'));
return;
}
$scope.ReleaseRoleSubmitBtnDisabled = true;
var toAssignReleaseClusterRoleUser = user.id;

var assignReleaseClusterRoleFunc = function (appId, env, clusterName, user) {
return PermissionService.assign_release_cluster_role(appId, env, clusterName, user);
}

assignReleaseClusterRoleFunc(
$scope.pageContext.appId,
$scope.pageContext.env,
$scope.pageContext.clusterName,
toAssignReleaseClusterRoleUser
).then(function () {
toastr.success($translate.instant('Cluster.Role.Added'));
$scope.ReleaseRoleSubmitBtnDisabled = false;
$scope.rolesAssignedUsers.releaseRoleUsers.push({ userId: toAssignReleaseClusterRoleUser });

$('.' + $scope.releaseRoleWidgetId).select2("val", "");
}, function (result) {
$scope.ReleaseRoleSubmitBtnDisabled = false;
toastr.error(AppUtil.errorMsg(result), $translate.instant('Cluster.Role.AddFailed'));
});
} else if ("ModifyCluster" === roleType) {
var user = $('.' + $scope.modifyRoleWidgetId).select2('data')[0];
if (!user) {
toastr.warning($translate.instant('Cluster.Role.PleaseChooseUser'));
return;
}
$scope.modifyRoleSubmitBtnDisabled = true;
var toAssignModifyClusterRoleUser = user.id;

var assignModifyClusterRoleFunc = function (appId, env, clusterName, user) {
return PermissionService.assign_modify_cluster_role(appId, env, clusterName, user);
}

assignModifyClusterRoleFunc(
$scope.pageContext.appId,
$scope.pageContext.env,
$scope.pageContext.clusterName,
toAssignModifyClusterRoleUser
).then(function () {
toastr.success($translate.instant('Cluster.Role.Added'));
$scope.modifyRoleSubmitBtnDisabled = false;
$scope.rolesAssignedUsers.modifyRoleUsers.push({ userId: toAssignModifyClusterRoleUser });

$('.' + $scope.modifyRoleWidgetId).select2("val", "");
}, function (result) {
$scope.modifyRoleSubmitBtnDisabled = false;
toastr.error(AppUtil.errorMsg(result), $translate.instant('Cluster.Role.AddFailed'));
});
}
};

$scope.removeUserRole = function (roleType, user) {
if ("ReleaseCluster" === roleType) {
var removeReleaseClusterRoleFunc = function (appId, env, clusterName, user) {
return PermissionService.remove_release_cluster_role(appId, env, clusterName, user);
}
removeReleaseClusterRoleFunc(
$scope.pageContext.appId,
$scope.pageContext.env,
$scope.pageContext.clusterName,
user
).then(function () {
toastr.success($translate.instant('Cluster.Role.Deleted'));
removeUserFromList($scope.rolesAssignedUsers.releaseRoleUsers, user);
}, function (result) {
toastr.error(AppUtil.errorMsg(result), $translate.instant('Namespace.Role.DeleteFailed'));
});
} else {
var removeModifyClusterRoleFunc = function (appId, namespaceName, user) {
return PermissionService.remove_modify_cluster_role(appId, namespaceName, user);
}

removeModifyClusterRoleFunc(
$scope.pageContext.appId,
$scope.pageContext.env,
$scope.pageContext.clusterName,
user
).then(function () {
toastr.success($translate.instant('Cluster.Role.Deleted'));
removeUserFromList($scope.rolesAssignedUsers.modifyRoleUsers, user);
}, function (result) {
toastr.error(AppUtil.errorMsg(result), $translate.instant('Cluster.Role.DeleteFailed'));
});
}
};

function removeUserFromList(list, user) {
var index = 0;
for (var i = 0; i < list.length; i++) {
if (list[i].userId === user) {
index = i;
break;
}
}
list.splice(index, 1);
}



}]);
Loading

0 comments on commit 200e108

Please sign in to comment.