package com.juick.service.security.entities; import com.juick.User; import com.juick.server.helpers.AnonymousUser; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import java.util.Collection; import java.util.Collections; import java.util.List; /** * Created by aalexeev on 11/21/16. */ public class JuickUser implements UserDetails { static final GrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER"); static final GrantedAuthority ROLE_ANONYMOUS = new SimpleGrantedAuthority("ROLE_ANONYMOUS"); public static final List USER_AUTHORITY = Collections.singletonList(ROLE_USER); public static final List ANONYMOUS_AUTHORITY = Collections.singletonList(ROLE_ANONYMOUS); public static final JuickUser ANONYMOUS_USER = new JuickUser(AnonymousUser.INSTANCE, ANONYMOUS_AUTHORITY); private final com.juick.User user; private final Collection authorities; public JuickUser(com.juick.User user) { this(user, USER_AUTHORITY); } public JuickUser(com.juick.User user, Collection authorities) { this.user = user; this.authorities = authorities; } @Override public Collection getAuthorities() { return authorities; } @Override public String getPassword() { return user.getCredentials(); } @Override public String getUsername() { return user.getName(); } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return !user.isBanned(); } public User getUser() { return user; } }