PageRenderTime 1ms CodeModel.GetById 59ms app.highlight 12ms RepoModel.GetById 14ms 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
 20-export([init/0, drop/0]).
 21
 22-export([add/2,
 23         delete/2,
 24         update/2,
 25         list/1,
 26         get/2,
 27         get_by_name/2]).
 28
 29-include("uce.hrl").
 30
 31init() ->
 32    case mnesia:create_table(uce_user,
 33                             [{disc_copies, [node()]},
 34                              {type, set},
 35                              {attributes, record_info(fields, uce_user)}]) of
 36        {atomic, ok} -> ok;
 37        {aborted, {already_exists, uce_user}} -> ok
 38    end.
 39
 40add(Domain, #uce_user{id=Id} = User) ->
 41    case mnesia:dirty_write(User#uce_user{id={Id, Domain}}) of
 42        ok ->
 43            {ok, created};
 44        {aborted, _} ->
 45            throw({error, bad_parameters})
 46    end.
 47
 48delete(Domain, Id) ->
 49    case mnesia:transaction(fun() ->
 50                                    mnesia:delete({uce_user, {Id, Domain}})
 51                            end) of
 52        {atomic, ok} ->
 53            {ok, deleted};
 54        {aborted, _} ->
 55            throw({error, bad_parameters})
 56    end.
 57
 58update(Domain, #uce_user{id=Id} = User) ->
 59    case mnesia:transaction(fun() ->
 60                                    mnesia:write(User#uce_user{id={Id, Domain}})
 61                            end) of
 62        {atomic, _} ->
 63            {ok, updated};
 64        {aborted, _} ->
 65            throw({error, bad_parameters})
 66    end.
 67
 68list(Domain) ->
 69    case mnesia:dirty_match_object(#uce_user{id={'_', Domain},
 70                                             name='_',
 71                                             auth='_',
 72                                             credential='_',
 73                                             metadata='_',
 74                                             roles='_'}) of
 75        Users when is_list(Users) ->
 76            {ok, remove_domain_from_id(Users)};
 77        {aborted, _} ->
 78            throw({error, bad_parameters})
 79    end.
 80
 81get_by_name(Domain, Name) ->
 82    case mnesia:dirty_match_object(#uce_user{id={'_', Domain},
 83                                             name=Name,
 84                                             auth='_',
 85                                             credential='_',
 86                                             metadata='_',
 87                                             roles='_'}) 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
 96get(Domain, Id) ->
 97    case mnesia:dirty_read(uce_user, {Id, Domain}) of
 98        [User] ->
 99            {ok, remove_domain_from_id(User)};
100        [] ->
101            throw({error, not_found});
102        {aborted, _} ->
103            throw({error, bad_parameters})
104    end.
105
106drop() ->
107    mnesia:clear_table(uce_user).
108
109remove_domain_from_id(Users) ->
110    ?REMOVE_ID_FROM_RECORD(Users, uce_user).