From 0b1c55f6a59459c79d71c26b8848374c5062a39f Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Fri, 6 Nov 2015 17:44:48 -0600 Subject: [PATCH] fallback to default avatar if email is not set auth submodules are registered with initialized check if auth.local is loaded before accessing registration route check DB_URI before attempt to create db --- realms/__init__.py | 8 +++++--- realms/config/__init__.py | 2 +- realms/lib/util.py | 3 ++- realms/modules/auth/__init__.py | 1 + realms/modules/auth/ldap/__init__.py | 2 ++ realms/modules/auth/local/__init__.py | 3 +++ realms/modules/auth/models.py | 11 ++++++----- realms/modules/auth/oauth/__init__.py | 3 +++ realms/templates/layout.html | 2 +- 9 files changed, 24 insertions(+), 11 deletions(-) diff --git a/realms/__init__.py b/realms/__init__.py index 808bf41..59899cc 100644 --- a/realms/__init__.py +++ b/realms/__init__.py @@ -113,6 +113,7 @@ class Assets(Environment): class MyLDAPLoginManager(LDAPLoginManager): @property def attrlist(self): + # the parent method doesn't always work return None class RegexConverter(BaseConverter): @@ -188,16 +189,17 @@ def create_app(config=None): def page_not_found(e): return render_template('errors/404.html'), 404 - if app.config['RELATIVE_PATH']: + if app.config.get('RELATIVE_PATH'): @app.route("/") def root(): - return redirect(url_for(app.config['ROOT_ENDPOINT'])) + return redirect(url_for(app.config.get('ROOT_ENDPOINT'))) app.discover() # This will be removed at some point with app.app_context(): - db.metadata.create_all(db.get_engine(app)) + if app.config.get('DB_URI'): + db.metadata.create_all(db.get_engine(app)) return app diff --git a/realms/config/__init__.py b/realms/config/__init__.py index 2f9fd58..ffe8050 100644 --- a/realms/config/__init__.py +++ b/realms/config/__init__.py @@ -86,7 +86,7 @@ DB_URI = 'sqlite:////tmp/wiki.db' LDAP = { 'URI': 'ldap://localhost:8389', - # This BIND_DN/BIND_PASSORD default to '', this is shown here for demonstrative purposes + # This BIND_DN/BIND_PASSWORD default to '', this is shown here for demonstrative purposes # The values '' perform an anonymous bind so we may use search/bind method 'BIND_DN': '', 'BIND_AUTH': '', diff --git a/realms/lib/util.py b/realms/lib/util.py index 0bd115d..5eed026 100644 --- a/realms/lib/util.py +++ b/realms/lib/util.py @@ -119,7 +119,8 @@ def filename_to_cname(filename): def gravatar_url(email): - return "https://www.gravatar.com/avatar/" + hashlib.md5(email).hexdigest() + email = hashlib.md5(email).hexdigest() if email else "default@realms.io" + return "https://www.gravatar.com/avatar/" + email def in_virtualenv(): diff --git a/realms/modules/auth/__init__.py b/realms/modules/auth/__init__.py index 8487cd9..a33ffda 100644 --- a/realms/modules/auth/__init__.py +++ b/realms/modules/auth/__init__.py @@ -2,6 +2,7 @@ from realms import login_manager from flask import request, flash, redirect from flask.ext.login import login_url +modules = set() @login_manager.unauthorized_handler def unauthorized(): diff --git a/realms/modules/auth/ldap/__init__.py b/realms/modules/auth/ldap/__init__.py index 8b13789..f3d364d 100644 --- a/realms/modules/auth/ldap/__init__.py +++ b/realms/modules/auth/ldap/__init__.py @@ -1 +1,3 @@ +from ..models import Auth +Auth.register('ldap') diff --git a/realms/modules/auth/local/__init__.py b/realms/modules/auth/local/__init__.py index e69de29..8c939c1 100644 --- a/realms/modules/auth/local/__init__.py +++ b/realms/modules/auth/local/__init__.py @@ -0,0 +1,3 @@ +from ..models import Auth + +Auth.register('local') diff --git a/realms/modules/auth/models.py b/realms/modules/auth/models.py index eca945d..f62b736 100644 --- a/realms/modules/auth/models.py +++ b/realms/modules/auth/models.py @@ -4,6 +4,7 @@ from realms import login_manager from realms.lib.util import gravatar_url from itsdangerous import URLSafeSerializer, BadSignature from hashlib import sha256 +from . import modules import bcrypt import importlib @@ -17,6 +18,10 @@ auth_users = {} class Auth(object): + @staticmethod + def register(module): + modules.add(module) + @staticmethod def get_auth_user(auth_type): mod = importlib.import_module('realms.modules.auth.%s.models' % auth_type) @@ -30,8 +35,7 @@ class Auth(object): @staticmethod def login_forms(): forms = [] - # TODO be dynamic - for t in ['local', 'ldap', 'oauth']: + for t in modules: forms.append(Auth.get_auth_user(t).login_form()) return "
".join(forms) @@ -61,9 +65,6 @@ class BaseUser(UserMixin): @property def avatar(self): - if not self.email: - # TODO return default avatar - return "" return gravatar_url(self.email) @staticmethod diff --git a/realms/modules/auth/oauth/__init__.py b/realms/modules/auth/oauth/__init__.py index e69de29..7a84b08 100644 --- a/realms/modules/auth/oauth/__init__.py +++ b/realms/modules/auth/oauth/__init__.py @@ -0,0 +1,3 @@ +from ..models import Auth + +Auth.register('oauth') diff --git a/realms/templates/layout.html b/realms/templates/layout.html index 85bbef6..59ea243 100644 --- a/realms/templates/layout.html +++ b/realms/templates/layout.html @@ -73,7 +73,7 @@ {% else %}
  •  Login
  • - {% if config.REGISTRATION_ENABLED %} + {% if config.REGISTRATION_ENABLED and 'auth.local' in config.MODULES %}
  •  Register
  • {% endif %} {% endif %}