001/* 002 * Copyright 2015 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.clientsideelement; 017 018import java.io.IOException; 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.HashSet; 022import java.util.List; 023import java.util.Map; 024import java.util.Set; 025 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.commons.lang.StringUtils; 029 030import org.ametys.core.group.Group; 031import org.ametys.core.group.GroupIdentity; 032import org.ametys.core.group.GroupManager; 033import org.ametys.core.ui.Callable; 034import org.ametys.core.ui.ClientSideElement; 035import org.ametys.core.ui.mail.StandardMailBodyHelper; 036import org.ametys.core.ui.mail.StandardMailBodyHelper.MailBodyBuilder; 037import org.ametys.core.user.User; 038import org.ametys.core.user.UserIdentity; 039import org.ametys.core.user.UserManager; 040import org.ametys.core.util.I18nUtils; 041import org.ametys.core.util.mail.SendMailHelper; 042import org.ametys.plugins.core.user.UserHelper; 043import org.ametys.runtime.config.Config; 044import org.ametys.runtime.i18n.I18nizableText; 045import org.ametys.runtime.i18n.I18nizableTextParameter; 046import org.ametys.web.repository.page.Page; 047import org.ametys.web.repository.site.Site; 048 049import jakarta.mail.MessagingException; 050 051/** 052 * This {@link ClientSideElement} creates a button that will be enabled if page and its hierarchy is valid. 053 * 054 */ 055public class LivePageClientSideElement extends AbstractPageClientSideElement 056{ 057 /** The {@link UserManager} */ 058 private UserManager _userManager; 059 060 /** The {@link UserManager} for front-end users */ 061 private UserManager _foUsersManager; 062 063 /** The {@link GroupManager} for front-end groups */ 064 private GroupManager _foGroupsManager; 065 066 /** The {@link I18nUtils} */ 067 private I18nUtils _i18nUtils; 068 069 private UserHelper _userHelper; 070 071 @Override 072 public void service(ServiceManager smanager) throws ServiceException 073 { 074 super.service(smanager); 075 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 076 _foUsersManager = (UserManager) smanager.lookup(UserManager.ROLE); 077 _foGroupsManager = (GroupManager) smanager.lookup(GroupManager.ROLE); 078 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 079 _userHelper = (UserHelper) smanager.lookup(UserHelper.ROLE); 080 } 081 082 /** 083 * Send a notification for online pages. 084 * @param pageIds the ids of the pages 085 * @param users the users that will be notified 086 * @param groups the groups that will be notified 087 * @param comment the comment of the notifier 088 * @return true 089 */ 090 @Callable 091 public boolean sendOnlineNotification(List<String> pageIds, List<Map<String, String>> users, List<Map<String, String>> groups, String comment) 092 { 093 UserIdentity userIdentity = _currentUserProvider.getUser(); 094 User notifier = _userManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 095 096 List<UserIdentity> userIdentities = new ArrayList<>(); 097 for (Map<String, String> user : users) 098 { 099 UserIdentity ui = _userHelper.json2userIdentity(user); 100 if (ui != null) 101 { 102 userIdentities.add(ui); 103 } 104 } 105 List<GroupIdentity> groupIdentities = new ArrayList<>(); 106 for (Map<String, String> group : groups) 107 { 108 String id = group.get("groupId"); 109 String groupDirectory = group.get("groupDirectory"); 110 groupIdentities.add(new GroupIdentity(id, groupDirectory)); 111 } 112 Set<User> distinctUsers = _getDistinctUsers (userIdentities, groupIdentities); 113 114 for (String pageId : pageIds) 115 { 116 Page page = _resolver.resolveById(pageId); 117 118 String from = _getSender(notifier, page); 119 String subject = _getSubject (notifier, page); 120 121 for (User user : distinctUsers) 122 { 123 if (StringUtils.isNotEmpty(user.getEmail())) 124 { 125 try 126 { 127 String body = _getBody(notifier, user, page, comment); 128 129 SendMailHelper.newMail() 130 .withSubject(subject) 131 .withHTMLBody(body) 132 .withSender(from) 133 .withRecipient(user.getEmail()) 134 .sendMail(); 135 } 136 catch (MessagingException | IOException e) 137 { 138 getLogger().error("Unable to send mail to user " + user.getEmail(), e); 139 } 140 } 141 } 142 } 143 144 return true; 145 } 146 147 /** 148 * Get the email sender 149 * @param sender The user responsible for the action 150 * @param page The published page 151 * @return The sender 152 */ 153 private String _getSender (User sender, Page page) 154 { 155 if (sender != null && sender.getEmail() != null) 156 { 157 return sender.getFullName() + " <" + sender.getEmail() + ">"; 158 } 159 160 Site site = page.getSite(); 161 String defaultFromValue = Config.getInstance().getValue("smtp.mail.from"); 162 return site.getValue("site-mail-from", false, defaultFromValue); 163 } 164 165 /** 166 * Get the email subject 167 * @param sender The user responsible for the notification 168 * @param page The published page 169 * @return The email subject 170 */ 171 private String _getSubject (User sender, Page page) 172 { 173 Site site = page.getSite(); 174 175 Map<String, I18nizableTextParameter> subjectI18nParameters = new HashMap<>(); 176 subjectI18nParameters.put("user", new I18nizableText(sender.getFullName())); 177 subjectI18nParameters.put("siteTitle", new I18nizableText(site.getTitle())); 178 subjectI18nParameters.put("pageTitle", new I18nizableText(page.getTitle())); 179 180 I18nizableText msg = new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_SUBJECT", subjectI18nParameters); 181 182 return _i18nUtils.translate(msg, page.getSitemapName()); 183 } 184 185 /** 186 * Get the email body 187 * @param sender The user responsible for the notification 188 * @param user The recipient of the notification 189 * @param page The published page 190 * @param comment The user's comment. Can be empty or null. 191 * @return The email body 192 * @throws IOException if faile dto build HTML mail body 193 */ 194 private String _getBody (User sender, User user, Page page, String comment) throws IOException 195 { 196 Site site = page.getSite(); 197 String pageUrl = site.getUrl() + "/" + page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html"; 198 199 Map<String, I18nizableTextParameter> i18nParams = new HashMap<>(); 200 i18nParams.put("sender", new I18nizableText(sender.getFullName())); 201 i18nParams.put("user", new I18nizableText(user.getFullName())); 202 i18nParams.put("siteTitle", new I18nizableText(site.getTitle())); 203 i18nParams.put("siteUrl", new I18nizableText(site.getUrl())); 204 i18nParams.put("pageTitle", new I18nizableText(page.getTitle())); 205 i18nParams.put("pageUrl", new I18nizableText(pageUrl)); 206 207 MailBodyBuilder bodyBuilder = StandardMailBodyHelper.newHTMLBody() 208 .withTitle(new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY_TITLE", i18nParams)) 209 .addMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY", i18nParams)) 210 .addMessage(new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY_LINK_TEXT", i18nParams)) 211 .withLink(pageUrl, new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY_LINK_BUTTON")); 212 213 if (StringUtils.isNotEmpty(comment)) 214 { 215 bodyBuilder.withDetails(new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY_COMMENT"), comment, false); 216 } 217 218 return bodyBuilder.build(); 219 } 220 221 private Set<User> _getDistinctUsers (List<UserIdentity> userIdentities, List<GroupIdentity> groupIdentities) 222 { 223 Set<User> users = new HashSet<>(); 224 225 for (UserIdentity userIdentity : userIdentities) 226 { 227 if (userIdentity != null && StringUtils.isNotEmpty(userIdentity.getLogin()) && StringUtils.isNotEmpty(userIdentity.getPopulationId())) 228 { 229 User user = _foUsersManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 230 if (user != null) 231 { 232 users.add(user); 233 } 234 } 235 } 236 237 for (GroupIdentity groupIdentity : groupIdentities) 238 { 239 if (groupIdentity != null && StringUtils.isNotEmpty(groupIdentity.getId()) && StringUtils.isNotEmpty(groupIdentity.getDirectoryId())) 240 { 241 Group group = _foGroupsManager.getGroup(groupIdentity.getDirectoryId(), groupIdentity.getId()); 242 if (group != null) 243 { 244 Set<UserIdentity> groupUsers = group.getUsers(); 245 for (UserIdentity userIdentity : groupUsers) 246 { 247 User user = _foUsersManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 248 if (user != null) 249 { 250 users.add(user); 251 } 252 } 253 } 254 } 255 } 256 257 return users; 258 } 259}