MBPage.py
Go to the documentation of this file.
00001 
00002 from pyclearsilver.CSPage import CSPage
00003 
00004 import time
00005 import string
00006 import os
00007 
00008 import urllib
00009 import nstart
00010 import config
00011 
00012 from pyclearsilver.log import *
00013 
00014 import neo_cgi
00015 from pyclearsilver import handle_error
00016 from auth import db_auth, cookieauth
00017 
00018 class MBPage(CSPage):
00019     def subclassinit(self):
00020 
00021         hdf = self.ncgi.hdf
00022         proxy_path = hdf.getValue("HTTP.Soap.Action", "")
00023         if proxy_path and not config.gBaseURL.startswith(proxy_path):
00024           config.gBaseURL = proxy_path + config.gBaseURL
00025           config.gROSURL = proxy_path + config.gROSURL
00026 
00027         hdf.setValue("Config.CompressionEnabled","1")
00028         hdf.setValue("Config.WhiteSpaceStrip","1")
00029 
00030         self.login = None
00031         self.username = None
00032         self.db = None
00033         self.userRec = None
00034 
00035         now = int(time.time())
00036         today = time.localtime(now)
00037         neo_cgi.exportDate(hdf, "CGI.Today", "US/Pacific", now)
00038 
00039         self.authdb = db_auth.initSchema()
00040 
00041         via = hdf.getValue("HTTP.Via", "")
00042         if via:
00043           #if via.find(":443") != -1:
00044           self.http = "https://"
00045         
00046         hdf.setValue("CGI.Robot", config.get_robot_name())
00047         hdf.setValue("CGI.robot_type", config.get_robot_type())
00048 
00049         hostport_prefix = self.http + "%s%s" % (self.domain.split(':')[0], config.gROSBridgePort)
00050         hdf.setValue("CGI.hostport_prefix", hostport_prefix)
00051         hdf.setValue("CGI.ros_bridge_uri", hostport_prefix + config.gROSURL)
00052                      
00053         hdf.setValue("CGI.home_server", config.gHomeServer)
00054         
00055         self.getUsername()
00056         self.setStyleSheet(hdf)
00057         
00058         hdf.setValue("CGI.home_page", self.default_app_path())
00059         request_uri = hdf.getValue("CGI.RequestURI", "")
00060         if request_uri.startswith(config.gBaseURL):
00061             page_name = request_uri[len(config.gBaseURL):].split('?', 1)[0]
00062             hdf.setValue("CGI.page_name", page_name)
00063 
00064     def setStyleSheet(self, hdf):
00065         useragent = hdf.getValue("HTTP.UserAgent", "").lower()
00066         if useragent.find("android") != -1 or useragent.find("iphone") != -1 or useragent.find("arora") != -1:
00067             hdf.setValue("CGI.cur.device_style", "style_phone.css")
00068 
00069     def handle_actions2(self):
00070         hdf = self.ncgi.hdf
00071         hdfobj = hdf.getObj("Query.Action")
00072         if hdfobj:
00073             self.checkLoginCookie()
00074         CSPage.handle_actions(self)
00075 
00076     def getUsername(self):
00077         hdf = self.ncgi.hdf
00078 
00079         logincookie = cookieauth.parseLoginCookie(self.ncgi)
00080         if logincookie:
00081             self.username = logincookie.username
00082 
00083             self.userRec = self.authdb.users.lookup(self.username)
00084 
00085             hdf.setValue("CGI.Login", self.username)
00086             hdf.setValue("CGI.Login.issued_at", str(logincookie.issued_at))
00087 
00088 
00089     def checkLoginCookie(self):
00090         hdf = self.ncgi.hdf
00091 
00092         requestURI = hdf.getValue("CGI.RequestURI", "")
00093 
00094         rurl = config.gBaseURL + "login/signin0.py"
00095 
00096         self.authdb = db_auth.initSchema()
00097 
00098         logincookie = cookieauth.parseLoginCookie(self.ncgi)
00099         if not logincookie:
00100           self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
00101 
00102         self.username = logincookie.username
00103         self.userRec = self.authdb.users.lookup(self.username)
00104         if self.userRec:
00105             hdf.setValue("CGI.Role", self.userRec.role)
00106 
00107         if self.userRec is None or cookieauth.checkLoginCookie(self.ncgi, logincookie, self.authdb, self.username, self.userRec) == 0:
00108           warn("invalid cookie", rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
00109           self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
00110         # -----  the cookie is valid!!!! -------
00111 
00112         persist = cookieauth.getPersistCookie(hdf)
00113         if persist == 0:
00114           # reissue a new cookie with an updated timeout
00115           if (time.time() - logincookie.issued_at) > config.REFRESH_COOKIE_TIMEOUT:
00116             cookieauth.issueLoginCookie(self.ncgi, self.authdb, self.username, self.userRec.pw_hash)
00117 
00118         self.login = self.username
00119 
00120         hdf.setValue("CGI.Login", self.username)
00121         hdf.setValue("CGI.Login.issued_at", str(logincookie.issued_at))
00122 
00123         active_user = self.get_active_user()
00124         time_since_activity = self.get_active_user_last_activity() # seconds since they did something
00125         
00126         if active_user:
00127             if time_since_activity > config.ACTIVE_USER_TIMEOUT:
00128                 # time out after one hour?
00129                 self.remove_active_user()
00130             else:
00131                 # touch the active user file
00132                 os.utime(config.ACTIVE_USER_FILE, None)
00133                 
00134         hdf.setValue("CGI.active_user", self.get_active_user())
00135 
00136         if self._pageparms.get("checkActive", True):
00137           if hdf.getValue("Cookie.inactive", "0") != "1":  
00138             if self.get_active_user() == "":
00139                 self.make_active_user(hdf)
00140             elif not self.is_active_user():
00141                 rurl = config.gBaseURL + "active/active.py"
00142                 if requestURI.find("/active/") == -1:
00143                     self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
00144 
00145     def get_active_user(self):
00146       active_user = ""
00147       try:
00148         active_user = open(config.ACTIVE_USER_FILE, "r").read().strip()
00149       except:
00150         pass
00151 
00152       return active_user
00153 
00154     def get_active_user_last_activity(self):
00155       time_since_mod = 0
00156       try:
00157         modified_time = os.path.getmtime(config.ACTIVE_USER_FILE)
00158         time_since_mod = (time.time() - modified_time)
00159       except:
00160         pass
00161 
00162       return time_since_mod
00163 
00164     def is_active_user(self):
00165       if self.username == self.get_active_user():
00166         return True
00167       return False
00168 
00169     def make_active_user(self, hdf):
00170       if not os.path.exists(config.ROS_VAR_DIR):
00171         os.umask(0)
00172         os.mkdir(config.ROS_VAR_DIR, 0777)
00173       active_user = open(config.ACTIVE_USER_FILE, "w")
00174       active_user.write(self.username)
00175       active_user.close()
00176 
00177       if config.get_robot_type().startswith("texas"):
00178           cookie = hdf.getValue("Query.cookie", "")
00179           if cookie:
00180               cookie_file = open(config.VALID_USER_COOKIE_FILE, "w")
00181               cookie_file.write(cookie)
00182               cookie_file.close()
00183 
00184       if config.gLobby:
00185         url = config.gLobby + "/lobby/lobby/userrec.py"
00186         postdata = {}
00187         postdata['Action.MakeActiveUser'] = "1"
00188         postdata['active_user'] = self.username
00189         postdata['robot'] = config.get_robot_name()
00190           
00191         fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
00192         fp.read()
00193         fp.close()
00194                      
00195 
00196     def remove_active_user(self):
00197       if config.get_robot_type().startswith("texas"):
00198           if os.path.exists(config.VALID_USER_COOKIE_FILE):
00199               os.remove(config.VALID_USER_COOKIE_FILE)
00200 
00201       if os.path.exists(config.ACTIVE_USER_FILE):
00202         os.remove(config.ACTIVE_USER_FILE)
00203 
00204         if config.gLobby:
00205           url = config.gLobby + "/lobby/lobby/userrec.py"
00206           postdata = {}
00207           postdata['Action.RemoveActiveUser'] = "1"
00208           postdata['robot'] = config.get_robot_name()
00209 
00210           fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
00211           fp.read()
00212           fp.close()
00213 
00214           
00215 
00216     def default_app_path(self):
00217       return self.http + self.ncgi.hdf.getValue("HTTP.Host", "") + config.gBaseURL + "%s/" % config.gDefaultModule
00218 
00219     def close(self):
00220       if hasattr(self, "db") and self.db:
00221         self.db.close()
00222         self.db = None
00223       if hasattr(self, "authdb") and self.authdb:
00224         self.authdb.close()
00225         self.authdb = None
00226 
00227     def __del__(self):
00228       self.close()
00229 
00230 


webui
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Sat Dec 28 2013 17:47:58