Revision 58476
Added by Konstantina Galouni almost 4 years ago
CommunitySubscribersController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoaadmintools.controllers; |
2 | 2 |
|
3 |
import eu.dnetlib.uoaadmintools.configuration.properties.SecurityConfig; |
|
3 | 4 |
import eu.dnetlib.uoaadmintools.dao.CommunityDAO; |
4 | 5 |
import eu.dnetlib.uoaadmintools.dao.CommunitySubscribersDAO; |
5 | 6 |
import eu.dnetlib.uoaadmintools.dao.SubscriberDAO; |
... | ... | |
7 | 8 |
import eu.dnetlib.uoaadmintools.entities.CommunitySubscribers; |
8 | 9 |
import eu.dnetlib.uoaadmintools.entities.Subscriber; |
9 | 10 |
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException; |
11 |
import eu.dnetlib.uoaadmintools.handlers.utils.AuthorizationUtils; |
|
12 |
import eu.dnetlib.uoaadmintools.handlers.utils.UserInfo; |
|
10 | 13 |
import org.apache.log4j.Logger; |
11 | 14 |
import org.springframework.beans.factory.annotation.Autowired; |
12 | 15 |
import org.springframework.web.bind.annotation.*; |
13 | 16 |
|
14 | 17 |
import java.util.ArrayList; |
18 |
import java.util.Iterator; |
|
15 | 19 |
import java.util.List; |
16 | 20 |
|
17 | 21 |
/** |
... | ... | |
27 | 31 |
@Autowired |
28 | 32 |
CommunityDAO communityDAO; |
29 | 33 |
|
34 |
@Autowired |
|
35 |
private SecurityConfig securityConfig; |
|
36 |
|
|
30 | 37 |
private final Logger log = Logger.getLogger(this.getClass()); |
31 | 38 |
@RequestMapping(value = "/community/subscribers", method = RequestMethod.GET) |
32 | 39 |
public List<CommunitySubscribers> getAllCommunitySubscribers(){ |
... | ... | |
43 | 50 |
} |
44 | 51 |
|
45 | 52 |
} |
53 |
|
|
54 |
@RequestMapping(value = "/community/{pid}/subscribers/count", method = RequestMethod.GET) |
|
55 |
public Integer getNumberOfSubscribersPerCommunity(@PathVariable(value="pid", required = true) String pid) throws ContentNotFoundException { |
|
56 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
57 |
if(communitySubscribers != null){ |
|
58 |
if(communitySubscribers.getSubscribers() != null) { |
|
59 |
return communitySubscribers.getSubscribers().size(); |
|
60 |
} |
|
61 |
}else{ |
|
62 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
63 |
|
|
64 |
} |
|
65 |
return 0; |
|
66 |
} |
|
67 |
|
|
68 |
@RequestMapping(value = "/community/{pid}/is-subscriber", method = RequestMethod.GET) |
|
69 |
public Boolean getIsSubscribedToCommunity(@PathVariable(value="pid", required = true) String pid, |
|
70 |
//@RequestBody String email, |
|
71 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
72 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
73 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
74 |
UserInfo userInfo = helper.getUserInfo(token); |
|
75 |
|
|
76 |
if(userInfo != null) { |
|
77 |
String email = userInfo.getEmail(); |
|
78 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
79 |
if (communitySubscribers != null) { |
|
80 |
if (communitySubscribers.getSubscribers() != null) { |
|
81 |
for (Subscriber subscriber : communitySubscribers.getSubscribers()) { |
|
82 |
if (subscriber.getEmail().equals(email)) { |
|
83 |
return true; |
|
84 |
} |
|
85 |
} |
|
86 |
} |
|
87 |
} else { |
|
88 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
89 |
|
|
90 |
} |
|
91 |
} |
|
92 |
return false; |
|
93 |
} |
|
94 |
|
|
95 |
@RequestMapping(value = "/community/{pid}/subscriber", method = RequestMethod.POST) |
|
96 |
public Boolean addSubscriberInCommunity(@PathVariable(value="pid", required = true) String pid, |
|
97 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
98 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
99 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
100 |
UserInfo userInfo = helper.getUserInfo(token); |
|
101 |
|
|
102 |
if(userInfo != null) { |
|
103 |
String email = userInfo.getEmail(); |
|
104 |
Subscriber subscriber = new Subscriber(email); |
|
105 |
|
|
106 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
107 |
if (communitySubscribers == null) { |
|
108 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
109 |
} |
|
110 |
|
|
111 |
Subscriber savedSubscriber = subscriberDAO.findByEmail(email); |
|
112 |
if (savedSubscriber == null) { |
|
113 |
savedSubscriber = subscriberDAO.save(subscriber); |
|
114 |
} |
|
115 |
for (Subscriber sub : communitySubscribers.getSubscribers()) { |
|
116 |
if (sub.getEmail().equals(subscriber.getEmail())) { |
|
117 |
//already subscribed |
|
118 |
return false; |
|
119 |
} |
|
120 |
} |
|
121 |
//not subscribed yet |
|
122 |
communitySubscribers.getSubscribers().add(savedSubscriber); |
|
123 |
communitySubscriberDAO.save(communitySubscribers); |
|
124 |
return true; |
|
125 |
} |
|
126 |
return false; |
|
127 |
|
|
128 |
} |
|
129 |
@RequestMapping(value = "/community/{pid}/subscriber/delete", method = RequestMethod.POST) |
|
130 |
public Boolean deleteSubscriberFromCommunity(@PathVariable(value="pid", required = true) String pid, |
|
131 |
@RequestHeader("X-XSRF-TOKEN") String token) throws ContentNotFoundException { |
|
132 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
133 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
134 |
UserInfo userInfo = helper.getUserInfo(token); |
|
135 |
|
|
136 |
if(userInfo != null) { |
|
137 |
String email = userInfo.getEmail(); |
|
138 |
|
|
139 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
|
140 |
if (communitySubscribers == null) { |
|
141 |
throw new ContentNotFoundException("Community Subscribers not found"); |
|
142 |
} |
|
143 |
|
|
144 |
Iterator<Subscriber> subscriberIterator = communitySubscribers.getSubscribers().iterator(); |
|
145 |
while(subscriberIterator.hasNext()) { |
|
146 |
Subscriber subscriber = subscriberIterator.next(); |
|
147 |
if(subscriber.getEmail().equals(email)) { |
|
148 |
subscriberIterator.remove(); |
|
149 |
communitySubscriberDAO.save(communitySubscribers); |
|
150 |
return true; |
|
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
return false; |
|
155 |
} |
|
156 |
|
|
46 | 157 |
@RequestMapping(value = "/community/{pid}/subscribers", method = RequestMethod.POST) |
47 |
public CommunitySubscribers addSubscriberInCommunity(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException { |
|
158 |
public CommunitySubscribers addSubscriberInCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody Subscriber subscriber) throws ContentNotFoundException {
|
|
48 | 159 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
49 | 160 |
if(communitySubscribers == null){ |
50 | 161 |
throw new ContentNotFoundException("Community Subscribers not found"); |
... | ... | |
66 | 177 |
|
67 | 178 |
} |
68 | 179 |
@RequestMapping(value = "/community/{pid}/subscribers/delete", method = RequestMethod.POST) |
69 |
public CommunitySubscribers deleteSubscriberFromCommunity(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException { |
|
180 |
public CommunitySubscribers deleteSubscriberFromCommunityByEmail(@PathVariable(value="pid", required = true) String pid, @RequestBody List<String> emails) throws ContentNotFoundException {
|
|
70 | 181 |
CommunitySubscribers communitySubscribers = communitySubscriberDAO.findByPid(pid); |
71 | 182 |
if(communitySubscribers == null){ |
72 | 183 |
throw new ContentNotFoundException("Community Subscribers not found"); |
... | ... | |
82 | 193 |
} |
83 | 194 |
|
84 | 195 |
@RequestMapping(value = "/subscriber/communities", method = RequestMethod.GET) |
85 |
public List<String> getCommunitiesPerSubcriber(@RequestParam(value="email", required = true) String email) { |
|
86 |
List<CommunitySubscribers> communitySubscribers = communitySubscriberDAO.findAll(); |
|
196 |
public List<String> getCommunitiesPerSubcriber(//@RequestParam(value="email", required = true) String email, |
|
197 |
@RequestHeader("X-XSRF-TOKEN") String token) { |
|
198 |
AuthorizationUtils helper = new AuthorizationUtils(); |
|
199 |
helper.setUserInfoUrl(securityConfig.getUserInfoUrl()); |
|
200 |
UserInfo userInfo = helper.getUserInfo(token); |
|
201 |
|
|
87 | 202 |
List<String> list = new ArrayList<>(); |
88 |
for(CommunitySubscribers s:communitySubscribers){ |
|
89 |
for(Subscriber sub:s.getSubscribers()) { |
|
90 |
if (sub.getEmail().equals(email)) { |
|
91 |
list.add(s.getPid()); |
|
92 |
break; |
|
203 |
|
|
204 |
if (userInfo != null) { |
|
205 |
String email = userInfo.getEmail(); |
|
206 |
List<CommunitySubscribers> communitySubscribers = communitySubscriberDAO.findAll(); |
|
207 |
|
|
208 |
for (CommunitySubscribers s : communitySubscribers) { |
|
209 |
for (Subscriber sub : s.getSubscribers()) { |
|
210 |
if (sub.getEmail().equals(email)) { |
|
211 |
list.add(s.getPid()); |
|
212 |
break; |
|
213 |
} |
|
93 | 214 |
} |
94 | 215 |
} |
95 | 216 |
} |
Also available in: Unified diff
[Trunk | Admin Tools Service]:
1. admintools.properties & UoaAdminToolsApplication.java: Uncomment admintools.properties and ignore if @PropertySource for dnet-override.properties not found, to let tests runs.
2. Subscriber.java: Add default constructor and constructor with email parameter.
3. CommunitySubscribersController.java:
a. Autowire SecurityConfig to let methods get UserInfo via token.
b. Add GET method "getNumberOfSubscribersPerCommunity()" (/community/{pid}/subscribers/count).
c. Add GET methos "getIsSubscribedToCommunity()" (/community/{pid}/is-subscriber): Return true/ false and gets user email via token in @RequestHeader("X-XSRF-TOKEN").
d. Method "addSubscriberInCommunity()" renamed to "addSubscriberInCommunityByEmail()" | Method "deleteSubscriberFromCommunity()" renamed to "deleteSubscriberFromCommunityByEmail()".
e. Add POST methods "addSubscriberInCommunity()" (/community/{pid}/subscriber) and "deleteSubscriberFromCommunity()" (/community/{pid}/subscriber/delete):
Return true/ false and get user email via token in @RequestHeader("X-XSRF-TOKEN").
f. Method getCommunitiesPerSubcriber(@RequestParam(value="email", required = true) String email) --> getCommunitiesPerSubcriber(@RequestHeader("X-XSRF-TOKEN") String token):
Get user email via token in @RequestHeader("X-XSRF-TOKEN").
4. update_db.js:
a. Add method "createNewPagesForConnect()": adds page "/publications" for connect (enabled).
b. Add method "createNewPagesForCommunities()": adds pages "/content" (enabled), "/projects" (disabled), "/national-bulletins" (disabled), "/subjects"(disabled) for communities.
c. Add method "removeContentPolicyPageAndAddHelpTextInCommunitiesSourcesPage()": remove old "/content" connect page and move its contents to new "/content" for every community (NOT connect & openaire).