more user stuff

This commit is contained in:
dave 2017-08-15 21:40:38 -07:00
parent f17d53296e
commit 517e147028
4 changed files with 41 additions and 20 deletions

View File

@ -285,11 +285,13 @@ class PysonicApi(object):
def getUser_view(self, u, username, **kwargs):
cherrypy.response.headers['Content-Type'] = 'text/xml; charset=utf-8'
doc, root = self.response()
user = doc.new_tag("user",
username="admin",
email="admin@localhost",
user = {} if self.options.disable_auth else self.library.db.get_user(cherrypy.request.login)
tag = doc.new_tag("user",
username=user["username"],
email=user["email"],
scrobblingEnabled="false",
adminRole="false",
adminRole="true" if user["admin"] else "false",
settingsRole="false",
downloadRole="true",
uploadRole="false",
@ -302,8 +304,9 @@ class PysonicApi(object):
shareRole="true",
videoConversionRole="false",
avatarLastChanged="2017-08-07T20:16:24.596Z")
root.append(user)
root.append(tag)
folder = doc.new_tag("folder")
folder.append("0")
user.append(folder)
tag.append(folder)
yield doc.prettify()

View File

@ -45,6 +45,7 @@ def main():
logging.warning("Artists: {}".format([i["name"] for i in library.get_artists()]))
logging.warning("Albums: {}".format(len(library.get_albums())))
api = PysonicApi(db, library, args)
api_config = {}
if args.disable_auth:
logging.warning("starting up with auth disabled")
@ -52,7 +53,7 @@ def main():
api_config.update({'tools.auth_basic.on': True,
'tools.auth_basic.realm': 'pysonic',
'tools.auth_basic.checkpassword': db.validate_password})
cherrypy.tree.mount(PysonicApi(db, library, args), '/rest/', {'/': api_config})
cherrypy.tree.mount(api, '/rest/', {'/': api_config})
cherrypy.config.update({
'sessionFilter.on': True,

View File

@ -17,6 +17,10 @@ def dict_factory(cursor, row):
return d
class NotFoundError(Exception):
pass
class PysonicDatabase(object):
def __init__(self, path):
self.sqlite_opts = dict(check_same_thread=False, cached_statements=0, isolation_level=None)
@ -141,3 +145,12 @@ class PysonicDatabase(object):
with closing(self.db.cursor()) as cursor:
cursor.execute("REPLACE INTO users (username, password, admin) VALUES (?, ?, ?)",
(username, self.hashit(password), is_admin)).fetchall()
def get_user(self, user):
with closing(self.db.cursor()) as cursor:
try:
column = "id" if type(user) is int else "username"
return cursor.execute("SELECT * FROM users WHERE {}=?;".format(column), (user, )).fetchall()[0]
except IndexError:
raise NotFoundError("User doesn't exist")

View File

@ -93,3 +93,7 @@ class PysonicLibrary(object):
"mediumImageUrl": "",
"largeImageUrl": "",
"similarArtists": []}
def get_user(self, user):
if type(user) is int:
return self.db.get_user(username)