コード例 #1
0
	def create_session(self, uid, ttl=86400):
		self.clean_sessions()

		# Create statement
		cols = SESSIONS_SCHEMA.columns()
		statement = 'INSERT INTO {} ({}) VALUES ({})'.format(SESSIONS_SCHEMA.name,
				','.join(cols), ','.join(['?' for c in cols]))

		# Attempt creating a session (max 5 times)
		ssid = None
		for i in range(5):
			try:
				ssid = self.__create_ssid(32)
				timestamp = datetime.datetime.utcnow().timestamp()
				self.db.execute_prepared(statement, ssid, uid, timestamp, timestamp + ttl)
				break
			except Error as e:
				# Most likely a unique constraint error
				print('Error creating session: {}', e, file=sys.stderr)
				ssid = None

		# Creating a session failed
		if not ssid:
			raise Error('Could not create a session')
		return ssid
コード例 #2
0
 def get_membership_level(self, gid, uid):
     statement = 'SELECT level FROM {} WHERE uid=? AND gid=?'.format(
         GROUPMEMBERSHIP_SCHEMA.name)
     res = self.db.execute_prepared(statement, uid, gid)
     if len(res) == 0:
         return None
     if len(res) > 1:
         raise Error('returned more than one membership')
     return int(res[0][0])
コード例 #3
0
	def get_user_by_id(self, uid, cols=None):
		columns = '*'
		if cols:
			columns = ','.join(cols)
		statement = 'SELECT {} FROM {} WHERE id=?'.format(columns, USERS_SCHEMA.name)
		res = self.db.execute_prepared(statement, uid)
		if len(res) == 0:
			return None
		elif len(res) > 1:
			raise Error('returned more than one user')
		return res[0]
コード例 #4
0
	def get_session(self, ssid, cols=None):
		columns = '*'
		if cols:
			columns = ','.join(cols)
		statement = 'SELECT {} FROM {} WHERE ssid=?'.format(columns, SESSIONS_SCHEMA.name)
		res = self.db.execute_prepared(statement, ssid)
		if len(res) == 0:
			return None
		if len(res) > 1:
			raise Error('returned more than one session')
		return res[0]
コード例 #5
0
	def get_id(self, email):
		user = self.get_user_by_email(email, ['id'])
		if not user:
			raise Error('user does not exist')
		return user[0]
コード例 #6
0
	def is_admin(self, uid):
		user = self.get_user_by_id(uid, ['admin'])
		if not user:
			raise Error('user does not exist')
		return bool(user[0])