Created
February 10, 2023 20:54
-
-
Save fedotxxl/514a67c0f58bef5ed373c023732ced87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.gramant.adplanner.auth; | |
import io.thedocs.soyuz.to; | |
import lombok.AllArgsConstructor; | |
import org.jooq.DSLContext; | |
import org.springframework.security.web.authentication.rememberme.PersistentRememberMeToken; | |
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; | |
import org.springframework.stereotype.Repository; | |
import java.util.Date; | |
import java.util.List; | |
import static ru.gramant.adplanner.dao.jooq.Tables.PERSISTENT_LOGINS_WITH_HISTORY; | |
@Repository | |
@AllArgsConstructor | |
public class CustomPersistentTokenRepository implements PersistentTokenRepository { | |
private DSLContext dsl; | |
@Override | |
public void createNewToken(PersistentRememberMeToken token) { | |
dsl.insertInto(PERSISTENT_LOGINS_WITH_HISTORY) | |
.set(PERSISTENT_LOGINS_WITH_HISTORY.SERIES, token.getSeries()) | |
.set(PERSISTENT_LOGINS_WITH_HISTORY.TOKEN, token.getTokenValue()) | |
.set(PERSISTENT_LOGINS_WITH_HISTORY.USERNAME, token.getUsername()) | |
.set(PERSISTENT_LOGINS_WITH_HISTORY.LAST_USED, to.sqlTimestamp(token.getDate())) | |
.execute(); | |
} | |
@Override | |
public void updateToken(String series, String tokenValue, Date lastUsed) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public PersistentRememberMeToken getTokenForSeries(String seriesId) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void removeUserTokens(String username) { | |
dsl.deleteFrom(PERSISTENT_LOGINS_WITH_HISTORY).where(PERSISTENT_LOGINS_WITH_HISTORY.USERNAME.eq(username)).execute(); | |
} | |
public List<PersistentRememberMeToken> findAllBySeries(String seriesId) { | |
return dsl.select() | |
.from(PERSISTENT_LOGINS_WITH_HISTORY) | |
.where(PERSISTENT_LOGINS_WITH_HISTORY.SERIES.eq(seriesId)) | |
.orderBy(PERSISTENT_LOGINS_WITH_HISTORY.LAST_USED.desc()) | |
.fetch(r -> new PersistentRememberMeToken( | |
r.get(PERSISTENT_LOGINS_WITH_HISTORY.USERNAME), | |
r.get(PERSISTENT_LOGINS_WITH_HISTORY.SERIES), | |
r.get(PERSISTENT_LOGINS_WITH_HISTORY.TOKEN), | |
r.get(PERSISTENT_LOGINS_WITH_HISTORY.LAST_USED) | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment