BrightSide Workbench Full Report + Source Code
elephant-www/src/main/java/org/turro/auth/SignInControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.auth;
20 
21 import java.io.IOException;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.action.Actions;
26 import org.turro.collections.KeyValueMap;
27 import org.turro.elephant.TemplateControl;
28 import org.turro.elephant.context.Application;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.IConstructor;
31 import org.turro.elephant.security.IUser;
32 import org.turro.elephant.web.ElContext;
33 import org.turro.elephant.zkoss.ZkossElephant;
34 import org.turro.external.Authentications;
35 import org.turro.i18n.I_;
36 import org.turro.sso.SSO;
37 import org.turro.zkoss.input.GenericCombobox;
38 import org.zkoss.zk.ui.Executions;
39 import org.zkoss.zk.ui.event.Event;
40 import org.zkoss.zk.ui.select.annotation.Listen;
41 import org.zkoss.zk.ui.select.annotation.Wire;
42 import org.zkoss.zk.ui.util.Clients;
43 import org.zkoss.zul.A;
44 import org.zkoss.zul.Checkbox;
45 import org.zkoss.zul.Div;
46 import org.zkoss.zul.Listbox;
47 import org.zkoss.zul.Listitem;
48 import org.zkoss.zul.Textbox;
49 
54 public class SignInControl extends TemplateControl {
55 
56  private String redir;
57 
58  @Wire("#signinform")
59  private Div siginform;
60 
61  @Wire("#signupform")
62  private Div signupform;
63 
64  @Wire("#procedencefield")
65  private Div procedencefield;
66 
67  @Wire("#user")
68  private Textbox user;
69 
70  @Wire("#procedence")
71  private GenericCombobox procedence;
72 
73  @Wire("#password")
74  private Textbox password;
75 
76  @Wire("#storepass")
77  private Checkbox storepass;
78 
79  @Wire("#signoutform")
80  private Div sigoutform;
81 
82  @Wire("#wadmin")
83  private Checkbox wadmin;
84 
85  @Wire("#langs")
86  private Div langs;
87 
88  @Wire("#lang")
89  private Listbox lang;
90 
91  @Wire("#signout")
92  private A signout;
93 
94  @Wire("#signup")
95  private A signup;
96 
97  @Listen("onCheck = #wadmin")
98  public void webAdminister() throws IOException {
99  org.turro.elephant.zkoss.ZkossElephant.doWebAdmin(wadmin.isChecked(), redir);
100  }
101 
102  @Listen("onOK = #signinform; onClick = #signin")
103  public void onSigIn() {
104  if(Authentication.canLogin(user.getText(), 30)) {
105  if(storepass.isChecked()) {
106  ZkossElephant.setCookie("xp_storepass", "true", "/", 365*24*60*60);
107  ZkossElephant.setCookie("xp_login", user.getText(), "/", 365*24*60*60);
108  if(SSO.hasSSO()) {
109  // do not use user/pass
110  ZkossElephant.deleteCookie("xp_pass", "/");
111  } else {
112  ZkossElephant.encryptCookie("xp_pass", password.getText(), "/", 365*24*60*60);
113  ZkossElephant.setCookie("xp_remember", "true", "/", 365*24*60*60);
114  }
115  } else {
116  ZkossElephant.deleteCookie("xp_pass", "/");
117  ZkossElephant.deleteCookie("xp_storepass", "/");
118  ZkossElephant.deleteCookie("xp_login", "/");
119  ZkossElephant.deleteCookie("xp_remember", "/");
120  }
121  try {
122  Authentication.doLogin(user.getText(), password.getText(), redir,
123  procedence == null ? null : procedence.getObjectValue());
124  } catch (IOException ex) {
125  Logger.getLogger(SignInControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
126  }
127  } else {
128  Clients.showNotification(I_.format("To many attempts, wait %d minutes before trying again.", 30));
129  }
130  }
131 
132  @Listen("onClick = #remind")
133  public void onRemind(Event event) throws IOException {
134  Application.getApplication().sendRedirect("/user/reminder");
135  }
136 
137  @Listen("onClick = #signout")
138  public void onSigOut() {
139  try {
140  Authentication.doLogout(redir);
141  } catch (IOException ex) {
142  Logger.getLogger(SignInControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
143  }
144  }
145 
146  @Listen("onSelect = #lang")
147  public void onLang(Event event) {
148  String sel = lang.getSelectedItem().getValue();
149  if(sel == null) {
150  ZkossElephant.deleteCookie("lang", "/");
151  } else {
152  ZkossElephant.setCookie("lang", sel, "/", 365*24*60*60);
153  }
154  Executions.getCurrent().sendRedirect("");
155  }
156 
157  @Override
158  public void doFinally() {
159  IUser userIn = Application.getUser();
160  redir = Executions.getCurrent().getParameter("redir");
161  if(Strings.isBlank(redir)) {
163  }
164  if(SSO.hasSSO()) {
165  storepass.setLabel(I_.get("Keep me signed"));
166  } else {
167  storepass.setLabel(I_.get("Remember me"));
168  }
169  if(userIn == null) {
170  siginform.setVisible(true);
171  if("true".equals(Application.getCookieValue("xp_storepass"))) {
172  storepass.setChecked(true);
173  if(SSO.hasSSO()) {
174  // do not restore user/pass
175  Application.deleteCookie("xp_pass", "/");
176  } else {
177  String xppass = Application.decryptCookieValue("xp_pass");
178  if(!Strings.isBlank(xppass)) {
179  password.setValue(xppass);
180  }
181  }
183  if(map != null && map.containsKey("email")) {
184  user.setValue(map.get("email"));
185  } else {
186  user.setValue(Application.getCookieValue("xp_login"));
187  }
188  }
189  /* Procedence */
190  procedencefield.setVisible(Authentications.requiresProcedence());
191  } else {
192  sigoutform.setVisible(true);
193  langs.setVisible(false);
195  Boolean isInternalSignIn = (Boolean) cons.getSessionAttribute(IUser.INTERNAL_SIGNIN);
196  if(isInternalSignIn != null && isInternalSignIn) {
197  //personaout.setVisible(false);
198  } else {
199  signout.setVisible(false);
200  }
201  if(cons.canWebAdminister()) {
202  wadmin.setVisible(true);
203  wadmin.setChecked(cons.isWebAdministering());
204  } else {
205  wadmin.setVisible(false);
206  }
207  }
208  String[] langs = ElephantContext.getSiteLocales().split(" *, *");
209  String currentLang = Application.getCookieValue("lang");
210  lang.appendItem(I_.get("Browser's default"), null);
211  for(String l : langs) {
212  Listitem li = lang.appendItem(I_.byKey(l), l);
213  if(l.equals(currentLang)) {
214  li.setSelected(true);
215  }
216  }
217  signup.setVisible(ElephantContext.hasSignup());
218  signupform.setVisible(userIn == null && signup.isVisible());
219  }
220 
221  public String getAreYouNewStr() {
222  return I_.format("Are you new in %s?", ElephantContext.getSiteName());
223  }
224 
225 }
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
static boolean canLogin(String login, int minutes)
static void doLogout(String redir)
static void doLogin(String login, String pass, String redir, Object extra)
static String decryptCookieValue(String name)
static String getCookieValue(String name)
abstract void sendRedirect(String uri)
static void encryptCookie(String name, String value, String path, int age)
static void deleteCookie(String name, String path)
static void setCookie(String name, String value, String path, int age)
abstract HttpServletRequest getHttpServletRequest()
static String extractPath(String path)
Definition: ElContext.java:574
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
static boolean hasSSO()
Definition: SSO.java:34
static final String INTERNAL_SIGNIN
Definition: IUser.java:33