001/*
002 * (C) Copyright 2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017package org.nuxeo.ecm.platform.ui.web.auth.service;
018
019import java.io.Serializable;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.nuxeo.common.xmap.XMap;
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeList;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * {@link XMap} object to manage configuration of the login screen (login.jsp)
031 *
032 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
033 * @since 5.7
034 */
035@XObject("loginScreenConfig")
036public class LoginScreenConfig implements Serializable {
037
038    private static final long serialVersionUID = 1L;
039
040    @XNodeList(value = "loginProviders/loginProvider", type = ArrayList.class, componentType = LoginProviderLink.class)
041    protected List<LoginProviderLink> providers;
042
043    protected String headerStyle;
044
045    protected String footerStyle;
046
047    @XNode("newsIframeUrl")
048    protected String newsIframeUrl = "//www.nuxeo.com/standalone-login-page/";
049
050    protected String bodyBackgroundStyle;
051
052    protected String loginBoxBackgroundStyle;
053
054    @XNode("loginBoxWidth")
055    protected String loginBoxWidth;
056
057    protected String logoUrl;
058
059    @XNode("logoAlt")
060    protected String logoAlt;
061
062    @XNode("logoWidth")
063    protected String logoWidth;
064
065    @XNode("logoHeight")
066    protected String logoHeight;
067
068    /**
069     * Boolean to disable background-cover CSS behavior on login page background, as it may not be compliant with all
070     * browsers (see NXP-12972/NXP-12978).
071     *
072     * @since 5.8
073     */
074    @XNode("disableBackgroundSizeCover")
075    protected Boolean disableBackgroundSizeCover;
076
077    public LoginScreenConfig() {
078    }
079
080    public List<LoginProviderLink> getProviders() {
081        return providers;
082    }
083
084    public void setProviders(List<LoginProviderLink> providers) {
085        this.providers = providers;
086    }
087
088    public LoginProviderLink getProvider(String name) {
089        if (getProviders() == null) {
090            return null;
091        }
092        for (LoginProviderLink provider : getProviders()) {
093            if (name.equals(provider.getName())) {
094                return provider;
095            }
096        }
097        return null;
098    }
099
100    protected void merge(LoginScreenConfig newConfig) {
101        if (newConfig.newsIframeUrl != null) {
102            this.newsIframeUrl = newConfig.newsIframeUrl;
103        }
104        if (newConfig.headerStyle != null) {
105            this.headerStyle = newConfig.headerStyle;
106        }
107        if (newConfig.footerStyle != null) {
108            this.footerStyle = newConfig.footerStyle;
109        }
110        if (newConfig.bodyBackgroundStyle != null) {
111            this.bodyBackgroundStyle = newConfig.bodyBackgroundStyle;
112        }
113        if (newConfig.loginBoxBackgroundStyle != null) {
114            this.loginBoxBackgroundStyle = newConfig.loginBoxBackgroundStyle;
115        }
116        if (newConfig.loginBoxWidth != null) {
117            this.loginBoxWidth = newConfig.loginBoxWidth;
118        }
119        if (newConfig.disableBackgroundSizeCover != null) {
120            this.disableBackgroundSizeCover = newConfig.disableBackgroundSizeCover;
121        }
122        if (newConfig.logoAlt != null) {
123            this.logoAlt = newConfig.logoAlt;
124        }
125        if (newConfig.logoHeight != null) {
126            this.logoHeight = newConfig.logoHeight;
127        }
128        if (newConfig.logoUrl != null) {
129            this.logoUrl = newConfig.logoUrl;
130        }
131        if (newConfig.logoWidth != null) {
132            this.logoWidth = newConfig.logoWidth;
133        }
134
135        if (providers == null) {
136            providers = newConfig.providers;
137        } else if (newConfig.providers != null && newConfig.providers.size() > 0) {
138            for (LoginProviderLink link : newConfig.providers) {
139
140                int idx = providers.indexOf(link);
141                if (idx >= 0) {
142                    if (link.remove) {
143                        providers.remove(idx);
144                    } else {
145                        providers.get(idx).merge(link);
146                    }
147                } else {
148                    providers.add(link);
149                }
150            }
151        }
152    }
153
154    public void registerLoginProvider(String name, String iconUrl, String link, String label, String description,
155            LoginProviderLinkComputer computer) {
156
157        LoginProviderLink newProvider = new LoginProviderLink();
158        newProvider.name = name;
159        newProvider.iconPath = iconUrl;
160        newProvider.link = link;
161        newProvider.label = label;
162        newProvider.description = description;
163        if (computer != null) {
164            newProvider.urlComputer = computer;
165        }
166
167        LoginProviderLink existingProvider = getProvider(name);
168        if (existingProvider != null) {
169            existingProvider.merge(newProvider);
170        } else {
171            if (providers == null) {
172                providers = new ArrayList<LoginProviderLink>();
173            }
174            providers.add(newProvider);
175        }
176    }
177
178    public String getHeaderStyle() {
179        return headerStyle;
180    }
181
182    public String getFooterStyle() {
183        return footerStyle;
184    }
185
186    public String getBodyBackgroundStyle() {
187        return bodyBackgroundStyle;
188    }
189
190    public String getLoginBoxBackgroundStyle() {
191        return loginBoxBackgroundStyle;
192    }
193
194    public String getLoginBoxWidth() {
195        return loginBoxWidth;
196    }
197
198    public String getLogoUrl() {
199        return logoUrl;
200    }
201
202    public String getLogoAlt() {
203        return logoAlt;
204    }
205
206    public String getLogoWidth() {
207        return logoWidth;
208    }
209
210    public String getLogoHeight() {
211        return logoHeight;
212    }
213
214    public boolean getDisplayNews() {
215        if (newsIframeUrl == null || newsIframeUrl.isEmpty()) {
216            return false;
217        }
218        return true;
219    }
220
221    @XNode("headerStyle")
222    public void setHeaderStyle(String headerStyle) {
223        this.headerStyle = Framework.expandVars(headerStyle);
224    }
225
226    @XNode("footerStyle")
227    public void setFooterStyle(String footerStyle) {
228        this.footerStyle = Framework.expandVars(footerStyle);
229    }
230
231    @XNode("bodyBackgroundStyle")
232    public void setBodyBackgroundStyle(String bodyBackgroundStyle) {
233        this.bodyBackgroundStyle = Framework.expandVars(bodyBackgroundStyle);
234    }
235
236    @XNode("loginBoxBackgroundStyle")
237    public void setLoginBoxBackgroundStyle(String loginBoxBackgroundStyle) {
238        this.loginBoxBackgroundStyle = Framework.expandVars(loginBoxBackgroundStyle);
239    }
240
241    @XNode("logoUrl")
242    public void setLogoUrl(String logoUrl) {
243        this.logoUrl = Framework.expandVars(logoUrl);
244    }
245
246    public String getNewsIframeUrl() {
247        return newsIframeUrl;
248    }
249
250    /**
251     * @since 5.8
252     * @see #disableBackgroundSizeCover
253     */
254    public Boolean getDisableBackgroundSizeCover() {
255        return disableBackgroundSizeCover;
256    }
257
258}