1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-12-30 12:00:38 +00:00

updated actix-session to support login and logout functionality (renew and purge)

This commit is contained in:
dowwie 2019-06-12 08:03:27 -04:00
parent e7ba67e1a8
commit 959eef05ae

View file

@ -98,10 +98,23 @@ impl UserSession for ServiceRequest {
} }
} }
#[derive(PartialEq, Clone, Debug)]
pub enum SessionStatus {
Changed,
Purged,
Renewed,
Unchanged
}
impl Default for SessionStatus {
fn default() -> SessionStatus {
SessionStatus::Unchanged
}
}
#[derive(Default)] #[derive(Default)]
struct SessionInner { struct SessionInner {
state: HashMap<String, String>, state: HashMap<String, String>,
changed: bool, pub status: SessionStatus,
} }
impl Session { impl Session {
@ -117,7 +130,7 @@ impl Session {
/// Set a `value` from the session. /// Set a `value` from the session.
pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), Error> { pub fn set<T: Serialize>(&self, key: &str, value: T) -> Result<(), Error> {
let mut inner = self.0.borrow_mut(); let mut inner = self.0.borrow_mut();
inner.changed = true; inner.status = SessionStatus::Changed;
inner inner
.state .state
.insert(key.to_owned(), serde_json::to_string(&value)?); .insert(key.to_owned(), serde_json::to_string(&value)?);
@ -127,17 +140,30 @@ impl Session {
/// Remove value from the session. /// Remove value from the session.
pub fn remove(&self, key: &str) { pub fn remove(&self, key: &str) {
let mut inner = self.0.borrow_mut(); let mut inner = self.0.borrow_mut();
inner.changed = true; inner.status = SessionStatus::Changed;
inner.state.remove(key); inner.state.remove(key);
} }
/// Clear the session. /// Clear the session.
pub fn clear(&self) { pub fn clear(&self) {
let mut inner = self.0.borrow_mut(); let mut inner = self.0.borrow_mut();
inner.changed = true; inner.status = SessionStatus::Changed;
inner.state.clear() inner.state.clear()
} }
/// Removes session, both client and server side.
pub fn purge(&self) {
let mut inner = self.0.borrow_mut();
inner.status = SessionStatus::Purged;
inner.state.clear();
}
/// Renews the session key, assigning existing session state to new key.
pub fn renew(&self) {
let mut inner = self.0.borrow_mut();
inner.status = SessionStatus::Renewed;
}
pub fn set_session( pub fn set_session(
data: impl Iterator<Item = (String, String)>, data: impl Iterator<Item = (String, String)>,
req: &mut ServiceRequest, req: &mut ServiceRequest,
@ -149,7 +175,7 @@ impl Session {
pub fn get_changes<B>( pub fn get_changes<B>(
res: &mut ServiceResponse<B>, res: &mut ServiceResponse<B>,
) -> Option<impl Iterator<Item = (String, String)>> { ) -> (SessionStatus, Option<impl Iterator<Item = (String, String)>>) {
if let Some(s_impl) = res if let Some(s_impl) = res
.request() .request()
.extensions() .extensions()
@ -157,9 +183,9 @@ impl Session {
{ {
let state = let state =
std::mem::replace(&mut s_impl.borrow_mut().state, HashMap::new()); std::mem::replace(&mut s_impl.borrow_mut().state, HashMap::new());
Some(state.into_iter()) (s_impl.borrow().status.clone(), Some(state.into_iter()))
} else { } else {
None (SessionStatus::Unchanged, None)
} }
} }
@ -224,7 +250,8 @@ mod tests {
session.remove("key"); session.remove("key");
let mut res = req.into_response(HttpResponse::Ok().finish()); let mut res = req.into_response(HttpResponse::Ok().finish());
let changes: Vec<_> = Session::get_changes(&mut res).unwrap().collect(); let (_status, state) = Session::get_changes(&mut res);
let changes: Vec<_> = state.unwrap().collect();
assert_eq!(changes, [("key2".to_string(), "\"value2\"".to_string())]); assert_eq!(changes, [("key2".to_string(), "\"value2\"".to_string())]);
} }
@ -241,4 +268,23 @@ mod tests {
let res = session.get::<String>("key").unwrap(); let res = session.get::<String>("key").unwrap();
assert_eq!(res, Some("value".to_string())); assert_eq!(res, Some("value".to_string()));
} }
#[test]
fn purge_session() {
let mut req = test::TestRequest::default().to_srv_request();
let session = Session::get_session(&mut *req.extensions_mut());
assert_eq!(session.0.borrow().status, SessionStatus::Unchanged);
session.purge();
assert_eq!(session.0.borrow().status, SessionStatus::Purged);
}
#[test]
fn renew_session() {
let mut req = test::TestRequest::default().to_srv_request();
let session = Session::get_session(&mut *req.extensions_mut());
assert_eq!(session.0.borrow().status, SessionStatus::Unchanged);
session.renew();
assert_eq!(session.0.borrow().status, SessionStatus::Renewed);
}
} }