From 8a0fbbd35c13054a947ea0d27ca117542bc452b9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 18 May 2018 15:33:38 +0300 Subject: JPA WIP --- .../main/java/com/juick/www/controllers/Login.java | 2 +- .../com/juick/www/controllers/MessagesWWW.java | 6 ++-- .../java/com/juick/www/controllers/NewMessage.java | 6 ++-- .../java/com/juick/www/controllers/Settings.java | 28 ++++++++--------- .../java/com/juick/www/controllers/SignUp.java | 35 +++++++++++----------- 5 files changed, 39 insertions(+), 38 deletions(-) (limited to 'juick-www/src/main/java/com/juick/www/controllers') diff --git a/juick-www/src/main/java/com/juick/www/controllers/Login.java b/juick-www/src/main/java/com/juick/www/controllers/Login.java index cb7df833..783430b2 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Login.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Login.java @@ -44,7 +44,7 @@ public class Login { } @GetMapping("/login/success") public String getSuccessLogin(ModelMap model) { - model.addAttribute("hash", userService.getHashByUID(UserUtils.getCurrentUser().getUid())); + model.addAttribute("hash", userService.getHashForUser(UserUtils.getCurrentUser())); return "views/login_success"; } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java index f416cb86..dc0729e0 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java +++ b/juick-www/src/main/java/com/juick/www/controllers/MessagesWWW.java @@ -456,7 +456,7 @@ public class MessagesWWW { return "redirect:/login"; } String title = "PM: Inbox"; - List msgs = pmQueriesService.getLastPMInbox(visitor.getUid()); + List msgs = pmQueriesService.getLastPMInbox(visitor); fillUserModel(model, visitor, visitor); model.addAttribute("title", title); model.addAttribute("visitor", visitor); @@ -473,7 +473,7 @@ public class MessagesWWW { return "redirect:/login"; } String title = "PM: Sent"; - List msgs = pmQueriesService.getLastPMSent(visitor.getUid()); + List msgs = pmQueriesService.getLastPMSent(visitor); if (WebUtils.isNotUserName(uname)) { uname = StringUtils.EMPTY; @@ -549,7 +549,7 @@ public class MessagesWWW { "\n" + "\n" + "\n"; - String twitterName = crosspostService.getTwitterName(msg.getUser().getUid()); + String twitterName = crosspostService.getTwitterName(msg.getUser()); if (StringUtils.isNotEmpty(twitterName)) { headers += "\n"; } diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index a464add2..a88698af 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -161,7 +161,7 @@ public class NewMessage { params.add("body", rid == 0 ? String.format("#%d %s", mid, body) : String.format("#%d/%d %s", mid, rid, body)); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); if (StringUtils.isNotEmpty(attachmentFName.toString())) { params.add("img", attachmentFName.toASCIIString()); } @@ -203,7 +203,7 @@ public class NewMessage { HttpEntity> request = new HttpEntity<>(params, headers); params.add("body", String.format("@%s %s", userTo.getName(), body)); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); URI postUri = UriComponentsBuilder.fromUriString(apiUrl).path("/post").build().toUri(); ResponseEntity result = rest.postForEntity( postUri, @@ -240,7 +240,7 @@ public class NewMessage { HttpEntity> request = new HttpEntity<>(params, headers); params.add("body", body); - params.add("hash", userService.getHashByUID(visitor.getUid())); + params.add("hash", userService.getHashForUser(visitor)); if (StringUtils.isNotEmpty(attachmentFName.toString())) { params.add("img", attachmentFName.toASCIIString()); } diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index f9527423..702f52af 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -96,20 +96,20 @@ public class Settings { model.addAttribute("visitor", visitor); model.addAttribute("tags", tagService.getPopularTags()); model.addAttribute("auths", userService.getAuthCodes(visitor)); - model.addAttribute("email_active", emailService.getNotificationsEmail(visitor.getUid())); + model.addAttribute("email_active", emailService.getNotificationsEmail(visitor)); model.addAttribute("ehash", userService.getEmailHash(visitor)); model.addAttribute("emails", userService.getEmails(visitor)); model.addAttribute("jids", userService.getAllJIDs(visitor)); List hours = IntStream.rangeClosed(0, 23).boxed() .map(i -> StringUtils.leftPad(String.format("%d", i), 2, "0")).collect(Collectors.toList()); model.addAttribute("hours", hours); - model.addAttribute("fbstatus", crosspostService.getFbCrossPostStatus(visitor.getUid())); - model.addAttribute("twitter_name", crosspostService.getTwitterName(visitor.getUid())); - model.addAttribute("telegram_name", crosspostService.getTelegramName(visitor.getUid())); + model.addAttribute("fbstatus", crosspostService.getFbCrossPostStatus(visitor)); + model.addAttribute("twitter_name", crosspostService.getTwitterName(visitor)); + model.addAttribute("telegram_name", crosspostService.getTelegramName(visitor)); model.addAttribute("notify_options", subscriptionService.getNotifyOptions(visitor)); model.addAttribute("userinfo", userService.getUserInfo(visitor)); if (page.equals("auth-email")) { - if (emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("code"))) { + if (emailService.verifyAddressByCode(visitor, request.getParameter("code"))) { ; model.addAttribute("result", "OK!"); } else { @@ -140,7 +140,7 @@ public class Settings { case "password": if (userService.updatePassword(visitor, request.getParameter("password"))) { result = "

Password has been changed.

"; - String hash = userService.getHashByUID(visitor.getUid()); + String hash = userService.getHashForUser(visitor); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); @@ -186,9 +186,9 @@ public class Settings { } break; case "email-add": - if (!emailService.verifyAddressByCode(visitor.getUid(), request.getParameter("account"))) { + if (!emailService.verifyAddressByCode(visitor, request.getParameter("account"))) { String authCode = RandomStringUtils.randomAlphanumeric(8).toUpperCase(); - if (emailService.addVerificationCode(visitor.getUid(), request.getParameter("account"), authCode)) { + if (emailService.addVerificationCode(visitor, request.getParameter("account"), authCode)) { Session session = Session.getDefaultInstance(System.getProperties()); try { MimeMessage message = new MimeMessage(session); @@ -210,14 +210,14 @@ public class Settings { } break; case "email-del": - if (emailService.deleteEmail(visitor.getUid(), request.getParameter("account"))) { + if (emailService.deleteEmail(visitor, request.getParameter("account"))) { result = "

Deleted. Back.

"; } else { result = "

An error occured while deleting.

"; } break; case "email-subscr": - if (emailService.setNotificationsEmail(visitor.getUid(), request.getParameter("account"))) { + if (emailService.setNotificationsEmail(visitor, request.getParameter("account"))) { result = String.format("

Saved! Will send notifications to %s." + "

Back

", request.getParameter("account")); } else { @@ -225,7 +225,7 @@ public class Settings { } break; case "twitter-del": - crosspostService.deleteTwitterToken(visitor.getUid()); + crosspostService.deleteTwitterToken(visitor); for (Cookie cookie : request.getCookies()) { if (cookie.getName().equals("request_token")) { cookie.setMaxAge(0); @@ -246,15 +246,15 @@ public class Settings { result = "

Back

"; break; case "facebook-disable": - crosspostService.disableFBCrosspost(visitor.getUid()); + crosspostService.disableFBCrosspost(visitor); result = "

Back

"; break; case "facebook-enable": - crosspostService.enableFBCrosspost(visitor.getUid()); + crosspostService.enableFBCrosspost(visitor); result = "

Back

"; break; case "vk-del": - crosspostService.deleteVKUser(visitor.getUid()); + crosspostService.deleteVKUser(visitor); result = "

Back

"; break; default: 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 index 02a8006b..11f02169 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/SignUp.java +++ b/juick-www/src/main/java/com/juick/www/controllers/SignUp.java @@ -16,6 +16,7 @@ */ package com.juick.www.controllers; +import com.juick.User; import com.juick.server.util.HttpBadRequestException; import com.juick.server.util.HttpForbiddenException; import com.juick.server.util.UserUtils; @@ -95,7 +96,7 @@ public class SignUp { @RequestParam(required = false) String username, @RequestParam(required = false) String password) { com.juick.User visitor = UserUtils.getCurrentUser(); - int uid = 0; + User signedUser = visitor; if (hash.length() > 36 || !type.matches("^[a-zA-Z0-9\\-]+$") || !hash.matches("^[a-zA-Z0-9\\-]+$")) { throw new HttpBadRequestException(); @@ -107,20 +108,20 @@ public class SignUp { if (username.length() > 32) { throw new HttpBadRequestException(); } - uid = userService.checkPassword(username, password); + signedUser = userService.checkPassword(username, password); } else { - uid = visitor.getUid(); + signedUser = visitor; } - if (uid <= 0) { + if (signedUser.isAnonymous()) { 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)) - && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, uid))) { + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, signedUser)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, signedUser)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, signedUser)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, signedUser)) + && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, signedUser.getUid()))) { throw new HttpBadRequestException(); } @@ -131,22 +132,22 @@ public class SignUp { // CHECK USERNAME - uid = userService.createUser(username, password); - if (uid <= 0) { + signedUser = userService.createUser(username, password); + if (signedUser.isAnonymous()) { 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)) - && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, uid))) { + if (!(type.charAt(0) == 'f' && crosspostService.setFacebookUser(hash, signedUser)) + && !(type.charAt(0) == 'v' && crosspostService.setVKUser(hash, signedUser)) + && !(type.charAt(0) == 'd' && crosspostService.setTelegramUser(hash, signedUser)) + && !(type.charAt(0) == 'x' && crosspostService.setJIDUser(hash, signedUser)) + && !(type.charAt(0) == 'm' && messengerService.linkMessengerUser(hash, signedUser.getUid()))) { throw new HttpBadRequestException(); } } if (visitor.isAnonymous()) { - hash = userService.getHashByUID(uid); + hash = userService.getHashForUser(signedUser); Cookie c = new Cookie("hash", hash); c.setMaxAge(365 * 24 * 60 * 60); response.addCookie(c); -- cgit v1.2.3