Bug 755425 - update views to support *only* the newstyle user API; r=telliott
authorRyan Kelly <rfkelly@mozilla.com>
Thu, 24 May 2012 16:15:59 +1000
changeset 520 8af97e47bd0e359e388208f9b9f2bd9d3d99c6b9
parent 519 902d603d901686a04513c06ef99de73e5086954f
child 521 448e4500f2e4645c880960fa35bc851ab005122a
push id109
push userrkelly@mozilla.com
push dateThu, 24 May 2012 06:16:13 +0000
reviewerstelliott
bugs755425
Bug 755425 - update views to support *only* the newstyle user API; r=telliott This is a backwards-incompatible change, removing support for oldstyle auth backends (i.e. services.auth.*) and replacing it with support for newstyle backend (i.e. services.user.*).
syncserver/controllers.py
--- a/syncserver/controllers.py
+++ b/syncserver/controllers.py
@@ -39,55 +39,63 @@ User controller. Implements all APIs fro
 https://wiki.mozilla.org/Labs/Weave/User/1.0/API
 
 """
 import os
 
 from webob.response import Response
 from mako.lookup import TemplateLookup
 
+from services.user import User, ServicesUser
 from services.util import (valid_password, text_response, html_response,
                            extract_username)
 
 from syncreg.util import render_mako
 
 _TPL_DIR = os.path.join(os.path.dirname(__file__), 'templates')
 _lookup = TemplateLookup(directories=[_TPL_DIR],
                          module_directory=_TPL_DIR)  # XXX defined in prod
 
 
 class MainController(object):
 
     def __init__(self, app):
         self.app = app
         self.auth = app.auth.backend
+        # Fail noisily if not used with a new-style auth backend.
+        try:
+            is_newstyle_auth = isinstance(self.auth, ServicesUser)
+        except Exception:
+            is_newstyle_auth = False
+        if not is_newstyle_auth:
+            msg = "This code will only work with new-style auth backends."\
+                  " Please set 'auth.backend' to a class from the"\
+                  " services.user package."
+            raise ValueError(msg)
 
     def delete_account_form(self, request, **kw):
         """Returns a form for deleting the account"""
         template = _lookup.get_template('delete_account.mako')
         return html_response(template.render())
 
     def do_delete_account(self, request):
         """Do the delete."""
         user_name = request.POST.get('username')
         password = request.POST.get('password')
         if user_name is None or password is None:
             return text_response('Missing data')
 
         user_name = extract_username(user_name)
-        user_id = self.auth.authenticate_user(user_name, password)
+        user = User(user_name)
+        user_id = self.auth.authenticate_user(user, password)
         if user_id is None:
             return text_response('Bad credentials')
 
         # data deletion
         self.app.get_storage(request).delete_user(user_id)
 
         # user deletion (ldap etc.)
-        user_id = self.auth.get_user_id(user_name)
-        if user_id is not None:
-            res = self.auth.delete_user(user_id, password)
-        else:
-            res = True
+        res = self.auth.delete_user(user, password)
 
         if res:
             return text_response('Account removed.')
         else:
             return text_response('Deletion failed.')