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