db_auth.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 """
4 usage: %(progname)s [args]
5 """
6 
7 import nstart
8 import os, sys, string, time, getopt
9 import urllib
10 
11 from pyclearsilver.log import *
12 
13 import config
14 
15 from pyclearsilver import odb, hdfhelp, odb_sqlite3
16 from pyclearsilver import CSPage
17 
18 from pyclearsilver.odb import *
19 
20 import pwauth
21 
22 gDBSubPath = "host"
23 gDBFilename = "auth"
24 gDBTablePrefix = "auth"
25 
26 class AuthDB(odb.Database):
27  def __init__(self,db,debug=0):
28  odb.Database.__init__(self, db, debug=debug)
29 
30  self.addTable("users", gDBTablePrefix + "_users", UserTable,
31  rowClass=UserRecord)
32  self.addTable("login", gDBTablePrefix + "_login", UserLoginTable)
33  self.addTable("vcode", gDBTablePrefix + "_vcode", VCodeTable)
34  self.addTable("browserid", gDBTablePrefix + "_browserid", BrowserTable)
35 
36  def defaultRowClass(self):
37  return hdfhelp.HdfRow
39  return hdfhelp.HdfItemList
40 
41  def getAllUsers(self):
42  users = []
43  rows = self.users.fetchAllRows()
44  for row in rows:
45  users.append(row.username)
46 
47  return users
48 
49 
50 
51 
52 class UserTable(odb.Table):
53  def _defineRows(self):
54  self.d_addColumn("uid",kInteger,None,primarykey = 1,
55  autoincrement = 1)
56 
57  self.d_addColumn("username",kVarString, indexed=1, unique=1)
58  self.d_addColumn("role", kVarString, default="")
59  self.d_addColumn("pw_hash",kVarString)
60  self.d_addColumn("status",kInteger, default=0)
61  ## status == 1: disabled user
62  self.d_addColumn("creationDate", kInteger, default=0)
63  self.d_addColumn("dismissed_notices", kVarString, default="")
64  self.d_addColumn("favorite_apps", kVarString, default="")
65  self.d_addColumn("changePassword",kInteger, default=0)
66  self.d_addColumn("skype_id", kVarString, default="")
67 
68  def lookup(self, username):
69  try:
70  row = self.fetchRow(('username', username))
71  except odb.eNoMatchingRows, reason:
72  row = None
73  return row
74 
75  def new(self, username, password):
76  row = self.lookup(username)
77  if row is not None: return row
78 
79  row = self.newRow()
80  row.username = username
81  row.creationDate = int(time.time())
82  row.setPassword(password)
83  row.save()
84 
85  return row
86 
87 class UserRecord(hdfhelp.HdfRow):
88  def checkPasswordHash(self, passwordHash):
89  if len(self.pw_hash) < 2: return 0
90  if passwordHash == self.pw_hash: return 1
91  return 0
92 
93 
94  def checkPassword(self, password):
95  if len(self.pw_hash) < 2: return 0
96 
97  return pwauth.checkPassword(password, self.pw_hash)
98 
99  def setPassword(self, new_password):
100  self.pw_hash = pwauth.cryptPassword(new_password)
101  self.changePassword = 0
102  self.save()
103 
105  return self.favorite_apps.split(',') if len(self.favorite_apps) > 0 else []
106 
107  def is_favorite_app(self, app_taskid):
108  return self.favorite_apps.find(app_taskid) != -1
109 
110  def add_favorite_app(self, app_taskid):
111  apps = self.favorite_apps_list()
112  if not app_taskid in apps:
113  apps.append(app_taskid)
114  self.favorite_apps = string.join(apps, ',')
115  self.save()
116  self.sync_with_lobby()
117 
118  def remove_favorite_app(self, app_taskid):
119  apps = self.favorite_apps_list()
120  if app_taskid in apps:
121  apps.remove(app_taskid)
122  self.favorite_apps = string.join(apps, ',')
123  self.save()
124  self.sync_with_lobby()
125 
127  return self.dismissed_notices.split(',') if len(self.dismissed_notices) > 0 else []
128 
129  def notice_dismissed(self, notice):
130  return self.dismissed_notices.find(notice) != -1
131 
132  def dismiss_notice(self, notice):
133  notices = self.dismissed_notice_list()
134  if not notice in notices:
135  notices.append(notice)
136  self.dismissed_notices = string.join(notices, ',')
137  self.save()
138  self.sync_with_lobby()
139 
140  def set_skype_id(self, id):
141  self.skype_id = id
142  self.save()
143 
144  self.sync_with_lobby()
145 
146  def sync_with_lobby(self):
147  if not config.gLobby: return
148 
149  ## post to the lobby
150  url = config.gLobby + "/lobby/lobby/userrec.py"
151  postdata = {}
152  postdata['username'] = self.username
153 
154  postdata['skype_id'] = self.skype_id
155  postdata['dismissed_notices'] = self.dismissed_notices
156  postdata['favorite_apps'] = self.favorite_apps
157 
158  fp = urllib.urlopen(url, urllib.urlencode(postdata.items()))
159  fp.read()
160  fp.close()
161 
162 
163 class UserLoginTable(odb.Table):
164  def _defineRows(self):
165  self.d_addColumn("uid",kInteger, primarykey=1)
166  self.d_addColumn("username",kVarString, indexed=1, primarykey=1)
167  self.d_addColumn("time", kCreatedStampMS, primarykey=1)
168 
169  self.d_addColumn("loginType", kInteger)
170  # 0 - incorrect password
171  # 1 - correct password
172 
173  self.d_addColumn("browserid",kVarString)
174  self.d_addColumn("ipaddr",kVarString)
175 
176 
177 class VCodeTable(odb.Table):
178  def _defineRows(self):
179  self.d_addColumn("username",kVarString, primarykey=1)
180  self.d_addColumn("vcode",kInteger, default=0)
181  self.d_addColumn("browserid",kInteger, default=0)
182  self.d_addColumn("creationDate", kInteger, default=0)
183 
184 
185 class BrowserTable(odb.Table):
186  def _defineRows(self):
187  self.d_addColumn("browserid",kInteger, primarykey=1, autoincrement=1)
188  self.d_addColumn("ipaddr", kVarString)
189  self.d_addColumn("creationDate", kInteger, default=0)
190 
191 
192 
193 def fullDBPath(path_to_store):
194  return os.path.join(path_to_store, gDBFilename + ".db3")
195 
196 def initSchema(create=0, timeout=None):
197  if timeout is None: timeout = 600
198 
199  path = config.getSiteDBPath(gDBSubPath)
200 
201  if create == 1:
202  config.createDBPath(path)
203 
204  conn = odb_sqlite3.Connection(fullDBPath(path),
205  timeout=timeout)
206 
207  db = AuthDB(conn,debug=debug)
208 
209  if create:
210  db.createTables()
211  db.synchronizeSchema()
212  db.createIndices()
213 
214  if 0:
215  if config.gWebUserID is not None and config.gWebGroupID is not None:
216  config.webChown(fullDBPath(path))
217 
218  return db
219 
220 def exists(username):
221  path = config.getSiteDBPath(gDBSubPath)
222  fn = fullDBPath(path)
223  if os.path.exists(fn):
224  return 1
225  return 0
226 
227 
228 def createDB():
229  db = initSchema(create=1)
230  return db
231 
232 
233 def test():
234  db = initSchema()
235 
236  rows = db.users.fetchAllRows()
237  for row in rows:
238  print row.username, row.pw_hash
239 
240 
241 
242 def usage(progname):
243  print __doc__ % vars()
244 
245 def main(argv, stdout, environ):
246  progname = argv[0]
247  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
248 
249  testflag = 0
250  for (field, val) in optlist:
251  if field == "--help":
252  usage(progname)
253  return
254  elif field == "--debug":
255  debugfull()
256  elif field == "--test":
257  testflag = 1
258 
259  if testflag:
260  test()
261  return
262 
263  db = initSchema(create=1)
264 
265 
266 
267 if __name__ == "__main__":
268  main(sys.argv, sys.stdout, os.environ)
269 
270 
271 
def dismiss_notice(self, notice)
Definition: db_auth.py:132
def main(argv, stdout, environ)
Definition: db_auth.py:245
def usage(progname)
Definition: db_auth.py:242
def setPassword(self, new_password)
Definition: db_auth.py:99
def checkPasswordHash(self, passwordHash)
Definition: db_auth.py:88
def fullDBPath(path_to_store)
Definition: db_auth.py:193
def add_favorite_app(self, app_taskid)
Definition: db_auth.py:110
def remove_favorite_app(self, app_taskid)
Definition: db_auth.py:118
def defaultRowListClass(self)
Definition: db_auth.py:38
def set_skype_id(self, id)
Definition: db_auth.py:140
def notice_dismissed(self, notice)
Definition: db_auth.py:129
def new(self, username, password)
Definition: db_auth.py:75
def checkPassword(self, password)
Definition: db_auth.py:94
def exists(username)
Definition: db_auth.py:220
def lookup(self, username)
Definition: db_auth.py:68
def initSchema(create=0, timeout=None)
Definition: db_auth.py:196
def __init__(self, db, debug=0)
Definition: db_auth.py:27
def is_favorite_app(self, app_taskid)
Definition: db_auth.py:107
def defaultRowClass(self)
Definition: db_auth.py:36


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