merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / config / ldap / ldapuserprofilebe.cxx
blob5826188c564dd6d9e14e834b0401c56f1b7d0e13
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org 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
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
31 #include "ldapaccess.hxx"
32 #include "ldapuserprofilebe.hxx"
33 #include <osl/file.hxx>
34 #include <osl/module.hxx>
35 #include <osl/process.h>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/byteseq.h>
39 #ifndef INCLUDED_RTL_INSTANCE_HXX_
40 #include <rtl/instance.hxx>
41 #endif
42 #include <com/sun/star/beans/NamedValue.hpp>
43 #include <com/sun/star/beans/Optional.hpp>
44 #include <osl/security.hxx>
46 //==============================================================================
47 namespace extensions { namespace config { namespace ldap {
49 LdapUserProfileBe::LdapUserProfileBe( const uno::Reference<uno::XComponentContext>& xContext)
50 : LdapProfileMutexHolder(),
51 BackendBase(mMutex)
53 LdapDefinition aDefinition;
54 rtl::OUString loggedOnUser;
56 // This whole rigmarole is to prevent an infinite recursion where reading
57 // the configuration for the backend would create another instance of the
58 // backend, which would try and read the configuration which would...
60 osl::Mutex & aInitMutex = rtl::Static< osl::Mutex, LdapUserProfileBe >::get();
61 osl::MutexGuard aInitGuard(aInitMutex);
63 static bool bReentrantCall; // = false
64 OSL_ENSURE(!bReentrantCall, "configuration: Ldap Backend constructor called reentrantly - probably a registration error.");
66 if (!bReentrantCall)
68 try
70 bReentrantCall = true ;
71 if (!readLdapConfiguration(
72 css::uno::Reference< css::lang::XMultiServiceFactory >(
73 xContext->getServiceManager(),
74 css::uno::UNO_QUERY_THROW),
75 &aDefinition, &loggedOnUser))
77 throw css::uno::RuntimeException(
78 rtl::OUString::createFromAscii("LdapUserProfileBe- LDAP not configured"),
79 NULL);
82 bReentrantCall = false ;
84 catch (...)
86 bReentrantCall = false;
87 throw;
92 LdapConnection connection;
93 connection.loadModule();
94 connection.connectSimple(aDefinition);
95 connection.getUserProfile(loggedOnUser, &data_);
97 //------------------------------------------------------------------------------
98 LdapUserProfileBe::~LdapUserProfileBe()
101 //------------------------------------------------------------------------------
103 bool LdapUserProfileBe::readLdapConfiguration(
104 css::uno::Reference< css::lang::XMultiServiceFactory > const & factory,
105 LdapDefinition * definition, rtl::OUString * loggedOnUser)
107 OSL_ASSERT(factory.is() && definition != 0 && loggedOnUser != 0);
108 const rtl::OUString kConfigurationProviderService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationProvider")) ;
109 const rtl::OUString kReadOnlyViewService( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.ConfigurationAccess")) ;
110 const rtl::OUString kComponent( RTL_CONSTASCII_USTRINGPARAM("org.openoffice.LDAP/UserDirectory"));
111 const rtl::OUString kServerDefiniton(RTL_CONSTASCII_USTRINGPARAM ("ServerDefinition"));
112 const rtl::OUString kServer(RTL_CONSTASCII_USTRINGPARAM ("Server"));
113 const rtl::OUString kPort(RTL_CONSTASCII_USTRINGPARAM("Port"));
114 const rtl::OUString kBaseDN(RTL_CONSTASCII_USTRINGPARAM("BaseDN"));
115 const rtl::OUString kUser(RTL_CONSTASCII_USTRINGPARAM("SearchUser"));
116 const rtl::OUString kPassword(RTL_CONSTASCII_USTRINGPARAM("SearchPassword"));
117 const rtl::OUString kUserObjectClass(RTL_CONSTASCII_USTRINGPARAM("UserObjectClass"));
118 const rtl::OUString kUserUniqueAttr(RTL_CONSTASCII_USTRINGPARAM("UserUniqueAttribute"));
120 uno::Reference< XInterface > xIface;
123 uno::Reference< lang::XMultiServiceFactory > xCfgProvider(
124 factory->createInstance(kConfigurationProviderService),
125 uno::UNO_QUERY);
126 OSL_ENSURE(xCfgProvider.is(),"LdapUserProfileBe: could not create the configuration provider");
127 if (!xCfgProvider.is())
128 return false;
130 css::beans::NamedValue aPath(rtl::OUString::createFromAscii("nodepath"), uno::makeAny(kComponent) );
132 uno::Sequence< uno::Any > aArgs(1);
133 aArgs[0] <<= aPath;
135 xIface = xCfgProvider->createInstanceWithArguments(kReadOnlyViewService, aArgs);
137 uno::Reference<container::XNameAccess > xAccess(xIface, uno::UNO_QUERY_THROW);
138 xAccess->getByName(kServerDefiniton) >>= xIface;
140 uno::Reference<container::XNameAccess > xChildAccess(xIface, uno::UNO_QUERY_THROW);
142 if (!getLdapStringParam(xChildAccess, kServer, definition->mServer))
143 return false;
144 if (!getLdapStringParam(xChildAccess, kBaseDN, definition->mBaseDN))
145 return false;
147 definition->mPort=0;
148 xChildAccess->getByName(kPort) >>= definition->mPort ;
149 if (definition->mPort == 0)
150 return false;
152 if (!getLdapStringParam(xAccess, kUserObjectClass, definition->mUserObjectClass))
153 return false;
154 if (!getLdapStringParam(xAccess, kUserUniqueAttr, definition->mUserUniqueAttr))
155 return false;
157 getLdapStringParam(xAccess, kUser, definition->mAnonUser);
158 getLdapStringParam(xAccess, kPassword, definition->mAnonCredentials);
160 catch (uno::Exception & e)
162 OSL_TRACE("LdapUserProfileBackend: access to configuration data failed: %s",
163 rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
164 return false;
167 osl::Security aSecurityContext;
168 if (!aSecurityContext.getUserName(*loggedOnUser))
169 OSL_TRACE("LdapUserProfileBackend - could not get Logged on user from system");
171 sal_Int32 nIndex = loggedOnUser->indexOf('/');
172 if (nIndex > 0)
173 *loggedOnUser = loggedOnUser->copy(nIndex+1);
175 //Remember to remove
176 OSL_TRACE("Logged on user is %s", rtl::OUStringToOString(*loggedOnUser,RTL_TEXTENCODING_ASCII_US).getStr());
178 return true;
181 //------------------------------------------------------------------------------
182 bool LdapUserProfileBe::getLdapStringParam(
183 uno::Reference<container::XNameAccess>& xAccess,
184 const rtl::OUString& aLdapSetting,
185 rtl::OString& aServerParameter)
187 rtl::OUString sParam;
188 xAccess->getByName(aLdapSetting) >>= sParam;
189 aServerParameter = rtl::OUStringToOString(sParam, RTL_TEXTENCODING_ASCII_US);
191 return aServerParameter.getLength() != 0;
193 //------------------------------------------------------------------------------
194 void LdapUserProfileBe::setPropertyValue(
195 rtl::OUString const &, css::uno::Any const &)
196 throw (
197 css::beans::UnknownPropertyException, css::beans::PropertyVetoException,
198 css::lang::IllegalArgumentException, css::lang::WrappedTargetException,
199 css::uno::RuntimeException)
201 throw css::lang::IllegalArgumentException(
202 rtl::OUString(
203 RTL_CONSTASCII_USTRINGPARAM("setPropertyValue not supported")),
204 static_cast< cppu::OWeakObject * >(this), -1);
207 css::uno::Any LdapUserProfileBe::getPropertyValue(
208 rtl::OUString const & PropertyName)
209 throw (
210 css::beans::UnknownPropertyException, css::lang::WrappedTargetException,
211 css::uno::RuntimeException)
213 for (sal_Int32 i = 0;;) {
214 sal_Int32 j = PropertyName.indexOf(',', i);
215 if (j == -1) {
216 j = PropertyName.getLength();
218 if (j == i) {
219 throw css::beans::UnknownPropertyException(
220 PropertyName, static_cast< cppu::OWeakObject * >(this));
222 LdapData::iterator k(data_.find(PropertyName.copy(i, j - i)));
223 if (k != data_.end()) {
224 return css::uno::makeAny(
225 css::beans::Optional< css::uno::Any >(
226 true, css::uno::makeAny(k->second)));
228 if (j == PropertyName.getLength()) {
229 break;
231 i = j + 1;
233 return css::uno::makeAny(css::beans::Optional< css::uno::Any >());
236 //------------------------------------------------------------------------------
237 rtl::OUString SAL_CALL LdapUserProfileBe::getLdapUserProfileBeName(void) {
238 return rtl::OUString::createFromAscii("com.sun.star.comp.configuration.backend.LdapUserProfileBe") ;
240 //------------------------------------------------------------------------------
242 rtl::OUString SAL_CALL LdapUserProfileBe::getImplementationName(void)
243 throw (uno::RuntimeException)
245 return getLdapUserProfileBeName() ;
247 //------------------------------------------------------------------------------
249 uno::Sequence<rtl::OUString> SAL_CALL LdapUserProfileBe::getLdapUserProfileBeServiceNames(void)
251 uno::Sequence<rtl::OUString> aServices(1) ;
252 aServices[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.configuration.backend.LdapUserProfileBe")) ;
253 return aServices ;
255 //------------------------------------------------------------------------------
257 sal_Bool SAL_CALL LdapUserProfileBe::supportsService(const rtl::OUString& aServiceName)
258 throw (uno::RuntimeException)
260 uno::Sequence< rtl::OUString > const svc = getLdapUserProfileBeServiceNames();
262 for(sal_Int32 i = 0; i < svc.getLength(); ++i )
263 if(svc[i] == aServiceName)
264 return true;
265 return false;
268 //------------------------------------------------------------------------------
270 uno::Sequence<rtl::OUString>
271 SAL_CALL LdapUserProfileBe::getSupportedServiceNames(void)
272 throw (uno::RuntimeException)
274 return getLdapUserProfileBeServiceNames() ;
276 // ---------------------------------------------------------------------------------------
278 // ---------------------------------------------------------------------------------------