Browse Source

HUE-962 [useradmin] Improve AllowAllBackend experience

Fix a Jobbrowser 2.4 Python compatibility
Romain Rigaux 12 years ago
parent
commit
4eeb114

+ 5 - 2
apps/jobbrowser/src/jobbrowser/views.py

@@ -282,18 +282,21 @@ def single_task_attempt_logs(request, job, taskid, attemptid):
   except (KeyError, RestException), e:
   except (KeyError, RestException), e:
     raise KeyError(_("Cannot find attempt '%(id)s' in task") % {'id': attemptid}, e)
     raise KeyError(_("Cannot find attempt '%(id)s' in task") % {'id': attemptid}, e)
 
 
+  first_log_tab = 0
+
   try:
   try:
     # Add a diagnostic log
     # Add a diagnostic log
     diagnostic_log = ", ".join(task.diagnosticMap[attempt.attemptId])
     diagnostic_log = ", ".join(task.diagnosticMap[attempt.attemptId])
     logs = [ diagnostic_log ]
     logs = [ diagnostic_log ]
     # Add remaining logs
     # Add remaining logs
     logs += [ section.strip() for section in attempt.get_task_log() ]
     logs += [ section.strip() for section in attempt.get_task_log() ]
-    first_log_tab = next((i for i, log in enumerate(logs) if log), 0)
+    log_tab = [i for i, log in enumerate(logs) if log]
+    if log_tab:
+      first_log_tab = log_tab[0]
   except TaskTrackerNotFoundException:
   except TaskTrackerNotFoundException:
     # Four entries,
     # Four entries,
     # for diagnostic, stdout, stderr and syslog
     # for diagnostic, stdout, stderr and syslog
     logs = [ _("Failed to retrieve log. TaskTracker not found.") ] * 4
     logs = [ _("Failed to retrieve log. TaskTracker not found.") ] * 4
-    first_log_tab = 0
 
 
   return render("attempt_logs.mako", request, {
   return render("attempt_logs.mako", request, {
       "attempt": attempt,
       "attempt": attempt,

+ 9 - 2
desktop/core/src/desktop/auth/backend.py

@@ -181,10 +181,17 @@ class AllowFirstUserDjangoBackend(django.contrib.auth.backends.ModelBackend):
 class AllowAllBackend(DesktopBackendBase):
 class AllowAllBackend(DesktopBackendBase):
   """
   """
   Authentication backend that allows any user to login as long
   Authentication backend that allows any user to login as long
-  as they have a username and password of any kind.
+  as they have a username. The users will be added to the 'default_user_group'.
   """
   """
   def check_auth(self, username, password):
   def check_auth(self, username, password):
-    return True
+    user = find_or_create_user(username, None)
+    user.is_superuser = False
+    user.save()
+    default_group = get_default_user_group()
+    if default_group is not None:
+      user.groups.add(default_group)
+
+    return user
 
 
   @classmethod
   @classmethod
   def manages_passwords_externally(cls):
   def manages_passwords_externally(cls):

+ 4 - 1
desktop/core/src/desktop/auth/forms.py

@@ -19,6 +19,8 @@ from django.contrib.auth.forms import AuthenticationForm as AuthAuthenticationFo
 from django.forms import CharField, TextInput, PasswordInput
 from django.forms import CharField, TextInput, PasswordInput
 from django.utils.translation import ugettext_lazy as _t
 from django.utils.translation import ugettext_lazy as _t
 
 
+
+
 class AuthenticationForm(AuthAuthenticationForm):
 class AuthenticationForm(AuthAuthenticationForm):
   """
   """
   Adds appropriate classes to authentication form
   Adds appropriate classes to authentication form
@@ -26,6 +28,7 @@ class AuthenticationForm(AuthAuthenticationForm):
   username = CharField(label=_t("Username"), max_length=30, widget=TextInput(attrs={'class': 'input-large', 'maxlength': 30}))
   username = CharField(label=_t("Username"), max_length=30, widget=TextInput(attrs={'class': 'input-large', 'maxlength': 30}))
   password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large', 'maxlength': 30}))
   password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large', 'maxlength': 30}))
 
 
+
 class UserCreationForm(AuthUserCreationForm):
 class UserCreationForm(AuthUserCreationForm):
   """
   """
   Accepts one password field and populates the others.
   Accepts one password field and populates the others.
@@ -39,4 +42,4 @@ class UserCreationForm(AuthUserCreationForm):
       data = data.copy()
       data = data.copy()
       data['password1'] = data['password']
       data['password1'] = data['password']
       data['password2'] = data['password']
       data['password2'] = data['password']
-    super(UserCreationForm, self).__init__(data=data, *args, **kwargs)
+    super(UserCreationForm, self).__init__(data=data, *args, **kwargs)

+ 8 - 2
desktop/core/src/desktop/auth/views.py

@@ -28,7 +28,7 @@ from django.utils.translation import ugettext as _
 from hadoop.fs.exceptions import WebHdfsException
 from hadoop.fs.exceptions import WebHdfsException
 from useradmin.views import ensure_home_directory
 from useradmin.views import ensure_home_directory
 
 
-from desktop.auth.backend import AllowFirstUserDjangoBackend
+from desktop.auth.backend import AllowFirstUserDjangoBackend, AllowAllBackend
 from desktop.auth.forms import UserCreationForm, AuthenticationForm
 from desktop.auth.forms import UserCreationForm, AuthenticationForm
 from desktop.lib.django_util import render
 from desktop.lib.django_util import render
 from desktop.lib.django_util import login_notrequired
 from desktop.lib.django_util import login_notrequired
@@ -66,11 +66,16 @@ def first_login_ever():
   return False
   return False
 
 
 
 
+def is_allow_all_backend():
+  return get_backends() and isinstance(get_backends()[0], AllowAllBackend)
+
+
 @login_notrequired
 @login_notrequired
 def dt_login(request):
 def dt_login(request):
   """Used by the non-jframe login"""
   """Used by the non-jframe login"""
   redirect_to = request.REQUEST.get('next', '/')
   redirect_to = request.REQUEST.get('next', '/')
   is_first_login_ever = first_login_ever()
   is_first_login_ever = first_login_ever()
+  is_allow_all = is_allow_all_backend()
 
 
   if request.method == 'POST':
   if request.method == 'POST':
     # For first login, need to validate user info!
     # For first login, need to validate user info!
@@ -89,7 +94,7 @@ def dt_login(request):
         if request.session.test_cookie_worked():
         if request.session.test_cookie_worked():
           request.session.delete_test_cookie()
           request.session.delete_test_cookie()
 
 
-        if is_first_login_ever:
+        if is_first_login_ever or is_allow_all:
           # Create home directory for first user.
           # Create home directory for first user.
           try:
           try:
             ensure_home_directory(request.fs, user.username)
             ensure_home_directory(request.fs, user.username)
@@ -114,6 +119,7 @@ def dt_login(request):
     'next': redirect_to,
     'next': redirect_to,
     'first_login_ever': is_first_login_ever,
     'first_login_ever': is_first_login_ever,
     'login_errors': request.method == 'POST',
     'login_errors': request.method == 'POST',
+    'is_allow_all': is_allow_all
   })
   })
 
 
 
 

+ 15 - 1
desktop/core/src/desktop/templates/login.mako

@@ -67,7 +67,11 @@ from django.utils.translation import ugettext as _
                         ${ form['username'] | n,unicode }
                         ${ form['username'] | n,unicode }
                         ${ form['username'].errors | n,unicode }
                         ${ form['username'].errors | n,unicode }
                     </label>
                     </label>
-                    <label>${_('Password')}
+                    <label
+                    % if is_allow_all:
+                      class="hide"
+                    % endif
+                    >${_('Password')}
                         ${ form['password'] | n,unicode }
                         ${ form['password'] | n,unicode }
                         ${ form['password'].errors | n,unicode }
                         ${ form['password'].errors | n,unicode }
                     </label>
                     </label>
@@ -101,5 +105,15 @@ from django.utils.translation import ugettext as _
         </div>
         </div>
         %endif
         %endif
     </div>
     </div>
+
+% if is_allow_all:
+  <script src="/static/ext/js/jquery/jquery-1.8.1.min.js"></script>
+  <script>
+    $(document).ready(function(){
+      $('#id_password').val('password');
+    });
+  </script>
+% endif
+
 </body>
 </body>
 </html>
 </html>