BrightSide Workbench Full Report + Source Code
SignUpCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.contacts.zul.register;
20 
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.apache.commons.mail.EmailException;
27 import org.turro.action.IRegisterCallback;
28 import org.turro.annotation.ElephantPlugin;
29 import org.turro.auth.Authentication;
30 import org.turro.collections.KeyValueMap;
31 import org.turro.contacts.SignUp;
32 import org.turro.contacts.db.ContactsPU;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.context.ElephantContext;
35 import org.turro.elephant.context.IConstructor;
36 import org.turro.elephant.direct.AbstractDirectEntityCtrl;
37 import org.turro.elephant.direct.DirectContent;
38 import org.turro.i18n.I_;
39 import org.turro.jpa.Dao;
40 import org.turro.mail.sender.MailPool;
41 import org.turro.marker.ElephantMarker;
42 
47 @ElephantPlugin(label="signup-ctrl")
48 @DirectContent(identifier="signup-action")
49 public class SignUpCtrl extends AbstractDirectEntityCtrl {
50 
51  public SignUpCtrl() {
52  super("widgets/auth", "user");
53  }
54 
55  /* IEntityCtrl */
56 
57  @Override
58  protected void prepareMarker(ElephantMarker marker) {
59  marker.put("type", "sign-up");
60  marker.put("actionLink", createFormAction());
61  }
62 
63  /* IDirectContent */
64 
65  @Override
66  protected String getIdentifier() {
67  return SignUpCtrl.class.getAnnotation(DirectContent.class).identifier();
68  }
69 
70  @Override
71  protected void doExecute(IConstructor constructor, KeyValueMap map) {
72  String type = map.get("type");
73  if("sign-up".equals(type)) {
74  processSignUp(constructor, map);
75  }
76  }
77 
78  @Override
79  protected void prepareCleanMarker(ElephantMarker marker, KeyValueMap map) {
80  throw new UnsupportedOperationException("Not supported yet.");
81  }
82 
83  private void processSignUp(IConstructor constructor, KeyValueMap map) {
84  if(isValidMap(map)) {
85  SignUp su = new SignUp();
86  su.setName(map.get("name"));
87  su.setEmail(map.get("email"));
88  su.setComment(map.get("comment"));
89  if(su.isValid()) {
90  SignUp pending = existPending(su.getEmail());
91  if(pending != null) {
92  sendEmail(pending);
93  } else if(existEmail(su.getEmail())) {
94  Authentication.sendReminder(constructor, su.getName(), su.getEmail());
95  } else {
96  su.setValueMap(keepValues(map));
97  su = new ContactsPU().saveObject(su);
98  sendEmail(su);
99  }
100  }
101  }
102  }
103 
104  private boolean isValidMap(KeyValueMap map) {
105  return !Strings.isBlank(map.get("name")) && !Strings.isBlank(map.get("email"));
106  }
107 
108  private SignUp existPending(String email) {
109  Dao dao = new ContactsPU();
110  List l = dao.getResultList(
111  "select c from SignUp c where c.email = ? and confirmed = FALSE",
112  new Object[] { email }
113  );
114  return !l.isEmpty() ? (SignUp) l.get(0) : null;
115  }
116 
117  private static boolean existEmail(String email) {
118  Dao dao = new ContactsPU();
119  List l = dao.getResultList(
120  "select c from Connector c where c.value = ?",
121  new Object[] { email }
122  );
123  if(l.isEmpty()) {
124  l = dao.getResultList(
125  "select c from SignUp c where c.email = ? and confirmed = TRUE",
126  new Object[] { email }
127  );
128  }
129  return !l.isEmpty();
130  }
131 
132  private void sendEmail(SignUp signUp) {
133  try {
134  new MailPool()
135  .addUser(signUp.getName(), signUp.getEmail())
136  .put("signUp", signUp)
137  .sendTemplate("sign-pending", I_.get("Email pending to confirm") + " : " + ElephantContext.getSiteName());
138  IRegisterCallback regcall = (IRegisterCallback) Application.getApplication().getConstructor()
139  .getSessionAttribute(IRegisterCallback.REGISTER_CALLBACK_KEY);
140  if(regcall != null) {
141  regcall.onRegister(signUp.getEmail());
142  }
143  } catch (EmailException ex) {
144  Logger.getLogger(SignUpCtrl.class.getName()).log(Level.SEVERE, null, ex);
145  }
146  }
147 
148  private String keepValues(KeyValueMap map) {
149  HashMap<String, String> keep = new HashMap<>();
150  for(String key : map.keySet()) {
151  if(key.startsWith("su_")) {
152  keep.put(key, map.get(key));
153  }
154  }
155  return keep.isEmpty() ? null : KeyValueMap.format(keep);
156  }
157 
158 }
static boolean sendReminder(IConstructor constructor, String name, String email)
void setComment(String comment)
Definition: SignUp.java:82
void setEmail(String email)
Definition: SignUp.java:74
void setName(String name)
Definition: SignUp.java:66
void setValueMap(String valueMap)
Definition: SignUp.java:98
void doExecute(IConstructor constructor, KeyValueMap map)
Definition: SignUpCtrl.java:71
void prepareCleanMarker(ElephantMarker marker, KeyValueMap map)
Definition: SignUpCtrl.java:79
void prepareMarker(ElephantMarker marker)
Definition: SignUpCtrl.java:58
Object put(Object key, Object value)