From ac6c86ddd482721e7011dcb727e4099b8cdf84b1 Mon Sep 17 00:00:00 2001 From: Alexander Alexeev Date: Sun, 11 Dec 2016 00:52:57 +0700 Subject: anonymous user support --- .../src/main/java/com/juick/entity/AnonymUser.java | 79 ++++++++++++++++++++++ .../src/main/java/com/juick/util/UserUtils.java | 27 +++++++- 2 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 juick-server/src/main/java/com/juick/entity/AnonymUser.java (limited to 'juick-server') diff --git a/juick-server/src/main/java/com/juick/entity/AnonymUser.java b/juick-server/src/main/java/com/juick/entity/AnonymUser.java new file mode 100644 index 00000000..071f5434 --- /dev/null +++ b/juick-server/src/main/java/com/juick/entity/AnonymUser.java @@ -0,0 +1,79 @@ +package com.juick.entity; + +import com.juick.User; + +/** + * Created by aalexeev on 12/11/16. + */ +public class AnonymUser extends User { + public static final AnonymUser INSTANCE = new AnonymUser(); + + + private AnonymUser() { + } + + @Override + public boolean equals(Object obj) { + return obj == this || obj instanceof AnonymUser; + } + + @Override + public int getUid() { + return 0; + } + + @Override + public String getName() { + return "Anonymous"; + } + + @Override + public String getFullName() { + return getName(); + } + + @Override + public String getJid() { + return "anonym@localhost"; + } + + @Override + public String getAuthHash() { + return null; + } + + @Override + public Integer getUnreadCount() { + return 0; + } + + @Override + public boolean isBanned() { + return false; + } + + @Override + public Object getAvatar() { + return null; + } + + @Override + public String getCredentials() { + return null; + } + + @Override + public String getLang() { + return "__"; + } + + @Override + public int getMessagesCount() { + return 0; + } + + @Override + public boolean isAnonym() { + return true; + } +} diff --git a/juick-server/src/main/java/com/juick/util/UserUtils.java b/juick-server/src/main/java/com/juick/util/UserUtils.java index ba1bd1ab..ddeedbbd 100644 --- a/juick-server/src/main/java/com/juick/util/UserUtils.java +++ b/juick-server/src/main/java/com/juick/util/UserUtils.java @@ -1,6 +1,11 @@ package com.juick.util; -import java.security.Principal; +import com.juick.User; +import com.juick.entity.AnonymUser; +import com.juick.server.security.entities.JuickUser; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; + import java.util.Random; /** @@ -22,7 +27,23 @@ public class UserUtils { return sb.toString(); } - public static String getUsername(final Principal principal, final String defaultUsername) { - return principal == null ? defaultUsername : principal.getName(); + public static Authentication getAuthentication() { + return SecurityContextHolder.getContext().getAuthentication(); + } + + public static Object getPrincipal(final Authentication authentication) { + return authentication == null ? null : authentication.getPrincipal(); + } + + public static User getCurrentUser() { + Object principal = getPrincipal(getAuthentication()); + + if (principal instanceof JuickUser) + return ((JuickUser) principal).getUser(); + + if (principal instanceof User) + return (User) principal; + + return AnonymUser.INSTANCE; } } -- cgit v1.2.3