[epiphany/wip/ephy-sync: 110/116] sync-service: Test user email against regex



commit 26445b6b77ba49b9e37e98c5df704cef796fadab
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Sat Jul 23 12:49:20 2016 +0300

    sync-service: Test user email against regex

 src/ephy-sync-service.c |   16 ++++++++++------
 src/prefs-dialog.c      |   13 +++++++------
 2 files changed, 17 insertions(+), 12 deletions(-)
---
diff --git a/src/ephy-sync-service.c b/src/ephy-sync-service.c
index 7384800..85f27ae 100644
--- a/src/ephy-sync-service.c
+++ b/src/ephy-sync-service.c
@@ -28,6 +28,7 @@
 #include <libsoup/soup.h>
 #include <string.h>
 
+#define EMAIL_REGEX       "^[a-z0-9]([a-z0-9.]+[a-z0-9])?@[a-z0-9.-]+$"
 #define TOKEN_SERVER_URL  "https://token.services.mozilla.com/1.0/sync/1.5";
 #define FXA_BASEURL       "https://api.accounts.firefox.com/";
 #define FXA_VERSION       "v1/"
@@ -459,16 +460,19 @@ ephy_sync_service_class_init (EphySyncServiceClass *klass)
 static void
 ephy_sync_service_init (EphySyncService *self)
 {
-  gchar *sync_user = NULL;
+  gchar *email;
 
   self->soup_session = soup_session_new ();
 
-  sync_user = g_settings_get_string (EPHY_SETTINGS_MAIN,
-                                     EPHY_PREFS_SYNC_USER);
+  email = g_settings_get_string (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_USER);
 
-  if (sync_user && sync_user[0]) {
-    ephy_sync_service_set_user_email (self, sync_user);
-    ephy_sync_secret_load_tokens (self);
+  if (g_str_equal (email, "") == FALSE) {
+    if (g_regex_match_simple (EMAIL_REGEX, email, 0, 0) == TRUE) {
+      ephy_sync_service_set_user_email (self, email);
+      ephy_sync_secret_load_tokens (self);
+    } else {
+      g_warning ("Invalid email");
+    }
   }
 }
 
diff --git a/src/prefs-dialog.c b/src/prefs-dialog.c
index b7146e4..4b58983 100644
--- a/src/prefs-dialog.c
+++ b/src/prefs-dialog.c
@@ -356,6 +356,7 @@ on_sync_sign_out_button_clicked (GtkWidget   *button,
   ephy_sync_service_destroy_session (service, sessionToken);
   ephy_sync_service_delete_all_tokens (service);
   ephy_sync_secret_forget_all_tokens ();
+  ephy_sync_service_set_user_email (service, NULL);
 
   g_settings_set_string (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_USER, "");
 
@@ -1485,13 +1486,13 @@ setup_language_page (PrefsDialog *dialog)
 static void
 setup_sync_page (PrefsDialog *dialog)
 {
-  gchar *sync_user = NULL;
-  gboolean logged_in;
+  EphySyncService *service;
+  gchar *email = NULL;
 
-  sync_user = g_settings_get_string (EPHY_SETTINGS_MAIN, EPHY_PREFS_SYNC_USER);
-  logged_in = sync_user && sync_user[0];
+  service = ephy_shell_get_global_sync_service (ephy_shell_get_default ());
+  email = ephy_sync_service_get_user_email (service);
 
-  if (logged_in == FALSE) {
+  if (email == NULL) {
     setup_fxa_sign_in_view (dialog);
     gtk_container_remove (GTK_CONTAINER (dialog->sync_authenticate_box),
                           dialog->sync_sign_out_box);
@@ -1500,7 +1501,7 @@ setup_sync_page (PrefsDialog *dialog)
                           dialog->sync_sign_in_box);
     /* Translators: the %s refers to the email of the currently logged in user. */
     gtk_label_set_markup (GTK_LABEL (dialog->sync_sign_out_details),
-                          g_strdup_printf (_("Currently logged in as <b>%s</b>"), sync_user));
+                          g_strdup_printf (_("Currently logged in as <b>%s</b>"), email));
   }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]