Allow logging in with email address

This commit is contained in:
JonnyWong16 2016-04-25 19:08:12 -07:00
parent 5d7ba8cf14
commit d462ebe8e5
2 changed files with 19 additions and 9 deletions

View file

@ -306,7 +306,7 @@ class Users(object):
except Exception as e:
logger.warn(u"PlexPy Users :: Unable to execute database query for set_config: %s." % e)
def get_details(self, user_id=None, user=None):
def get_details(self, user_id=None, user=None, email=None):
from plexpy import plextv
default_return = {'user_id': 0,
@ -322,10 +322,10 @@ class Users(object):
'allow_guest': 0
}
if not user_id and not user:
if not user_id and not user and not email:
return default_return
def get_user_details(user_id=user_id, user=user):
def get_user_details(user_id=user_id, user=user, email=email):
monitor_db = database.MonitorDatabase()
try:
@ -341,6 +341,12 @@ class Users(object):
'FROM users ' \
'WHERE username = ? '
result = monitor_db.select(query, args=[user])
elif email:
query = 'SELECT user_id, username, friendly_name, thumb AS user_thumb, custom_avatar_url AS custom_thumb, ' \
'email, is_home_user, is_allow_sync, is_restricted, do_notify, keep_history, deleted_user, allow_guest ' \
'FROM users ' \
'WHERE email = ? '
result = monitor_db.select(query, args=[email])
else:
result = []
except Exception as e:

View file

@ -18,10 +18,11 @@
# Form based authentication for CherryPy. Requires the
# Session tool to be loaded.
import cherrypy
from cgi import escape
from hashing_passwords import check_hash
import cherrypy
from datetime import datetime, timedelta
from hashing_passwords import check_hash
import re
import plexpy
from plexpy import logger
@ -33,7 +34,7 @@ SESSION_KEY = '_cp_username'
def check_credentials(username, password):
"""Verifies credentials for username and password.
Returns None on success or a string describing the error on failure"""
Returns True and the user group on success or False and no user group"""
if plexpy.CONFIG.HTTP_HASHED_PASSWORD and \
username == plexpy.CONFIG.HTTP_USERNAME and check_hash(password, plexpy.CONFIG.HTTP_PASSWORD):
@ -146,6 +147,9 @@ class AuthController(object):
if vaild_login:
if user_group == 'guest':
if re.match(r"[^@]+@[^@]+\.[^@]+", username):
user_details = Users().get_details(email=username)
else:
user_details = Users().get_details(user=username)
user_id = user_details['user_id']
@ -173,7 +177,7 @@ class AuthController(object):
else:
logger.debug(u"Invalid login attempt from '%s'." % username)
return self.get_loginform(username, u"Incorrect username or password.")
return self.get_loginform(username, u"Incorrect username/email or password.")
@cherrypy.expose
def logout(self):
@ -183,7 +187,7 @@ class AuthController(object):
_session = cherrypy.session.get(SESSION_KEY)
cherrypy.session[SESSION_KEY] = None
if _session['user']:
if _session and _session['user']:
cherrypy.request.login = None
self.on_logout(_session['user'])
raise cherrypy.HTTPRedirect("login")