aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/controllers/SignUp.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-28 22:38:21 +0300
committerGravatar Vitaly Takmazov2017-01-10 12:08:35 +0300
commit2f682b5e3cfc3fc5f961b60129be7bc90e0d6a03 (patch)
tree840aea9eb94d5c1f667a710d3298135854bc5002 /juick-www/src/main/java/com/juick/www/controllers/SignUp.java
parenteb440ea4f120115613880e340b010eed5397e72c (diff)
juick-www: now on spring-webmvc
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/controllers/SignUp.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/SignUp.java170
1 files changed, 170 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java
new file mode 100644
index 00000000..937a3242
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java
@@ -0,0 +1,170 @@
+/*
+ * Juick
+ * Copyright (C) 2008-2013, Ugnich Anton
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.juick.www.controllers;
+
+import com.juick.server.util.HttpBadRequestException;
+import com.juick.server.util.HttpForbiddenException;
+import com.juick.service.CrosspostService;
+import com.juick.service.UserService;
+import com.juick.www.Utils;
+import com.juick.www.WebApp;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import javax.inject.Inject;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ *
+ * @author Ugnich Anton
+ */
+@Controller
+public class SignUp {
+
+ @Inject
+ WebApp webApp;
+ @Inject
+ UserService userService;
+ @Inject
+ CrosspostService crosspostService;
+
+
+ @RequestMapping(value = "/signup", method = RequestMethod.GET)
+ protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
+ com.juick.User visitor = webApp.getVisitorUser(request, response);
+
+ String type = request.getParameter("type");
+ String hash = request.getParameter("hash");
+ if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36
+ || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
+ throw new HttpBadRequestException();
+ }
+
+ String account = null;
+ switch (type) {
+ case "fb":
+ account = crosspostService.getFacebookNameByHash(hash);
+ break;
+ case "vk":
+ account = crosspostService.getVKNameByHash(hash);
+ break;
+ case "xmpp":
+ account = crosspostService.getJIDByHash(hash);
+ break;
+ case "durov":
+ account = crosspostService.getTelegramNameByHash(hash);
+ break;
+ }
+ if (account == null) {
+ throw new HttpBadRequestException();
+ }
+
+ model.addAttribute("title", "Новый пользователь");
+ model.addAttribute("visitor", visitor);
+ model.addAttribute("account", account);
+ model.addAttribute("type", type);
+ model.addAttribute("hash", hash);
+ return "views/signup";
+ }
+
+ @RequestMapping(value = "/signup", method = RequestMethod.POST)
+ protected String doPost(HttpServletRequest request, HttpServletResponse response) {
+ com.juick.User visitor = webApp.getVisitorUser(request, response);
+ int uid = 0;
+
+ String type = request.getParameter("type");
+ String hash = request.getParameter("hash");
+ if (type == null || type.isEmpty() || hash == null || hash.isEmpty() || hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) {
+ throw new HttpBadRequestException();
+ }
+
+ String action = request.getParameter("action");
+ if (action.charAt(0) == 'l') {
+
+ if (visitor.getUid() == 0) {
+ String username = request.getParameter("username");
+ String password = request.getParameter("password");
+ if (username == null || password == null || username.length() > 32 || password.isEmpty()) {
+ throw new HttpBadRequestException();
+ }
+ uid = userService.checkPassword(username, password);
+ } else {
+ uid = visitor.getUid();
+ }
+
+ if (uid <= 0) {
+ throw new HttpForbiddenException();
+ }
+
+ if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid))
+ && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid))
+ && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid))
+ && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid))) {
+ throw new HttpBadRequestException();
+ }
+
+ } else { // Create new account
+ String username = request.getParameter("username");
+ String password = request.getParameter("password");
+ if (username == null || password == null || username.length() < 2 || username.length() > 16 || !username.matches("^[a-zA-Z0-9\\-]+$") || password.length() < 6 || password.length() > 32) {
+ throw new HttpBadRequestException();
+ }
+
+ // CHECK USERNAME
+
+ uid = userService.createUser(username, password);
+ if (uid <= 0) {
+ throw new HttpBadRequestException();
+ }
+
+ if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, uid))
+ && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, uid))
+ && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, uid))
+ && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, uid))) {
+ throw new HttpBadRequestException();
+ }
+
+ int ref = 0;
+ String sRef = Utils.getCookie(request, "ref");
+ if (sRef != null) {
+ try {
+ ref = Integer.parseInt(sRef);
+ } catch (Exception e) {
+ }
+ }
+
+ if (ref > 0) {
+ crosspostService.setUserRef(uid, ref);
+ }
+
+ visitor = null;
+ }
+
+ if (visitor == null) {
+ hash = userService.getHashByUID(uid);
+ Cookie c = new Cookie("hash", hash);
+ c.setMaxAge(365 * 24 * 60 * 60);
+ response.addCookie(c);
+ }
+ return "redirect:/";
+ }
+}