76
76
Takes no parameters - gets all the DB info from the configuration."""
77
77
self.db = pg.connect(dbname=conf.db_dbname, host=conf.db_host,
78
78
port=conf.db_port, user=conf.db_user, passwd=conf.db_password)
80
85
# USER MANAGEMENT FUNCTIONS #
82
87
def create_user(self, login, password, unixid, email, nick, fullname,
83
rolenm, studentid, dry=False):
88
rolenm, studentid, acct_exp=None, dry=False):
84
89
"""Creates a user login entry in the database.
85
90
Arguments are the same as those in the "login" table of the schema.
86
91
The exception is "password", which is a cleartext password. makeuser
92
97
passhash = _passhash(password)
93
98
query = ("INSERT INTO login (login, passhash, state, unixid, email, "
94
"nick, fullname, rolenm, studentid) VALUES "
99
"nick, fullname, rolenm, studentid, acct_exp) VALUES "
95
100
"(%s, %s, 'no_agreement', %d, %s, %s, %s, %s, %s);" %
96
101
(_escape(login), _escape(passhash), unixid, _escape(email),
97
102
_escape(nick), _escape(fullname), _escape(rolenm),
103
_escape(studentid), _escape(acct_exp))
99
104
if dry: return query
100
105
self.db.query(query)
102
107
def update_user(self, login, password=None, state=None, email=None,
103
nick=None, fullname=None, rolenm=None, dry=False):
108
nick=None, fullname=None, rolenm=None, acct_exp=None, pass_exp=None,
109
last_login=None, dry=False):
104
110
"""Updates fields of a particular user. login is the name of the user
105
111
to update. The other arguments are optional fields which may be
106
112
modified. If None or omitted, they do not get modified. login and
111
117
changed without knowing the old password. The caller should check
112
118
that the user knows the existing password before calling this function
121
FIXME: this interface does not allow fields to be set to NULL (None).
115
123
# Make a list of SQL fragments of the form "field = 'new value'"
116
124
# These fragments are ALREADY-ESCAPED
118
if password is not None:
126
if passhash is not None:
119
127
setlist.append("passhash = " + _escape(_passhash(password)))
120
128
if state is not None:
121
129
setlist.append("state = " + _escape(state))
127
135
setlist.append("fullname = " + _escape(fullname))
128
136
if rolenm is not None:
129
137
setlist.append("rolenm = " + _escape(rolenm))
138
if pass_exp is not None:
139
setlist.append("pass_exp = " + _escape(pass_exp))
140
if acct_exp is not None:
141
setlist.append("acct_exp = " + _escape(acct_exp))
142
if last_login is not None:
143
setlist.append("last_login = " + _escape(last_login))
130
144
if len(setlist) == 0:
132
146
# Join the fragments into a comma-separated string
173
187
def user_authenticate(self, login, password, dry=False):
174
188
"""Performs a password authentication on a user. Returns True if
175
"password" is the correct password for the given login, False
176
otherwise. "password" is cleartext.
189
"passhash" is the correct passhash for the given login, False
177
191
Also returns False if the login does not exist (so if you want to
178
192
differentiate these cases, use get_user and catch an exception).