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

Classes in this File | Line Coverage | Branch Coverage | Complexity
RolePermissionService |
95% | 83/87
82% | 23/28
| 0.0;0

1 |  |
     package com.ctrip.framework.apollo.portal.service;
2 |  |
3 |  |
     import com.google.common.base.Preconditions;
4 |  |
     import com.google.common.collect.FluentIterable;
5 |  |
     import com.google.common.collect.HashMultimap;
6 |  |
     import com.google.common.collect.Multimap;
7 |  |
     import com.google.common.collect.Sets;
8 |  |
9 |  |
     import com.ctrip.framework.apollo.portal.entity.po.Permission;
10 |  |
     import com.ctrip.framework.apollo.portal.entity.po.Role;
11 |  |
     import com.ctrip.framework.apollo.portal.entity.po.RolePermission;
12 |  |
     import com.ctrip.framework.apollo.portal.entity.po.UserInfo;
13 |  |
     import com.ctrip.framework.apollo.portal.entity.po.UserRole;
14 |  |
     import com.ctrip.framework.apollo.portal.repository.PermissionRepository;
15 |  |
     import com.ctrip.framework.apollo.portal.repository.RolePermissionRepository;
16 |  |
     import com.ctrip.framework.apollo.portal.repository.RoleRepository;
17 |  |
     import com.ctrip.framework.apollo.portal.repository.UserRoleRepository;
18 |  |
19 |  |
     import org.springframework.beans.factory.annotation.Autowired;
20 |  |
     import org.springframework.stereotype.Service;
21 |  |
     import org.springframework.transaction.annotation.Transactional;
22 |  |
     import org.springframework.util.CollectionUtils;
23 |  |
24 |  |
     import java.util.Collection;
25 |  |
     import java.util.Collections;
26 |  |
     import java.util.Date;
27 |  |
     import java.util.List;
28 |  |
     import java.util.Set;
29 |  |
30 |  |
     /**
31 |  |
      * @author Jason Song(song_s@ctrip.com)
32 |  |
      */
33 |  |
     @Service
34 | 1 |
     public class RolePermissionService {
35 |  |
       @Autowired
36 |  |
       private RoleRepository roleRepository;
37 |  |
38 |  |
       @Autowired
39 |  |
       private RolePermissionRepository rolePermissionRepository;
40 |  |
41 |  |
       @Autowired
42 |  |
       private UserRoleRepository userRoleRepository;
43 |  |
44 |  |
       @Autowired
45 |  |
       private PermissionRepository permissionRepository;
46 |  |
47 |  |
       /**
48 |  |
        * Create role with permissions, note that role name should be unique
49 |  |
        */
50 |  |
       @Transactional
51 |  |
       public Role createRoleWithPermissions(Role role, Set<Long> permissionIds) {
52 | 2 |
         Role current = findRoleByRoleName(role.getRoleName());
53 | 2 |
         Preconditions.checkState(current == null, "Role %s already exists!", role.getRoleName());
54 |  |
55 | 1 |
         Role createdRole = roleRepository.save(role);
56 |  |
57 | 1 |
         if (!CollectionUtils.isEmpty(permissionIds)) {
58 | 1 |
           Iterable<RolePermission> rolePermissions = FluentIterable.from(permissionIds).transform(
59 |  |
               permissionId -> {
60 | 2 |
                 RolePermission rolePermission = new RolePermission();
61 | 2 |
                 rolePermission.setRoleId(createdRole.getId());
62 | 2 |
                 rolePermission.setPermissionId(permissionId);
63 | 2 |
                 rolePermission.setDataChangeCreatedBy(createdRole.getDataChangeCreatedBy());
64 | 2 |
                 rolePermission.setDataChangeLastModifiedBy(createdRole.getDataChangeLastModifiedBy());
65 | 2 |
                 return rolePermission;
66 |  |
               });
67 | 1 |
           rolePermissionRepository.save(rolePermissions);
68 |  |
         }
69 |  |
70 | 1 |
         return createdRole;
71 |  |
       }
72 |  |
73 |  |
       /**
74 |  |
        * Assign role to users
75 |  |
        */
76 |  |
       @Transactional
77 |  |
       public void assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) {
78 | 3 |
         Role role = findRoleByRoleName(roleName);
79 | 3 |
         Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
80 |  |
81 | 2 |
         List<UserRole> existedUserRoles =
82 | 2 |
             userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
83 | 2 |
         Set<String> existedUserIds =
84 | 4 |
             FluentIterable.from(existedUserRoles).transform(userRole -> userRole.getUserId()).toSet();
85 |  |
86 | 2 |
         Set<String> toAssignUserIds = Sets.difference(userIds, existedUserIds);
87 |  |
88 | 2 |
         Iterable<UserRole> toCreate = FluentIterable.from(toAssignUserIds).transform(userId -> {
89 | 2 |
           UserRole userRole = new UserRole();
90 | 2 |
           userRole.setRoleId(role.getId());
91 | 2 |
           userRole.setUserId(userId);
92 | 2 |
           userRole.setDataChangeCreatedBy(operatorUserId);
93 | 2 |
           userRole.setDataChangeLastModifiedBy(operatorUserId);
94 | 2 |
           return userRole;
95 |  |
         });
96 |  |
97 | 2 |
         userRoleRepository.save(toCreate);
98 | 2 |
       }
99 |  |
100 |  |
       /**
101 |  |
        * Remove role from users
102 |  |
        */
103 |  |
       @Transactional
104 |  |
       public void removeRoleFromUsers(String roleName, Set<String> userIds, String operatorUserId) {
105 | 2 |
         Role role = findRoleByRoleName(roleName);
106 | 2 |
         Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
107 |  |
108 | 1 |
         List<UserRole> existedUserRoles =
109 | 1 |
             userRoleRepository.findByUserIdInAndRoleId(userIds, role.getId());
110 |  |
111 | 1 |
         for (UserRole userRole : existedUserRoles) {
112 | 2 |
           userRole.setDeleted(true);
113 | 2 |
           userRole.setDataChangeLastModifiedTime(new Date());
114 | 2 |
           userRole.setDataChangeLastModifiedBy(operatorUserId);
115 | 2 |
         }
116 |  |
117 | 1 |
         userRoleRepository.save(existedUserRoles);
118 | 1 |
       }
119 |  |
120 |  |
       /**
121 |  |
        * Query users with role
122 |  |
        */
123 |  |
       public Set<UserInfo> queryUsersWithRole(String roleName) {
124 | 1 |
         Role role = findRoleByRoleName(roleName);
125 |  |
126 | 1 |
         if (role == null) {
127 | 0 |
           return Collections.emptySet();
128 |  |
         }
129 |  |
130 | 1 |
         List<UserRole> userRoles = userRoleRepository.findByRoleId(role.getId());
131 |  |
132 | 1 |
         Set<UserInfo> users = FluentIterable.from(userRoles).transform(userRole -> {
133 | 2 |
           UserInfo userInfo = new UserInfo();
134 | 2 |
           userInfo.setUserId(userRole.getUserId());
135 | 2 |
           return userInfo;
136 | 1 |
         }).toSet();
137 |  |
138 | 1 |
         return users;
139 |  |
       }
140 |  |
141 |  |
       /**
142 |  |
        * Find role by role name, note that roleName should be unique
143 |  |
        */
144 |  |
       public Role findRoleByRoleName(String roleName) {
145 | 8 |
         return roleRepository.findTopByRoleName(roleName);
146 |  |
       }
147 |  |
148 |  |
       /**
149 |  |
        * Check whether user has the permission
150 |  |
        */
151 |  |
       public boolean userHasPermission(String userId, String permissionType, String targetId) {
152 | 6 |
         Permission permission =
153 | 6 |
             permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
154 | 6 |
         if (permission == null) {
155 | 0 |
           return false;
156 |  |
         }
157 |  |
158 | 6 |
         List<UserRole> userRoles = userRoleRepository.findByUserId(userId);
159 | 6 |
         if (CollectionUtils.isEmpty(userRoles)) {
160 | 2 |
           return false;
161 |  |
         }
162 |  |
163 | 4 |
         Set<Long> roleIds =
164 | 8 |
             FluentIterable.from(userRoles).transform(userRole -> userRole.getRoleId()).toSet();
165 | 4 |
         List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleIdIn(roleIds);
166 | 4 |
         if (CollectionUtils.isEmpty(rolePermissions)) {
167 | 0 |
           return false;
168 |  |
         }
169 |  |
170 | 4 |
         for (RolePermission rolePermission : rolePermissions) {
171 | 6 |
           if (rolePermission.getPermissionId() == permission.getId()) {
172 | 4 |
             return true;
173 |  |
           }
174 | 2 |
         }
175 |  |
176 | 0 |
         return false;
177 |  |
       }
178 |  |
179 |  |
       /**
180 |  |
        * Create permission, note that permissionType + targetId should be unique
181 |  |
        */
182 |  |
       @Transactional
183 |  |
       public Permission createPermission(Permission permission) {
184 | 2 |
         String permissionType = permission.getPermissionType();
185 | 2 |
         String targetId = permission.getTargetId();
186 | 2 |
         Permission current =
187 | 2 |
             permissionRepository.findTopByPermissionTypeAndTargetId(permissionType, targetId);
188 | 2 |
         Preconditions.checkState(current == null,
189 |  |
             "Permission with permissionType %s targetId %s already exists!", permissionType, targetId);
190 |  |
191 | 1 |
         return permissionRepository.save(permission);
192 |  |
       }
193 |  |
194 |  |
       /**
195 |  |
        * Create permissions, note that permissionType + targetId should be unique
196 |  |
        */
197 |  |
       @Transactional
198 |  |
       public Set<Permission> createPermissions(Set<Permission> permissions) {
199 | 2 |
         Multimap<String, String> targetIdPermissionTypes = HashMultimap.create();
200 | 2 |
         for (Permission permission : permissions) {
201 | 4 |
           targetIdPermissionTypes.put(permission.getTargetId(), permission.getPermissionType());
202 | 4 |
         }
203 |  |
204 | 2 |
         for (String targetId : targetIdPermissionTypes.keySet()) {
205 | 3 |
           Collection<String> permissionTypes = targetIdPermissionTypes.get(targetId);
206 | 3 |
           List<Permission> current =
207 | 3 |
               permissionRepository.findByPermissionTypeInAndTargetId(permissionTypes, targetId);
208 | 3 |
           Preconditions.checkState(CollectionUtils.isEmpty(current),
209 |  |
               "Permission with permissionType %s targetId %s already exists!", permissionTypes,
210 |  |
               targetId);
211 | 2 |
         }
212 |  |
213 | 1 |
         Iterable<Permission> results = permissionRepository.save(permissions);
214 | 1 |
         return FluentIterable.from(results).toSet();
215 |  |
       }
216 |  |
     }