a3df543e964484070a202af9f2da33b2f91e8a00
chinhli
  Mon Mar 26 15:48:46 2012 -0700
Inithial hgLogin work
diff --git src/hg/hgSession/hgSession.c src/hg/hgSession/hgSession.c
index 81dd735..01b3e25 100644
--- src/hg/hgSession/hgSession.c
+++ src/hg/hgSession/hgSession.c
@@ -3,30 +3,31 @@
 #include "common.h"
 #include "hash.h"
 #include "htmshell.h"
 #include "cheapcgi.h"
 #include "linefile.h"
 #include "net.h"
 #include "textOut.h"
 #include "hCommon.h"
 #include "hui.h"
 #include "cart.h"
 #include "jsHelper.h"
 #include "web.h"
 #include "hdb.h"
 #include "ra.h"
 #include "wikiLink.h"
+#include "hgLoginLink.h"
 #include "customTrack.h"
 #include "customFactory.h"
 #include "hgSession.h"
 
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "hgSession - Interface with wiki login and do session saving/loading.\n"
   "usage:\n"
   "    hgSession <various CGI settings>\n"
   );
 }
 
@@ -71,30 +72,85 @@
 char *wikiHost = wikiLinkHost();
 
 cartWebStart(cart, NULL, "Sign in to UCSC Genome Bioinformatics");
 jsInit();
 printf("Signing in enables you to save current settings into a "
        "named session, and then restore settings from the session later.\n"
        "If you wish, you can share named sessions with other users.\n");
 printf("<P>The sign-in page is handled by our "
        "<A HREF=\"http://%s/\" TARGET=_BLANK>wiki system</A>:\n", wikiHost);
 printf("<A HREF=\"%s\"><B>click here to sign in.</B></A>\n",
        wikiLinkUserLoginUrl(cartSessionId(cart)));
 printf("The wiki also serves as a forum for users "
        "to share knowledge and ideas.\n");
 }
 
+void welcomeGBUser(char *hgLoginUserName)
+/* Tell the user they are not logged in to the hgLogin and tell them how
+ * to
+ * do so. */
+{
+char *hgLoginHost = hgLoginLinkHost();
+
+cartWebStart(cart, NULL, "Welcome %s", hgLoginUserName);
+jsInit();
+printf("If you are not %s (on the hgLogin at "
+       "<A HREF=\"http://%s/\" TARGET=_BLANK>%s</A>) "
+       "and would like to sign out or change identity, \n",
+       hgLoginUserName, hgLoginHost, hgLoginHost);
+printf("<A HREF=\"%s\"><B>click here to sign out XXXX from hgLogin.</B></A>\n",
+       hgLoginLinkUserLogoutUrl(cartSessionId(cart)));
+}
+
+
+void offerGBLogin()
+/* Tell the user they are not logged in to the hgLogin and tell them how
+ * to
+ * do so. */
+{
+char *hgLoginHost = hgLoginLinkHost();
+
+cartWebStart(cart, NULL, "Sign in to UCSC Genome Bioinformatics");
+jsInit();
+printf("Signing in enables you to save current settings into a "
+       "named session, and then restore settings from the session later.\n"
+       "If you wish, you can share named sessions with other users.\n");
+printf("<P>The sign-in page is handled by our XXXX hgLogin "
+       "<A HREF=\"http://%s/\" TARGET=_BLANK>hgLogin system</A>:\n", hgLoginHost);
+printf("<A HREF=\"%s\"><B>click here to sign in.</B></A>\n",
+       hgLoginLinkUserLoginUrl(cartSessionId(cart)));
+printf("The hgLogin also serves as a forum for users "
+       "to share knowledge and ideas.\n");
+}
+
+
+char *getLinkUserName()
+/* Return the user name specified in cookies from the browser, or NULL
+ * if 
+ * the user doesn't appear to be logged in. */
+{
+if (hgLoginLinkEnabled())
+    return cloneString(hgLoginLinkUserName());
+else     
+    if (wikiLinkEnabled())
+       {
+       return cloneString(wikiLinkUserName());
+       }
+
+return NULL;
+}
+
 void showCartLinks()
 /* Print out links to cartDump and cartReset. */
 {
 char *session = cartSidUrlString(cart);
 char returnAddress[512];
 
 safef(returnAddress, sizeof(returnAddress), "%s?%s", hgSessionName(), session);
 printf("<A HREF=\"../cgi-bin/cartReset?%s&destination=%s\">Click here to "
        "reset</A> the browser user interface settings to their defaults.\n",
        session, cgiEncodeFull(returnAddress));
 }
 
 
 char *destAppScriptName()
 /* Return the complete path (/cgi-bin/... on our systems) of the destination
@@ -484,95 +540,129 @@
        "you can send email to others with a link such as "
        "%s<B>U</B> where <B>U</B> is the URL of your "
        "settings file, e.g. http://www.mysite.edu/~me/mySession.txt .  "
        "In this type of link, you can replace "
        "\"hgSession\" with \"hgTracks\" in order to proceed directly to "
        "the Genome Browser.</LI>\n",
        dyUrl->string);
 printf("</UL>\n");
 dyStringFree(&dyUrl);
 }
 
 void doMainPage(char *message)
 /* Login status/links and session controls. */
 {
 puts("Content-Type:text/html\n");
+if (hgLoginLinkEnabled())
+  {
+  /* XXXX hgLogin  */
+printf("HELLO HGLOGIN\n");
+    char *hgLoginUserName = hgLoginLinkUserName();
+/* DEBUG: */ printf("<BR>DEBUG X: hgLoginUserName is %s<BR>",hgLoginUserName);
+
+    if (hgLoginUserName)
+        welcomeGBUser(hgLoginUserName);
+    else
+        offerGBLogin();
+
+/* DEBUG: */ printf("<BR>DEBUG Y: hgLoginUserName is %s<BR>",hgLoginUserName);
+/* DEBUG: */ printf("<BR>DEBUG Z: message is %s<BR>",message);
+
+    if (isNotEmpty(message))
+        {
+/* DEBUG: */ printf("<BR>DEBUG A: message is %s<BR>", message);
+        if (cartVarExists(cart, hgsDoSessionDetail))
+            webNewSection("Session Details");
+        else
+            webNewSection("Updated Session");
+        puts(message);
+/* DEBUG: */ printf("<BR>DEBUG B: message is %s<BR>", message);
+
+        }
+    showSessionControls(hgLoginUserName, TRUE, TRUE);
+    showLinkingTemplates(hgLoginUserName);
+
+
+  } else { /* use wiki login */
 if (wikiLinkEnabled())
     {
     char *wikiUserName = wikiLinkUserName();
     if (wikiUserName)
 	welcomeUser(wikiUserName);
     else
 	offerLogin();
     if (isNotEmpty(message))
 	{
 	if (cartVarExists(cart, hgsDoSessionDetail))
 	    webNewSection("Session Details");
 	else
 	    webNewSection("Updated Session");
 	puts(message);
 	}
+/* DEBUG: */ printf("<BR>DEBUG WIKI message is %s<BR>", message);
     showSessionControls(wikiUserName, TRUE, TRUE);
     showLinkingTemplates(wikiUserName);
     }
 else
     {
     if (isNotEmpty(message))
 	{
 	if (cartVarExists(cart, hgsDoSessionDetail))
 	    webNewSection("Session Details");
 	else
 	    cartWebStart(cart, NULL, "Updated Session");
 	jsInit();
 	puts(message);
 	showSessionControls(NULL, FALSE, TRUE);
 	}
     else
 	showSessionControls(NULL, FALSE, FALSE);
     showLinkingTemplates(NULL);
     }
+  }
 cartWebEnd();
 }
 
 
 void cleanHgSessionFromCart(struct cart *cart)
 /* Remove hgSession action variables that should not stay in the cart. */
 {
 char varName[256];
 safef(varName, sizeof(varName), "%s%s", cgiBooleanShadowPrefix(), hgsSharePrefix);
 cartRemovePrefix(cart, varName);
 cartRemovePrefix(cart, hgsSharePrefix);
 cartRemovePrefix(cart, hgsLoadPrefix);
 cartRemovePrefix(cart, hgsLoadLocalFileName);
 cartRemovePrefix(cart, hgsDeletePrefix);
 cartRemovePrefix(cart, hgsDo);
 cartRemove(cart, hgsOldSessionName);
 cartRemove(cart, hgsCancel);
 }
 
 void checkForCustomTracks(struct dyString *dyMessage);
 
 #define INITIAL_USE_COUNT 0
 char *doNewSession()
 /* Save current settings in a new named session.
  * Return a message confirming what we did. */
 {
 struct dyString *dyMessage = dyStringNew(2048);
 char *sessionName = trimSpaces(cartString(cart, hgsNewSessionName));
 char *encSessionName = cgiEncodeFull(sessionName);
 boolean shareSession = cartBoolean(cart, hgsNewSessionShare);
-char *userName = wikiLinkUserName();
+/* char *userName = wikiLinkUserName(); */
+char *userName = getLinkUserName();
 char *encUserName = cgiEncodeFull(userName);
 struct sqlConnection *conn = hConnectCentral();
 
 if (sqlTableExists(conn, namedSessionTable))
     {
     struct sqlResult *sr = NULL;
     struct dyString *dy = dyStringNew(16 * 1024);
     char **row;
     char *firstUse = "now()";
     int useCount = INITIAL_USE_COUNT;
     char firstUseBuf[32];
 
     /* If this session already existed, preserve its firstUse and useCount. */
     dyStringPrintf(dy, "SELECT firstUse, useCount FROM %s "
 		       "WHERE userName = '%s' AND sessionName = '%s';",
@@ -731,31 +821,32 @@
 		   "BLAT results are subject to an "
 		   "<A HREF=\"../goldenPath/help/hgSessionHelp.html#CTs\" TARGET=_BLANK>"
 		   "expiration policy</A>.");
     }
 }
 
 char *doUpdateSessions()
 /* Look for cart variables matching prefixes for sharing/unsharing,
  * loading or deleting a previously saved session.
  * Return a message confirming what we did, or NULL if no such variables
  * were in the cart. */
 {
 struct dyString *dyMessage = dyStringNew(1024);
 struct hashEl *cartHelList = NULL, *hel = NULL;
 struct sqlConnection *conn = hConnectCentral();
-char *userName = wikiLinkUserName();
+/* char *userName = wikiLinkUserName(); */
+char *userName = getLinkUserName();
 char *encUserName = cgiEncodeFull(userName);
 boolean didSomething = FALSE;
 char query[512];
 
 cartHelList = cartFindPrefix(cart, hgsSharePrefix);
 if (cartHelList != NULL)
     {
     struct hash *sharedHash = hashNew(0);
     char **row;
     struct sqlResult *sr;
     safef(query, sizeof(query),
 	  "select sessionName,shared from %s where userName = '%s'",
 	  namedSessionTable, encUserName);
     sr = sqlGetResult(conn, query);
     while ((row = sqlNextRow(sr)) != NULL)
@@ -938,31 +1029,32 @@
     }
 if (lf != NULL)
     {
     cartLoadSettings(lf, cart, NULL, actionVar);
     checkForCustomTracks(dyMessage);
     lineFileClose(&lf);
     }
 return dyStringCannibalize(&dyMessage);
 }
 
 char *doSessionDetail(char *sessionName)
 /* Show details about a particular session. */
 {
 struct dyString *dyMessage = dyStringNew(4096);
 char *encSessionName = cgiEncodeFull(sessionName);
-char *userName = wikiLinkUserName();
+/* char *userName = wikiLinkUserName(); */
+char *userName = getLinkUserName();
 char *encUserName = cgiEncodeFull(userName);
 struct sqlConnection *conn = hConnectCentral();
 struct sqlResult *sr = NULL;
 char **row = NULL;
 char query[512];
 webPushErrHandlersCart(cart);
 boolean gotSettings = (sqlFieldIndex(conn, namedSessionTable, "settings") >= 0);
 
 if (gotSettings)
     safef(query, sizeof(query), "SELECT shared, firstUse, settings from %s "
 	  "WHERE userName = '%s' AND sessionName = '%s'",
           namedSessionTable, encUserName, encSessionName);
 else
     safef(query, sizeof(query), "SELECT shared, firstUse from %s "
 	  "WHERE userName = '%s' AND sessionName = '%s'",
@@ -1043,31 +1135,32 @@
     {
     cartRemove(cart, varName);
     safef(varName, sizeof(varName), "%s%s", prefix, newName);
     cartSetString(cart, varName, "submit");
     }
 }
 
 char *doSessionChange(char *oldSessionName)
 /* Process changes to session from session details page. */
 {
 struct dyString *dyMessage = dyStringNew(1024);
 webPushErrHandlersCart(cart);
 char *sessionName = oldSessionName;
 char *encSessionName = cgiEncodeFull(sessionName);
 char *encOldSessionName = encSessionName;
-char *userName = wikiLinkUserName();
+/* char *userName = wikiLinkUserName(); */
+char *userName = getLinkUserName();
 char *encUserName = cgiEncodeFull(userName);
 struct sqlConnection *conn = hConnectCentral();
 struct sqlResult *sr = NULL;
 char **row = NULL;
 char query[512];
 boolean shared = TRUE;
 char *settings = NULL;
 boolean gotSettings = (sqlFieldIndex(conn, namedSessionTable, "settings") >= 0);
 
 if (gotSettings)
     safef(query, sizeof(query), "SELECT shared, settings from %s "
 	  "WHERE userName = '%s' AND sessionName = '%s'",
           namedSessionTable, encUserName, encSessionName);
 else
     safef(query, sizeof(query), "SELECT shared from %s "