PageRenderTime 68ms CodeModel.GetById 55ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 6%% Copyright 2010 Marc Worrell
 7%%
 8%% Licensed under the Apache License, Version 2.0 (the "License");
 9%% you may not use this file except in compliance with the License.
10%% You may obtain a copy of the License at
11%% 
12%%     http://www.apache.org/licenses/LICENSE-2.0
13%% 
14%% Unless required by applicable law or agreed to in writing, software
15%% distributed under the License is distributed on an "AS IS" BASIS,
16%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17%% See the License for the specific language governing permissions and
18%% limitations under the License.
19
20-module(resource_signup_confirm).
21-author("Marc Worrell <marc@worrell.nl>").
22
23-export([init/1, service_available/2, charsets_provided/2, content_types_provided/2]).
24-export([provide_content/2]).
25-export([event/2]).
26
27-include_lib("webmachine_resource.hrl").
28-include_lib("include/zotonic.hrl").
29
30
31init(DispatchArgs) -> {ok, DispatchArgs}.
32
33service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
34    Context  = z_context:new(ReqData, ?MODULE),
35    Context1 = z_context:set(DispatchArgs, Context),
36    ?WM_REPLY(true, Context1).
37
38charsets_provided(ReqData, Context) ->
39    {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
40
41content_types_provided(ReqData, Context) ->
42    {[{"text/html", provide_content}], ReqData, Context}.
43
44
45provide_content(ReqData, Context) ->
46    Context1 = ?WM_REQ(ReqData, Context),
47    Context2 = z_context:ensure_all(Context1),
48    Key = z_context:get_q(key, Context2, []),
49    {Vars, ContextConfirm} = case Key of
50                                [] -> 
51                                    {[], Context2};
52                                _ ->
53                                    case confirm(Key, Context2) of
54                                        {ok, UserId} ->
55                                            {ok, ContextUser} = z_auth:logon(UserId, Context2),
56                                            Location = confirm_location(UserId, ContextUser),
57                                            {[{user_id, UserId}, {location,Location}], ContextUser};
58                                        {error, _Reason} ->
59                                            {[{error, true}], Context2}
60                                    end
61                              end,
62    Rendered = z_template:render("signup_confirm.tpl", Vars, ContextConfirm),
63    {Output, OutputContext} = z_context:output(Rendered, ContextConfirm),
64    ?WM_REPLY(Output, OutputContext).
65
66
67%% @doc Handle the submit of the signup form.
68event({submit, _, _Trigger, _Target}, Context) ->
69    Key = z_context:get_q(key, Context, []),
70    case confirm(Key, Context) of
71        {ok, UserId} ->
72            {ok, ContextUser} = z_auth:logon(UserId, Context),
73            Location = confirm_location(UserId, ContextUser),
74            z_render:wire({redirect, [{location, Location}]}, ContextUser);
75        {error, _Reason} -> 
76            z_render:wire({show, [{target,"confirm_error"}]}, Context)
77    end.
78
79
80confirm(Key, Context) ->
81    case m_identity:lookup_by_verify_key(Key, Context) of
82        undefined ->
83            {error, unknown_key};
84        Row ->
85            UserId = proplists:get_value(rsc_id, Row),
86            {ok, UserId} = m_rsc:update(UserId, [{is_published, true},{is_verified_account, true}], z_acl:sudo(Context)),
87            m_identity:set_verified(proplists:get_value(id, Row), Context),
88            z_notifier:map({signup_confirm, UserId}, Context),
89            {ok, UserId}
90    end.
91
92confirm_location(UserId, Context) ->
93    case z_notifier:first({signup_confirm_redirect, UserId}, Context) of
94        undefined -> m_rsc:p(UserId, page_url, Context);
95        Loc -> Loc
96    end.