|
6 | 6 | import java.util.Collections; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.stream.Collectors; |
9 | | - |
10 | 9 | import org.phoebus.channelfinder.entity.Channel; |
11 | 10 | import org.phoebus.channelfinder.entity.Property; |
12 | 11 | import org.phoebus.channelfinder.entity.Tag; |
|
18 | 17 | @Service |
19 | 18 | public class AuthorizationService { |
20 | 19 |
|
21 | | - public static List<String> admin_groups; |
22 | | - public static List<String> channel_groups; |
23 | | - public static List<String> property_groups; |
24 | | - public static List<String> tag_groups; |
| 20 | + public static List<String> admin_groups; |
| 21 | + public static List<String> channel_groups; |
| 22 | + public static List<String> property_groups; |
| 23 | + public static List<String> tag_groups; |
25 | 24 |
|
26 | | - @Value("${admin-groups:cf-admins}") |
27 | | - void initializeAdminRoles(String groups) { |
28 | | - AuthorizationService.admin_groups = Arrays.asList(groups.split(",")).stream().map(g -> |
29 | | - "ROLE_" + g.trim().toUpperCase() |
30 | | - ).collect(Collectors.toList()); |
31 | | - } |
32 | | - @Value("${channel-groups:cf-channels}") |
33 | | - void initializeChannelModRoles(String groups) { |
34 | | - AuthorizationService.channel_groups = Arrays.asList(groups.split(",")).stream().map(g -> |
35 | | - "ROLE_" + g.trim().toUpperCase() |
36 | | - ).collect(Collectors.toList()); |
37 | | - } |
38 | | - @Value("${property-groups:cf-properties}") |
39 | | - void initializePropertyRoles(String groups) { |
40 | | - AuthorizationService.property_groups = Arrays.asList(groups.split(",")).stream().map(g -> |
41 | | - "ROLE_" + g.trim().toUpperCase() |
42 | | - ).collect(Collectors.toList()); |
43 | | - } |
44 | | - @Value("${tag-groups:cf-tags}") |
45 | | - void initializeTagRoles(String groups) { |
46 | | - AuthorizationService.tag_groups = Arrays.asList(groups.split(",")).stream().map(g -> |
47 | | - "ROLE_" + g.trim().toUpperCase() |
48 | | - ).collect(Collectors.toList()); |
49 | | - } |
| 25 | + @Value("${admin-groups:cf-admins}") |
| 26 | + void initializeAdminRoles(String groups) { |
| 27 | + AuthorizationService.admin_groups = |
| 28 | + Arrays.asList(groups.split(",")).stream() |
| 29 | + .map(g -> "ROLE_" + g.trim().toUpperCase()) |
| 30 | + .collect(Collectors.toList()); |
| 31 | + } |
50 | 32 |
|
51 | | - public enum ROLES { |
52 | | - CF_ADMIN(admin_groups), |
53 | | - CF_CHANNEL(channel_groups), |
54 | | - CF_PROPERTY(property_groups), |
55 | | - CF_TAG(tag_groups); |
| 33 | + @Value("${channel-groups:cf-channels}") |
| 34 | + void initializeChannelModRoles(String groups) { |
| 35 | + AuthorizationService.channel_groups = |
| 36 | + Arrays.asList(groups.split(",")).stream() |
| 37 | + .map(g -> "ROLE_" + g.trim().toUpperCase()) |
| 38 | + .collect(Collectors.toList()); |
| 39 | + } |
56 | 40 |
|
57 | | - private final List<String> groups; |
| 41 | + @Value("${property-groups:cf-properties}") |
| 42 | + void initializePropertyRoles(String groups) { |
| 43 | + AuthorizationService.property_groups = |
| 44 | + Arrays.asList(groups.split(",")).stream() |
| 45 | + .map(g -> "ROLE_" + g.trim().toUpperCase()) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + } |
58 | 48 |
|
59 | | - private ROLES(List<String> groups) { |
60 | | - this.groups = groups; |
61 | | - } |
62 | | - } |
| 49 | + @Value("${tag-groups:cf-tags}") |
| 50 | + void initializeTagRoles(String groups) { |
| 51 | + AuthorizationService.tag_groups = |
| 52 | + Arrays.asList(groups.split(",")).stream() |
| 53 | + .map(g -> "ROLE_" + g.trim().toUpperCase()) |
| 54 | + .collect(Collectors.toList()); |
| 55 | + } |
63 | 56 |
|
64 | | - public boolean isAuthorizedOwner(Authentication authentication, Tag data) { |
65 | | - ArrayList<String> auth = new ArrayList<>(); |
66 | | - Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
67 | | - for(GrantedAuthority a: auths) |
68 | | - auth.add(a.getAuthority()); |
69 | | - |
70 | | - if(!Collections.disjoint(auth,ROLES.CF_ADMIN.groups)) |
71 | | - return true; |
72 | | - if(authentication.getName().equals(data.getOwner()) || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) |
73 | | - return true; |
74 | | - return false; |
75 | | - } |
| 57 | + public enum ROLES { |
| 58 | + CF_ADMIN(admin_groups), |
| 59 | + CF_CHANNEL(channel_groups), |
| 60 | + CF_PROPERTY(property_groups), |
| 61 | + CF_TAG(tag_groups); |
76 | 62 |
|
77 | | - public boolean isAuthorizedOwner(Authentication authentication, Property data) { |
78 | | - ArrayList<String> auth = new ArrayList<>(); |
79 | | - Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
80 | | - for(GrantedAuthority a: auths) |
81 | | - auth.add(a.getAuthority()); |
82 | | - |
83 | | - if(!Collections.disjoint(auth,ROLES.CF_ADMIN.groups)) |
84 | | - return true; |
85 | | - if(authentication.getName().equals(data.getOwner()) || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) |
86 | | - return true; |
87 | | - return false; |
88 | | - } |
| 63 | + private final List<String> groups; |
89 | 64 |
|
90 | | - public boolean isAuthorizedOwner(Authentication authentication, Channel data) { |
91 | | - ArrayList<String> auth = new ArrayList<>(); |
92 | | - Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
93 | | - for(GrantedAuthority a: auths) |
94 | | - auth.add(a.getAuthority()); |
95 | | - |
96 | | - if(!Collections.disjoint(auth,ROLES.CF_ADMIN.groups)) |
97 | | - return true; |
98 | | - if(authentication.getName().equals(data.getOwner()) || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) |
99 | | - return true; |
100 | | - return false; |
| 65 | + private ROLES(List<String> groups) { |
| 66 | + this.groups = groups; |
101 | 67 | } |
| 68 | + } |
102 | 69 |
|
103 | | - public boolean isAuthorizedRole(Authentication authentication, ROLES expectedRole) { |
104 | | - ArrayList<String> auth = new ArrayList<>(); |
105 | | - Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
106 | | - for(GrantedAuthority a: auths) |
107 | | - auth.add(a.getAuthority()); |
108 | | - |
109 | | - if(!Collections.disjoint(auth,ROLES.CF_ADMIN.groups)) |
110 | | - return true; |
111 | | - else if(!Collections.disjoint(auth,ROLES.CF_CHANNEL.groups) && expectedRole != ROLES.CF_ADMIN) |
112 | | - return true; |
113 | | - else if(!Collections.disjoint(auth,ROLES.CF_PROPERTY.groups) && (expectedRole == ROLES.CF_PROPERTY || expectedRole == ROLES.CF_TAG)) |
114 | | - return true; |
115 | | - else if(!Collections.disjoint(auth,ROLES.CF_TAG.groups) && expectedRole == ROLES.CF_TAG) |
116 | | - return true; |
117 | | - return false; |
118 | | - } |
| 70 | + public boolean isAuthorizedOwner(Authentication authentication, Tag data) { |
| 71 | + ArrayList<String> auth = new ArrayList<>(); |
| 72 | + Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
| 73 | + for (GrantedAuthority a : auths) auth.add(a.getAuthority()); |
| 74 | + |
| 75 | + if (!Collections.disjoint(auth, ROLES.CF_ADMIN.groups)) return true; |
| 76 | + if (authentication.getName().equals(data.getOwner()) |
| 77 | + || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) return true; |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + public boolean isAuthorizedOwner(Authentication authentication, Property data) { |
| 82 | + ArrayList<String> auth = new ArrayList<>(); |
| 83 | + Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
| 84 | + for (GrantedAuthority a : auths) auth.add(a.getAuthority()); |
| 85 | + |
| 86 | + if (!Collections.disjoint(auth, ROLES.CF_ADMIN.groups)) return true; |
| 87 | + if (authentication.getName().equals(data.getOwner()) |
| 88 | + || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) return true; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + public boolean isAuthorizedOwner(Authentication authentication, Channel data) { |
| 93 | + ArrayList<String> auth = new ArrayList<>(); |
| 94 | + Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
| 95 | + for (GrantedAuthority a : auths) auth.add(a.getAuthority()); |
| 96 | + |
| 97 | + if (!Collections.disjoint(auth, ROLES.CF_ADMIN.groups)) return true; |
| 98 | + if (authentication.getName().equals(data.getOwner()) |
| 99 | + || auth.contains("ROLE_" + data.getOwner().trim().toUpperCase())) return true; |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + public boolean isAuthorizedRole(Authentication authentication, ROLES expectedRole) { |
| 104 | + ArrayList<String> auth = new ArrayList<>(); |
| 105 | + Collection<? extends GrantedAuthority> auths = authentication.getAuthorities(); |
| 106 | + for (GrantedAuthority a : auths) auth.add(a.getAuthority()); |
| 107 | + |
| 108 | + if (!Collections.disjoint(auth, ROLES.CF_ADMIN.groups)) return true; |
| 109 | + else if (!Collections.disjoint(auth, ROLES.CF_CHANNEL.groups) && expectedRole != ROLES.CF_ADMIN) |
| 110 | + return true; |
| 111 | + else if (!Collections.disjoint(auth, ROLES.CF_PROPERTY.groups) |
| 112 | + && (expectedRole == ROLES.CF_PROPERTY || expectedRole == ROLES.CF_TAG)) return true; |
| 113 | + else if (!Collections.disjoint(auth, ROLES.CF_TAG.groups) && expectedRole == ROLES.CF_TAG) |
| 114 | + return true; |
| 115 | + return false; |
| 116 | + } |
119 | 117 | } |
0 commit comments