1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ldapuserprof.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "ldapuserprof.hxx"
34 #include "ldapaccess.hxx"
35 namespace extensions
{ namespace config
{ namespace ldap
{
36 //==============================================================================
38 //------------------------------------------------------------------------------
41 Finds the next line in a buffer and returns it, along with a
42 modified version of the buffer with the line removed.
44 @param aString string to extract the next line from
45 @param aLine next line
46 @return sal_True if a line has been extracted, sal_False otherwise
48 static sal_Bool
getNextLine(rtl::OString
& aString
,
51 aString
= aString
.trim() ;
52 const sal_Char
*currentChar
= aString
;
53 const sal_Char
*endChar
= currentChar
+ aString
.getLength() ;
54 sal_Int32 lineThreshold
= 0 ;
56 while (currentChar
< endChar
&&
57 *currentChar
!= '\r' && *currentChar
!= '\n') { ++ currentChar
; }
58 lineThreshold
= currentChar
- static_cast<const sal_Char
*>(aString
) ;
59 if (lineThreshold
== 0) { return sal_False
; }
60 aLine
= aString
.copy(0, lineThreshold
) ;
61 aString
= aString
.copy(lineThreshold
) ;
64 //------------------------------------------------------------------------------
66 LdapUserProfileMap::~LdapUserProfileMap(void)
68 // No need to delete the contents of the mAttributes array,
69 // since they refer to rtl::OStrings stored in the mLdapAttributes
71 if (mAttributes
!= NULL
)
73 delete [] mAttributes
;
76 //------------------------------------------------------------------------------
78 void LdapUserProfileMap::source(const rtl::OString
& aMap
)
80 if (mAttributes
!= NULL
)
82 delete [] mAttributes
; mAttributes
= NULL
;
85 rtl::OString currentLine
;
86 rtl::OString buffer
= aMap
;
87 std::set
<rtl::OString
> attributes
;
90 // First, parse the buffer to find all the mapping definitions.
91 // While we're at it, we collect the list of unique LDAP attributes
92 // involved in the mapping.
93 while (getNextLine(buffer
, currentLine
))
95 addNewMapping(currentLine
, attributes
, prefix
) ;
97 // Now we use the list of attributes to build mAttributes
98 mAttributes
= new const sal_Char
* [attributes
.size() + 1] ;
99 std::set
<rtl::OString
>::const_iterator attribute
;
102 for (attribute
= attributes
.begin() ;
103 attribute
!= attributes
.end() ; ++ attribute
)
105 mAttributes
[i
++] = static_cast<const sal_Char
*>(*attribute
) ;
107 mAttributes
[i
] = NULL
;
109 //------------------------------------------------------------------------------
111 void LdapUserProfileMap::ldapToUserProfile(LDAP
*aConnection
,
113 LdapUserProfile
& aProfile
) const
115 if (aEntry
== NULL
) { return ; }
116 // Ensure return value has proper size
117 aProfile
.mProfile
.resize(mMapping
.size()) ;
118 sal_Char
**values
= NULL
;
120 for (sal_uInt32 i
= 0 ; i
< mMapping
.size() ; ++ i
)
122 aProfile
.mProfile
[i
].mAttribute
= rtl::OStringToOUString(
123 mMapping
[i
].mProfileElement
,
124 RTL_TEXTENCODING_ASCII_US
);
125 rtl::OUString debugStr
= aProfile
.mProfile
[i
].mAttribute
;
127 for (sal_uInt32 j
= 0 ;
128 j
< mMapping
[i
].mLdapAttributes
.size() ; ++ j
)
130 values
= (*LdapConnection::s_p_get_values
)(aConnection
, aEntry
,
131 mMapping
[i
].mLdapAttributes
[j
]) ;
135 aProfile
.mProfile
[i
].mValue
= rtl::OStringToOUString(
136 *values
, RTL_TEXTENCODING_UTF8
);
137 (*LdapConnection::s_p_value_free
)(values
);
143 //------------------------------------------------------------------------------
144 void LdapUserProfileMap::addNewMapping(const rtl::OString
& aLine
,
145 std::set
<rtl::OString
>& aLdapAttributes
,
146 rtl::OString
& aPrefix
)
148 if (aLine
.getStr() [0] == '#') { return ; }
149 sal_Int32 prefixLength
= aPrefix
.getLength() ;
151 if (prefixLength
== 0)
153 sal_Int32 firstSlash
= aLine
.indexOf('/') ;
155 if (firstSlash
== -1) { return ; }
156 sal_Int32 secondSlash
= aLine
.indexOf('/', firstSlash
+ 1) ;
158 if (secondSlash
== -1){ return; }
162 rtl::OUString::createFromAscii(aLine
.copy(0, firstSlash
)) ;
164 rtl::OUString::createFromAscii(aLine
.copy(firstSlash
+ 1,
165 secondSlash
- firstSlash
- 1)) ;
166 aPrefix
= aLine
.copy(0, secondSlash
+ 1) ;
167 prefixLength
= secondSlash
+ 1 ;
170 else if (aLine
.compareTo(aPrefix
, prefixLength
) != 0)
174 mMapping
.push_back(Mapping()) ;
175 if (!mMapping
.back().parse(aLine
.copy(prefixLength
)))
177 mMapping
.pop_back() ;
181 const std::vector
<rtl::OString
>& attributes
=
182 mMapping
.back().mLdapAttributes
;
183 std::vector
<rtl::OString
>::const_iterator ldapAttribute
;
185 for (ldapAttribute
= attributes
.begin() ;
186 ldapAttribute
!= attributes
.end() ; ++ ldapAttribute
)
188 aLdapAttributes
.insert(*ldapAttribute
) ;
192 //------------------------------------------------------------------------------
194 static sal_Char kMappingSeparator
= '=' ;
195 static sal_Char kLdapMapSeparator
= ',' ;
197 sal_Bool
LdapUserProfileMap::Mapping::parse(const rtl::OString
& aLine
)
199 sal_Int32 index
= aLine
.indexOf(kMappingSeparator
) ;
206 sal_Int32 oldIndex
= index
+ 1 ;
208 mProfileElement
= aLine
.copy(0, index
).trim() ;
209 mLdapAttributes
.clear() ;
210 index
= aLine
.indexOf(kLdapMapSeparator
, oldIndex
) ;
213 mLdapAttributes
.push_back(
214 aLine
.copy(oldIndex
, index
- oldIndex
).trim()) ;
215 oldIndex
= index
+ 1 ;
216 index
= aLine
.indexOf(kLdapMapSeparator
, oldIndex
) ;
218 rtl::OString endOfLine
= aLine
.copy(oldIndex
).trim() ;
220 if (endOfLine
.getLength() > 0)
222 mLdapAttributes
.push_back(endOfLine
) ;
226 //------------------------------------------------------------------------------
228 } } } // extensiond.config.ldap