<commit_before>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2012 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

-module(riak_moss_wm_user).

-export([init/1,
         service_available/2,
         forbidden/2,
         content_types_provided/2,
         allowed_methods/2,
         produce_json/2,
         produce_xml/2,
         process_post/2,
         finish_request/2]).

-include("riak_moss.hrl").
-include_lib("webmachine/include/webmachine.hrl").

%% -------------------------------------------------------------------
%% Webmachine callbacks
%% -------------------------------------------------------------------

init(Config) ->
    dt_entry(<<"init">>),
    %% Check if authentication is disabled and
    %% set that in the context.
    AuthBypass = proplists:get_value(auth_bypass, Config),
    {ok, #context{auth_bypass=AuthBypass}}.

-spec service_available(term(), term()) -> {true, term(), term()}.
service_available(RD, Ctx) ->
    dt_entry(<<"service_available">>),
    riak_moss_wm_utils:service_available(RD, Ctx).

-spec allowed_methods(term(), term()) -> {[atom()], term(), term()}.
allowed_methods(RD, Ctx) ->
    dt_entry(<<"allowed_methods">>),
    {['GET', 'HEAD', 'POST'], RD, Ctx}.

forbidden(RD, Ctx) ->
    dt_entry(<<"forbidden">>),
    case wrq:method(RD) of
        'POST' ->
            dt_return(<<"forbidden">>, [], [<<"POST method">>]),
            {false, RD, Ctx};
        _ ->
            Next = fun(NewRD, NewCtx=#context{user=User}) ->
                           AccessRD = riak_moss_access_logger:set_user(User, NewRD),
                           forbidden(AccessRD, NewCtx, User)
                   end,
            case riak_moss_wm_utils:find_and_auth_user(RD, Ctx, Next) of
                {false, _RD2, Ctx2} = FalseRet ->
                    dt_return(<<"forbidden">>, [], [extract_name((Ctx2#key_context.context)#context.user), <<"false">>]),
                    FalseRet;
                {Rsn, _RD2, Ctx2} = Ret ->
                    Reason = case Rsn of
                                 {halt, Code} -> Code;
                                 _            -> -1
                             end,
                    dt_return(<<"forbidden">>, [Reason], [extract_name(Ctx2#context.user), <<"true">>]),
                    Ret
            end
    end.

content_types_provided(RD, Ctx) ->
    dt_entry(<<"content_types_provided">>),
    {[{?XML_TYPE, produce_xml}, {?JSON_TYPE, produce_json}], RD, Ctx}.

produce_json(RD, #context{user=User}=Ctx) ->
    dt_entry(<<"produce_json">>),
    MJ = {struct, riak_moss_wm_utils:user_record_to_proplist(User)},
    Body = mochijson2:encode(MJ),
    Etag = etag(Body),
    RD2 = wrq:set_resp_header("ETag", Etag, RD),
    {Body, RD2, Ctx}.

produce_xml(RD, #context{user=User}=Ctx) ->
    dt_entry(<<"produce_xml">>),<commit_msg>Remove reference to key_context record in dt_return call in riak_moss_wm_user.<commit_after>%% -------------------------------------------------------------------
%%
%% Copyright (c) 2007-2012 Basho Technologies, Inc.  All Rights Reserved.
%%
%% -------------------------------------------------------------------

-module(riak_moss_wm_user).

-export([init/1,
         service_available/2,
         forbidden/2,
         content_types_provided/2,
         allowed_methods/2,
         produce_json/2,
         produce_xml/2,
         process_post/2,
         finish_request/2]).

-include("riak_moss.hrl").
-include_lib("webmachine/include/webmachine.hrl").

%% -------------------------------------------------------------------
%% Webmachine callbacks
%% -------------------------------------------------------------------

init(Config) ->
    dt_entry(<<"init">>),
    %% Check if authentication is disabled and
    %% set that in the context.
    AuthBypass = proplists:get_value(auth_bypass, Config),
    {ok, #context{auth_bypass=AuthBypass}}.

-spec service_available(term(), term()) -> {true, term(), term()}.
service_available(RD, Ctx) ->
    dt_entry(<<"service_available">>),
    riak_moss_wm_utils:service_available(RD, Ctx).

-spec allowed_methods(term(), term()) -> {[atom()], term(), term()}.
allowed_methods(RD, Ctx) ->
    dt_entry(<<"allowed_methods">>),
    {['GET', 'HEAD', 'POST'], RD, Ctx}.

forbidden(RD, Ctx) ->
    dt_entry(<<"forbidden">>),
    case wrq:method(RD) of
        'POST' ->
            dt_return(<<"forbidden">>, [], [<<"POST method">>]),
            {false, RD, Ctx};
        _ ->
            Next = fun(NewRD, NewCtx=#context{user=User}) ->
                           AccessRD = riak_moss_access_logger:set_user(User, NewRD),
                           forbidden(AccessRD, NewCtx, User)
                   end,
            case riak_moss_wm_utils:find_and_auth_user(RD, Ctx, Next) of
                {false, _RD2, Ctx2} = FalseRet ->
                    dt_return(<<"forbidden">>, [], [extract_name(Ctx2#context.user), <<"false">>]),
                    FalseRet;
                {Rsn, _RD2, Ctx2} = Ret ->
                    Reason = case Rsn of
                                 {halt, Code} -> Code;
                                 _            -> -1
                             end,
                    dt_return(<<"forbidden">>, [Reason], [extract_name(Ctx2#context.user), <<"true">>]),
                    Ret
            end
    end.

content_types_provided(RD, Ctx) ->
    dt_entry(<<"content_types_provided">>),
    {[{?XML_TYPE, produce_xml}, {?JSON_TYPE, produce_json}], RD, Ctx}.

produce_json(RD, #context{user=User}=Ctx) ->
    dt_entry(<<"produce_json">>),
    MJ = {struct, riak_moss_wm_utils:user_record_to_proplist(User)},
    Body = mochijson2:encode(MJ),
    Etag = etag(Body),
    RD2 = wrq:set_resp_header("ETag", Etag, RD),
    {Body, RD2, Ctx}.

produce_xml(RD, #context{user=User}=Ctx) ->
    dt_entry(<<"produce_xml">>),
{'fraction_non_alphanumeric': 0.24085622787797067, 'fraction_numerical': 0.009101634923310299, 'mean_word_length': 1.7548746518105849, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6169218', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1874, 'n_words': 312}