PageRenderTime 39ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/AF83/ucengine
Erlang | 155 lines | 85 code | 13 blank | 57 comment | 3 complexity | f128a35f2e4671c98cbfbc96d872f8f6 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_user_mongodb).
  19. -behaviour(gen_uce_user).
  20. -export([add/2,
  21. delete/2,
  22. update/2,
  23. list/1,
  24. get/2,
  25. get_by_name/2,
  26. index/1]).
  27. -include("uce.hrl").
  28. -include("mongodb.hrl").
  29. %%--------------------------------------------------------------------
  30. %% @spec (Domain::list, #uce_user{}) -> {ok, created}
  31. %% @doc Insert given record #uce_user{} in uce_user mongodb table
  32. %% @end
  33. %%--------------------------------------------------------------------
  34. add(Domain, #uce_user{} = User) ->
  35. mongodb_helpers:ok(emongo:insert_sync(Domain, "uce_user", to_collection(Domain, User))),
  36. {ok, created}.
  37. %%--------------------------------------------------------------------
  38. %% @spec (Domain::list, Id::list) -> {ok, deleted}
  39. %% @doc Delete uce_user record which corresponds to given id and domain
  40. %% @end
  41. %%--------------------------------------------------------------------
  42. delete(Domain, Id) ->
  43. mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_user", [{"id", Id},
  44. {"domain", Domain}])),
  45. {ok, deleted}.
  46. %%--------------------------------------------------------------------
  47. %% @spec (Domain::list, #uce_user{}) -> {ok, updated}
  48. %% @doc Update given record #uce_user{} in uce_user mongodb table
  49. %% @end
  50. %%--------------------------------------------------------------------
  51. update(Domain, #uce_user{id=Id} = User) ->
  52. mongodb_helpers:updated(emongo:update_sync(Domain, "uce_user", [{"id", Id},
  53. {"domain", Domain}],
  54. to_collection(Domain, User), false)),
  55. {ok, updated}.
  56. %%--------------------------------------------------------------------
  57. %% @spec (Domain::list) -> {ok, [#uce_user{}, #uce_user{}, ...] = Users::list} | {error, bad_parameters}
  58. %% @doc List all uce_user record for given domain
  59. %% @end
  60. %%--------------------------------------------------------------------
  61. list(Domain) ->
  62. Collections = emongo:find_all(Domain, "uce_user", [{"domain", Domain}]),
  63. Users = lists:map(fun(Collection) ->
  64. from_collection(Collection)
  65. end,
  66. Collections),
  67. {ok, Users}.
  68. %%--------------------------------------------------------------------
  69. %% @spec (Domain::list, Id::list) -> {ok, #uce_user{}} | {error, not_found} | {error, bad_parameters}
  70. %% @doc Get uce_user record for given name or id and domain
  71. %% @end
  72. %%--------------------------------------------------------------------
  73. get_by_name(Domain, Name) ->
  74. case emongo:find_one(Domain, "uce_user", [{"name", Name},
  75. {"domain", Domain}]) of
  76. [Collection] ->
  77. {ok, from_collection(Collection)};
  78. [] ->
  79. throw({error, not_found})
  80. end.
  81. get(Domain, UId) ->
  82. case emongo:find_one(Domain, "uce_user", [{"id", UId},
  83. {"domain", Domain}]) of
  84. [Collection] ->
  85. {ok, from_collection(Collection)};
  86. [] ->
  87. throw({error, not_found})
  88. end.
  89. %%--------------------------------------------------------------------
  90. %% @spec ([{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list) -> #uce_user{} | {error, bad_parameters}
  91. %% @doc Convert collection returned by mongodb to valid record #uce_user{}
  92. %% @end
  93. %%--------------------------------------------------------------------
  94. from_collection(Collection) ->
  95. case utils:get(mongodb_helpers:collection_to_list(Collection),
  96. ["id", "name", "domain", "auth", "credential", "metadata", "roles"]) of
  97. [Id, Name, _Domain, Auth, Credential, Metadata, {array, Roles}] ->
  98. Metadata2 = case Metadata of
  99. [] ->
  100. {struct, []};
  101. _ ->
  102. Metadata
  103. end,
  104. #uce_user{id=Id,
  105. name=Name,
  106. auth=Auth,
  107. credential=Credential,
  108. metadata=Metadata2,
  109. roles=[{Role, Location} || {array, [Role, Location]} <- Roles]};
  110. _ ->
  111. throw({error, bad_parameters})
  112. end.
  113. %%--------------------------------------------------------------------
  114. %% @spec (#uce_user{}) -> [{Key::list, Value::list}, {Key::list, Value::list}, ...] = Collection::list
  115. %% @doc Convert #uce_user{} record to valid collection
  116. %% @end
  117. %%--------------------------------------------------------------------
  118. to_collection(Domain, #uce_user{id=Id,
  119. name=Name,
  120. auth=Auth,
  121. credential=Credential,
  122. metadata=Metadata,
  123. roles=Roles}) ->
  124. [{"id", Id},
  125. {"name", Name},
  126. {"domain", Domain},
  127. {"auth", Auth},
  128. {"credential", Credential},
  129. {"metadata", mongodb_helpers:to_bson(Metadata)},
  130. {"roles", {array, [{array, [Role, Location]} || {Role, Location} <- Roles]}}].
  131. %%--------------------------------------------------------------------
  132. %% @spec (Domain::list) -> ok::atom
  133. %% @doc Create index for uce_user collection in database
  134. %% @end
  135. %%--------------------------------------------------------------------
  136. index(Domain) ->
  137. Indexes = [
  138. [{"id", 1}, {"domain", 1}],
  139. [{"name", 1}, {"domain", 1}]
  140. ],
  141. [emongo:ensure_index(Domain, "uce_user", Index) || Index <- Indexes],
  142. ok.