Commits

Thejesh GN committed 5e5162d

User registration complete

Comments (0)

Files changed (5)

db/test.sqlite

Binary file modified.

gui/basic/main.html

                 <li class="dropdown">
                   <a href="#" class="dropdown-toggle" data-toggle="dropdown">You<b class="caret"></b></a>
                   <ul class="dropdown-menu">
-                  <li><a href="#"><?=$this->get('SESSION["user_email"]'); ?></a></li>
+                  <li><a href="/user/<?=$this->get('SESSION["user_name"]'); ?>">Public Profile</a></li>
+                  <li><a href="/auth/user/edit/<?=$this->get('SESSION["user_name"]'); ?>">Edit Profile</a></li>
                   <li><a href="#" id="signout">Sign out</a></li>
                   </ul>
                 </li>  
     </div>
 
     <div class="span9">
+
+<? if( $this->get('SESSION["user_email"]')  && $this->get('SESSION["user_name"]') == NULL  ): ?>
+      <form  method="post" action="/auth/register">
+        You need to choose an user name before proceeding.<br>
+        <b><div id="check_user_name_msg"></div></b><br>
+        User Name: <input name="user_name" type="text" id="user_name" /><br>
+        <input type="hidden" id="user_name_hid" name="user_name_hid" /><br>
+        <button onclick="checkUserNameAvailability()" type="button" id="check_user_name" class="btn" >Check Availability</button>
+        <button style="display:none;" type="submit"  class="btn" id="submit_user_name" >Save</button>
+      </form>
+
+<? else: ?>
           <?= $this->get('sub_out_put'); ?>
+<? endif; ?>
+    
+
+
     </div>
 
   </div>

gui/js/browser.js

 }
 
  
+function checkUserNameAvailability(){
+   $.ajax({
+          type: 'GET',
+          url: '/auth/checkUserNameAvailability', 
+          data:{user_name:$('#user_name').val()}
+        }).done(function ( data ) {
+          if( console && console.log ) {
+            console.log("Sample of data:", data);
+          }
+
+          if(data == "okay"){
+              $('#check_user_name').hide();
+              $('#check_user_name_msg_label').show();
+              $('#submit_user_name').show();
+              $('#user_name_hid').val($('#user_name').val());              
+              $('#check_user_name_msg').text("Availabe. Go ahead and save.");
+              $('#user_name').attr('disabled', 'disabled');
+          }else{
+              $('#check_user_name_msg_label').show();
+              $('#check_user_name_msg').text("Not Available. Please try another user name.");
+          }
+      });
+   return false;
+}
         $result = $persona->verifyAssertion($_POST['assertion']);
         if ($result->status === 'okay') {
             $session = $this->set('SESSION["user_email"]', $result->email);
+            $this->set('SESSION["user_name"]', NULL);
+
+            $sql_query = 'select user_name from user where email=:email';
+            $sql_query_params = array("email"=> $result->email );
+            $ASKYOURGOVT_DB=F3::get('ASKYOURGOVT_DB');
+            $ASKYOURGOVT_DB->exec($sql_query, $sql_query_params);
+            foreach (F3::get('ASKYOURGOVT_DB->result') as $row){
+                $this->set('SESSION["user_name"]', $row['user_name']);
+            }
             echo $result->email;
         } else {
             header('HTTP/1.1 500 Internal Server Error');
     }
 }
 
+function checkUserNameAvailability(){
+    if ($this->get('SESSION["user_email"]')  && $this->get('SESSION["user_name"]') == NULL){
+        if (isset($_GET['user_name'])){
+            $user_name = $_GET['user_name'];
+            $sql_query = ' select * from user where user_name =:user_name';
+            $sql_query_params = array("user_name"=>$user_name);
+            $ASKYOURGOVT_DB=F3::get('ASKYOURGOVT_DB');
+            $ASKYOURGOVT_DB->exec($sql_query, $sql_query_params);
+            $row_exists = 0;
+            foreach (F3::get('ASKYOURGOVT_DB->result') as $row){
+                $row_exists = 1;
+            }
+            if($row_exists === 1){
+                echo "error";        
+            }else{
+                echo "okay";
+            }
+        }           
+    }else{
+        echo "error";        
+    }
+}
+
+function register(){
+    if ($this->get('SESSION["user_email"]')  && $this->get('SESSION["user_name"]') == NULL){
+        if (isset($_POST['user_name_hid'])){
+            $user_name = $_POST['user_name_hid'];
+            //print $user_name;
+            $sql_query = 'insert into user("user_name", "email","user_full_name") values(:user_name,:email,:user_full_name)';
+            $sql_query_params = array("user_name"=>$user_name,"email"=>  $this->get('SESSION["user_email"]'), "user_full_name"=> "");
+            print_r($sql_query_params);
+            $ASKYOURGOVT_DB=F3::get('ASKYOURGOVT_DB');
+            $row = $ASKYOURGOVT_DB->exec($sql_query,$sql_query_params);
+            if($row > 0){
+                $this->set('SESSION["user_name"]', $user_name);
+                $this->reroute('/auth/user/'.$user_name);
+            }
+        }           
+    }else{
+            $this->reroute('/');
+    }
+}
+
+
 function logout(){
     $session = $this->set('SESSION["user_email"]', NULL);
 }
 $app->route('POST /auth/login','Users->login');
 $app->route('POST /auth/logout','Users->logout');
 $app->route('GET /auth/logout','Users->logout');
+$app->route('GET /auth/checkUserNameAvailability','Users->checkUserNameAvailability');
+$app->route('POST /auth/register','Users->register');
 
 
 $app->route('GET /document/id/@document_id','Document->document_view_by_id');