Project

General

Profile

1
package eu.dnetlib.uoamonitorservice.handlers.utils;
2

    
3
import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService;
4
import org.apache.log4j.Logger;
5

    
6
import java.util.List;
7

    
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10

    
11
@Component
12
public class RolesUtils {
13
    @Autowired
14
    private AuthorizationService authorizationService;
15

    
16
    private final Logger log = Logger.getLogger(this.getClass());
17

    
18
    public List<String> getRoles() {
19
        return authorizationService.getRoles();
20
    }
21

    
22
    public boolean isPortalAdmin(List<String> roles) {
23
        if(roles == null) {
24
            return false;
25
        }
26
//        log.debug(authorizationService.PORTAL_ADMIN);
27
//        log.debug("PortalAdmin: "+roles.contains(authorizationService.PORTAL_ADMIN));
28
        return roles.contains(authorizationService.PORTAL_ADMIN);
29
    }
30

    
31
    public boolean isCurator(List<String> roles, String type) {
32
        if(roles == null) {
33
            return false;
34
        }
35
//        log.debug(authorizationService.curator(type));
36
//        log.debug("Curator in "+type+": "+roles.contains(authorizationService.curator(type)));
37
        return roles.contains(authorizationService.curator(type));
38
    }
39

    
40
    public boolean isManager(List<String> roles, String type, String id) {
41
        if(roles == null) {
42
            return false;
43
        }
44
//        log.debug(authorizationService.manager(type, id));
45
//        log.debug("Manager in "+type+" - "+id+": "+roles.contains(authorizationService.manager(type, id)));
46
        return roles.contains(authorizationService.manager(type, id));
47
    }
48

    
49
    public boolean isMember(List<String> roles, String type, String id) {
50
        if(roles == null) {
51
            return false;
52
        }
53
//        log.debug(authorizationService.member(type, id));
54
//        log.debug("Member in "+type+" - "+id+": "+roles.contains(authorizationService.member(type, id)));
55
        return roles.contains(authorizationService.member(type, id));
56
    }
57

    
58
    public boolean hasUpdateAuthority(List<String> roles, String type, String id) {
59
        return isPortalAdmin(roles) || isCurator(roles, type) || isManager(roles, type, id);
60
    }
61

    
62
    public boolean hasCreateAndDeleteAuthority(List<String> roles, String type) {
63
        return isPortalAdmin(roles) || isCurator(roles, type);
64
    }
65
}
(1-1/2)