db_auth.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 """
00004 usage: %(progname)s [args]
00005 """
00006 
00007 import nstart
00008 import os, sys, string, time, getopt
00009 import urllib
00010 
00011 from pyclearsilver.log import *
00012 
00013 import config
00014 
00015 from pyclearsilver import odb, hdfhelp, odb_sqlite3
00016 from pyclearsilver import CSPage
00017 
00018 from pyclearsilver.odb import *
00019 
00020 import pwauth
00021 
00022 gDBSubPath = "host"
00023 gDBFilename = "auth"
00024 gDBTablePrefix = "auth"
00025 
00026 class AuthDB(odb.Database):
00027   def __init__(self,db,debug=0):
00028     odb.Database.__init__(self, db, debug=debug)
00029 
00030     self.addTable("users", gDBTablePrefix + "_users", UserTable,
00031                   rowClass=UserRecord)
00032     self.addTable("login", gDBTablePrefix + "_login", UserLoginTable)
00033     self.addTable("vcode", gDBTablePrefix + "_vcode", VCodeTable)
00034     self.addTable("browserid", gDBTablePrefix + "_browserid", BrowserTable)
00035 
00036   def defaultRowClass(self):
00037     return hdfhelp.HdfRow
00038   def defaultRowListClass(self):
00039     return hdfhelp.HdfItemList
00040 
00041   def getAllUsers(self):
00042     users = []
00043     rows = self.users.fetchAllRows()
00044     for row in rows:
00045       users.append(row.username)
00046 
00047     return users
00048     
00049 
00050 
00051 
00052 class UserTable(odb.Table):
00053   def _defineRows(self):
00054     self.d_addColumn("uid",kInteger,None,primarykey = 1,
00055                      autoincrement = 1)
00056 
00057     self.d_addColumn("username",kVarString, indexed=1, unique=1)
00058     self.d_addColumn("role", kVarString, default="")
00059     self.d_addColumn("pw_hash",kVarString)
00060     self.d_addColumn("status",kInteger, default=0)
00061     ## status == 1: disabled user
00062     self.d_addColumn("creationDate", kInteger, default=0)
00063     self.d_addColumn("dismissed_notices", kVarString, default="")
00064     self.d_addColumn("favorite_apps", kVarString, default="")
00065     self.d_addColumn("changePassword",kInteger, default=0)
00066     self.d_addColumn("skype_id", kVarString, default="") 
00067 
00068   def lookup(self, username):
00069     try:
00070       row = self.fetchRow(('username', username))
00071     except odb.eNoMatchingRows, reason:
00072       row = None
00073     return row
00074 
00075   def new(self, username, password):
00076     row = self.lookup(username)
00077     if row is not None: return row
00078 
00079     row = self.newRow()
00080     row.username = username
00081     row.creationDate = int(time.time())
00082     row.setPassword(password)
00083     row.save()
00084 
00085     return row
00086     
00087 class UserRecord(hdfhelp.HdfRow):
00088   def checkPasswordHash(self, passwordHash):
00089     if len(self.pw_hash) < 2: return 0
00090     if passwordHash == self.pw_hash: return 1
00091     return 0
00092 
00093 
00094   def checkPassword(self, password):
00095     if len(self.pw_hash) < 2: return 0
00096 
00097     return pwauth.checkPassword(password, self.pw_hash)
00098 
00099   def setPassword(self, new_password):
00100     self.pw_hash = pwauth.cryptPassword(new_password)
00101     self.changePassword = 0
00102     self.save()
00103     
00104   def favorite_apps_list(self):
00105     return self.favorite_apps.split(',') if len(self.favorite_apps) > 0 else []
00106     
00107   def is_favorite_app(self, app_taskid):
00108     return self.favorite_apps.find(app_taskid) != -1
00109     
00110   def add_favorite_app(self, app_taskid):
00111     apps = self.favorite_apps_list()
00112     if not app_taskid in apps:
00113       apps.append(app_taskid)
00114       self.favorite_apps = string.join(apps, ',')
00115       self.save()  
00116       self.sync_with_lobby()
00117 
00118   def remove_favorite_app(self, app_taskid):
00119     apps = self.favorite_apps_list()
00120     if app_taskid in apps:
00121       apps.remove(app_taskid)
00122       self.favorite_apps = string.join(apps, ',')
00123       self.save()        
00124       self.sync_with_lobby()
00125 
00126   def dismissed_notice_list(self):
00127     return self.dismissed_notices.split(',') if len(self.dismissed_notices) > 0 else []
00128 
00129   def notice_dismissed(self, notice):
00130     return self.dismissed_notices.find(notice) != -1
00131 
00132   def dismiss_notice(self, notice):
00133     notices = self.dismissed_notice_list()
00134     if not notice in notices:
00135       notices.append(notice)
00136       self.dismissed_notices = string.join(notices, ',')
00137       self.save()
00138       self.sync_with_lobby()
00139 
00140   def set_skype_id(self, id):
00141     self.skype_id = id
00142     self.save()
00143 
00144     self.sync_with_lobby()
00145 
00146   def sync_with_lobby(self):
00147     if not config.gLobby: return
00148 
00149     ## post to the lobby
00150     url = config.gLobby + "/lobby/lobby/userrec.py"
00151     postdata = {}
00152     postdata['username'] = self.username
00153 
00154     postdata['skype_id'] = self.skype_id
00155     postdata['dismissed_notices'] = self.dismissed_notices
00156     postdata['favorite_apps'] = self.favorite_apps
00157 
00158     fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
00159     fp.read()
00160     fp.close()
00161                                               
00162 
00163 class UserLoginTable(odb.Table):
00164   def _defineRows(self):
00165     self.d_addColumn("uid",kInteger, primarykey=1)
00166     self.d_addColumn("username",kVarString, indexed=1, primarykey=1)
00167     self.d_addColumn("time", kCreatedStampMS, primarykey=1)
00168 
00169     self.d_addColumn("loginType", kInteger)   
00170     # 0 - incorrect password
00171     # 1 - correct password
00172 
00173     self.d_addColumn("browserid",kVarString)
00174     self.d_addColumn("ipaddr",kVarString)
00175 
00176 
00177 class VCodeTable(odb.Table):
00178   def _defineRows(self):
00179     self.d_addColumn("username",kVarString, primarykey=1)
00180     self.d_addColumn("vcode",kInteger, default=0)
00181     self.d_addColumn("browserid",kInteger, default=0)
00182     self.d_addColumn("creationDate", kInteger, default=0)
00183 
00184 
00185 class BrowserTable(odb.Table):
00186   def _defineRows(self):
00187     self.d_addColumn("browserid",kInteger, primarykey=1, autoincrement=1)
00188     self.d_addColumn("ipaddr", kVarString)
00189     self.d_addColumn("creationDate", kInteger, default=0)
00190 
00191     
00192 
00193 def fullDBPath(path_to_store):
00194   return os.path.join(path_to_store, gDBFilename + ".db3")
00195 
00196 def initSchema(create=0, timeout=None):
00197   if timeout is None: timeout = 600
00198 
00199   path = config.getSiteDBPath(gDBSubPath)
00200 
00201   if create == 1:
00202     config.createDBPath(path)
00203 
00204   conn = odb_sqlite3.Connection(fullDBPath(path),
00205                                timeout=timeout)
00206 
00207   db = AuthDB(conn,debug=debug)
00208 
00209   if create:
00210     db.createTables()
00211     db.synchronizeSchema()
00212     db.createIndices()
00213 
00214     if 0:
00215       if config.gWebUserID is not None and config.gWebGroupID is not None:
00216         config.webChown(fullDBPath(path))
00217 
00218   return db
00219 
00220 def exists(username):
00221   path = config.getSiteDBPath(gDBSubPath)
00222   fn = fullDBPath(path)
00223   if os.path.exists(fn): 
00224     return 1
00225   return 0
00226   
00227 
00228 def createDB():
00229   db = initSchema(create=1)
00230   return db
00231   
00232   
00233 def test():
00234   db = initSchema()
00235 
00236   rows = db.users.fetchAllRows()
00237   for row in rows:
00238     print row.username, row.pw_hash
00239 
00240 
00241 
00242 def usage(progname):
00243   print __doc__ % vars()
00244 
00245 def main(argv, stdout, environ):
00246   progname = argv[0]
00247   optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00248 
00249   testflag = 0
00250   for (field, val) in optlist:
00251     if field == "--help":
00252       usage(progname)
00253       return
00254     elif field == "--debug":
00255       debugfull()
00256     elif field == "--test":
00257       testflag = 1
00258 
00259   if testflag:
00260     test()
00261     return
00262 
00263   db = initSchema(create=1)
00264 
00265 
00266 
00267 if __name__ == "__main__":
00268   main(sys.argv, sys.stdout, os.environ)
00269 
00270   
00271   


webui
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Wed Apr 23 2014 10:36:00