/modules/mod_signup/resources/resource_signup_confirm.erl

https://code.google.com/p/zotonic/ · Erlang · 96 lines · 62 code · 16 blank · 18 comment · 0 complexity · 94b384f38ca381cb13fa9753cddb4cfe MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2010 Marc Worrell
  3. %% Date: 2010-05-12
  4. %% @doc Display a form to sign up.
  5. %% Copyright 2010 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. -module(resource_signup_confirm).
  19. -author("Marc Worrell <marc@worrell.nl>").
  20. -export([init/1, service_available/2, charsets_provided/2, content_types_provided/2]).
  21. -export([provide_content/2]).
  22. -export([event/2]).
  23. -include_lib("webmachine_resource.hrl").
  24. -include_lib("include/zotonic.hrl").
  25. init(DispatchArgs) -> {ok, DispatchArgs}.
  26. service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
  27. Context = z_context:new(ReqData, ?MODULE),
  28. Context1 = z_context:set(DispatchArgs, Context),
  29. ?WM_REPLY(true, Context1).
  30. charsets_provided(ReqData, Context) ->
  31. {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
  32. content_types_provided(ReqData, Context) ->
  33. {[{"text/html", provide_content}], ReqData, Context}.
  34. provide_content(ReqData, Context) ->
  35. Context1 = ?WM_REQ(ReqData, Context),
  36. Context2 = z_context:ensure_all(Context1),
  37. Key = z_context:get_q(key, Context2, []),
  38. {Vars, ContextConfirm} = case Key of
  39. [] ->
  40. {[], Context2};
  41. _ ->
  42. case confirm(Key, Context2) of
  43. {ok, UserId} ->
  44. {ok, ContextUser} = z_auth:logon(UserId, Context2),
  45. Location = confirm_location(UserId, ContextUser),
  46. {[{user_id, UserId}, {location,Location}], ContextUser};
  47. {error, _Reason} ->
  48. {[{error, true}], Context2}
  49. end
  50. end,
  51. Rendered = z_template:render("signup_confirm.tpl", Vars, ContextConfirm),
  52. {Output, OutputContext} = z_context:output(Rendered, ContextConfirm),
  53. ?WM_REPLY(Output, OutputContext).
  54. %% @doc Handle the submit of the signup form.
  55. event({submit, _, _Trigger, _Target}, Context) ->
  56. Key = z_context:get_q(key, Context, []),
  57. case confirm(Key, Context) of
  58. {ok, UserId} ->
  59. {ok, ContextUser} = z_auth:logon(UserId, Context),
  60. Location = confirm_location(UserId, ContextUser),
  61. z_render:wire({redirect, [{location, Location}]}, ContextUser);
  62. {error, _Reason} ->
  63. z_render:wire({show, [{target,"confirm_error"}]}, Context)
  64. end.
  65. confirm(Key, Context) ->
  66. case m_identity:lookup_by_verify_key(Key, Context) of
  67. undefined ->
  68. {error, unknown_key};
  69. Row ->
  70. UserId = proplists:get_value(rsc_id, Row),
  71. {ok, UserId} = m_rsc:update(UserId, [{is_published, true},{is_verified_account, true}], z_acl:sudo(Context)),
  72. m_identity:set_verified(proplists:get_value(id, Row), Context),
  73. z_notifier:map({signup_confirm, UserId}, Context),
  74. {ok, UserId}
  75. end.
  76. confirm_location(UserId, Context) ->
  77. case z_notifier:first({signup_confirm_redirect, UserId}, Context) of
  78. undefined -> m_rsc:p(UserId, page_url, Context);
  79. Loc -> Loc
  80. end.