##### Coverage Report - com.ctrip.framework.apollo.portal.service.RoleInitializationService

Classes in this File | Line Coverage | Branch Coverage | Complexity
RoleInitializationService |
2% | 1/49
0% | 0/6
| 0.0;0

1 |  |
     package com.ctrip.framework.apollo.portal.service;
2 |  |
3 |  |
     import com.google.common.collect.FluentIterable;
4 |  |
     import com.google.common.collect.Lists;
5 |  |
     import com.google.common.collect.Sets;
6 |  |
7 |  |
     import com.ctrip.framework.apollo.common.entity.App;
8 |  |
     import com.ctrip.framework.apollo.core.ConfigConsts;
9 |  |
     import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
10 |  |
     import com.ctrip.framework.apollo.portal.constant.PermissionType;
11 |  |
     import com.ctrip.framework.apollo.portal.entity.po.Permission;
12 |  |
     import com.ctrip.framework.apollo.portal.entity.po.Role;
13 |  |
     import com.ctrip.framework.apollo.portal.util.RoleUtils;
14 |  |
15 |  |
     import org.springframework.beans.factory.annotation.Autowired;
16 |  |
     import org.springframework.stereotype.Service;
17 |  |
     import org.springframework.transaction.annotation.Transactional;
18 |  |
19 |  |
     import java.util.Set;
20 |  |
21 |  |
     @Service
22 | 1 |
     public class RoleInitializationService {
23 |  |
24 |  |
       @Autowired
25 |  |
       private UserInfoHolder userInfoHolder;
26 |  |
       @Autowired
27 |  |
       private RolePermissionService rolePermissionService;
28 |  |
29 |  |
       @Transactional
30 |  |
       public void initAppRoles(App app) {
31 | 0 |
         String appId = app.getAppId();
32 |  |
33 | 0 |
         String appMasterRoleName = RoleUtils.buildAppMasterRoleName(appId);
34 |  |
35 |  |
         //has created before
36 | 0 |
         if (rolePermissionService.findRoleByRoleName(appMasterRoleName) != null){
37 | 0 |
           return;
38 |  |
         }
39 | 0 |
         String operaterUserId = userInfoHolder.getUser().getUserId();
40 |  |
         //create app permissions
41 | 0 |
         createAppMasterRole(appId);
42 |  |
43 |  |
         //assign master role to user
44 | 0 |
         rolePermissionService
45 | 0 |
             .assignRoleToUsers(RoleUtils.buildAppMasterRoleName(appId), Sets.newHashSet(app.getOwnerName()),
46 |  |
                                operaterUserId);
47 |  |
48 | 0 |
         initNamespaceRoles(appId, ConfigConsts.NAMESPACE_APPLICATION);
49 |  |
50 | 0 |
       }
51 |  |
52 |  |
       @Transactional
53 |  |
       public void initNamespaceRoles(String appId, String namespaceName) {
54 |  |
55 | 0 |
         String modifyNamespaceRoleName = RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName);
56 | 0 |
         if (rolePermissionService.findRoleByRoleName(modifyNamespaceRoleName) == null) {
57 | 0 |
           createDefaultNamespaceRole(appId, namespaceName, PermissionType.MODIFY_NAMESPACE,
58 | 0 |
                                      RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName));
59 |  |
         }
60 |  |
61 | 0 |
         String releaseNamespaceRoleName = RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName);
62 | 0 |
         if (rolePermissionService.findRoleByRoleName(releaseNamespaceRoleName) == null) {
63 | 0 |
           createDefaultNamespaceRole(appId, namespaceName, PermissionType.RELEASE_NAMESPACE,
64 | 0 |
                                      RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName));
65 |  |
         }
66 | 0 |
       }
67 |  |
68 |  |
       private void createAppMasterRole(String appId) {
69 | 0 |
         Set<Permission> appPermissions =
70 | 0 |
             FluentIterable.from(Lists.newArrayList(
71 |  |
                 PermissionType.CREATE_CLUSTER, PermissionType.CREATE_NAMESPACE, PermissionType.ASSIGN_ROLE))
72 | 0 |
                 .transform(permissionType -> createPermisson(appId, permissionType)).toSet();
73 | 0 |
         Set<Permission> createdAppPermissions = rolePermissionService.createPermissions(appPermissions);
74 |  |
         Set<Long>
75 | 0 |
             appPermissionIds =
76 | 0 |
             FluentIterable.from(createdAppPermissions).transform(permission -> permission.getId()).toSet();
77 |  |
78 |  |
         //create app master role
79 | 0 |
         Role appMasterRole = createRole(RoleUtils.buildAppMasterRoleName(appId));
80 |  |
81 | 0 |
         rolePermissionService.createRoleWithPermissions(appMasterRole, appPermissionIds);
82 | 0 |
       }
83 |  |
84 |  |
       private Permission createPermisson(String targetId, String permisson) {
85 | 0 |
         Permission permission = new Permission();
86 | 0 |
         permission.setPermissionType(permisson);
87 | 0 |
         permission.setTargetId(targetId);
88 | 0 |
         String userId = userInfoHolder.getUser().getUserId();
89 | 0 |
         permission.setDataChangeCreatedBy(userId);
90 | 0 |
         permission.setDataChangeLastModifiedBy(userId);
91 | 0 |
         return permission;
92 |  |
       }
93 |  |
94 |  |
       private Role createRole(String roleName) {
95 | 0 |
         Role role = new Role();
96 | 0 |
         role.setRoleName(roleName);
97 | 0 |
         String operaterUserId = userInfoHolder.getUser().getUserId();
98 | 0 |
         role.setDataChangeCreatedBy(operaterUserId);
99 | 0 |
         role.setDataChangeLastModifiedBy(operaterUserId);
100 | 0 |
         return role;
101 |  |
       }
102 |  |
103 |  |
       private void createDefaultNamespaceRole(String appId, String namespaceName, String permissionType, String roleName) {
104 |  |
105 |  |
         Permission
106 | 0 |
             modifyDefaultNsPermission =
107 | 0 |
             createPermisson(RoleUtils.buildNamespaceTargetId(appId, namespaceName), permissionType);
108 | 0 |
         Permission createdModifyDefaultNsPermission = rolePermissionService.createPermission(modifyDefaultNsPermission);
109 |  |
110 | 0 |
         Role role = createRole(roleName);
111 | 0 |
         rolePermissionService
112 | 0 |
             .createRoleWithPermissions(role, Sets.newHashSet(createdModifyDefaultNsPermission.getId()));
113 | 0 |
       }
114 |  |
     }