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