Bläddra i källkod

HUE-8530 [organization] Port make_logged_in_client util

Romain 6 år sedan
förälder
incheckning
88a7936933

+ 1 - 1
apps/beeswax/src/beeswax/management/commands/beeswax_install_examples.py

@@ -67,7 +67,7 @@ class Command(BaseCommand):
 
     # Documents will belong to this user but we run the install as the current user
     try:
-      sample_user = install_sample_user()
+      sample_user = install_sample_user(user)
       self._install_queries(sample_user, app_name)
       self._install_tables(user, app_name, db_name, tables)
     except Exception as ex:

+ 34 - 24
apps/useradmin/src/useradmin/models.py

@@ -62,7 +62,7 @@ from useradmin.conf import DEFAULT_USER_GROUP
 
 
 if ENABLE_ORGANIZATIONS.get():
-  from useradmin.models2 import OrganizationUser as User, OrganizationGroup as Group, Organization, default_organization
+  from useradmin.models2 import OrganizationUser as User, OrganizationGroup as Group, Organization, default_organization, get_organization
 else:
   from django.contrib.auth.models import User, Group
   class Organization(): pass
@@ -335,41 +335,51 @@ models.signals.post_migrate.connect(update_app_permissions)
 # models.signals.post_migrate.connect(get_default_user_group)
 
 
-def install_sample_user():
+def install_sample_user(django_user=None):
   """
   Setup the de-activated sample user with a certain id. Do not create a user profile.
   """
-  #Moved to avoid circular import with is_admin
-  from desktop.models import SAMPLE_USER_ID, SAMPLE_USER_INSTALL
+  from desktop.models import SAMPLE_USER_ID, get_sample_user_install
   from hadoop import cluster
 
   user = None
+  django_username = get_sample_user_install(django_user)
+
+  if ENABLE_ORGANIZATIONS.get():
+    lookup = {'email': django_username}
+    django_username_short = django_user.username_short
+  else:
+    lookup = {'username': django_username}
+    django_username_short = django_username
 
   try:
     if User.objects.filter(id=SAMPLE_USER_ID).exists():
       user = User.objects.get(id=SAMPLE_USER_ID)
       LOG.info('Sample user found with username "%s" and User ID: %s' % (user.username, user.id))
-    elif User.objects.filter(username=SAMPLE_USER_INSTALL).exists():
-      user = User.objects.get(username=SAMPLE_USER_INSTALL)
-      LOG.info('Sample user found: %s' % user.username)
+    elif User.objects.filter(**lookup).exists():
+      user = User.objects.get(**lookup)
+      LOG.info('Sample user found: %s' % lookup)
     else:
-      user, created = User.objects.get_or_create(
-        username=SAMPLE_USER_INSTALL,
-        password='!',
-        is_active=False,
-        is_superuser=False,
-        id=SAMPLE_USER_ID,
-        pk=SAMPLE_USER_ID
-      )
+      user_attributes = lookup.copy()
+      if ENABLE_ORGANIZATIONS.get():
+        user_attributes['organization'] = get_organization(django_user)
+      user_attributes.update({
+        'password': '!',
+        'is_active': False,
+        'is_superuser': False,
+        'id': SAMPLE_USER_ID,
+        'pk': SAMPLE_USER_ID
+      })
+      user, created = User.objects.get_or_create(**user_attributes)
 
       if created:
-        LOG.info('Installed a user called "%s"' % SAMPLE_USER_INSTALL)
+        LOG.info('Installed a user "%s"' % lookup)
 
-    if user.username != SAMPLE_USER_INSTALL:
-      LOG.warn('Sample user does not have username "%s", will attempt to modify the username.' % SAMPLE_USER_INSTALL)
+    if user.username != django_username and not ENABLE_ORGANIZATIONS.get():
+      LOG.warn('Sample user does not have username "%s", will attempt to modify the username.' % django_username)
       with transaction.atomic():
         user = User.objects.get(id=SAMPLE_USER_ID)
-        user.username = SAMPLE_USER_INSTALL
+        user.username = django_username
         user.save()
   except Exception as ex:
     LOG.exception('Failed to get or create sample user')
@@ -383,13 +393,13 @@ def install_sample_user():
   fs = cluster.get_hdfs()
   # If home directory doesn't exist for sample user, create it
   try:
-    if not fs.do_as_user(SAMPLE_USER_INSTALL, fs.get_home_dir):
-      fs.do_as_user(SAMPLE_USER_INSTALL, fs.create_home_dir)
-      LOG.info('Created home directory for user: %s' % SAMPLE_USER_INSTALL)
+    if not fs.do_as_user(django_username_short, fs.get_home_dir):
+      fs.do_as_user(django_username_short, fs.create_home_dir)
+      LOG.info('Created home directory for user: %s' % django_username_short)
     else:
-      LOG.info('Home directory already exists for user: %s' % SAMPLE_USER_INSTALL)
+      LOG.info('Home directory already exists for user: %s' % django_username)
   except Exception as ex:
-    LOG.exception('Failed to create home directory for user %s: %s' % (SAMPLE_USER_INSTALL, str(ex)))
+    LOG.exception('Failed to create home directory for user %s: %s' % (django_username, str(ex)))
 
   return user
 

+ 9 - 0
apps/useradmin/src/useradmin/models2.py

@@ -120,6 +120,11 @@ def default_organization():
   default_organization, created = Organization.objects.get_or_create(name='default')
   return default_organization
 
+def get_organization(user):
+  # TODO: depends on the logged-in user and its organization
+  return default_organization()
+
+
 class OrganizationUser(AbstractUser):
     """User model in a multi tenant setup."""
 
@@ -150,6 +155,10 @@ class OrganizationUser(AbstractUser):
     def username(self):
       return self.email
 
+    @property
+    def username_short(self):
+      return self.email.split('@')[0]
+
     @username.setter
     def username(self, value):
       pass

+ 5 - 3
desktop/core/src/desktop/lib/django_test_util.py

@@ -14,9 +14,6 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-"""
-Common utilities for testing Desktop django apps.
-"""
 
 import logging
 import re
@@ -38,6 +35,7 @@ class Client(django.test.client.Client):
     response = self.get(*args, **kwargs)
     return json.JSONDecoder().decode(response.content)
 
+
 def assert_ok_response(response):
   """
   Checks that the response returned successfully.
@@ -47,6 +45,7 @@ def assert_ok_response(response):
   nose.tools.assert_true(200, response.status_code)
   return response
 
+
 def make_logged_in_client(username="test", password="test", is_superuser=True, recreate=False, groupname=None):
   """
   Create a client with a user already logged in.
@@ -54,6 +53,9 @@ def make_logged_in_client(username="test", password="test", is_superuser=True, r
   Sometimes we recreate the user, because some tests like to
   mess with is_active and such.
   """
+  if ENABLE_ORGANIZATIONS.get() and username == 'test':
+    username = username + '@gethue.com'
+
   try:
     lookup = {orm_user_lookup(): username}
     user = User.objects.get(**lookup)

+ 6 - 0
desktop/core/src/desktop/models.py

@@ -94,6 +94,12 @@ def hue_version():
 def _version_from_properties(f):
   return dict(line.strip().split('=') for line in f.readlines() if len(line.strip().split('=')) == 2).get('cloudera.cdh.release')
 
+def get_sample_user_install(user):
+  if ENABLE_ORGANIZATIONS.get():
+   return SAMPLE_USER_INSTALL + '@' + get_organization(user).name + '.com' # TODO: proper default domain
+  else:
+    return SAMPLE_USER_INSTALL
+
 
 ###################################################################################################
 # Custom Settings