1 | package eu.clarin.cmdi.virtualcollectionregistry; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStreamReader; |
---|
7 | import java.util.HashSet; |
---|
8 | import java.util.Set; |
---|
9 | import javax.annotation.PostConstruct; |
---|
10 | import org.slf4j.Logger; |
---|
11 | import org.slf4j.LoggerFactory; |
---|
12 | import org.springframework.beans.factory.annotation.Value; |
---|
13 | import org.springframework.stereotype.Component; |
---|
14 | |
---|
15 | /** |
---|
16 | * |
---|
17 | * @author twagoo |
---|
18 | */ |
---|
19 | @Component |
---|
20 | public class AdminUsersServiceImpl implements AdminUsersService { |
---|
21 | |
---|
22 | private final static Logger logger = LoggerFactory.getLogger(AdminUsersServiceImpl.class); |
---|
23 | private final Set<String> adminUsers = new HashSet<>(); |
---|
24 | |
---|
25 | @Value("${eu.clarin.cmdi.virtualcollectionregistry.admindb:}") |
---|
26 | private String adminDb; |
---|
27 | |
---|
28 | @Override |
---|
29 | public final boolean isAdmin(String user) { |
---|
30 | logger.debug("Checking admin rights of {}", user); |
---|
31 | return adminUsers.contains(user); |
---|
32 | } |
---|
33 | |
---|
34 | @PostConstruct |
---|
35 | protected final void init() { |
---|
36 | if (adminDb != null && !adminDb.isEmpty()) { |
---|
37 | logger.info("Reading admin user database"); |
---|
38 | try { |
---|
39 | loadAdminDatabase(adminDb); |
---|
40 | } catch (IOException e) { |
---|
41 | throw new RuntimeException("Could not load admin user database", e); |
---|
42 | } |
---|
43 | } |
---|
44 | if (adminUsers.isEmpty()) { |
---|
45 | logger.warn("No admin users have been defined"); |
---|
46 | } else { |
---|
47 | logger.debug("Admin users: {}", adminUsers); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | private void loadAdminDatabase(String filename) throws IOException { |
---|
52 | adminUsers.clear(); |
---|
53 | try (BufferedReader reader = new BufferedReader(new InputStreamReader( |
---|
54 | new FileInputStream(filename)))) { |
---|
55 | String line; |
---|
56 | while ((line = reader.readLine()) != null) { |
---|
57 | line = line.trim(); |
---|
58 | if (line.isEmpty() || line.startsWith("#")) { |
---|
59 | continue; |
---|
60 | } |
---|
61 | adminUsers.add(line); |
---|
62 | } // while |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | } |
---|