| 1 |
|
-module(shurbej_http_group). |
| 2 |
|
-include_lib("shurbej_store/include/shurbej_records.hrl"). |
| 3 |
|
|
| 4 |
|
-export([init/2]). |
| 5 |
|
|
| 6 |
|
%% GET /groups/:group_id — single group metadata. |
| 7 |
|
%% Authorization requires group membership. |
| 8 |
|
init(Req0, State) -> |
| 9 |
4 |
case cowboy_req:method(Req0) of |
| 10 |
|
<<"GET">> -> |
| 11 |
3 |
case shurbej_http_common:authorize(Req0) of |
| 12 |
|
{ok, {group, GroupId} = LibRef, _} -> |
| 13 |
2 |
case shurbej_http_common:check_lib_perm(read, LibRef) of |
| 14 |
1 |
ok -> handle_get(GroupId, Req0, State); |
| 15 |
|
{error, forbidden} -> |
| 16 |
1 |
Req = shurbej_http_common:error_response(403, |
| 17 |
|
<<"Access denied">>, Req0), |
| 18 |
1 |
{ok, Req, State} |
| 19 |
|
end; |
| 20 |
|
{error, Reason, _} -> |
| 21 |
1 |
Req = shurbej_http_common:auth_error_response(Reason, Req0), |
| 22 |
1 |
{ok, Req, State} |
| 23 |
|
end; |
| 24 |
|
_ -> |
| 25 |
1 |
Req = shurbej_http_common:error_response(405, <<"Method not allowed">>, Req0), |
| 26 |
1 |
{ok, Req, State} |
| 27 |
|
end. |
| 28 |
|
|
| 29 |
|
handle_get(GroupId, Req0, State) -> |
| 30 |
1 |
case shurbej_db:get_group(GroupId) of |
| 31 |
|
{ok, Group} -> |
| 32 |
1 |
Body = shurbej_http_common:envelope_group(Group), |
| 33 |
1 |
Req = shurbej_http_common:json_response(200, Body, |
| 34 |
|
Group#shurbej_group.version, Req0), |
| 35 |
1 |
{ok, Req, State}; |
| 36 |
|
undefined -> |
| 37 |
:-( |
Req = shurbej_http_common:error_response(404, <<"Group not found">>, Req0), |
| 38 |
:-( |
{ok, Req, State} |
| 39 |
|
end. |