-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsr_single_session_handler.erl
81 lines (74 loc) · 2.05 KB
/
sr_single_session_handler.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
%%% @doc GET|DELETE /sessions/:id handler
-module(sr_single_session_handler).
-behaviour(trails_handler).
-include_lib("mixer/include/mixer.hrl").
-mixin([{ sr_single_entity_handler
, [ init/3
, rest_init/2
, allowed_methods/2
, resource_exists/2
, content_types_accepted/2
, content_types_provided/2
, handle_get/2
, handle_put/2
, delete_resource/2
]
}]).
-mixin([{ sr_sessions_handler
, [ is_authorized/2
]
}]).
-export([ trails/0
, forbidden/2
, is_conflict/2
]).
-type state() :: sr_single_entity_handler:state().
-spec trails() -> trails:trails().
trails() ->
RequestBody =
#{ name => <<"request body">>
, in => body
, description => <<"request body (as json)">>
, required => true
},
Id =
#{ name => id
, in => path
, description => <<"Session Id">>
, required => true
, type => string
},
Metadata =
#{ put =>
#{ tags => ["sessions"]
, description => "Updates a session"
, consumes => ["application/json"]
, produces => ["application/json"]
, parameters => [RequestBody, Id]
}
, delete =>
#{ tags => ["sessions"]
, description => "Deletes a session"
, parameters => [Id]
}
},
Path = "/sessions/:id",
Opts = #{ path => Path
, model => sessions
, verbose => true
},
[trails:trail(Path, ?MODULE, Opts, Metadata)].
-spec forbidden(cowboy_req:req(), state()) ->
{boolean(), cowboy_req:req(), state()}.
forbidden(Req, State) ->
{User, _} = sr_state:retrieve(user, State, undefined),
Id = sr_state:id(State),
case sumo:fetch(sessions, Id) of
notfound -> {false, Req, State};
Session -> {User =/= sr_sessions:user(Session), Req, State}
end.
-spec is_conflict(cowboy_req:req(), state()) ->
{boolean(), cowboy_req:req(), state()}.
is_conflict(Req, State) ->
Entity = sr_state:entity(State),
{Entity =:= undefined, Req, State}.