diff --git a/pysonic/api.py b/pysonic/api.py index a1ab0e4..ce2a354 100644 --- a/pysonic/api.py +++ b/pysonic/api.py @@ -285,25 +285,28 @@ 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", - scrobblingEnabled="false", - adminRole="false", - settingsRole="false", - downloadRole="true", - uploadRole="false", - playlistRole="true", - coverArtRole="false", - commentRole="false", - podcastRole="false", - streamRole="true", - jukeboxRole="false", - shareRole="true", - videoConversionRole="false", - avatarLastChanged="2017-08-07T20:16:24.596Z") - root.append(user) + + 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="true" if user["admin"] else "false", + settingsRole="false", + downloadRole="true", + uploadRole="false", + playlistRole="true", + coverArtRole="false", + commentRole="false", + podcastRole="false", + streamRole="true", + jukeboxRole="false", + shareRole="true", + videoConversionRole="false", + avatarLastChanged="2017-08-07T20:16:24.596Z") + root.append(tag) folder = doc.new_tag("folder") folder.append("0") - user.append(folder) + tag.append(folder) yield doc.prettify() + diff --git a/pysonic/daemon.py b/pysonic/daemon.py index 658f320..ddbeba1 100644 --- a/pysonic/daemon.py +++ b/pysonic/daemon.py @@ -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, diff --git a/pysonic/database.py b/pysonic/database.py index d2d410e..1d9e677 100644 --- a/pysonic/database.py +++ b/pysonic/database.py @@ -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") + diff --git a/pysonic/library.py b/pysonic/library.py index 086df7b..9357d98 100644 --- a/pysonic/library.py +++ b/pysonic/library.py @@ -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)