MBPage.py
Go to the documentation of this file.
1 
2 from pyclearsilver.CSPage import CSPage
3 
4 import time
5 import string
6 import os
7 
8 import urllib
9 import nstart
10 import config
11 
12 from pyclearsilver.log import *
13 
14 import neo_cgi
15 from pyclearsilver import handle_error
16 from auth import db_auth, cookieauth
17 
18 class MBPage(CSPage):
19  def subclassinit(self):
20 
21  hdf = self.ncgi.hdf
22  proxy_path = hdf.getValue("HTTP.Soap.Action", "")
23  if proxy_path and not config.gBaseURL.startswith(proxy_path):
24  config.gBaseURL = proxy_path + config.gBaseURL
25  config.gROSURL = proxy_path + config.gROSURL
26 
27  hdf.setValue("Config.CompressionEnabled","1")
28  hdf.setValue("Config.WhiteSpaceStrip","1")
29 
30  self.login = None
31  self.username = None
32  self.db = None
33  self.userRec = None
34 
35  now = int(time.time())
36  today = time.localtime(now)
37  neo_cgi.exportDate(hdf, "CGI.Today", "US/Pacific", now)
38 
39  self.authdb = db_auth.initSchema()
40 
41  via = hdf.getValue("HTTP.Via", "")
42  if via:
43  #if via.find(":443") != -1:
44  self.http = "https://"
45 
46  hdf.setValue("CGI.Robot", config.get_robot_name())
47  hdf.setValue("CGI.robot_type", config.get_robot_type())
48 
49  hostport_prefix = self.http + "%s%s" % (self.domain.split(':')[0], config.gROSBridgePort)
50  hdf.setValue("CGI.hostport_prefix", hostport_prefix)
51  hdf.setValue("CGI.ros_bridge_uri", hostport_prefix + config.gROSURL)
52 
53  hdf.setValue("CGI.home_server", config.gHomeServer)
54 
55  self.getUsername()
56  self.setStyleSheet(hdf)
57 
58  hdf.setValue("CGI.home_page", self.default_app_path())
59  request_uri = hdf.getValue("CGI.RequestURI", "")
60  if request_uri.startswith(config.gBaseURL):
61  page_name = request_uri[len(config.gBaseURL):].split('?', 1)[0]
62  hdf.setValue("CGI.page_name", page_name)
63 
64  def setStyleSheet(self, hdf):
65  useragent = hdf.getValue("HTTP.UserAgent", "").lower()
66  if useragent.find("android") != -1 or useragent.find("iphone") != -1 or useragent.find("arora") != -1:
67  hdf.setValue("CGI.cur.device_style", "style_phone.css")
68 
69  def handle_actions2(self):
70  hdf = self.ncgi.hdf
71  hdfobj = hdf.getObj("Query.Action")
72  if hdfobj:
73  self.checkLoginCookie()
74  CSPage.handle_actions(self)
75 
76  def getUsername(self):
77  hdf = self.ncgi.hdf
78 
79  logincookie = cookieauth.parseLoginCookie(self.ncgi)
80  if logincookie:
81  self.username = logincookie.username
82 
83  self.userRec = self.authdb.users.lookup(self.username)
84 
85  hdf.setValue("CGI.Login", self.username)
86  hdf.setValue("CGI.Login.issued_at", str(logincookie.issued_at))
87 
88 
89  def checkLoginCookie(self):
90  hdf = self.ncgi.hdf
91 
92  requestURI = hdf.getValue("CGI.RequestURI", "")
93 
94  rurl = config.gBaseURL + "login/signin0.py"
95 
96  self.authdb = db_auth.initSchema()
97 
98  logincookie = cookieauth.parseLoginCookie(self.ncgi)
99  if not logincookie:
100  self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
101 
102  self.username = logincookie.username
103  self.userRec = self.authdb.users.lookup(self.username)
104  if self.userRec:
105  hdf.setValue("CGI.Role", self.userRec.role)
106 
107  if self.userRec is None or cookieauth.checkLoginCookie(self.ncgi, logincookie, self.authdb, self.username, self.userRec) == 0:
108  warn("invalid cookie", rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
109  self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
110  # ----- the cookie is valid!!!! -------
111 
112  persist = cookieauth.getPersistCookie(hdf)
113  if persist == 0:
114  # reissue a new cookie with an updated timeout
115  if (time.time() - logincookie.issued_at) > config.REFRESH_COOKIE_TIMEOUT:
116  cookieauth.issueLoginCookie(self.ncgi, self.authdb, self.username, self.userRec.pw_hash)
117 
118  self.login = self.username
119 
120  hdf.setValue("CGI.Login", self.username)
121  hdf.setValue("CGI.Login.issued_at", str(logincookie.issued_at))
122 
123  active_user = self.get_active_user()
124  time_since_activity = self.get_active_user_last_activity() # seconds since they did something
125 
126  if active_user:
127  if time_since_activity > config.ACTIVE_USER_TIMEOUT:
128  # time out after one hour?
129  self.remove_active_user()
130  else:
131  # touch the active user file
132  os.utime(config.ACTIVE_USER_FILE, None)
133 
134  hdf.setValue("CGI.active_user", self.get_active_user())
135 
136  if self._pageparms.get("checkActive", True):
137  if hdf.getValue("Cookie.inactive", "0") != "1":
138  if self.get_active_user() == "":
139  self.make_active_user(hdf)
140  elif not self.is_active_user():
141  rurl = config.gBaseURL + "active/active.py"
142  if requestURI.find("/active/") == -1:
143  self.redirectUri(rurl + "?q=1&request=%s" % neo_cgi.urlEscape(requestURI))
144 
145  def get_active_user(self):
146  active_user = ""
147  try:
148  active_user = open(config.ACTIVE_USER_FILE, "r").read().strip()
149  except:
150  pass
151 
152  return active_user
153 
155  time_since_mod = 0
156  try:
157  modified_time = os.path.getmtime(config.ACTIVE_USER_FILE)
158  time_since_mod = (time.time() - modified_time)
159  except:
160  pass
161 
162  return time_since_mod
163 
164  def is_active_user(self):
165  if self.username == self.get_active_user():
166  return True
167  return False
168 
169  def make_active_user(self, hdf):
170  if not os.path.exists(config.ROS_VAR_DIR):
171  os.umask(0)
172  os.mkdir(config.ROS_VAR_DIR, 0777)
173  active_user = open(config.ACTIVE_USER_FILE, "w")
174  active_user.write(self.username)
175  active_user.close()
176 
177  if config.get_robot_type().startswith("texas"):
178  cookie = hdf.getValue("Query.cookie", "")
179  if cookie:
180  cookie_file = open(config.VALID_USER_COOKIE_FILE, "w")
181  cookie_file.write(cookie)
182  cookie_file.close()
183 
184  if config.gLobby:
185  url = config.gLobby + "/lobby/lobby/userrec.py"
186  postdata = {}
187  postdata['Action.MakeActiveUser'] = "1"
188  postdata['active_user'] = self.username
189  postdata['robot'] = config.get_robot_name()
190 
191  fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
192  fp.read()
193  fp.close()
194 
195 
197  if config.get_robot_type().startswith("texas"):
198  if os.path.exists(config.VALID_USER_COOKIE_FILE):
199  os.remove(config.VALID_USER_COOKIE_FILE)
200 
201  if os.path.exists(config.ACTIVE_USER_FILE):
202  os.remove(config.ACTIVE_USER_FILE)
203 
204  if config.gLobby:
205  url = config.gLobby + "/lobby/lobby/userrec.py"
206  postdata = {}
207  postdata['Action.RemoveActiveUser'] = "1"
208  postdata['robot'] = config.get_robot_name()
209 
210  fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
211  fp.read()
212  fp.close()
213 
214 
215 
216  def default_app_path(self):
217  return self.http + self.ncgi.hdf.getValue("HTTP.Host", "") + config.gBaseURL + "%s/" % config.gDefaultModule
218 
219  def close(self):
220  if hasattr(self, "db") and self.db:
221  self.db.close()
222  self.db = None
223  if hasattr(self, "authdb") and self.authdb:
224  self.authdb.close()
225  self.authdb = None
226 
227  def __del__(self):
228  self.close()
229 
230 
def get_active_user(self)
Definition: MBPage.py:145
def setStyleSheet(self, hdf)
Definition: MBPage.py:64
def make_active_user(self, hdf)
Definition: MBPage.py:169
def redirectUri(self, redirectTo)
def handle_actions2(self)
Definition: MBPage.py:69
def is_active_user(self)
Definition: MBPage.py:164
def __del__(self)
Definition: MBPage.py:227
def getUsername(self)
Definition: MBPage.py:76
def get_active_user_last_activity(self)
Definition: MBPage.py:154
def remove_active_user(self)
Definition: MBPage.py:196
def checkLoginCookie(self)
Definition: MBPage.py:89
def subclassinit(self)
Definition: MBPage.py:19
def default_app_path(self)
Definition: MBPage.py:216
def close(self)
Definition: MBPage.py:219


webui
Author(s): Scott Hassan
autogenerated on Mon Jun 10 2019 15:51:24