/modules/mod_base/resources/resource_template.erl

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