From 7168df933faf12b6faba5fb76607a8c61e02696c Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Tue, 25 Oct 2016 13:23:03 -0400
Subject: [PATCH] smartpanel : hardening ring account creation

Change-Id: I48e064e891118427d83c3fb3e44846dbf39d64ab
Tuleap: #1247
---
 Account.h           | 11 ++++++++++-
 RingD.cpp           | 33 ++++++++++++++++++++++++---------
 SmartPanel.xaml     | 10 +++++++---
 SmartPanel.xaml.cpp | 19 +++++++++++++------
 SmartPanel.xaml.h   |  1 +
 5 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/Account.h b/Account.h
index 61bd0e5..565cfbe 100644
--- a/Account.h
+++ b/Account.h
@@ -42,7 +42,15 @@ public:
             NotifyPropertyChanged("name_");
         }
     }
-    property String^ ringID_;
+    property String^ ringID_ {
+        String^ get() {
+            return ringID__;
+        }
+        void set(String^ value) {
+            ringID__ = value;
+            NotifyPropertyChanged("ringID_");
+        }
+    }
     property String^ accountType_; // refacto : create a enum accountType
     property String^ accountID_;
     property String^ _deviceId;
@@ -62,6 +70,7 @@ protected:
 private:
     Windows::Foundation::Collections::IVector<String^>^ devicesIdList_;
     String^ name__;
+    String^ ringID__;
 
 };
 }
diff --git a/RingD.cpp b/RingD.cpp
index 5fe326e..ea5bd28 100644
--- a/RingD.cpp
+++ b/RingD.cpp
@@ -75,6 +75,7 @@ RingClientUWP::RingD::reloadAccountList()
         auto alias = accountDetails.find(DRing::Account::ConfProperties::ALIAS)->second;
         auto type = accountDetails.find(DRing::Account::ConfProperties::TYPE)->second;
         auto deviceId = (type == "SIP")? std::string() : accountDetails.find(DRing::Account::ConfProperties::RING_DEVICE_ID)->second;
+        auto ringId = accountDetails.find(DRing::Account::ConfProperties::USERNAME)->second;
 
         auto accountId = Utils::toPlatformString(*rit);
         auto account = AccountsViewModel::instance->findItem(accountId);
@@ -82,17 +83,14 @@ RingClientUWP::RingD::reloadAccountList()
         if (account) {
             account->name_ = Utils::toPlatformString(alias);
             account->_upnpState = upnpState;
+            account->accountType_ = Utils::toPlatformString(type);
+            account->ringID_ = Utils::toPlatformString(ringId);
             accountUpdated(account);
         }
         else {
             RingClientUWP::ViewModel::AccountsViewModel::instance->add(alias, ringID, type, *rit /*account id*/, deviceId, upnpState);
         }
 
-        if (editModeOn_) {
-            auto frame = dynamic_cast<Frame^>(Window::Current->Content);
-            dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(false, false);
-        }
-
     }
 
     // load user preferences
@@ -167,6 +165,12 @@ void RingClientUWP::RingD::sendSIPTextMessage(String^ message)
 void
 RingD::createRINGAccount(String^ alias)
 {
+
+    editModeOn_ = true;
+
+    auto frame = dynamic_cast<Frame^>(Window::Current->Content);
+    dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(true, true);
+
     // refactoring : create a dedicated class constructor task and removes accountName from RingD
     accountName = Utils::toString(alias);
     tasksList_.push(ref new RingD::Task(Request::AddRingAccount));
@@ -175,6 +179,11 @@ RingD::createRINGAccount(String^ alias)
 void
 RingD::createSIPAccount(String^ alias)
 {
+    editModeOn_ = true;
+
+    auto frame = dynamic_cast<Frame^>(Window::Current->Content);
+    dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(true, true);
+
     // refactoring : create a dedicated class constructor task and removes accountName from RingD
     accountName = Utils::toString(alias);
     tasksList_.push(ref new RingD::Task(Request::AddSIPAccount));
@@ -310,6 +319,8 @@ void
 RingClientUWP::RingD::startDaemon()
 {
     eraseCacheFolder();
+    editModeOn_ = true;
+
 
     create_task([&]()
     {
@@ -431,16 +442,15 @@ RingClientUWP::RingD::startDaemon()
                         const std::string& account_id, const std::string& state,
                         int detailsCode, const std::string& detailsStr)
             {
-                MSG_("<RegistrationStateChanged>: ID = " + account_id + "state = " + state);
+                MSG_("<RegistrationStateChanged>: ID = " + account_id + " state = " + state);
                 if (state == DRing::Account::States::REGISTERED) {
                     CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
                     ref new DispatchedHandler([=]() {
                         reloadAccountList();
-                        std::vector<std::string> accountList = DRing::getAccountList(); // refacto : there is already a callo to getAccountList in reloadAccountList
-                        auto last_id = accountList.back();
-                        if (!account_id.compare(last_id)) {
+                        if (editModeOn_) {
                             auto frame = dynamic_cast<Frame^>(Window::Current->Content);
                             dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(false, false);
+                            editModeOn_ = false;
                         }
                     }));
                 }
@@ -451,6 +461,11 @@ RingClientUWP::RingD::startDaemon()
                 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
                 ref new DispatchedHandler([=]() {
                     reloadAccountList();
+                    if (editModeOn_) {
+                        auto frame = dynamic_cast<Frame^>(Window::Current->Content);
+                        dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(false, false);
+                        editModeOn_ = false;
+                    }
                 }));
             }),
             DRing::exportable_callback<DRing::Debug::MessageSend>([&](const std::string& toto)
diff --git a/SmartPanel.xaml b/SmartPanel.xaml
index abab1f7..0d1fabd 100644
--- a/SmartPanel.xaml
+++ b/SmartPanel.xaml
@@ -209,7 +209,7 @@
                     <TextBlock x:Name="_ringID_"
                            Grid.ColumnSpan="2"
                            Style="{StaticResource TextStyle6}"
-                           Text="{x:Bind  _account.ringID_}"/>
+                           Text="{x:Bind  _account.ringID_, Mode=OneWay}"/>
                 </StackPanel>
             </Grid>
         </DataTemplate>
@@ -483,9 +483,13 @@
                         <TextBox  x:Name="_aliasTextBox_"
                                   Margin="10"
                                   PlaceholderText="Enter your username"/>
-                        <PasswordBox Margin="10"
+                        <PasswordBox x:Name="_passwordBoxAccountCreation_"
+                                     Margin="10"
+                                     PasswordChanged="_passwordBoxAccountCreationCheck__PasswordChanged"
                                      PlaceholderText="Enter your password"/>
-                        <PasswordBox  Margin="10"
+                        <PasswordBox  x:Name="_passwordBoxAccountCreationCheck_"
+                                      Margin="10"
+                                      PasswordChanged="_passwordBoxAccountCreationCheck__PasswordChanged"
                                       PlaceholderText="Repeat your Password"/>
                     </StackPanel>
                 </StackPanel>
diff --git a/SmartPanel.xaml.cpp b/SmartPanel.xaml.cpp
index b8f55fa..951a693 100644
--- a/SmartPanel.xaml.cpp
+++ b/SmartPanel.xaml.cpp
@@ -248,6 +248,7 @@ void RingClientUWP::Views::SmartPanel::_addAccountBtn__Click(Platform::Object^ s
 {
     _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
     _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
+    _createAccountYes_->IsEnabled = false;
 }
 
 void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
@@ -256,11 +257,6 @@ void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object
     {
     case 0:
     {
-        CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
-        ref new DispatchedHandler([=]() {
-            auto frame = dynamic_cast<Windows::UI::Xaml::Controls::Frame^>(Window::Current->Content);
-            dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(true, true);
-        }));
         RingD::instance->createRINGAccount(_aliasTextBox_->Text);
         _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
         _accountsMenuButton__Checked(nullptr, nullptr);
@@ -278,6 +274,8 @@ void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object
         break;
     }
     _aliasTextBox_->Text = "";
+    _passwordBoxAccountCreation_->Password = "";
+    _passwordBoxAccountCreationCheck_->Password = "";
 }
 
 
@@ -729,6 +727,7 @@ void RingClientUWP::Views::SmartPanel::_editAccountMenuButton__Click(Platform::O
     _accountEditionMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
     _deleteAccountBtnEditionMenu_->IsChecked = false;
     _deleteAccountBtnEditionMenu_->IsEnabled = (AccountListItemsViewModel::instance->itemsList->Size > 1)? true : false;
+    _createAccountYes_->IsEnabled = false;
 }
 
 
@@ -776,4 +775,12 @@ void RingClientUWP::Views::SmartPanel::_cancelAccountModification__Click(Platfor
 void RingClientUWP::Views::SmartPanel::OnaccountUpdated(RingClientUWP::Account ^account)
 {
     updatePageContent();
-}
\ No newline at end of file
+}
+
+void RingClientUWP::Views::SmartPanel::_passwordBoxAccountCreationCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
+{
+    _createAccountYes_->IsEnabled = (_passwordBoxAccountCreation_->Password
+                                     == _passwordBoxAccountCreationCheck_->Password
+                                     && _passwordBoxAccountCreation_->Password->Length() > 0)
+                                    ? true : false;
+}
diff --git a/SmartPanel.xaml.h b/SmartPanel.xaml.h
index 89fd270..4cd5cab 100644
--- a/SmartPanel.xaml.h
+++ b/SmartPanel.xaml.h
@@ -123,6 +123,7 @@ private:
     void _acceptAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
     void _cancelAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
     void OnaccountUpdated(RingClientUWP::Account ^account);
+    void _passwordBoxAccountCreationCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
 };
 }
 }
\ No newline at end of file
-- 
GitLab