##### Coverage Report - com.ctrip.framework.apollo.portal.controller.PermissionController

Classes in this File | Line Coverage | Branch Coverage | Complexity
PermissionController |
2% | 1/49
0% | 0/8
| 2.0;2

1 |  |
     package com.ctrip.framework.apollo.portal.controller;
2 |  |
3 |  |
     import com.google.common.collect.Sets;
4 |  |
5 |  |
     import com.ctrip.framework.apollo.core.exception.BadRequestException;
6 |  |
     import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
7 |  |
     import com.ctrip.framework.apollo.portal.constant.RoleType;
8 |  |
     import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
9 |  |
     import com.ctrip.framework.apollo.portal.entity.vo.AppRolesAssignedUsers;
10 |  |
     import com.ctrip.framework.apollo.portal.entity.vo.NamespaceRolesAssignedUsers;
11 |  |
     import com.ctrip.framework.apollo.portal.entity.vo.PermissionCondition;
12 |  |
     import com.ctrip.framework.apollo.portal.service.RolePermissionService;
13 |  |
     import com.ctrip.framework.apollo.portal.util.RoleUtils;
14 |  |
15 |  |
     import org.springframework.beans.factory.annotation.Autowired;
16 |  |
     import org.springframework.http.ResponseEntity;
17 |  |
     import org.springframework.security.access.prepost.PreAuthorize;
18 |  |
     import org.springframework.web.bind.annotation.PathVariable;
19 |  |
     import org.springframework.web.bind.annotation.RequestBody;
20 |  |
     import org.springframework.web.bind.annotation.RequestMapping;
21 |  |
     import org.springframework.web.bind.annotation.RequestMethod;
22 |  |
     import org.springframework.web.bind.annotation.RequestParam;
23 |  |
     import org.springframework.web.bind.annotation.RestController;
24 |  |
25 |  |
     import java.util.Set;
26 |  |
27 |  |
     import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument;
28 |  |
29 |  |
30 |  |
     @RestController
31 | 1 |
     public class PermissionController {
32 |  |
33 |  |
       @Autowired
34 |  |
       private UserInfoHolder userInfoHolder;
35 |  |
       @Autowired
36 |  |
       private RolePermissionService rolePermissionService;
37 |  |
38 |  |
       @RequestMapping("/apps/{appId}/permissions/{permissionType}")
39 |  |
       public ResponseEntity<PermissionCondition> hasPermission(@PathVariable String appId, @PathVariable String permissionType) {
40 | 0 |
         PermissionCondition permissionCondition = new PermissionCondition();
41 |  |
42 | 0 |
         permissionCondition.setHasPermission(
43 | 0 |
             rolePermissionService.userHasPermission(userInfoHolder.getUser().getUserId(), permissionType, appId));
44 |  |
45 | 0 |
         return ResponseEntity.ok().body(permissionCondition);
46 |  |
       }
47 |  |
48 |  |
       @RequestMapping("/apps/{appId}/namespaces/{namespaceName}/permissions/{permissionType}")
49 |  |
       public ResponseEntity<PermissionCondition> hasPermission(@PathVariable String appId, @PathVariable String namespaceName,
50 |  |
                                                                @PathVariable String permissionType) {
51 | 0 |
         PermissionCondition permissionCondition = new PermissionCondition();
52 |  |
53 | 0 |
         permissionCondition.setHasPermission(
54 | 0 |
             rolePermissionService.userHasPermission(userInfoHolder.getUser().getUserId(), permissionType,
55 | 0 |
                                                     RoleUtils.buildNamespaceTargetId(appId, namespaceName)));
56 |  |
57 | 0 |
         return ResponseEntity.ok().body(permissionCondition);
58 |  |
       }
59 |  |
60 |  |
61 |  |
       @RequestMapping("/apps/{appId}/namespaces/{namespaceName}/role_users")
62 |  |
       public NamespaceRolesAssignedUsers getNamespaceRoles(@PathVariable String appId, @PathVariable String namespaceName){
63 |  |
64 | 0 |
         NamespaceRolesAssignedUsers assignedUsers = new NamespaceRolesAssignedUsers();
65 | 0 |
         assignedUsers.setNamespaceName(namespaceName);
66 | 0 |
         assignedUsers.setAppId(appId);
67 |  |
68 | 0 |
         Set<UserInfo> releaseNamespaceUsers =
69 | 0 |
             rolePermissionService.queryUsersWithRole(RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName));
70 | 0 |
         assignedUsers.setReleaseRoleUsers(releaseNamespaceUsers);
71 |  |
72 | 0 |
         Set<UserInfo> modifyNamespaceUsers =
73 | 0 |
             rolePermissionService.queryUsersWithRole(RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName));
74 | 0 |
         assignedUsers.setModifyRoleUsers(modifyNamespaceUsers);
75 |  |
76 | 0 |
         return assignedUsers;
77 |  |
       }
78 |  |
79 |  |
       @PreAuthorize(value = "@permissionValidator.hasAssignRolePermission(#appId)")
80 |  |
       @RequestMapping(value = "/apps/{appId}/namespaces/{namespaceName}/roles/{roleType}", method = RequestMethod.POST)
81 |  |
       public ResponseEntity<Void> assignNamespaceRoleToUser(@PathVariable String appId, @PathVariable String namespaceName,
82 |  |
                                                             @PathVariable String roleType, @RequestBody String user){
83 |  |
84 | 0 |
         checkArgument(user);
85 |  |
86 | 0 |
         if (!RoleType.isValidRoleType(roleType)){
87 | 0 |
           throw new BadRequestException("role type is illegal");
88 |  |
         }
89 | 0 |
         rolePermissionService.assignRoleToUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType),
90 | 0 |
                                                 Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
91 |  |
92 | 0 |
         return ResponseEntity.ok().build();
93 |  |
       }
94 |  |
95 |  |
       @PreAuthorize(value = "@permissionValidator.hasAssignRolePermission(#appId)")
96 |  |
       @RequestMapping(value = "/apps/{appId}/namespaces/{namespaceName}/roles/{roleType}", method = RequestMethod.DELETE)
97 |  |
       public ResponseEntity<Void> removeNamespaceRoleFromUser(@PathVariable String appId, @PathVariable String namespaceName,
98 |  |
                                                      @PathVariable String roleType, @RequestParam String user){
99 | 0 |
         checkArgument(user);
100 |  |
101 | 0 |
         if (!RoleType.isValidRoleType(roleType)){
102 | 0 |
           throw new BadRequestException("role type is illegal");
103 |  |
         }
104 | 0 |
         rolePermissionService.removeRoleFromUsers(RoleUtils.buildNamespaceRoleName(appId, namespaceName, roleType),
105 | 0 |
                                                   Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
106 | 0 |
         return ResponseEntity.ok().build();
107 |  |
       }
108 |  |
109 |  |
       @RequestMapping(value = "/apps/{appId}/role_users")
110 |  |
       public AppRolesAssignedUsers getAppRoles(@PathVariable String appId){
111 | 0 |
         AppRolesAssignedUsers users = new AppRolesAssignedUsers();
112 | 0 |
         users.setAppId(appId);
113 |  |
114 | 0 |
         Set<UserInfo> masterUsers = rolePermissionService.queryUsersWithRole(RoleUtils.buildAppMasterRoleName(appId));
115 | 0 |
         users.setMasterUsers(masterUsers);
116 |  |
117 | 0 |
         return users;
118 |  |
       }
119 |  |
120 |  |
       @PreAuthorize(value = "@permissionValidator.hasAssignRolePermission(#appId)")
121 |  |
       @RequestMapping(value = "/apps/{appId}/roles/{roleType}", method = RequestMethod.POST)
122 |  |
       public ResponseEntity<Void> assignAppRoleToUser(@PathVariable String appId, @PathVariable String roleType,
123 |  |
                                                       @RequestBody String user){
124 |  |
125 | 0 |
         checkArgument(user);
126 |  |
127 | 0 |
         if (!RoleType.isValidRoleType(roleType)){
128 | 0 |
           throw new BadRequestException("role type is illegal");
129 |  |
         }
130 | 0 |
         rolePermissionService.assignRoleToUsers(RoleUtils.buildAppRoleName(appId, roleType),
131 | 0 |
                                                 Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
132 |  |
133 | 0 |
         return ResponseEntity.ok().build();
134 |  |
       }
135 |  |
136 |  |
       @PreAuthorize(value = "@permissionValidator.hasAssignRolePermission(#appId)")
137 |  |
       @RequestMapping(value = "/apps/{appId}/roles/{roleType}", method = RequestMethod.DELETE)
138 |  |
       public ResponseEntity<Void> removeAppRoleFromUser(@PathVariable String appId, @PathVariable String roleType,
139 |  |
                                                      @RequestParam String user){
140 | 0 |
         checkArgument(user);
141 |  |
142 | 0 |
         if (!RoleType.isValidRoleType(roleType)){
143 | 0 |
           throw new BadRequestException("role type is illegal");
144 |  |
         }
145 | 0 |
         rolePermissionService.removeRoleFromUsers(RoleUtils.buildAppRoleName(appId, roleType),
146 | 0 |
                                                   Sets.newHashSet(user), userInfoHolder.getUser().getUserId());
147 | 0 |
         return ResponseEntity.ok().build();
148 |  |
       }
149 |  |
150 |  |
151 |  |
     }