Created
July 14, 2011 18:24
-
-
Save tsloughter/1083068 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
%%%------------------------------------------------------------------- | |
%%% @author Tristan Sloughter <> | |
%%% @copyright (C) 2011, Tristan Sloughter | |
%%% @doc | |
%%% | |
%%% @end | |
%%% Created : 5 Jul 2011 by Tristan Sloughter <> | |
%%%------------------------------------------------------------------- | |
-module(ct_model_sessions). | |
-include("ct_model.hrl"). | |
%% API | |
-export([is_valid/1]). | |
-record(ct_model_sessions, {id=uuid() :: ct_model_types:ct_key(), | |
created=idioms:time_in_seconds() :: integer()}). | |
%% never expire session | |
-define(INFINTE_SESSION, true). | |
-define(EXPIRE, infinity). | |
%%%=================================================================== | |
%%% API | |
%%%=================================================================== | |
-spec is_valid(list()) -> true | false. | |
is_valid(SessionID) -> | |
case ct_db:get(?MODULE, SessionID) of | |
not_found -> | |
false; | |
Session -> | |
not(expired(Session)) | |
end. | |
%%%=================================================================== | |
%%% Internal functions | |
%%%=================================================================== | |
uuid() -> | |
<<I:224/integer>> = sha2:hexdigest224(term_to_binary({make_ref(), now()})), | |
erlang:integer_to_list(I, 16). | |
-ifdef(INFINITE_SESSION). | |
expired(_Session) -> | |
false. | |
-else. | |
expired(Session) -> | |
(idioms:time_in_seconds() - Session#ct_model_sessions.created) > ?EXPIRE. | |
-endif. | |
%%%=================================================================== | |
%%% Test functions | |
%%%=================================================================== | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment