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