Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "syscreds.hxx"
21 : : #include "com/sun/star/beans/PropertyValue.hpp"
22 : :
23 : : using namespace com::sun::star;
24 : :
25 : 2 : SysCredentialsConfigItem::SysCredentialsConfigItem(
26 : : SysCredentialsConfig * pOwner )
27 : : : utl::ConfigItem( rtl::OUString("Office.Common/Passwords"),
28 : : CONFIG_MODE_IMMEDIATE_UPDATE ),
29 : : m_bInited( false ),
30 [ + - ][ + - ]: 2 : m_pOwner( pOwner )
[ + - ]
31 : : {
32 [ + - ]: 2 : uno::Sequence< ::rtl::OUString > aNode( 1 );
33 [ + - ]: 2 : aNode[ 0 ] = rtl::OUString( "Office.Common/Passwords/AuthenticateUsingSystemCredentials" );
34 [ + - ][ + - ]: 2 : EnableNotification( aNode );
35 : 2 : }
36 : :
37 : : //virtual
38 : 0 : void SysCredentialsConfigItem::Notify(
39 : : const uno::Sequence< rtl::OUString > & /*seqPropertyNames*/ )
40 : : {
41 : : {
42 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
43 : 0 : m_bInited = false;
44 : : // rebuild m_seqURLs
45 [ # # ][ # # ]: 0 : getSystemCredentialsURLs();
[ # # ]
46 : : }
47 : 0 : m_pOwner->persistentConfigChanged();
48 : 0 : }
49 : :
50 : 0 : void SysCredentialsConfigItem::Commit()
51 : : {
52 : : // does nothing
53 : 0 : }
54 : :
55 : : uno::Sequence< rtl::OUString >
56 : 0 : SysCredentialsConfigItem::getSystemCredentialsURLs()
57 : : {
58 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
59 [ # # ]: 0 : if ( !m_bInited )
60 : : {
61 : : // read config item
62 [ # # ]: 0 : uno::Sequence< ::rtl::OUString > aPropNames( 1 );
63 [ # # ]: 0 : aPropNames[ 0 ] = rtl::OUString( "AuthenticateUsingSystemCredentials" );
64 : : uno::Sequence< uno::Any > aAnyValues(
65 [ # # ]: 0 : utl::ConfigItem::GetProperties( aPropNames ) );
66 : :
67 : : OSL_ENSURE(
68 : : aAnyValues.getLength() == 1,
69 : : "SysCredentialsConfigItem::getSystemCredentialsURLs: "
70 : : "Error reading config item!" );
71 : :
72 [ # # ]: 0 : uno::Sequence< rtl::OUString > aValues;
73 [ # # ][ # # ]: 0 : if ( ( aAnyValues[ 0 ] >>= aValues ) ||
[ # # # # ]
[ # # ]
74 [ # # ]: 0 : ( !aAnyValues[ 0 ].hasValue() ) )
75 : : {
76 [ # # ]: 0 : m_seqURLs = aValues;
77 : 0 : m_bInited = true;
78 [ # # ][ # # ]: 0 : }
[ # # ]
79 : : }
80 [ # # ][ # # ]: 0 : return m_seqURLs;
81 : : }
82 : :
83 : 0 : void SysCredentialsConfigItem::setSystemCredentialsURLs(
84 : : const uno::Sequence< rtl::OUString > & seqURLList )
85 : : {
86 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
87 : :
88 : : // write config item.
89 [ # # ]: 0 : uno::Sequence< rtl::OUString > aPropNames( 1 );
90 [ # # ]: 0 : uno::Sequence< uno::Any > aPropValues( 1 );
91 [ # # ]: 0 : aPropNames[ 0 ] = ::rtl::OUString( "AuthenticateUsingSystemCredentials" );
92 [ # # ][ # # ]: 0 : aPropValues[ 0 ] <<= seqURLList;
93 : :
94 [ # # ]: 0 : utl::ConfigItem::SetModified();
95 [ # # ]: 0 : utl::ConfigItem::PutProperties( aPropNames, aPropValues );
96 : :
97 [ # # ]: 0 : m_seqURLs = seqURLList;
98 [ # # ][ # # ]: 0 : m_bInited = true;
[ # # ]
99 : 0 : }
100 : :
101 : : //============================================================================
102 : :
103 : : namespace
104 : : {
105 : : // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
106 : 0 : bool removeLastSegment( ::rtl::OUString & aURL )
107 : : {
108 : 0 : sal_Int32 aInd = aURL.lastIndexOf( sal_Unicode( '/' ) );
109 : :
110 [ # # ]: 0 : if( aInd > 0 )
111 : : {
112 : 0 : sal_Int32 aPrevInd = aURL.lastIndexOf( sal_Unicode( '/' ), aInd );
113 [ # # ]: 0 : if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
[ # # # # ]
114 : 0 : aInd != aURL.getLength() - 1 )
115 : : {
116 : 0 : aURL = aURL.copy( 0, aInd );
117 : 0 : return true;
118 : : }
119 : : }
120 : :
121 : 0 : return false;
122 : : }
123 : :
124 : 0 : bool findURL( StringSet const & rContainer, rtl::OUString const & aURL, rtl::OUString & aResult )
125 : : {
126 : : // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
127 [ # # ][ # # ]: 0 : if( !rContainer.empty() && !aURL.isEmpty() )
[ # # ]
128 : : {
129 : 0 : ::rtl::OUString aUrl( aURL );
130 : :
131 : : // each iteration remove last '/...' section from the aUrl
132 : : // while it's possible, up to the most left '://'
133 [ # # # # ]: 0 : do
[ # # ]
134 : : {
135 : : // first look for <url>/somename and then look for <url>/somename/...
136 [ # # ]: 0 : StringSet::const_iterator aIter = rContainer.find( aUrl );
137 [ # # ]: 0 : if( aIter != rContainer.end() )
138 : : {
139 : 0 : aResult = *aIter;
140 : 0 : return true;
141 : : }
142 : : else
143 : : {
144 : 0 : ::rtl::OUString tmpUrl( aUrl );
145 [ # # ]: 0 : if ( tmpUrl.getStr()[tmpUrl.getLength() - 1] != (sal_Unicode)'/' )
146 : 0 : tmpUrl += ::rtl::OUString("/");
147 : :
148 [ # # ]: 0 : aIter = rContainer.lower_bound( tmpUrl );
149 [ # # ][ # # ]: 0 : if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
[ # # ][ # # ]
150 : : {
151 : 0 : aResult = *aIter;
152 : 0 : return true;
153 [ # # ]: 0 : }
154 : : }
155 : : }
156 [ # # ][ # # ]: 0 : while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
157 : : }
158 : 0 : aResult = rtl::OUString();
159 : 0 : return false;
160 : : }
161 : :
162 : : } // namespace
163 : :
164 : 2 : SysCredentialsConfig::SysCredentialsConfig()
165 : : : m_aConfigItem( this ),
166 [ + - ][ + - ]: 2 : m_bCfgInited( false )
[ + - ]
167 : : {
168 : 2 : }
169 : :
170 : 0 : void SysCredentialsConfig::initCfg()
171 : : {
172 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
173 [ # # ]: 0 : if ( !m_bCfgInited )
174 : : {
175 : : uno::Sequence< rtl::OUString > aURLs(
176 [ # # ]: 0 : m_aConfigItem.getSystemCredentialsURLs() );
177 [ # # ]: 0 : for ( sal_Int32 n = 0; n < aURLs.getLength(); ++n )
178 [ # # ][ # # ]: 0 : m_aCfgContainer.insert( aURLs[ n ] );
179 : :
180 [ # # ]: 0 : m_bCfgInited = true;
181 [ # # ]: 0 : }
182 : 0 : }
183 : :
184 : 0 : void SysCredentialsConfig::writeCfg()
185 : : {
186 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
187 : :
188 : : OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" );
189 : :
190 [ # # ]: 0 : uno::Sequence< rtl::OUString > aURLs( m_aCfgContainer.size() );
191 : 0 : StringSet::const_iterator it = m_aCfgContainer.begin();
192 : 0 : const StringSet::const_iterator end = m_aCfgContainer.end();
193 : 0 : sal_Int32 n = 0;
194 : :
195 [ # # ]: 0 : while ( it != end )
196 : : {
197 [ # # ]: 0 : aURLs[ n ] = *it;
198 : 0 : ++it;
199 : 0 : ++n;
200 : : }
201 : :
202 [ # # ][ # # ]: 0 : m_aConfigItem.setSystemCredentialsURLs( aURLs );
[ # # ]
203 : 0 : }
204 : :
205 : 0 : rtl::OUString SysCredentialsConfig::find( rtl::OUString const & aURL )
206 : : {
207 [ # # ]: 0 : osl::MutexGuard aGuard( m_aMutex );
208 : 0 : rtl::OUString aResult;
209 [ # # ][ # # ]: 0 : if ( findURL( m_aMemContainer, aURL, aResult ) )
210 : 0 : return aResult;
211 : :
212 [ # # ]: 0 : initCfg();
213 [ # # ][ # # ]: 0 : if ( findURL( m_aCfgContainer, aURL, aResult ) )
214 : 0 : return aResult;
215 : :
216 [ # # ]: 0 : return rtl::OUString();
217 : : }
218 : :
219 : 0 : void SysCredentialsConfig::add( rtl::OUString const & rURL, bool bPersistent )
220 : : {
221 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
222 : :
223 [ # # ]: 0 : if ( bPersistent )
224 : : {
225 [ # # ]: 0 : m_aMemContainer.erase( rURL );
226 : :
227 [ # # ]: 0 : initCfg();
228 [ # # ]: 0 : m_aCfgContainer.insert( rURL );
229 [ # # ]: 0 : writeCfg();
230 : : }
231 : : else
232 : : {
233 [ # # ]: 0 : initCfg();
234 [ # # ][ # # ]: 0 : if ( m_aCfgContainer.erase( rURL ) > 0 )
235 [ # # ]: 0 : writeCfg();
236 : :
237 [ # # ]: 0 : m_aMemContainer.insert( rURL );
238 [ # # ]: 0 : }
239 : 0 : }
240 : :
241 : 0 : void SysCredentialsConfig::remove( rtl::OUString const & rURL )
242 : : {
243 : 0 : m_aMemContainer.erase( rURL );
244 : :
245 : 0 : initCfg();
246 [ # # ]: 0 : if ( m_aCfgContainer.erase( rURL ) > 0 )
247 : 0 : writeCfg();
248 : 0 : }
249 : :
250 : 0 : uno::Sequence< rtl::OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
251 : : {
252 [ # # ]: 0 : initCfg();
253 : 0 : sal_Int32 nCount = m_aCfgContainer.size()
254 [ # # ]: 0 : + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
255 [ # # ]: 0 : uno::Sequence< rtl::OUString > aResult( nCount );
256 : :
257 : 0 : StringSet::const_iterator it = m_aCfgContainer.begin();
258 : 0 : StringSet::const_iterator end = m_aCfgContainer.end();
259 : 0 : sal_Int32 n = 0;
260 : :
261 [ # # ]: 0 : while ( it != end )
262 : : {
263 [ # # ]: 0 : aResult[ n ] = *it;
264 : 0 : ++it;
265 : 0 : ++n;
266 : : }
267 : :
268 [ # # ]: 0 : if ( !bOnlyPersistent )
269 : : {
270 : 0 : it = m_aMemContainer.begin();
271 : 0 : end = m_aMemContainer.end();
272 : :
273 [ # # ]: 0 : while ( it != end )
274 : : {
275 [ # # ]: 0 : aResult[ n ] = *it;
276 : 0 : ++it;
277 : 0 : ++n;
278 : : }
279 : : }
280 : 0 : return aResult;
281 : : }
282 : :
283 : 0 : void SysCredentialsConfig::persistentConfigChanged()
284 : : {
285 [ # # ]: 0 : ::osl::MutexGuard aGuard( m_aMutex );
286 [ # # ]: 0 : m_bCfgInited = false; // re-init on demand.
287 : 0 : }
288 : :
289 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|