001/* 002 * Copyright 2017 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.plugins.messagingconnector; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.parameters.Parameters; 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.acting.ServiceableAction; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Redirector; 027import org.apache.cocoon.environment.Request; 028import org.apache.cocoon.environment.SourceResolver; 029import org.apache.commons.lang3.StringUtils; 030 031import org.ametys.core.cocoon.ActionResultGenerator; 032import org.ametys.core.cocoon.JSonReader; 033import org.ametys.core.user.CurrentUserProvider; 034import org.ametys.core.user.UserIdentity; 035import org.ametys.core.userpref.UserPreferencesException; 036import org.ametys.runtime.i18n.I18nizableText; 037 038/** 039 * 040 * This class is the action used by the messaging connector plugin. 041 * It uses the correct mail server component. 042 * 043 */ 044public class SetPasswordAction extends ServiceableAction 045{ 046 /** The password in the request */ 047 private static final String PASSWORD_FIELD = "password"; 048 049 /** The current user provider */ 050 protected CurrentUserProvider _currentUserProvider; 051 052 private MessagingConnector _messagingConnector; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 058 _messagingConnector = (MessagingConnector) smanager.lookup(MessagingConnector.ROLE); 059 } 060 061 @Override 062 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws UserPreferencesException, MessagingConnectorException 063 { 064 Map<String, Object> result = new HashMap<>(); 065 Request request = ObjectModelHelper.getRequest(objectModel); 066 067 UserIdentity identity = _currentUserProvider.getUser(); 068 if (identity != null) 069 { 070 String password = request.getParameter(PASSWORD_FIELD); 071 if (!StringUtils.isBlank(password)) 072 { 073 _messagingConnector.setUserPassword(identity, password); 074 result.put("success", true); 075 } 076 else 077 { 078 result.put("success", false); 079 result.put("error", "password-empty"); 080 I18nizableText message = new I18nizableText("plugin.messaging-connector", "PLUGINS_MESSAGINGCONNECTOR_ERROR_INPUT_PASSWORD_EMPTY"); 081 result.put("message", message); 082 } 083 } 084 085 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 086 request.setAttribute(ActionResultGenerator.MAP_REQUEST_ATTR, result); 087 return EMPTY_MAP; 088 } 089 090}