./ct_report/coverage/eldap_pool.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% File : eldap_pool.erl
3 %%% Author : Evgeniy Khramtsov <xram@jabber.ru>
4 %%% Purpose : LDAP connections pool
5 %%% Created : 12 Nov 2006 by Evgeniy Khramtsov <xram@jabber.ru>
6 %%%
7 %%%
8 %%% ejabberd, Copyright (C) 2002-2013 ProcessOne
9 %%%
10 %%% This program is free software; you can redistribute it and/or
11 %%% modify it under the terms of the GNU General Public License as
12 %%% published by the Free Software Foundation; either version 2 of the
13 %%% License, or (at your option) any later version.
14 %%%
15 %%% This program is distributed in the hope that it will be useful,
16 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
17 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 %%% General Public License for more details.
19 %%%
20 %%% You should have received a copy of the GNU General Public License
21 %%% along with this program; if not, write to the Free Software
22 %%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 %%%
24 %%%-------------------------------------------------------------------
25
26 -module(eldap_pool).
27
28 -author('xram@jabber.ru').
29
30 %% API
31 -export([bind/3, search/2, delete/2, add/3, modify_passwd/3]).
32 -import(eldap_utils, [maybe_b2list/1]).
33 -include_lib("eldap/include/eldap.hrl").
34
35 %%====================================================================
36 %% API
37 %%====================================================================
38
39 bind(PoolName, DN, Passwd) ->
40
:-(
do_request(PoolName, {simple_bind, [maybe_b2list(DN), maybe_b2list(Passwd)]}).
41
42 parse_search_opts(Opts) ->
43
:-(
[parse_opt(O) || O <- Opts].
44
45
:-(
parse_opt({base, Bin}) -> {base, maybe_b2list(Bin)};
46
:-(
parse_opt({attributes, BinList}) -> {attributes, [maybe_b2list(B) || B <- BinList]};
47
:-(
parse_opt({Atom, List}) -> {Atom, List}.
48
49 search(PoolName, Opts) ->
50
:-(
parse_search_result(do_request(PoolName, {search, [parse_search_opts(Opts)]})).
51
52 parse_search_result({ok, #eldap_search_result{entries = Entries, referrals = Refs}}) ->
53
:-(
#eldap_search_result{entries = parse_entries(Entries), referrals = parse_refs(Refs)};
54 parse_search_result(R) ->
55
:-(
R.
56
57 parse_entries(Entries) ->
58
:-(
[#eldap_entry{object_name = list_to_binary(Obj), attributes = parse_attrs(Attrs)} ||
59
:-(
#eldap_entry{object_name = Obj, attributes = Attrs} <- Entries].
60
61 parse_attrs(Attrs) ->
62
:-(
[{list_to_binary(Name), parse_values(Values)} || {Name, Values} <- Attrs].
63
64 parse_values(Values) ->
65
:-(
[list_to_binary(V) || V <- Values].
66
67
:-(
parse_refs(R) -> R.
68
69
70 modify_passwd(PoolName, DN, Passwd) ->
71
:-(
do_request(PoolName, {modify_password, [maybe_b2list(DN), maybe_b2list(Passwd)]}).
72
73
74 delete(PoolName, DN) ->
75
:-(
case do_request(PoolName, {delete, [maybe_b2list(DN)]}) of
76
:-(
false -> not_exists;
77
:-(
R -> R
78 end.
79
80 %% Applies eldap:add/3
81 add(PoolName, DN, Attrs) ->
82
:-(
do_request(PoolName, {add, [maybe_b2list(DN), parse_add_atrs(Attrs)]}).
83
84 parse_add_atrs(Attrs) ->
85
:-(
[parse_add_attr(A) || A <- Attrs].
86
87 parse_add_attr({N, List}) ->
88
:-(
{maybe_b2list(N), [maybe_b2list(L) || L <- List]}.
89
90
91 %%====================================================================
92 %% Internal functions
93 %%====================================================================
94
95 %% Calls mongoose_ldap_worker
96 %% Which calls eldap:F
97 do_request({Host, PoolTag}, {_F, _Args} = Request) ->
98
:-(
mongoose_wpool:call(ldap, Host, PoolTag, Request).
Line Hits Source