Skip to content

Commit

Permalink
已授权校验 & 创建app 用户模糊搜索
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Jun 28, 2016
1 parent a474d5c commit f763525
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
Expand Up @@ -8,9 +8,11 @@
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.portal.PortalSettings;
import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
import com.ctrip.framework.apollo.portal.entity.vo.EnvClusterInfo;
import com.ctrip.framework.apollo.portal.listener.AppCreationEvent;
import com.ctrip.framework.apollo.portal.service.AppService;
import com.ctrip.framework.apollo.portal.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
Expand Down Expand Up @@ -40,6 +42,8 @@ public class AppController {
@Autowired
private ApplicationEventPublisher publisher;

@Autowired
private UserService userService;

@RequestMapping("")
public List<App> findAllApp() {
Expand Down Expand Up @@ -71,12 +75,17 @@ public MultiResponseEntity<EnvClusterInfo> nav(@PathVariable String appId) {
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<Void> create(@RequestBody App app) {

checkArgument(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName(),
checkArgument(app.getName(), app.getAppId(), app.getOwnerName(),
app.getOrgId(), app.getOrgName());
if (!InputValidator.isValidClusterNamespace(app.getAppId())) {
throw new BadRequestException(String.format("AppId格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
}

UserInfo userInfo = userService.findByUserId(app.getOwnerName());
if (userInfo == null){
throw new BadRequestException("应用负责人不存在");
}
app.setOwnerEmail(userInfo.getEmail());
appService.enrichUserInfo(app);
App createdApp = appService.create(app);

Expand Down
Expand Up @@ -16,6 +16,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -89,8 +90,11 @@ public ResponseEntity<Void> assignNamespaceRoleToUser(@PathVariable String appId
if (!RoleType.isValidRoleType(roleType)){
throw new BadRequestException("role type is illegal");
}
rolePermissionService.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType),
Set<String> assignedUser = rolePermissionService.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType),
Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
if (CollectionUtils.isEmpty(assignedUser)){
throw new BadRequestException(user + "已授权");
}

return ResponseEntity.ok().build();
}
Expand Down Expand Up @@ -130,8 +134,11 @@ public ResponseEntity<Void> assignAppRoleToUser(@PathVariable String appId, @Pat
if (!RoleType.isValidRoleType(roleType)){
throw new BadRequestException("role type is illegal");
}
rolePermissionService.assignRoleToUsers(RoleUtils.buildAppRoleName(appId, roleType),
Set<String> assignedUsers = rolePermissionService.assignRoleToUsers(RoleUtils.buildAppRoleName(appId, roleType),
Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
if (CollectionUtils.isEmpty(assignedUsers)){
throw new BadRequestException(user + "已授权");
}

return ResponseEntity.ok().build();
}
Expand Down
5 changes: 1 addition & 4 deletions apollo-portal/src/main/resources/static/app.html
Expand Up @@ -48,10 +48,7 @@
<div class="form-group">
<label class="col-sm-2 control-label"><apollorequiredfiled></apollorequiredfiled> 应用负责人</label>
<div class="col-sm-6">
<div class="input-group">
<input type="text" class="form-control" name="appOwner" ng-model="app.ownerName" required>
<div class="input-group-addon" ng-bind="emailPostfix"></div>
</div>
<apollouserselector apollo-id="userSelectWidgetId"></apollouserselector>
<small>(负责人具有项目管理的最高权限,比如分配配置的修改权,发布权等)</small>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions apollo-portal/src/main/resources/static/namespace/role.html
Expand Up @@ -73,8 +73,8 @@ <h4 class="modal-title">权限管理<small>(AppId:<label ng-bind="pageContext.ap
<button type="submit" class="btn btn-default" style="margin-left: 20px;">添加</button>
</form>
<!-- Split button -->
<div style="margin-top: 15px;">
<div class="btn-group" ng-repeat="user in rolesAssignedUsers.releaseRoleUsers">
<div class="user-container">
<div class="btn-group user-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('ReleaseNamespace', user.userId)">
Expand Down
Expand Up @@ -26,7 +26,7 @@ create_app_module.controller('CreateAppController', ['$scope', '$window', 'toast

});

$scope.emailPostfix = "@ctrip.com";
$scope.userSelectWidgetId = "userSelectWidgetId";

$scope.create = function () {
var selectedOrg = $('#organization').select2('data')[0];
Expand All @@ -38,7 +38,14 @@ create_app_module.controller('CreateAppController', ['$scope', '$window', 'toast

$scope.app.orgId = selectedOrg.id;
$scope.app.orgName = selectedOrg.name;
$scope.app.ownerEmail = $scope.app.ownerName + $scope.emailPostfix;

// ownerName
var user = $('.' + $scope.userSelectWidgetId).select2('data')[0];
if (!user){
toastr.warning("请输入应用负责人");
return;
}
$scope.app.ownerName = user.id;

AppService.create($scope.app).then(function (result) {
toastr.success('添加成功!');
Expand Down
Expand Up @@ -36,6 +36,7 @@ role_module.controller('AppRoleController',
.then(function (result) {
toastr.success("添加成功");
$scope.appRoleUsers.masterUsers.push({userId: toAssignMasterRoleUser});
$('.' + $scope.userSelectWidgetId).select2("val", "");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "添加失败");
});
Expand Down
Expand Up @@ -9,7 +9,7 @@ role_module.controller('NamespaceRoleController',
appId: params.appid,
namespaceName: params.namespaceName
};

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

Expand All @@ -24,16 +24,14 @@ role_module.controller('NamespaceRoleController',
$scope.pageContext.namespaceName)
.then(function (result) {
$scope.rolesAssignedUsers = result;
console.log(result);
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "加载授权用户出错");
});


$scope.assignRoleToUser = function (roleType) {
if ('ReleaseNamespace' == roleType) {
var user = $('.' + $scope.releaseRoleWidgetId).select2('data')[0];
if (!user){
if (!user) {
toastr.warning("请选择用户");
return;
}
Expand All @@ -45,12 +43,13 @@ role_module.controller('NamespaceRoleController',
toastr.success("添加成功");
$scope.rolesAssignedUsers.releaseRoleUsers.push(
{userId: toAssignReleaseNamespaceRoleUser});
$('.' + $scope.releaseRoleWidgetId).select2("val", "");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "添加失败");
});
} else {
var user = $('.' + $scope.modifyRoleWidgetId).select2('data')[0];
if (!user){
if (!user) {
toastr.warning("请选择用户");
return;
}
Expand All @@ -62,6 +61,7 @@ role_module.controller('NamespaceRoleController',
toastr.success("添加成功");
$scope.rolesAssignedUsers.modifyRoleUsers.push(
{userId: toAssignModifyNamespaceRoleUser});
$('.' + $scope.modifyRoleWidgetId).select2("val", "");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "添加失败");
});
Expand Down Expand Up @@ -102,5 +102,5 @@ role_module.controller('NamespaceRoleController',
}
list.splice(index, 1);
}

}]);
Expand Up @@ -26,7 +26,7 @@
import com.ctrip.framework.apollo.core.dto.AppDTO;
import com.ctrip.framework.apollo.core.exception.ServiceException;
import com.ctrip.framework.apollo.portal.controller.AppController;
import com.ctrip.framework.apollo.portal.service.AppService;
import com.ctrip.framework.apollo.portal.service.UserService;

import com.google.gson.Gson;

Expand All @@ -35,12 +35,12 @@ public class ServiceExceptionTest extends AbstractPortalTest {
@Autowired
private AppController appController;
@Mock
private AppService appService;
private UserService userService;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(appController, "appService", appService);
ReflectionTestUtils.setField(appController, "userService", userService);
}

private String getBaseAppUrl() {
Expand All @@ -61,7 +61,7 @@ public void testAdminServiceException() {
new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "admin server error",
new Gson().toJson(errorAttributes).getBytes(), Charset.defaultCharset());

when(appService.create(any(App.class))).thenThrow(adminException);
when(userService.findByUserId(any(String.class))).thenThrow(adminException);

App app = generateSampleApp();
try {
Expand Down

0 comments on commit f763525

Please sign in to comment.