001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.portalsettings.action;
016    
017    import com.liferay.portal.AccountNameException;
018    import com.liferay.portal.AddressCityException;
019    import com.liferay.portal.AddressStreetException;
020    import com.liferay.portal.AddressZipException;
021    import com.liferay.portal.CompanyMxException;
022    import com.liferay.portal.CompanyVirtualHostException;
023    import com.liferay.portal.CompanyWebIdException;
024    import com.liferay.portal.EmailAddressException;
025    import com.liferay.portal.LocaleException;
026    import com.liferay.portal.NoSuchCountryException;
027    import com.liferay.portal.NoSuchListTypeException;
028    import com.liferay.portal.NoSuchRegionException;
029    import com.liferay.portal.PhoneNumberException;
030    import com.liferay.portal.WebsiteURLException;
031    import com.liferay.portal.kernel.servlet.SessionErrors;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.PropertiesParamUtil;
035    import com.liferay.portal.kernel.util.PropsKeys;
036    import com.liferay.portal.kernel.util.UnicodeProperties;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.Address;
039    import com.liferay.portal.model.Company;
040    import com.liferay.portal.model.EmailAddress;
041    import com.liferay.portal.model.Phone;
042    import com.liferay.portal.model.Website;
043    import com.liferay.portal.security.auth.PrincipalException;
044    import com.liferay.portal.service.CompanyServiceUtil;
045    import com.liferay.portal.struts.PortletAction;
046    import com.liferay.portal.util.PortalUtil;
047    import com.liferay.portal.util.PropsValues;
048    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
049    
050    import java.util.List;
051    
052    import javax.portlet.ActionRequest;
053    import javax.portlet.ActionResponse;
054    import javax.portlet.PortletConfig;
055    import javax.portlet.RenderRequest;
056    import javax.portlet.RenderResponse;
057    
058    import org.apache.struts.action.ActionForm;
059    import org.apache.struts.action.ActionForward;
060    import org.apache.struts.action.ActionMapping;
061    
062    /**
063     * @author Brian Wing Shun Chan
064     * @author Julio Camarero
065     */
066    public class EditCompanyAction extends PortletAction {
067    
068            @Override
069            public void processAction(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            PortletConfig portletConfig, ActionRequest actionRequest,
072                            ActionResponse actionResponse)
073                    throws Exception {
074    
075                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
076    
077                    try {
078                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
079                                    validateCAS(actionRequest);
080                                    validateLDAP(actionRequest);
081    
082                                    if (!SessionErrors.isEmpty(actionRequest)) {
083                                            setForward(
084                                                    actionRequest, "portlet.portal_settings.edit_company");
085                                    }
086                                    else {
087                                            updateCompany(actionRequest);
088                                            updateDisplay(actionRequest);
089    
090                                            sendRedirect(actionRequest, actionResponse);
091                                    }
092                            }
093                    }
094                    catch (Exception e) {
095                            if (e instanceof PrincipalException) {
096                                    SessionErrors.add(actionRequest, e.getClass());
097    
098                                    setForward(actionRequest, "portlet.portal_settings.error");
099                            }
100                            else if (e instanceof AddressCityException ||
101                                             e instanceof AccountNameException ||
102                                             e instanceof AddressStreetException ||
103                                             e instanceof AddressZipException ||
104                                             e instanceof CompanyMxException ||
105                                             e instanceof CompanyVirtualHostException ||
106                                             e instanceof CompanyWebIdException ||
107                                             e instanceof EmailAddressException ||
108                                             e instanceof LocaleException ||
109                                             e instanceof NoSuchCountryException ||
110                                             e instanceof NoSuchListTypeException ||
111                                             e instanceof NoSuchRegionException ||
112                                             e instanceof PhoneNumberException ||
113                                             e instanceof WebsiteURLException) {
114    
115                                    if (e instanceof NoSuchListTypeException) {
116                                            NoSuchListTypeException nslte = (NoSuchListTypeException)e;
117    
118                                            SessionErrors.add(
119                                                    actionRequest,
120                                                    e.getClass().getName() + nslte.getType());
121                                    }
122                                    else {
123                                            SessionErrors.add(actionRequest, e.getClass(), e);
124                                    }
125    
126                                    setForward(
127                                            actionRequest, "portlet.portal_settings.edit_company");
128                            }
129                            else {
130                                    throw e;
131                            }
132                    }
133            }
134    
135            @Override
136            public ActionForward render(
137                            ActionMapping actionMapping, ActionForm actionForm,
138                            PortletConfig portletConfig, RenderRequest renderRequest,
139                            RenderResponse renderResponse)
140                    throws Exception {
141    
142                    return actionMapping.findForward(
143                            getForward(renderRequest, "portlet.portal_settings.edit_company"));
144            }
145    
146            protected void updateCompany(ActionRequest actionRequest) throws Exception {
147                    long companyId = PortalUtil.getCompanyId(actionRequest);
148    
149                    String virtualHostname = ParamUtil.getString(
150                            actionRequest, "virtualHostname");
151                    String mx = ParamUtil.getString(actionRequest, "mx");
152                    String homeURL = ParamUtil.getString(actionRequest, "homeURL");
153                    String name = ParamUtil.getString(actionRequest, "name");
154                    String legalName = ParamUtil.getString(actionRequest, "legalName");
155                    String legalId = ParamUtil.getString(actionRequest, "legalId");
156                    String legalType = ParamUtil.getString(actionRequest, "legalType");
157                    String sicCode = ParamUtil.getString(actionRequest, "sicCode");
158                    String tickerSymbol = ParamUtil.getString(
159                            actionRequest, "tickerSymbol");
160                    String industry = ParamUtil.getString(actionRequest, "industry");
161                    String type = ParamUtil.getString(actionRequest, "type");
162                    String size = ParamUtil.getString(actionRequest, "size");
163                    String languageId = ParamUtil.getString(actionRequest, "languageId");
164                    String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
165                    List<Address> addresses = UsersAdminUtil.getAddresses(actionRequest);
166                    List<EmailAddress> emailAddresses = UsersAdminUtil.getEmailAddresses(
167                            actionRequest);
168                    List<Phone> phones = UsersAdminUtil.getPhones(actionRequest);
169                    List<Website> websites = UsersAdminUtil.getWebsites(actionRequest);
170                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
171                            actionRequest, "settings--");
172    
173                    CompanyServiceUtil.updateCompany(
174                            companyId, virtualHostname, mx, homeURL, name, legalName, legalId,
175                            legalType, sicCode, tickerSymbol, industry, type, size, languageId,
176                            timeZoneId, addresses, emailAddresses, phones, websites,
177                            properties);
178    
179                    PortalUtil.resetCDNHosts();
180            }
181    
182            protected void updateDisplay(ActionRequest actionRequest) throws Exception {
183                    Company company = PortalUtil.getCompany(actionRequest);
184    
185                    String languageId = ParamUtil.getString(actionRequest, "languageId");
186                    String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
187    
188                    CompanyServiceUtil.updateDisplay(
189                            company.getCompanyId(), languageId, timeZoneId);
190    
191                    boolean siteLogo = ParamUtil.getBoolean(
192                            actionRequest,
193                            "settings--" + PropsKeys.COMPANY_SECURITY_SITE_LOGO + "--");
194    
195                    CompanyServiceUtil.updateSecurity(
196                            company.getCompanyId(), company.getAuthType(),
197                            company.isAutoLogin(), company.isSendPassword(),
198                            company.isStrangers(), company.isStrangersWithMx(),
199                            company.isStrangersVerify(), siteLogo);
200    
201                    boolean deleteLogo = ParamUtil.getBoolean(actionRequest, "deleteLogo");
202    
203                    if (deleteLogo) {
204                            CompanyServiceUtil.deleteLogo(company.getCompanyId());
205                    }
206            }
207    
208            protected void validateCAS(ActionRequest actionRequest) throws Exception {
209                    boolean casEnabled = ParamUtil.getBoolean(
210                            actionRequest, "settings--" + PropsKeys.CAS_AUTH_ENABLED + "--");
211    
212                    if (!casEnabled) {
213                            return;
214                    }
215    
216                    String casLoginURL = ParamUtil.getString(
217                            actionRequest, "settings--" + PropsKeys.CAS_LOGIN_URL + "--");
218                    String casLogoutURL = ParamUtil.getString(
219                            actionRequest, "settings--" + PropsKeys.CAS_LOGOUT_URL + "--");
220                    String casServerName = ParamUtil.getString(
221                            actionRequest, "settings--" + PropsKeys.CAS_SERVER_NAME + "--");
222                    String casServerURL = ParamUtil.getString(
223                            actionRequest, "settings--" + PropsKeys.CAS_SERVER_URL + "--");
224                    String casServiceURL = ParamUtil.getString(
225                            actionRequest, "settings--" + PropsKeys.CAS_SERVICE_URL + "--");
226                    String casNoSuchUserRedirectURL = ParamUtil.getString(
227                            actionRequest, "settings--" +
228                            PropsKeys.CAS_NO_SUCH_USER_REDIRECT_URL + "--");
229    
230                    if (!Validator.isUrl(casLoginURL)) {
231                            SessionErrors.add(actionRequest, "casLoginURLInvalid");
232                    }
233    
234                    if (!Validator.isUrl(casLogoutURL)) {
235                            SessionErrors.add(actionRequest, "casLogoutURLInvalid");
236                    }
237    
238                    if (Validator.isNull(casServerName)) {
239                            SessionErrors.add(actionRequest, "casServerNameInvalid");
240                    }
241    
242                    if (Validator.isNotNull(casServerURL) &&
243                            Validator.isNotNull(casServiceURL)) {
244    
245                            SessionErrors.add(
246                                    actionRequest, "casServerURLAndServiceURLConflict");
247                    }
248                    else if (Validator.isNull(casServerURL) &&
249                                     Validator.isNull(casServiceURL)) {
250    
251                            SessionErrors.add(actionRequest, "casServerURLAndServiceURLNotSet");
252                    }
253                    else {
254                            if (Validator.isNotNull(casServerURL) &&
255                                    !Validator.isUrl(casServerURL)) {
256    
257                                    SessionErrors.add(actionRequest, "casServerURLInvalid");
258                            }
259    
260                            if (Validator.isNotNull(casServiceURL) &&
261                                    !Validator.isUrl(casServiceURL)) {
262    
263                                    SessionErrors.add(actionRequest, "casServiceURLInvalid");
264                            }
265                    }
266    
267                    if (Validator.isNotNull(casNoSuchUserRedirectURL) &&
268                            !Validator.isUrl(casNoSuchUserRedirectURL)) {
269    
270                            SessionErrors.add(actionRequest, "casNoSuchUserURLInvalid");
271                    }
272            }
273    
274            protected void validateLDAP(ActionRequest actionRequest) throws Exception {
275                    if (!PropsValues.LDAP_IMPORT_USER_PASSWORD_AUTOGENERATED) {
276                            return;
277                    }
278    
279                    boolean ldapExportEnabled = ParamUtil.getBoolean(
280                            actionRequest, "settings--" + PropsKeys.LDAP_EXPORT_ENABLED + "--");
281                    boolean ldapImportEnabled = ParamUtil.getBoolean(
282                            actionRequest, "settings--" + PropsKeys.LDAP_IMPORT_ENABLED + "--");
283    
284                    if (ldapExportEnabled && ldapImportEnabled) {
285                            SessionErrors.add(
286                                    actionRequest, "ldapExportAndImportOnPasswordAutogeneration");
287                    }
288            }
289    
290    }