From a84618d8076b877ca77c217d33aa7457ee95fc1b Mon Sep 17 00:00:00 2001
From: Kai Sommerfeld <kso@openoffice.org>
Date: Wed, 17 Mar 2010 08:53:20 +0100
Subject: whitespace cleanup.

---
 uui/source/loginerr.hxx | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

(limited to 'uui')

diff --git a/uui/source/loginerr.hxx b/uui/source/loginerr.hxx
index c8cbba96564b..14cce88e0534 100644
--- a/uui/source/loginerr.hxx
+++ b/uui/source/loginerr.hxx
@@ -32,10 +32,10 @@
 
 //=========================================================================
 
-#define LOGINERROR_FLAG_SET_SAVE_PASSWORD   1
-#define LOGINERROR_FLAG_MODIFY_ACCOUNT      2
-#define LOGINERROR_FLAG_MODIFY_USER_NAME    4
-#define LOGINERROR_FLAG_PERSISTENT_PASSWORD 8
+#define LOGINERROR_FLAG_SET_SAVE_PASSWORD    1
+#define LOGINERROR_FLAG_MODIFY_ACCOUNT       2
+#define LOGINERROR_FLAG_MODIFY_USER_NAME     4
+#define LOGINERROR_FLAG_PERSISTENT_PASSWORD  8
 #define LOGINERROR_FLAG_CAN_USE_SYSCREDS    16
 #define LOGINERROR_FLAG_IS_USE_SYSCREDS     32
 
-- 
cgit 


From 52a1c736c09537fdf0de42c13100f2616c8305f1 Mon Sep 17 00:00:00 2001
From: Kai Sommerfeld <kso@openoffice.org>
Date: Thu, 18 Mar 2010 11:11:15 +0100
Subject: #i110213# - credentials handling improvements.

---
 uui/source/iahndl-authentication.cxx | 194 +++++++++++++++++++++++++----------
 uui/source/loginerr.hxx              |  55 ++++++----
 uui/source/passwordcontainer.cxx     |  13 ++-
 3 files changed, 184 insertions(+), 78 deletions(-)

(limited to 'uui')

diff --git a/uui/source/iahndl-authentication.cxx b/uui/source/iahndl-authentication.cxx
index f31397f1ef92..81f79c1b75e4 100644
--- a/uui/source/iahndl-authentication.cxx
+++ b/uui/source/iahndl-authentication.cxx
@@ -67,10 +67,9 @@ executeLoginDialog(
     {
         vos::OGuard aGuard(Application::GetSolarMutex());
 
-        bool bAccount = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT)
-                            != 0;
-        bool bSavePassword = rInfo.GetIsPersistentPassword()
-                             || rInfo.GetIsSavePassword();
+        bool bAccount
+            = (rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_ACCOUNT) != 0;
+        bool bSavePassword   = rInfo.GetCanRememberPassword();
         bool bCanUseSysCreds = rInfo.GetCanUseSystemCredentials();
 
         sal_uInt16 nFlags = 0;
@@ -82,10 +81,8 @@ executeLoginDialog(
             nFlags |= LF_NO_ACCOUNT;
         if (!(rInfo.GetFlags() & LOGINERROR_FLAG_MODIFY_USER_NAME))
             nFlags |= LF_USERNAME_READONLY;
-
         if (!bSavePassword)
             nFlags |= LF_NO_SAVEPASSWORD;
-
         if (!bCanUseSysCreds)
             nFlags |= LF_NO_USESYSCREDS;
 
@@ -109,12 +106,13 @@ executeLoginDialog(
 
         if (bSavePassword)
         {
-            xDialog->
-                SetSavePasswordText(ResId(rInfo.GetIsPersistentPassword() ?
-                                              RID_SAVE_PASSWORD :
-                                              RID_KEEP_PASSWORD,
-                                          *xManager.get()));
-            xDialog->SetSavePassword(rInfo.GetIsSavePassword());
+            xDialog->SetSavePasswordText(
+                ResId(rInfo.GetIsRememberPersistent()
+                          ? RID_SAVE_PASSWORD
+                          : RID_KEEP_PASSWORD,
+                      *xManager.get()));
+
+            xDialog->SetSavePassword(rInfo.GetIsRememberPassword());
         }
 
         if ( bCanUseSysCreds )
@@ -126,7 +124,7 @@ executeLoginDialog(
         rInfo.SetUserName(xDialog->GetName());
         rInfo.SetPassword(xDialog->GetPassword());
         rInfo.SetAccount(xDialog->GetAccount());
-        rInfo.SetSavePassword(xDialog->IsSavePassword());
+        rInfo.SetIsRememberPassword(xDialog->IsSavePassword());
 
         if ( bCanUseSysCreds )
           rInfo.SetIsUseSystemCredentials( xDialog->IsUseSystemCredentials() );
@@ -139,6 +137,61 @@ executeLoginDialog(
     }
 }
 
+void
+getRememberModes(
+    uno::Sequence< ucb::RememberAuthentication > const & rRememberModes,
+    ucb::RememberAuthentication & rPreferredMode,
+    ucb::RememberAuthentication & rAlternateMode )
+{
+    sal_Int32 nCount = rRememberModes.getLength();
+    OSL_ENSURE( (nCount > 0) && (nCount < 4),
+                "ucb::RememberAuthentication sequence size mismatch!" );
+    if ( nCount == 1 )
+    {
+        rPreferredMode = rAlternateMode = rRememberModes[ 0 ];
+        return;
+    }
+    else
+    {
+        //bool bHasRememberModeNo = false;
+        bool bHasRememberModeSession = false;
+        bool bHasRememberModePersistent = false;
+
+        for (sal_Int32 i = 0; i < nCount; ++i)
+        {
+            switch ( rRememberModes[i] )
+            {
+            case ucb::RememberAuthentication_NO:
+                //bHasRememberModeNo = true;
+                break;
+            case ucb::RememberAuthentication_SESSION:
+                bHasRememberModeSession = true;
+                break;
+            case ucb::RememberAuthentication_PERSISTENT:
+                bHasRememberModePersistent = true;
+                break;
+            default:
+                OSL_TRACE( "Unsupported RememberAuthentication value" );
+                break;
+            }
+        }
+
+        if (bHasRememberModePersistent)
+        {
+            rPreferredMode = ucb::RememberAuthentication_PERSISTENT;
+            if (bHasRememberModeSession)
+                rAlternateMode = ucb::RememberAuthentication_SESSION;
+            else
+                rAlternateMode = ucb::RememberAuthentication_NO;
+        }
+        else
+        {
+            rPreferredMode = ucb::RememberAuthentication_SESSION;
+            rAlternateMode = ucb::RememberAuthentication_NO;
+        }
+    }
+}
+
 void
 handleAuthenticationRequest_(
     Window * pParent,
@@ -161,7 +214,7 @@ handleAuthenticationRequest_(
         xSupplyAuthentication2.set(xSupplyAuthentication, uno::UNO_QUERY);
 
     //////////////////////////
-    // First, try to obatin credentials from password container service.
+    // First, try to obtain credentials from password container service.
     uui::PasswordContainerHelper aPwContainerHelper(xServiceFactory);
     if (aPwContainerHelper.handleAuthenticationRequest(rRequest,
                                                        xSupplyAuthentication,
@@ -174,26 +227,20 @@ handleAuthenticationRequest_(
 
     //////////////////////////
     // Second, try to obtain credentials from user via password dialog.
-    bool bRemember;
-    bool bRememberPersistent;
+    ucb::RememberAuthentication eDefaultRememberMode
+        = ucb::RememberAuthentication_SESSION;
+    ucb::RememberAuthentication ePreferredRememberMode
+        = eDefaultRememberMode;
+    ucb::RememberAuthentication eAlternateRememberMode
+        = ucb::RememberAuthentication_NO;
+
     if (xSupplyAuthentication.is())
     {
-        ucb::RememberAuthentication eDefault;
-        uno::Sequence< ucb::RememberAuthentication >
-            aModes(xSupplyAuthentication->getRememberPasswordModes(eDefault));
-        bRemember = eDefault != ucb::RememberAuthentication_NO;
-        bRememberPersistent = false;
-        for (sal_Int32 i = 0; i < aModes.getLength(); ++i)
-            if (aModes[i] == ucb::RememberAuthentication_PERSISTENT)
-            {
-                bRememberPersistent = true;
-                break;
-            }
-    }
-    else
-    {
-        bRemember = false;
-        bRememberPersistent = false;
+        getRememberModes(
+            xSupplyAuthentication->getRememberPasswordModes(
+                eDefaultRememberMode),
+            ePreferredRememberMode,
+            eAlternateRememberMode);
     }
 
     sal_Bool bCanUseSystemCredentials;
@@ -220,10 +267,16 @@ handleAuthenticationRequest_(
     if (rRequest.HasPassword)
         aInfo.SetPassword(rRequest.Password);
     aInfo.SetErrorText(rRequest.Diagnostic);
-    aInfo.SetPersistentPassword(bRememberPersistent);
-    aInfo.SetSavePassword(bRemember);
+
+    aInfo.SetCanRememberPassword(
+        ePreferredRememberMode != eAlternateRememberMode);
+    aInfo.SetIsRememberPassword(
+        eDefaultRememberMode != ucb::RememberAuthentication_NO);
+    aInfo.SetIsRememberPersistent(
+        ePreferredRememberMode == ucb::RememberAuthentication_PERSISTENT);
+
     aInfo.SetCanUseSystemCredentials(bCanUseSystemCredentials);
-    aInfo.SetIsUseSystemCredentials( bDefaultUseSystemCredentials );
+    aInfo.SetIsUseSystemCredentials(bDefaultUseSystemCredentials);
     aInfo.SetModifyAccount(rRequest.HasAccount
                            && xSupplyAuthentication.is()
                            && xSupplyAuthentication->canSetAccount());
@@ -242,13 +295,24 @@ handleAuthenticationRequest_(
                 xSupplyAuthentication->setUserName(aInfo.GetUserName());
             if (xSupplyAuthentication->canSetPassword())
                 xSupplyAuthentication->setPassword(aInfo.GetPassword());
-            xSupplyAuthentication->
-                setRememberPassword(
-                    aInfo.GetIsSavePassword() ?
-                       bRememberPersistent ?
-                       ucb::RememberAuthentication_PERSISTENT :
-                           ucb::RememberAuthentication_SESSION :
-                               ucb::RememberAuthentication_NO);
+
+            if (ePreferredRememberMode != eAlternateRememberMode)
+            {
+                // user had te choice.
+                if (aInfo.GetIsRememberPassword())
+                    xSupplyAuthentication->setRememberPassword(
+                        ePreferredRememberMode);
+                else
+                    xSupplyAuthentication->setRememberPassword(
+                        eAlternateRememberMode);
+            }
+            else
+            {
+                // user had no choice.
+                xSupplyAuthentication->setRememberPassword(
+                    ePreferredRememberMode);
+            }
+
             if (rRequest.HasRealm)
             {
                 if (xSupplyAuthentication->canSetRealm())
@@ -267,22 +331,31 @@ handleAuthenticationRequest_(
         //////////////////////////
         // Third, store credentials in password container.
 
-        if ( aInfo.GetIsUseSystemCredentials() )
+        if (aInfo.GetIsUseSystemCredentials())
         {
-            if (aInfo.GetIsSavePassword())
+            if (aInfo.GetIsRememberPassword() ||
+                (eAlternateRememberMode == ucb::RememberAuthentication_SESSION))
             {
-                aPwContainerHelper.addRecord(
-                    rURL.getLength() ? rURL : rRequest.ServerName,
-                    rtl::OUString(), // empty u/p -> sys creds
-                    uno::Sequence< rtl::OUString >(),
-                    xIH,
-                    bRememberPersistent);
+                if (!aPwContainerHelper.addRecord(
+                        rURL.getLength() ? rURL : rRequest.ServerName,
+                        rtl::OUString(), // empty u/p -> sys creds
+                        uno::Sequence< rtl::OUString >(),
+                        xIH,
+                        !aInfo.GetIsRememberPassword()
+                            ? false /* SESSION */
+                            : ePreferredRememberMode
+                                == ucb::RememberAuthentication_PERSISTENT))
+                {
+                    xSupplyAuthentication->setRememberPassword(
+                        ucb::RememberAuthentication_NO);
+                }
             }
         }
         // Empty user name can not be valid:
         else if (aInfo.GetUserName().Len() != 0)
         {
-            if (aInfo.GetIsSavePassword())
+            if (aInfo.GetIsRememberPassword() ||
+                (eAlternateRememberMode == ucb::RememberAuthentication_SESSION))
             {
                 uno::Sequence< rtl::OUString >
                     aPassList(aInfo.GetAccount().Len() == 0 ? 1 : 2);
@@ -290,12 +363,19 @@ handleAuthenticationRequest_(
                 if (aInfo.GetAccount().Len() != 0)
                     aPassList[1] = aInfo.GetAccount();
 
-                aPwContainerHelper.addRecord(
-                    rURL.getLength() ? rURL : rRequest.ServerName,
-                    aInfo.GetUserName(),
-                    aPassList,
-                    xIH,
-                    bRememberPersistent);
+                if (!aPwContainerHelper.addRecord(
+                        rURL.getLength() ? rURL : rRequest.ServerName,
+                        aInfo.GetUserName(),
+                        aPassList,
+                        xIH,
+                        !aInfo.GetIsRememberPassword()
+                            ? false /* SESSION */
+                            : ePreferredRememberMode
+                                == ucb::RememberAuthentication_PERSISTENT))
+                {
+                    xSupplyAuthentication->setRememberPassword(
+                        ucb::RememberAuthentication_NO);
+                }
             }
         }
         break;
diff --git a/uui/source/loginerr.hxx b/uui/source/loginerr.hxx
index 14cce88e0534..d713fbafa053 100644
--- a/uui/source/loginerr.hxx
+++ b/uui/source/loginerr.hxx
@@ -32,12 +32,13 @@
 
 //=========================================================================
 
-#define LOGINERROR_FLAG_SET_SAVE_PASSWORD    1
-#define LOGINERROR_FLAG_MODIFY_ACCOUNT       2
-#define LOGINERROR_FLAG_MODIFY_USER_NAME     4
-#define LOGINERROR_FLAG_PERSISTENT_PASSWORD  8
-#define LOGINERROR_FLAG_CAN_USE_SYSCREDS    16
-#define LOGINERROR_FLAG_IS_USE_SYSCREDS     32
+#define LOGINERROR_FLAG_MODIFY_ACCOUNT         1
+#define LOGINERROR_FLAG_MODIFY_USER_NAME       2
+#define LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD  4
+#define LOGINERROR_FLAG_IS_REMEMBER_PASSWORD   8
+#define LOGINERROR_FLAG_CAN_USE_SYSCREDS      16
+#define LOGINERROR_FLAG_IS_USE_SYSCREDS       32
+#define LOGINERROR_FLAG_REMEMBER_PERSISTENT   64
 
 class LoginErrorInfo
 {
@@ -56,8 +57,7 @@ public:
                     LoginErrorInfo()
                     : _nFlags( LOGINERROR_FLAG_MODIFY_USER_NAME ),
                       _nRet( ERRCODE_BUTTON_CANCEL )
-                    {
-                    }
+                    {}
 
     const String&   GetTitle() const        { return _aTitle; }
     const String&   GetServer() const       { return _aServer; }
@@ -66,10 +66,14 @@ public:
     const String&   GetPassword() const     { return _aPassword; }
     const String&   GetPath() const         { return _aPath; }
     const String&   GetErrorText() const    { return _aErrorText; }
-    BOOL            GetIsPersistentPassword() const
-                    { return ( _nFlags & LOGINERROR_FLAG_PERSISTENT_PASSWORD ); }
-    BOOL            GetIsSavePassword() const
-                    { return ( _nFlags & LOGINERROR_FLAG_SET_SAVE_PASSWORD ); }
+
+    BOOL            GetCanRememberPassword() const
+                    { return ( _nFlags & LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD ); }
+    BOOL            GetIsRememberPersistent() const
+                    { return ( _nFlags & LOGINERROR_FLAG_REMEMBER_PERSISTENT ); }
+    BOOL            GetIsRememberPassword() const
+                    { return ( _nFlags & LOGINERROR_FLAG_IS_REMEMBER_PASSWORD ); }
+
     BOOL            GetCanUseSystemCredentials() const
                     { return ( _nFlags & LOGINERROR_FLAG_CAN_USE_SYSCREDS ); }
     BOOL            GetIsUseSystemCredentials() const
@@ -94,8 +98,11 @@ public:
                     { _aErrorText = aErrorText; }
     void            SetFlags( BYTE nFlags )
                     { _nFlags = nFlags; }
-    inline void     SetSavePassword( BOOL bSet );
-    inline void     SetPersistentPassword( BOOL bSet );
+
+    inline void     SetCanRememberPassword( BOOL bSet );
+    inline void     SetIsRememberPassword( BOOL bSet );
+    inline void     SetIsRememberPersistent( BOOL bSet );
+
     inline void     SetCanUseSystemCredentials( BOOL bSet );
     inline void     SetIsUseSystemCredentials( BOOL bSet );
     inline void     SetModifyAccount( BOOL bSet );
@@ -104,20 +111,28 @@ public:
                     { _nRet = nRet; }
 };
 
-inline void LoginErrorInfo::SetSavePassword( BOOL bSet )
+inline void LoginErrorInfo::SetCanRememberPassword( BOOL bSet )
+{
+    if ( bSet )
+        _nFlags |= LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD;
+    else
+        _nFlags &= ~LOGINERROR_FLAG_CAN_REMEMBER_PASSWORD;
+}
+
+inline void LoginErrorInfo::SetIsRememberPassword( BOOL bSet )
 {
     if ( bSet )
-        _nFlags |= LOGINERROR_FLAG_SET_SAVE_PASSWORD;
+        _nFlags |= LOGINERROR_FLAG_IS_REMEMBER_PASSWORD;
     else
-        _nFlags &= ~LOGINERROR_FLAG_SET_SAVE_PASSWORD;
+        _nFlags &= ~LOGINERROR_FLAG_IS_REMEMBER_PASSWORD;
 }
 
-inline void LoginErrorInfo::SetPersistentPassword( BOOL bSet )
+inline void LoginErrorInfo::SetIsRememberPersistent( BOOL bSet )
 {
     if ( bSet )
-        _nFlags |= LOGINERROR_FLAG_PERSISTENT_PASSWORD;
+        _nFlags |= LOGINERROR_FLAG_REMEMBER_PERSISTENT;
     else
-        _nFlags &= ~LOGINERROR_FLAG_PERSISTENT_PASSWORD;
+        _nFlags &= ~LOGINERROR_FLAG_REMEMBER_PERSISTENT;
 }
 
 inline void LoginErrorInfo::SetCanUseSystemCredentials( BOOL bSet )
diff --git a/uui/source/passwordcontainer.cxx b/uui/source/passwordcontainer.cxx
index 26d22b320d8a..0a056289c29d 100644
--- a/uui/source/passwordcontainer.cxx
+++ b/uui/source/passwordcontainer.cxx
@@ -30,6 +30,7 @@
 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
 #include "com/sun/star/task/NoMasterException.hpp"
 #include "com/sun/star/task/XInteractionHandler.hpp"
+#include "com/sun/star/task/XMasterPasswordHandling.hpp"
 #include "com/sun/star/task/XPasswordContainer.hpp"
 #include "com/sun/star/task/XUrlContainer.hpp"
 #include "com/sun/star/ucb/AuthenticationRequest.hpp"
@@ -271,10 +272,20 @@ bool PasswordContainerHelper::addRecord(
                 return false;
 
             if ( bPersist )
+            {
+                uno::Reference< task::XMasterPasswordHandling > xMPH(
+                    m_xPasswordContainer, uno::UNO_QUERY_THROW );
+
+                // If persistent storing of passwords is not yet
+                // allowed, enable it.
+                if ( !xMPH->isPersistentStoringAllowed() )
+                    xMPH->allowPersistentStoring( sal_True );
+
                 m_xPasswordContainer->addPersistent( rURL,
                                                      rUsername,
                                                      rPasswords,
                                                      xIH );
+            }
             else
                 m_xPasswordContainer->add( rURL,
                                            rUsername,
@@ -429,7 +440,7 @@ PasswordContainerInteractionHandler::handle(
                                           // @@@ FIXME: this not able to
                                           // handle master pw request!
                                           // master pw request is never
-                                          // solvabe without UI!
+                                          // solvable without UI!
                                           this ) )
     {
         // successfully handled
-- 
cgit