##### Coverage Report - com.ctrip.framework.apollo.biz.service.PrivilegeService

Classes in this File | Line Coverage | Branch Coverage | Complexity
PrivilegeService |
100% | 22/22
66% | 4/6
| 1.75;1.75
PrivilegeService$PrivilType |
100% | 2/2
N/A | N/A
| 1.75;1.75

1 |  |
     package com.ctrip.framework.apollo.biz.service;
2 |  |
3 |  |
     import com.ctrip.framework.apollo.biz.entity.Audit;
4 |  |
     import com.ctrip.framework.apollo.biz.entity.Privilege;
5 |  |
     import com.ctrip.framework.apollo.biz.repository.PrivilegeRepository;
6 |  |
7 |  |
     import org.springframework.beans.factory.annotation.Autowired;
8 |  |
     import org.springframework.stereotype.Service;
9 |  |
     import org.springframework.transaction.annotation.Transactional;
10 |  |
11 |  |
     import java.util.List;
12 |  |
13 |  |
     @Service
14 | 1 |
     public class PrivilegeService {
15 |  |
16 | 1 |
       enum PrivilType {
17 | 1 |
         EDIT, REVIEW, RELEASE
18 |  |
       }
19 |  |
20 |  |
       @Autowired
21 |  |
       private PrivilegeRepository privilRepo;
22 |  |
23 |  |
       @Autowired
24 |  |
       private AuditService auditService;
25 |  |
26 |  |
       @Transactional
27 |  |
       public Privilege addPrivilege(long namespaceId, String name, PrivilType privilType) {
28 | 4 |
         Privilege privil =
29 | 4 |
             privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
30 | 4 |
         if (privil == null) {
31 | 4 |
           privil = new Privilege();
32 | 4 |
           privil.setNamespaceId(namespaceId);
33 | 4 |
           privil.setPrivilType(privilType.name());
34 | 4 |
           privil.setName(name);
35 | 4 |
           privilRepo.save(privil);
36 |  |
37 | 4 |
           auditService.audit(Privilege.class.getSimpleName(), privil.getId(), Audit.OP.INSERT, name);
38 |  |
         }
39 | 4 |
         return privil;
40 |  |
       }
41 |  |
42 |  |
       public boolean hasPrivilege(long namespaceId, String name, PrivilType privilType) {
43 | 6 |
         Privilege privil =
44 | 6 |
             privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
45 | 6 |
         return (privil != null) ? true : false;
46 |  |
       }
47 |  |
48 |  |
       public List<Privilege> listPrivileges(long namespaceId) {
49 | 2 |
         return privilRepo.findByNamespaceId(namespaceId);
50 |  |
       }
51 |  |
52 |  |
       @Transactional
53 |  |
       public void removePrivilege(long namespaceId, String name, PrivilType privilType) {
54 | 1 |
         Privilege privil =
55 | 1 |
             privilRepo.findByNamespaceIdAndNameAndPrivilType(namespaceId, name, privilType.name());
56 | 1 |
         if (privil != null) {
57 | 1 |
           privilRepo.delete(privil);
58 |  |
59 | 1 |
           auditService.audit(Privilege.class.getSimpleName(), privil.getId(), Audit.OP.DELETE, name);
60 |  |
         }
61 | 1 |
       }
62 |  |
     }