PageRenderTime 36ms CodeModel.GetById 22ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_base/resources/resource_template.erl

https://code.google.com/p/zotonic/
Erlang | 90 lines | 61 code | 12 blank | 17 comment | 0 complexity | 04ae7eeb8772976c1c94f23ece67002b MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%% @doc Generic template controller, serves the template mentioned in the dispatch configuration.
 4
 5%% Copyright 2009 Marc Worrell
 6%%
 7%% Licensed under the Apache License, Version 2.0 (the "License");
 8%% you may not use this file except in compliance with the License.
 9%% You may obtain a copy of the License at
10%% 
11%%     http://www.apache.org/licenses/LICENSE-2.0
12%% 
13%% Unless required by applicable law or agreed to in writing, software
14%% distributed under the License is distributed on an "AS IS" BASIS,
15%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16%% See the License for the specific language governing permissions and
17%% limitations under the License.
18
19-module(resource_template).
20-author("Marc Worrell <marc@worrell.nl>").
21
22-export([
23    init/1,
24    service_available/2,
25    charsets_provided/2,
26    content_types_provided/2,
27    is_authorized/2,
28    provide_content/2
29]).
30
31-include_lib("webmachine_resource.hrl").
32-include_lib("include/zotonic.hrl").
33
34
35init(DispatchArgs) -> {ok, DispatchArgs}.
36
37service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
38    Context  = z_context:new(ReqData, ?MODULE),
39    Context1 = z_context:set(DispatchArgs, Context),
40    ?WM_REPLY(true, Context1).
41
42charsets_provided(ReqData, Context) ->
43    {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
44
45content_types_provided(ReqData, Context) ->
46    case z_context:get(content_type, Context) of
47        undefined ->
48            {[{"text/html", provide_content}], ReqData, Context};
49        Mime -> 
50            {[{Mime, provide_content}], ReqData, Context}
51    end.
52
53%% @doc Check if the current user is allowed to view the resource. 
54is_authorized(ReqData, Context) ->
55    Context1 = ?WM_REQ(ReqData, Context),
56    case z_context:get(acl, Context1) of
57        undefined -> 
58            ?WM_REPLY(true, Context1);
59        is_auth -> 
60            Context2 = z_context:ensure_all(Context1),
61            z_acl:wm_is_authorized(z_auth:is_auth(Context2), Context2);
62        logoff ->
63            Context2 = z_context:ensure_all(Context1),
64            case z_auth:is_auth(Context2) of
65                true ->
66                    Context3 = z_auth:logoff(Context2),
67                    ?WM_REPLY(true, Context3);
68                false ->
69                    ?WM_REPLY(true, Context2)
70            end;
71        Acl -> 
72            Context2 = z_context:ensure_all(Context1),
73            z_acl:wm_is_authorized(Acl, Context2)
74    end.
75
76
77provide_content(ReqData, Context) ->
78    Context1 = ?WM_REQ(ReqData, Context),
79    Context2 = case z_context:get(anonymous, Context) of
80        true -> z_context:ensure_qs(Context1);
81        _ -> z_context:ensure_all(Context1)
82    end,
83    Context3 = case z_context:get(seo_noindex, Context2) of
84        true ->  z_context:set_resp_header("X-Robots-Tag", "noindex", Context2);
85        _ -> Context2
86    end,
87    Template = z_context:get(template, Context3),
88    Rendered = z_template:render(Template, z_context:get_all(Context), Context3),
89    {Output, OutputContext} = z_context:output(Rendered, Context3),
90    ?WM_REPLY(Output, OutputContext).