PageRenderTime 25ms CodeModel.GetById 16ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/modules/mod_admin_identity/actions/action_admin_identity_dialog_user_add.erl

https://code.google.com/p/zotonic/
Erlang | 104 lines | 73 code | 11 blank | 20 comment | 0 complexity | d2c0765b7927b616f84bfda288a4288d MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2009 Marc Worrell
  3%% Date: 2009-07-13
  4%% @doc Add a complete new person and make it into an user.
  5
  6%% Copyright 2009 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(action_admin_identity_dialog_user_add).
 21-author("Marc Worrell <marc@worrell.nl").
 22
 23%% interface functions
 24-export([
 25    render_action/4,
 26    event/2
 27]).
 28
 29-include("zotonic.hrl").
 30
 31render_action(TriggerId, TargetId, Args, Context) ->
 32    OnSuccess = proplists:get_all_values(on_success, Args),
 33    Postback = {dialog_user_add, OnSuccess},
 34	{PostbackMsgJS, _PickledPostback} = z_render:make_postback(Postback, click, TriggerId, TargetId, ?MODULE, Context),
 35	{PostbackMsgJS, Context}.
 36
 37
 38event({postback, {dialog_user_add, OnSuccess}, _TriggerId, _TargetId}, Context) ->
 39    case z_acl:is_allowed(insert, identity, Context) of
 40        true ->
 41            Vars = [
 42                {on_success, OnSuccess}
 43            ],
 44            z_render:dialog("Add a new user.", "_action_dialog_user_add.tpl", Vars, Context);
 45        false ->
 46            z_render:growl_error("Only administrators can add users.", Context)
 47    end;
 48
 49%% @doc Delete an username from an user.
 50%% @spec event(Event, Context1) -> Context2
 51event({submit, {user_add, Props}, _TriggerId, _TargetId}, Context) ->
 52    case z_acl:is_allowed(insert, identity, Context) of
 53        true ->
 54            NameFirst = z_context:get_q_validated("name_first", Context),
 55            NamePrefix = z_context:get_q("surprefix", Context),
 56            NameSur = z_context:get_q_validated("name_surname", Context),
 57            Title = case NamePrefix of
 58                [] -> [ NameFirst, " ", NameSur ];
 59                _ -> [ NameFirst, " ", NamePrefix, " ", NameSur ]
 60            end,
 61
 62            PersonProps = [
 63                {is_published, true},
 64                {category, person},
 65                {title, lists:flatten(Title)},
 66                {name_first, NameFirst},
 67                {name_surname_prefix, NamePrefix},
 68                {name_surname, NameSur},
 69                {email, z_context:get_q_validated("email", Context)},
 70                {creator_id, self}
 71            ],
 72            
 73            F = fun(Ctx) ->
 74                case m_rsc:insert(PersonProps, Ctx) of
 75                    {ok, PersonId} ->
 76                        Username = z_context:get_q_validated("new_username", Ctx),
 77                        Password = z_context:get_q_validated("new_password", Ctx),
 78                        case m_identity:set_username_pw(PersonId, Username, Password, Ctx) of
 79                            ok -> {ok, PersonId};
 80                            {error, PWReason} -> throw({error, PWReason})
 81                        end;
 82                    {error, InsReason} ->
 83                        throw({error, InsReason})
 84                end
 85            end,
 86            
 87            case z_db:transaction(F, Context) of
 88                {ok, _PersonId} ->
 89                    Context1 = z_render:growl(["Created the user ",z_html:escape(Title), "."], Context),
 90                    z_render:wire(proplists:get_all_values(on_success, Props), Context1);
 91                {rollback, {Error, _CallStack}} ->
 92                    case Error of
 93                        {error, eexist} ->
 94                            z_render:growl_error("Duplicate username, please choose another username.", Context);
 95                        {error, eacces} ->
 96                            z_render:growl_error("You are not allowed to create the person page.", Context);
 97                        _OtherError ->
 98                            ?DEBUG(Error),
 99                            z_render:growl_error("Could not create the user. Sorry.", Context)
100                    end
101            end;
102        false ->
103            z_render:growl_error("Only administrators can add users.", Context)
104    end.