changeset 6205:12bc5b6057a7

auth: cleanup of EXTERN_TYPE_INTERNAL Don't set it in top level namespace - it is a weak link between the database and the actual implementation. Don't make it more than that. Don't hardcode in that many places that 'internal' is the default - just call it DEFAULT_AUTH_TYPE. Don't use it for extern_name - it is only intended for use as extern_type. Remove unused uses.
author Mads Kiilerich <madski@unity3d.com>
date Mon, 12 Sep 2016 17:41:19 +0200
parents 09dc083f461f
children a9b9af11699e
files kallithea/__init__.py kallithea/controllers/admin/my_account.py kallithea/controllers/admin/users.py kallithea/controllers/api/api.py kallithea/lib/auth_modules/__init__.py kallithea/lib/auth_modules/auth_internal.py kallithea/lib/db_manage.py kallithea/model/db.py kallithea/model/user.py
diffstat 9 files changed, 14 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/__init__.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/__init__.py	Mon Sep 12 17:41:19 2016 +0200
@@ -51,8 +51,6 @@
 else:
     assert False, 'Database rebranding is no longer supported; see README.'
 
-# Users.extern_type and .extern_name value for local users
-EXTERN_TYPE_INTERNAL = 'internal'
 
 __version__ = '.'.join(str(each) for each in VERSION)
 __dbversion__ = 31  # defines current db version for migrations
--- a/kallithea/controllers/admin/my_account.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/controllers/admin/my_account.py	Mon Sep 12 17:41:19 2016 +0200
@@ -35,7 +35,6 @@
 from pylons.i18n.translation import _
 from webob.exc import HTTPFound
 
-from kallithea import EXTERN_TYPE_INTERNAL
 from kallithea.lib import helpers as h
 from kallithea.lib import auth_modules
 from kallithea.lib.auth import LoginRequired, NotAnonymous, AuthUser
@@ -70,7 +69,6 @@
             h.flash(_("You can't edit this user since it's"
                       " crucial for entire application"), category='warning')
             raise HTTPFound(location=url('users'))
-        c.EXTERN_TYPE_INTERNAL = EXTERN_TYPE_INTERNAL
 
     def _load_my_repos_data(self, watched=False):
         if watched:
--- a/kallithea/controllers/admin/users.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/controllers/admin/users.py	Mon Sep 12 17:41:19 2016 +0200
@@ -42,7 +42,6 @@
 from kallithea.lib.auth import LoginRequired, HasPermissionAnyDecorator, \
     AuthUser
 from kallithea.lib import auth_modules
-from kallithea.lib.auth_modules import auth_internal
 from kallithea.lib.base import BaseController, render
 from kallithea.model.api_key import ApiKeyModel
 
@@ -65,7 +64,6 @@
     def __before__(self):
         super(UsersController, self).__before__()
         c.available_permissions = config['available_permissions']
-        c.EXTERN_TYPE_INTERNAL = kallithea.EXTERN_TYPE_INTERNAL
 
     def index(self, format='html'):
         c.users_list = User.query().order_by(User.username) \
@@ -115,8 +113,8 @@
         return render('admin/users/users.html')
 
     def create(self):
-        c.default_extern_type = auth_internal.KallitheaAuthPlugin.name
-        c.default_extern_name = auth_internal.KallitheaAuthPlugin.name
+        c.default_extern_type = User.DEFAULT_AUTH_TYPE
+        c.default_extern_name = ''
         user_model = UserModel()
         user_form = UserForm()()
         try:
@@ -144,8 +142,8 @@
         raise HTTPFound(location=url('edit_user', id=user.user_id))
 
     def new(self, format='html'):
-        c.default_extern_type = auth_internal.KallitheaAuthPlugin.name
-        c.default_extern_name = auth_internal.KallitheaAuthPlugin.name
+        c.default_extern_type = User.DEFAULT_AUTH_TYPE
+        c.default_extern_name = ''
         return render('admin/users/user_add.html')
 
     def update(self, id):
--- a/kallithea/controllers/api/api.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/controllers/api/api.py	Mon Sep 12 17:41:19 2016 +0200
@@ -30,7 +30,6 @@
 import logging
 from sqlalchemy import or_
 
-from kallithea import EXTERN_TYPE_INTERNAL
 from kallithea.controllers.api import JSONRPCController, JSONRPCError
 from kallithea.lib.auth import (
     PasswordGenerator, AuthUser, HasPermissionAnyDecorator,
@@ -620,8 +619,8 @@
     def create_user(self, apiuser, username, email, password=Optional(''),
                     firstname=Optional(''), lastname=Optional(''),
                     active=Optional(True), admin=Optional(False),
-                    extern_name=Optional(EXTERN_TYPE_INTERNAL),
-                    extern_type=Optional(EXTERN_TYPE_INTERNAL)):
+                    extern_type=Optional(User.DEFAULT_AUTH_TYPE),
+                    extern_name=Optional('')):
         """
         Creates new user. Returns new user object. This command can
         be executed only using api_key belonging to user with admin rights.
--- a/kallithea/lib/auth_modules/__init__.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/lib/auth_modules/__init__.py	Mon Sep 12 17:41:19 2016 +0200
@@ -18,7 +18,6 @@
 import logging
 import traceback
 
-from kallithea import EXTERN_TYPE_INTERNAL
 from kallithea.lib.compat import importlib
 from kallithea.lib.utils2 import str2bool
 from kallithea.lib.compat import formatted_json, hybrid_property
@@ -314,8 +313,6 @@
         parts = plugin.split(u'.lib.auth_modules.auth_', 1)
         if len(parts) == 2:
             _module, pn = parts
-            if pn == EXTERN_TYPE_INTERNAL:
-                pn = "internal"
             plugin = u'kallithea.lib.auth_modules.auth_' + pn
     PLUGIN_CLASS_NAME = "KallitheaAuthPlugin"
     try:
--- a/kallithea/lib/auth_modules/auth_internal.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/lib/auth_modules/auth_internal.py	Mon Sep 12 17:41:19 2016 +0200
@@ -28,7 +28,6 @@
 
 import logging
 
-from kallithea import EXTERN_TYPE_INTERNAL
 from kallithea.lib import auth_modules
 from kallithea.lib.compat import formatted_json, hybrid_property
 from kallithea.model.db import User
@@ -42,7 +41,8 @@
 
     @hybrid_property
     def name(self):
-        return EXTERN_TYPE_INTERNAL
+        # Also found as kallithea.lib.model.db.User.DEFAULT_AUTH_TYPE
+        return 'internal'
 
     def settings(self):
         return []
--- a/kallithea/lib/db_manage.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/lib/db_manage.py	Mon Sep 12 17:41:19 2016 +0200
@@ -36,7 +36,7 @@
 import alembic.config
 import alembic.command
 
-from kallithea import __dbversion__, __py_version__, EXTERN_TYPE_INTERNAL
+from kallithea import __dbversion__, __py_version__
 from kallithea.lib.paster_commands.common import ask_ok
 from kallithea.model.user import UserModel
 from kallithea.model import init_model
@@ -464,7 +464,7 @@
         UserModel().create_or_update(username, password, email,
                                      firstname=u'Kallithea', lastname=u'Admin',
                                      active=True, admin=admin,
-                                     extern_type=EXTERN_TYPE_INTERNAL)
+                                     extern_type=User.DEFAULT_AUTH_TYPE)
 
     def create_default_user(self):
         log.info('creating default user')
--- a/kallithea/model/db.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/model/db.py	Mon Sep 12 17:41:19 2016 +0200
@@ -427,6 +427,8 @@
 
     DEFAULT_USER = 'default'
     DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
+    # The name of the default auth type in extern_type, 'internal' lives in auth_internal.py
+    DEFAULT_AUTH_TYPE = 'internal'
 
     user_id = Column(Integer(), primary_key=True)
     username = Column(String(255), nullable=False, unique=True)
--- a/kallithea/model/user.py	Mon Sep 12 17:41:19 2016 +0200
+++ b/kallithea/model/user.py	Mon Sep 12 17:41:19 2016 +0200
@@ -37,7 +37,6 @@
 
 from sqlalchemy.exc import DatabaseError
 
-from kallithea import EXTERN_TYPE_INTERNAL
 from kallithea.lib.utils2 import safe_str, generate_api_key, get_current_authuser
 from kallithea.lib.caching_query import FromCache
 from kallithea.model import BaseModel
@@ -179,8 +178,8 @@
         import kallithea.lib.helpers as h
 
         form_data['admin'] = False
-        form_data['extern_name'] = EXTERN_TYPE_INTERNAL
-        form_data['extern_type'] = EXTERN_TYPE_INTERNAL
+        form_data['extern_type'] = User.DEFAULT_AUTH_TYPE
+        form_data['extern_name'] = ''
         new_user = self.create(form_data)
 
         self.sa.add(new_user)