/* * 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 . */ 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.MessagesService; import com.juick.service.UserService; import com.juick.util.UserUtils; 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.GetMapping; import org.springframework.web.bind.annotation.PostMapping; 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; @Inject MessagesService messagesService; @GetMapping("/signup") protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) { com.juick.User visitor = UserUtils.getCurrentUser(); 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); model.addAttribute("readonly", messagesService.isReadonly()); return "views/signup"; } @PostMapping("/signup") protected String doPost(HttpServletRequest request, HttpServletResponse response) { com.juick.User visitor = UserUtils.getCurrentUser(); 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:/"; } }