Created
April 12, 2016 19:47
-
-
Save inash/cbb25ab5febf0609a708b5aad8418a15 to your computer and use it in GitHub Desktop.
Overridden version of xitrum.hazelcast.Session store to limit a user to a single session
This file contains hidden or 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 test.hazelcast | |
import com.hazelcast.core.IMap | |
import xitrum.hazelcast.{Hz, Session ⇒ HazelcastSession} | |
/** Overridden version of xitrum.hazelcast.Session store to limit a user to a | |
* single session. | |
*/ | |
class Session extends HazelcastSession { | |
private[this] lazy val store = Hz.instance.getMap("xitrum/session").asInstanceOf[IMap[String, Map[String, Any]]] | |
override def put(sessionId: String, immutableMap: Map[String, Any]): Unit = { | |
store.set(sessionId, immutableMap) | |
// Store current session id for the user | |
immutableMap.get("username").map { username ⇒ | |
Option(store.get(username.asInstanceOf[String])).map { usermap ⇒ | |
usermap.get("username").map { sessionIdPrev ⇒ | |
if (sessionIdPrev.asInstanceOf[String] != sessionId) store.remove(usermap.getOrElse("username", "")) | |
} | |
} | |
store.put(username.asInstanceOf[String], Map("username" → sessionId)) | |
} | |
} | |
override def remove(sessionId: String): Unit = { | |
// First remove username -> sessionId mapping if available | |
Option(store.get(sessionId)).map(_.get("username").map(username ⇒ store.remove(username.asInstanceOf[String]))) | |
store.remove(sessionId) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment