d1bb0edd3d17fee2b0e51fd2747af9cc8bef3da0
[Sone.git] / src / main / java / net / pterodactylus / sone / web / LoginPage.java
1 /*
2  * Sone - LoginPage.java - Copyright © 2010–2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.logging.Logger;
24
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
27 import net.pterodactylus.sone.web.page.FreenetRequest;
28 import net.pterodactylus.util.logging.Logging;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.web.Method;
32
33 import freenet.clients.http.ToadletContext;
34
35 import com.google.common.base.Optional;
36
37 /**
38  * The login page manages logging the user in.
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class LoginPage extends SoneTemplatePage {
43
44         /** The logger. */
45         @SuppressWarnings("unused")
46         private static final Logger logger = Logging.getLogger(LoginPage.class);
47
48         /**
49          * Creates a new login page.
50          *
51          * @param template
52          *            The template to render
53          * @param webInterface
54          *            The Sone web interface
55          */
56         public LoginPage(Template template, WebInterface webInterface) {
57                 super("login.html", template, "Page.Login.Title", webInterface, false);
58         }
59
60         //
61         // TEMPLATEPAGE METHODS
62         //
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException {
69                 super.processTemplate(request, templateContext);
70                 /* get all own identities. */
71                 List<Sone> localSones = new ArrayList<Sone>(webInterface.getCore().getLocalSones());
72                 Collections.sort(localSones, Sone.NICE_NAME_COMPARATOR);
73                 templateContext.set("sones", localSones);
74                 if (request.getMethod() == Method.POST) {
75                         String soneId = request.getHttpRequest().getPartAsStringFailsafe("sone-id", 100);
76                         Optional<Sone> selectedSone = webInterface.getCore().getLocalSone(soneId);
77                         if (selectedSone.isPresent()) {
78                                 setCurrentSone(request.getToadletContext(), selectedSone.get());
79                                 String target = request.getHttpRequest().getParam("target");
80                                 if ((target == null) || (target.length() == 0)) {
81                                         target = "index.html";
82                                 }
83                                 throw new RedirectException(target);
84                         }
85                 }
86                 List<OwnIdentity> ownIdentitiesWithoutSone = CreateSonePage.getOwnIdentitiesWithoutSone(webInterface.getCore());
87                 templateContext.set("identitiesWithoutSone", ownIdentitiesWithoutSone);
88         }
89
90         /**
91          * {@inheritDoc}
92          */
93         @Override
94         protected String getRedirectTarget(FreenetRequest request) {
95                 if (getCurrentSone(request.getToadletContext(), false) != null) {
96                         return "index.html";
97                 }
98                 return null;
99         }
100
101         //
102         // SONETEMPLATEPAGE METHODS
103         //
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public boolean isEnabled(ToadletContext toadletContext) {
110                 if (webInterface.getCore().getPreferences().isRequireFullAccess() && !toadletContext.isAllowedFullAccess()) {
111                         return false;
112                 }
113                 return getCurrentSone(toadletContext, false) == null;
114         }
115
116 }