BrightSide Workbench Full Report + Source Code
elephant-zkoss/src/main/java/org/turro/signin/SignInControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 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.signin;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.turro.action.Actions;
27 import org.turro.auth.Authentication;
28 import org.turro.collections.KeyValueMap;
29 import org.turro.command.Command;
30 import org.turro.elephant.TemplateControl;
31 import org.turro.elephant.context.Application;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.elephant.context.IConstructor;
34 import org.turro.elephant.security.IUser;
35 import org.turro.elephant.web.ElContext;
36 import org.turro.elephant.zkoss.Modal;
37 import org.turro.elephant.zkoss.ZkossElephant;
38 import org.turro.i18n.I_;
39 import org.turro.log.SystemLogType;
40 import org.turro.log.SystemLogger;
41 import org.zkoss.zk.ui.Executions;
42 import org.zkoss.zk.ui.event.Event;
43 import org.zkoss.zk.ui.event.InputEvent;
44 import org.zkoss.zk.ui.select.annotation.Listen;
45 import org.zkoss.zk.ui.select.annotation.Wire;
46 import org.zkoss.zk.ui.util.Clients;
47 import org.zkoss.zul.A;
48 import org.zkoss.zul.Checkbox;
49 import org.zkoss.zul.Div;
50 import org.zkoss.zul.Listbox;
51 import org.zkoss.zul.Listitem;
52 import org.zkoss.zul.Textbox;
53 
58 @Deprecated
59 public class SignInControl extends TemplateControl {
60 
61  private String redir;
62 
63  @Wire("#signinform")
64  private Div siginform;
65 
66  @Wire("#user")
67  private Textbox user;
68 
69  @Wire("#password")
70  private Textbox password;
71 
72  @Wire("#storepass")
73  private Checkbox storepass;
74 
75  @Wire("#signoutform")
76  private Div sigoutform;
77 
78  @Wire("#wadmin")
79  private Checkbox wadmin;
80 
81  @Wire("#langs")
82  private Div langs;
83 
84  @Wire("#lang")
85  private Listbox lang;
86 
87 // @Wire("#personain")
88 // private Div personain;
89 //
90 // @Wire("#personaout")
91 // private Div personaout;
92 
93  @Wire("#profile")
94  private A profile;
95 
96  @Wire("#signout")
97  private A signout;
98 
99  @Wire("#signup")
100  private A signup;
101 
102  @Listen("onChanging = #user")
103  public void onChangingUser(InputEvent evt) throws IOException {
104  //personain.setVisible(Strings.isBlank(evt.getValue()));
105  }
106 
107  @Listen("onCheck = #wadmin")
108  public void webAdminister() throws IOException {
109  org.turro.elephant.zkoss.ZkossElephant.doWebAdmin(wadmin.isChecked(), redir);
110  }
111 
112  @Listen("onOK = #signinform; onClick = #signin")
113  public void onSigIn() {
114  if(storepass.isChecked()) {
115  ZkossElephant.encryptCookie("xp_pass", password.getText(), "/", 365*24*60*60);
116  ZkossElephant.setCookie("xp_storepass", "true", "/", 365*24*60*60);
117  ZkossElephant.setCookie("xp_login", user.getText(), "/", 365*24*60*60);
118  ZkossElephant.setCookie("xp_remember", "true", "/", 365*24*60*60);
119  } else {
120  ZkossElephant.deleteCookie("xp_pass", "/");
121  ZkossElephant.deleteCookie("xp_storepass", "/");
122  ZkossElephant.deleteCookie("xp_login", "/");
123  ZkossElephant.deleteCookie("xp_remember", "/");
124  }
125  try {
126  Authentication.doLogin(user.getText(), password.getText(), redir, null);
127  } catch (IOException ex) {
128  Logger.getLogger(SignInComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
129  }
130  }
131 
132  @Listen("onClick = #remind")
133  public void onRemind(Event event) throws IOException {
134  Clients.clearWrongValue(user);
135  if(org.turro.elephant.zkoss.ZkossElephant.doSendReminder(user.getText())) {
136  Clients.showNotification(I_.get("A reminder has been sent"));
137  } else {
138  Clients.wrongValue(user, I_.get("Missing") + ": " + I_.get("User/Email"));
139  }
140  }
141 
142  @Listen("onClick = #signout")
143  public void onSigOut() {
144  try {
145  Authentication.doLogout(redir);
146  } catch (IOException ex) {
147  Logger.getLogger(SignInComposer.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
148  }
149  }
150 
151  @Listen("onClick = #profile")
152  public void onProfile(Event event) throws IOException, InterruptedException {
153  SystemLogger.getInstance().doLog(SystemLogType.LOG_INFO, "/log/profile", null, null);
154  Modal.doModal("/WEB-INF/_zul/www/profile.zul", (Command) null);
155  }
156 
157  @Listen("onSelect = #lang")
158  public void onLang(Event event) {
159  String sel = lang.getSelectedItem().getValue();
160  if(sel == null) {
161  ZkossElephant.deleteCookie("lang", "/");
162  } else {
163  ZkossElephant.setCookie("lang", sel, "/", 365*24*60*60);
164  }
165  Executions.getCurrent().sendRedirect("");
166  }
167 
168  @Override
169  public void doFinally() {
170  IUser userIn = ZkossElephant.getUser();
171  redir = Executions.getCurrent().getParameter("redir");
172  if(Strings.isBlank(redir)) {
174  }
175  if(userIn == null) {
176  siginform.setVisible(true);
177  if("true".equals(ZkossElephant.getCookieValue("xp_storepass"))) {
178  storepass.setChecked(true);
179  String xppass = ZkossElephant.decryptCookieValue("xp_pass");
180  if(!Strings.isBlank(xppass)) {
181  password.setValue(xppass);
182  }
184  if(map != null && map.containsKey("email")) {
185  user.setValue(map.get("email"));
186  } else {
187  user.setValue(ZkossElephant.getCookieValue("xp_login"));
188  }
189  //personain.setVisible(Strings.isBlank(user.getValue()));
190  }
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  profile.setVisible(false);
200  signout.setVisible(false);
201  }
202  if(cons.canWebAdminister()) {
203  wadmin.setVisible(true);
204  wadmin.setChecked(cons.isWebAdministering());
205  } else {
206  wadmin.setVisible(false);
207  }
208  }
209  String[] langs = ElephantContext.getSiteLocales().split(" *, *");
210  String currentLang = org.turro.elephant.zkoss.ZkossElephant.getCookieValue("lang");
211  lang.appendItem(I_.get("Browser's default"), null);
212  for(String l : langs) {
213  Listitem li = lang.appendItem(I_.byKey(l), l);
214  if(l.equals(currentLang)) {
215  li.setSelected(true);
216  }
217  }
218  signup.setVisible(new File(ElephantContext.getRealPath("/user/signup")).isDirectory());
219  }
220 
221 }
static KeyValueMap getRightNowAction(IConstructor constructor)
Definition: Actions.java:341
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)
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 int doModal(String file)
Definition: Modal.java:38
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
static ISystemLogger getInstance()
static final String INTERNAL_SIGNIN
Definition: IUser.java:33
void doLog(SystemLogType type, Object entity, String comment, Serializable data)