PageRenderTime 42ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/backends/db/mongodb/uce_role_mongodb.erl

http://github.com/AF83/ucengine
Erlang | 82 lines | 50 code | 10 blank | 22 comment | 1 complexity | d851b85aa1af1855f1e4f77b2b910345 MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(uce_role_mongodb).
  19. -behaviour(gen_uce_role).
  20. -export([add/2,
  21. delete/2,
  22. update/2,
  23. get/2,
  24. index/1]).
  25. -include("uce.hrl").
  26. -include("mongodb.hrl").
  27. add(Domain, #uce_role{} = Role) ->
  28. mongodb_helpers:ok(emongo:insert_sync(Domain, "uce_role", to_collection(Domain, Role))),
  29. {ok, created}.
  30. update(Domain, #uce_role{id=Name} = Role) ->
  31. mongodb_helpers:updated(emongo:update_sync(Domain, "uce_role", [{"name", Name},
  32. {"domain", Domain}],
  33. to_collection(Domain, Role), false)),
  34. {ok, updated}.
  35. delete(Domain, Name) ->
  36. mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_role", [{"name", Name},
  37. {"domain", Domain}])),
  38. {ok, deleted}.
  39. get(Domain, Name) ->
  40. case emongo:find_one(Domain, "uce_role", [{"name", Name},
  41. {"domain", Domain}]) of
  42. [Record] ->
  43. {ok, from_collection(Record)};
  44. [] ->
  45. throw({error, not_found})
  46. end.
  47. from_collection(Collection) ->
  48. case utils:get(mongodb_helpers:collection_to_list(Collection),
  49. ["name", "domain", "acl"]) of
  50. [Name, _Domain, {array, ACL}] ->
  51. #uce_role{id=Name,
  52. acl=[#uce_access{object=Object, action=Action, conditions=Conditions} ||
  53. {array, [Object, Action, Conditions]} <- ACL]};
  54. _ ->
  55. throw({error, bad_parameters})
  56. end.
  57. to_collection(Domain, #uce_role{id=Name,
  58. acl=ACL}) ->
  59. [{"name", Name},
  60. {"domain", Domain},
  61. {"acl", {array, [[Object, Action, Conditions] || #uce_access{object=Object,
  62. action=Action,
  63. conditions=Conditions} <- ACL]}}].
  64. %%--------------------------------------------------------------------
  65. %% @spec (Domain::list) -> ok::atom
  66. %% @doc Create index for uce_role collection in database
  67. %% @end
  68. %%--------------------------------------------------------------------
  69. index(Domain) ->
  70. Indexes = [{"name", 1}, {"domain", 1}],
  71. emongo:ensure_index(Domain, "uce_role", Indexes),
  72. ok.