PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/ucengine/src/backends/db/mnesia/uce_user_mnesia.erl

http://github.com/AF83/ucengine
Erlang | 110 lines | 81 code | 12 blank | 17 comment | 0 complexity | 7c1a837e73a4f366edf427b54b1ffbaf 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_mnesia).
  19. -export([init/0, drop/0]).
  20. -export([add/2,
  21. delete/2,
  22. update/2,
  23. list/1,
  24. get/2,
  25. get_by_name/2]).
  26. -include("uce.hrl").
  27. init() ->
  28. case mnesia:create_table(uce_user,
  29. [{disc_copies, [node()]},
  30. {type, set},
  31. {attributes, record_info(fields, uce_user)}]) of
  32. {atomic, ok} -> ok;
  33. {aborted, {already_exists, uce_user}} -> ok
  34. end.
  35. add(Domain, #uce_user{id=Id} = User) ->
  36. case mnesia:dirty_write(User#uce_user{id={Id, Domain}}) of
  37. ok ->
  38. {ok, created};
  39. {aborted, _} ->
  40. throw({error, bad_parameters})
  41. end.
  42. delete(Domain, Id) ->
  43. case mnesia:transaction(fun() ->
  44. mnesia:delete({uce_user, {Id, Domain}})
  45. end) of
  46. {atomic, ok} ->
  47. {ok, deleted};
  48. {aborted, _} ->
  49. throw({error, bad_parameters})
  50. end.
  51. update(Domain, #uce_user{id=Id} = User) ->
  52. case mnesia:transaction(fun() ->
  53. mnesia:write(User#uce_user{id={Id, Domain}})
  54. end) of
  55. {atomic, _} ->
  56. {ok, updated};
  57. {aborted, _} ->
  58. throw({error, bad_parameters})
  59. end.
  60. list(Domain) ->
  61. case mnesia:dirty_match_object(#uce_user{id={'_', Domain},
  62. name='_',
  63. auth='_',
  64. credential='_',
  65. metadata='_',
  66. roles='_'}) of
  67. Users when is_list(Users) ->
  68. {ok, remove_domain_from_id(Users)};
  69. {aborted, _} ->
  70. throw({error, bad_parameters})
  71. end.
  72. get_by_name(Domain, Name) ->
  73. case mnesia:dirty_match_object(#uce_user{id={'_', Domain},
  74. name=Name,
  75. auth='_',
  76. credential='_',
  77. metadata='_',
  78. roles='_'}) of
  79. [User] ->
  80. {ok, remove_domain_from_id(User)};
  81. [] ->
  82. throw({error, not_found});
  83. {aborted, _} ->
  84. throw({error, bad_parameters})
  85. end.
  86. get(Domain, Id) ->
  87. case mnesia:dirty_read(uce_user, {Id, Domain}) of
  88. [User] ->
  89. {ok, remove_domain_from_id(User)};
  90. [] ->
  91. throw({error, not_found});
  92. {aborted, _} ->
  93. throw({error, bad_parameters})
  94. end.
  95. drop() ->
  96. mnesia:clear_table(uce_user).
  97. remove_domain_from_id(Users) ->
  98. ?REMOVE_ID_FROM_RECORD(Users, uce_user).