/modules/mod_admin_identity/validators/validator_admin_identity_username_unique.erl

https://code.google.com/p/zotonic/ · Erlang · 64 lines · 39 code · 5 blank · 20 comment · 0 complexity · becc424079008b9daeb4ea2fd1163b58 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2010 Marc Worrell
  3. %% @doc Check if an entered username is unique
  4. %% Copyright 2010 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(validator_admin_identity_username_unique).
  18. -include("zotonic.hrl").
  19. -export([
  20. render_validator/5,
  21. validate/5,
  22. event/2
  23. ]).
  24. render_validator(username_unique, TriggerId, TargetId, Args, Context) ->
  25. {_PostbackJS, PostbackInfo} = z_render:make_postback({validate, Args}, 'postback', TriggerId, TargetId, ?MODULE, Context),
  26. JsObject = z_utils:js_object(z_validation:rename_args([{z_postback, PostbackInfo}|Args])),
  27. Script = [<<"z_add_validator(\"">>,TriggerId,<<"\", \"postback\", ">>, JsObject, <<");\n">>],
  28. {Args, Script, Context}.
  29. %% @spec validate(Type, TriggerId, Value, Args, Context) -> {{ok,AcceptedValue}, NewContext} | {{error,Id,Error}, NewContext}
  30. %% Error = invalid | novalue | {script, Script} | novalidator | string()
  31. validate(username_unique, Id, Value, Args, Context) ->
  32. UserId = z_convert:to_integer(proplists:get_value(id, Args)),
  33. Username = z_string:trim(Value),
  34. case Username of
  35. [] ->
  36. {{ok, []}, Context};
  37. _ ->
  38. case m_identity:lookup_by_username(Username, Context) of
  39. undefined ->
  40. {{ok, Username}, Context};
  41. Identity ->
  42. case proplists:get_value(rsc_id, Identity) of
  43. UserId -> {{ok, Username}, Context};
  44. _Other -> {{error, Id, invalid}, Context}
  45. end
  46. end
  47. end.
  48. %% @spec event(Event, Context) -> Context
  49. %% @doc Handle the validation during form entry.
  50. event({postback, {validate, Args}, TriggerId, _TargetId}, Context) ->
  51. Value = z_context:get_q(triggervalue, Context),
  52. {IsValid, ContextValidated} = case validate(username_unique, TriggerId, Value, Args, Context) of
  53. {{ok, _}, ContextOk} ->
  54. {"true", z_render:wire({fade_out, [{target, TriggerId ++ "_username_unique_error"}]}, ContextOk)};
  55. {{error, Id, _} = Error, ContextScript} ->
  56. {"false", z_render:wire({fade_in, [{target, TriggerId ++ "_username_unique_error"}]},
  57. z_validation:report_errors([{Id,Error}], ContextScript))}
  58. end,
  59. z_script:add_script(["z_async_validation_result('",TriggerId,"', ",IsValid,", '",z_utils:js_escape(Value),"');"], ContextValidated).