コード例 #1
0
def create_user(username, password, is_superuser=True):
  if ENABLE_ORGANIZATIONS.get():
    organization = get_organization(email=username)
    attrs = {'email': username, 'organization': organization}
  else:
    attrs = {'username': username}

  user = User(**attrs)

  if password is None:
    user.set_unusable_password()
  else:
    user.set_password(password)

  user.is_superuser = is_superuser

  if ENABLE_ORGANIZATIONS.get():
    user.is_admin = is_superuser or not organization.organizationuser_set.exists() or not organization.is_multi_user
    user.save()
    ensure_has_a_group(user)

  user.save()

  LOG.info("User %s was created." % username)

  return user
コード例 #2
0
ファイル: models.py プロジェクト: zhichao8756/hue
def install_sample_user(django_user=None):
    """
  Setup the de-activated sample user with a certain id. Do not create a user profile.
  """
    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() and not ENABLE_ORGANIZATIONS.get():
            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(**lookup).exists():
            user = User.objects.get(**lookup)
            LOG.info('Sample user found: %s' % lookup)
        else:
            user_attributes = lookup.copy()
            if ENABLE_ORGANIZATIONS.get():
                user_attributes['organization'] = get_organization(
                    email=django_username)
            else:
                user_attributes['id'] = SAMPLE_USER_ID

            user_attributes.update({
                'password': '******',
                'is_active': False,
                'is_superuser': False,
            })
            user, created = User.objects.get_or_create(**user_attributes)

            if created:
                LOG.info('Installed a user "%s"' % lookup)

        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 = django_username
                user.save()
    except:
        LOG.exception('Failed to get or create sample user')

    # If sample user doesn't belong to default group, add to default group
    default_group = get_default_user_group(user=user)
    if user is not None and default_group is not None and default_group not in user.groups.all(
    ):
        user.groups.add(default_group)
        user.save()

    # If home directory doesn't exist for sample user, create it
    fs = cluster.get_hdfs()
    try:
        if not fs:
            LOG.info(
                'No fs configured, skipping home directory creation for user: %s'
                % django_username_short)
        elif 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' %
                     django_username)
    except Exception as ex:
        LOG.exception('Failed to create home directory for user %s: %s' %
                      (django_username, str(ex)))

    return user