PageRenderTime 143ms CodeModel.GetById 100ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 1ms

/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
20-behaviour(gen_uce_role).
21
22-export([add/2,
23         delete/2,
24         update/2,
25         get/2,
26         index/1]).
27
28-include("uce.hrl").
29-include("mongodb.hrl").
30
31add(Domain, #uce_role{} = Role) ->
32    mongodb_helpers:ok(emongo:insert_sync(Domain, "uce_role", to_collection(Domain, Role))),
33    {ok, created}.
34
35update(Domain, #uce_role{id=Name} = Role) ->
36    mongodb_helpers:updated(emongo:update_sync(Domain, "uce_role", [{"name", Name},
37                                                                    {"domain", Domain}],
38                                               to_collection(Domain, Role), false)),
39    {ok, updated}.
40
41delete(Domain, Name) ->
42    mongodb_helpers:ok(emongo:delete_sync(Domain, "uce_role", [{"name", Name},
43                                                               {"domain", Domain}])),
44    {ok, deleted}.
45
46get(Domain, Name) ->
47    case emongo:find_one(Domain, "uce_role", [{"name", Name},
48                                              {"domain", Domain}]) of
49        [Record] ->
50            {ok, from_collection(Record)};
51        [] ->
52            throw({error, not_found})
53    end.
54
55from_collection(Collection) ->
56    case utils:get(mongodb_helpers:collection_to_list(Collection),
57                   ["name", "domain", "acl"]) of
58        [Name, _Domain, {array, ACL}] ->
59            #uce_role{id=Name,
60                      acl=[#uce_access{object=Object, action=Action, conditions=Conditions} ||
61                              {array, [Object, Action, Conditions]} <- ACL]};
62        _ ->
63            throw({error, bad_parameters})
64    end.
65
66to_collection(Domain, #uce_role{id=Name,
67                                acl=ACL}) ->
68    [{"name", Name},
69     {"domain", Domain},
70     {"acl", {array, [[Object, Action, Conditions] || #uce_access{object=Object,
71                                                                   action=Action,
72                                                                   conditions=Conditions} <- ACL]}}].
73
74%%--------------------------------------------------------------------
75%% @spec (Domain::list) -> ok::atom
76%% @doc Create index for uce_role collection in database
77%% @end
78%%--------------------------------------------------------------------
79index(Domain) ->
80    Indexes = [{"name", 1}, {"domain", 1}],
81    emongo:ensure_index(Domain, "uce_role", Indexes),
82    ok.