Browse Source

Adding models to support LDAP integration

This adds an LdapGroup model, which will be used to denote groups that have
been imported from an LDAP system, as well as adds a creation_method field to
the UserProfile model, which will identify a user as a Hue-created user or a
user imported from an external system. Additionally, the authentication
backends which import users from external systems have been augmented to set
the field.
Jon Natkins 13 years ago
parent
commit
76db34d556

+ 2 - 10
apps/useradmin/src/useradmin/migrations/0001_permissions_and_profiles.py

@@ -1,12 +1,10 @@
 # encoding: utf-8
 # encoding: utf-8
 import datetime
 import datetime
 from south.db import db
 from south.db import db
-from south.v2 import DataMigration
+from south.v2 import SchemaMigration
 from django.db import models
 from django.db import models
-from django.contrib.auth.models import User
-from useradmin.models import create_profile_for_user
 
 
-class Migration(DataMigration):
+class Migration(SchemaMigration):
     
     
     def forwards(self, orm):
     def forwards(self, orm):
         
         
@@ -35,12 +33,6 @@ class Migration(DataMigration):
         ))
         ))
         db.send_create_signal('useradmin', ['HuePermission'])
         db.send_create_signal('useradmin', ['HuePermission'])
     
     
-        for user in User.objects.all():
-          try:
-            orm.UserProfile.objects.get(user=user)
-          except orm.UserProfile.DoesNotExist:
-            create_profile_for_user(user)
-    
     def backwards(self, orm):
     def backwards(self, orm):
         
         
         # Deleting model 'UserProfile'
         # Deleting model 'UserProfile'

+ 105 - 0
apps/useradmin/src/useradmin/migrations/0002_add_ldap_support.py

@@ -0,0 +1,105 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import DataMigration
+from django.db import models
+
+from django.contrib.auth.models import User
+from useradmin.models import create_profile_for_user
+
+class Migration(DataMigration):
+    
+    def forwards(self, orm):
+        
+        # Adding model 'LdapGroup'
+        db.create_table('useradmin_ldapgroup', (
+            ('group', self.gf('django.db.models.fields.related.ForeignKey')(related_name='group', to=orm['auth.Group'])),
+            ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+        ))
+        db.send_create_signal('useradmin', ['LdapGroup'])
+
+        # Adding field 'UserProfile.creation_method'
+        db.add_column('useradmin_userprofile', 'creation_method', self.gf('django.db.models.fields.IntegerField')(default=0), keep_default=False)
+    
+        for user in User.objects.all():
+          try:
+            orm.UserProfile.objects.get(user=user)
+          except orm.UserProfile.DoesNotExist:
+            create_profile_for_user(user)
+
+    
+    def backwards(self, orm):
+        
+        # Deleting model 'LdapGroup'
+        db.delete_table('useradmin_ldapgroup')
+
+        # Deleting field 'UserProfile.creation_method'
+        db.delete_column('useradmin_userprofile', 'creation_method')
+    
+    
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True', 'blank': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False', 'blank': 'True'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'useradmin.grouppermission': {
+            'Meta': {'object_name': 'GroupPermission'},
+            'group': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.Group']"}),
+            'hue_permission': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['useradmin.HuePermission']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'useradmin.huepermission': {
+            'Meta': {'object_name': 'HuePermission'},
+            'action': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'app': ('django.db.models.fields.CharField', [], {'max_length': '30'}),
+            'description': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'through': "orm['useradmin.GroupPermission']", 'symmetrical': 'False'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'useradmin.ldapgroup': {
+            'Meta': {'object_name': 'LdapGroup'},
+            'group': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'group'", 'to': "orm['auth.Group']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'useradmin.userprofile': {
+            'Meta': {'object_name': 'UserProfile'},
+            'creation_method': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'home_directory': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'unique': 'True'})
+        }
+    }
+    
+    complete_apps = ['useradmin']

+ 11 - 0
apps/useradmin/src/useradmin/models.py

@@ -55,6 +55,7 @@ from django.db import models
 from django.contrib.auth import models as auth_models
 from django.contrib.auth import models as auth_models
 from desktop import appmanager
 from desktop import appmanager
 from desktop.lib.django_util import PopupException
 from desktop.lib.django_util import PopupException
+from enum import Enum
 
 
 LOG = logging.getLogger(__name__)
 LOG = logging.getLogger(__name__)
 
 
@@ -77,9 +78,12 @@ class UserProfile(models.Model):
   OUT OF THE CURRENT MIGRATION, AND INTO THE NEW ONE, OR UPGRADES WILL NOT WORK
   OUT OF THE CURRENT MIGRATION, AND INTO THE NEW ONE, OR UPGRADES WILL NOT WORK
   PROPERLY
   PROPERLY
   """
   """
+  # Enum for describing the creation method of a user.
+  CreationMethod = Enum('HUE', 'EXTERNAL')
 
 
   user = models.ForeignKey(auth_models.User, unique=True)
   user = models.ForeignKey(auth_models.User, unique=True)
   home_directory = models.CharField(editable=True, max_length=1024, null=True)
   home_directory = models.CharField(editable=True, max_length=1024, null=True)
+  creation_method = models.CharField(editable=True, null=False, max_length=64, default=CreationMethod.HUE)
 
 
   def get_groups(self):
   def get_groups(self):
     return self.user.groups.all()
     return self.user.groups.all()
@@ -149,6 +153,13 @@ def create_user_signal_handler(sender, **kwargs):
 # Create a user profile every time a user gets created.
 # Create a user profile every time a user gets created.
 models.signals.post_save.connect(create_user_signal_handler, sender=auth_models.User)
 models.signals.post_save.connect(create_user_signal_handler, sender=auth_models.User)
 
 
+class LdapGroup(models.Model):
+  """
+  Groups that come from LDAP originally will have an LdapGroup 
+  record generated at creation time.
+  """
+  group = models.ForeignKey(auth_models.Group, related_name="group")
+
 class GroupPermission(models.Model):
 class GroupPermission(models.Model):
   """
   """
   Represents the permissions a group has.
   Represents the permissions a group has.

+ 3 - 1
desktop/core/src/desktop/auth/backend.py

@@ -35,7 +35,7 @@ import logging
 import desktop.conf
 import desktop.conf
 from django.utils.importlib import import_module
 from django.utils.importlib import import_module
 from django.core.exceptions import ImproperlyConfigured
 from django.core.exceptions import ImproperlyConfigured
-from useradmin.models import get_profile
+from useradmin.models import get_profile, UserProfile
 
 
 import pam
 import pam
 from django_auth_ldap.backend import LDAPBackend, ldap_settings
 from django_auth_ldap.backend import LDAPBackend, ldap_settings
@@ -203,6 +203,7 @@ class PamBackend(DesktopBackendBase):
         user = find_or_create_user(username, None)
         user = find_or_create_user(username, None)
         if user is not None and user.is_active:
         if user is not None and user.is_active:
           profile = get_profile(user)
           profile = get_profile(user)
+          profile.creation_method = UserProfile.EXTERNAL
           profile.save()
           profile.save()
           user.is_superuser = is_super
           user.is_superuser = is_super
           user.save()
           user.save()
@@ -263,6 +264,7 @@ class LdapBackend(object):
 
 
     if user is not None and user.is_active:
     if user is not None and user.is_active:
       profile = get_profile(user)
       profile = get_profile(user)
+      profile.creation_method = UserProfile.EXTERNAL
       profile.save()
       profile.save()
       user.is_superuser = is_super
       user.is_superuser = is_super
       user = rewrite_user(user)
       user = rewrite_user(user)