Skip to content

Commit

Permalink
WHISTLE-1311: provide white/black list for flat rate authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anderson committed Jun 22, 2012
1 parent 52e5dfd commit 09fce2a
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions whistle_apps/apps/jonny5/src/j5_authz_req.erl
Expand Up @@ -18,36 +18,79 @@ handle_req(JObj, Props) ->

AccountId = wh_json:get_value(<<"Account-ID">>, JObj),
Limits = j5_util:get_limits(AccountId),

Routines = [fun(_) ->
case calls_at_limit(Limits, JObj) of
false -> {ok, under_limit};
true -> {error, call_limit}
true ->
send_system_alert(<<"call limit">>, JObj, Limits),
{error, call_limit}
end
end
,fun({error, _}=E) -> E;
({ok, _}) ->
case resource_consumption_at_limit(Limits, JObj) of
false -> {ok, under_limit};
true -> {error, resource_consumption_limit}
true ->
send_system_alert(<<"max resource consumption limit">>, JObj, Limits),
{error, resource_consumption_limit}
end
end
,fun({error, _}=E) -> E;
({ok, _}) ->
case trunks_at_limit(Limits, JObj) of
case inellegable_for_flat_rate(JObj)
orelse trunks_at_limit(Limits, JObj)
of
false -> {ok, flat_rate};
true -> {error, trunk_limit}
end
end
,fun({error, trunk_limit}) ->
case credit_is_available(Limits, JObj) of
true -> {ok, per_minute};
false -> {error, trunk_limit}
false ->
send_system_alert(<<"no flat rate or credit">>, JObj, Limits),
{error, trunk_limit}
end;
(Else) -> Else
end
],
send_resp(JObj, props:get_value(queue, Props), lists:foldl(fun(F, A) -> F(A) end, ok, Routines)).

-spec inellegable_for_flat_rate/1 :: (wh_json:json_object()) -> boolean().
inellegable_for_flat_rate(JObj) ->
[Number, _] = binary:split(wh_json:get_value(<<"Request">>, JObj), <<"@">>),
TrunkWhitelist = whapps_config:get(<<"jonny5">>, <<"flat_rate_whitelist">>, <<"^\\+?1\\d{10}$">>),
%% Example blacklist "^\\+?1(900|800)\\d{7}$"
TrunkBlacklist = whapps_config:get(<<"jonny5">>, <<"flat_rate_blacklist">>),
(wh_util:is_empty(TrunkWhitelist) orelse re:run(Number, TrunkWhitelist) =/= nomatch)
andalso
(wh_util:is_empty(TrunkBlacklist) orelse re:run(Number, TrunkBlacklist) =:= nomatch).

-spec send_system_alert/3 :: (ne_binary(), wh_json:json_object(), #limits{}) -> pid().
send_system_alert(Reason, JObj, Limits) ->
spawn(fun() ->
AccountId = wh_json:get_value(<<"Account-ID">>, JObj),
Routines = [fun(J) -> wh_json:set_value(<<"Request">>, wh_json:get_value(<<"Request">>, JObj), J) end
,fun(J) -> wh_json:set_value(<<"Call-ID">>, wh_json:get_value(<<"Call-ID">>, JObj), J) end
,fun(J) -> wh_json:set_value(<<"Node">>, wh_json:get_value(<<"Node">>, JObj), J) end
,fun(J) -> wh_json:set_value(<<"Inbound-Only-Limit">>, wh_util:to_binary(Limits#limits.inbound_trunks), J) end
,fun(J) -> wh_json:set_value(<<"Twoway-Limit">>, wh_util:to_binary(Limits#limits.twoway_trunks), J) end
,fun(J) -> wh_json:set_value(<<"Resource-Limit">>, wh_util:to_binary(Limits#limits.resource_consuming_calls), J) end
,fun(J) -> wh_json:set_value(<<"Call-Limit">>, wh_util:to_binary(Limits#limits.calls), J) end
,fun(J) -> wh_json:set_value(<<"Allow-Prepay">>, wh_util:to_binary(Limits#limits.allow_prepay), J) end
,fun(J) -> wh_json:set_value(<<"Allow-Postpay">>, wh_util:to_binary(Limits#limits.allow_postpay), J) end
,fun(J) -> wh_json:set_value(<<"Max-Postpay">>, <<"$", (wh_util:to_binary(Limits#limits.max_postpay_amount))/binary>>, J) end
,fun(J) -> wh_json:set_value(<<"Reserve-Amount">>, <<"$", (wh_util:to_binary(Limits#limits.reserve_amount))/binary>>, J) end
,fun(J) ->
Balance = wapi_money:units_to_dollars(j5_util:current_balance(AccountId)),
wh_json:set_value(<<"Available-Balance">>, <<"$", (wh_util:to_binary(Balance))/binary>>, J)
end
],
Details = lists:foldl(fun(F, A) -> F(A) end, wh_json:get_value(<<"Usage">>, JObj, wh_json:new()), Routines),
wh_notify:system_alert("authz blocked ~s ~s", [AccountId, Reason], wh_json:to_proplist(Details))
end).

-spec calls_at_limit/2 :: (#limits{}, wh_json:json_object()) -> boolean().
calls_at_limit(#limits{calls=-1}, _) ->
false;
Expand Down

0 comments on commit 09fce2a

Please sign in to comment.