/src/models/m_acl.erl

https://code.google.com/p/zotonic/ · Erlang · 64 lines · 29 code · 10 blank · 25 comment · 6 complexity · b29f870a598f1158c2763006cb431ae1 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% Date: 2009-04-27
  4. %%
  5. %% @doc Template access for access control functions and state
  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. -module(m_acl).
  20. -author("Marc Worrell <marc@worrell.nl").
  21. -behaviour(gen_model).
  22. %% interface functions
  23. -export([
  24. m_find_value/3,
  25. m_to_list/2,
  26. m_value/2
  27. ]).
  28. -include_lib("zotonic.hrl").
  29. %% @doc Fetch the value for the key from a model source
  30. %% @spec m_find_value(Key, Source, Context) -> term()
  31. m_find_value(user, #m{value=undefined}, Context) ->
  32. z_acl:user(Context);
  33. m_find_value(is_admin, #m{value=undefined}, Context) ->
  34. z_acl:is_allowed(use, mod_admin_config, Context);
  35. m_find_value(Action, #m{value=undefined} = M, _Context)
  36. when Action == use orelse Action == admin orelse Action == view
  37. orelse Action == delete orelse Action == update orelse Action == insert ->
  38. M#m{value={is_allowed, Action}};
  39. m_find_value(is_allowed, #m{value=undefined} = M, _Context) ->
  40. M#m{value=is_allowed};
  41. m_find_value(Action, #m{value=is_allowed} = M, _Context) ->
  42. M#m{value={is_allowed, Action}};
  43. m_find_value(Object, #m{value={is_allowed, Action}}, Context) when is_binary(Object) ->
  44. z_acl:is_allowed(Action, z_convert:to_atom(Object), Context);
  45. m_find_value(Object, #m{value={is_allowed, Action}}, Context) ->
  46. z_acl:is_allowed(Action, Object, Context).
  47. %% @doc Transform a m_config value to a list, used for template loops
  48. %% @spec m_to_list(Source, Context) -> List
  49. m_to_list(_, _Context) ->
  50. [].
  51. %% @doc Transform a model value so that it can be formatted or piped through filters
  52. %% @spec m_value(Source, Context) -> term()
  53. m_value(#m{value=undefined}, _Context) ->
  54. undefined.