Ejemplo n.º 1
0
def _get_profile_user(username):
    '''Find the :class:`~django.contrib.auth.models.User` and
    :class:`~openemory.accounts.models.UserProfile` for a specified
    username, for use in profile pages.  The specified ``username``
    must exist as an :class:`~openemory.accounts.models.EsdPerson`; if
    the corresponding :class:`~openemory.accounts.models.UserProfile`
    does not yet exist, it will be initialized from LDAP.

    Raises a :class:`django.http.Http404` if any of the models cannot
    be found or created.

    Helper method for profile views (:meth:`rdf_profile` and
    :meth:`profile`).
    '''
    # FIXME: It made sense in the past to require an EsdPerson for every
    # User/UserProfile. As of 2012-03-06, this shouldn't be so important.
    # At some point we should make this work if the User and UserProfile
    # exist (assuming profile.has_profile_page()) even if the EsdPerson
    # doesn't.

    # retrieve the ESD db record for the requested user
    esdperson = get_object_or_404(EsdPerson, netid=username.upper())
    # get the corresponding local profile & user
    try:
        profile = esdperson.profile()
        user = profile.user
        # 404 if the user exists but should not have a profile page
        # (check against UserProfile if there is one, for local profile override)
        if not profile.has_profile_page():
            raise Http404
    except UserProfile.DoesNotExist:
        # if local account doesn't exist, make sure ESD indicates the
        # user should have a profile before proceeding
        if not esdperson.has_profile_page():
            raise Http404

        # local account doesn't exist but user should have a profile:
        # attempt to init local user & profile

        backend = EmoryLDAPBackend()
        user_dn, user = backend.find_user(username)
        if not user:
            raise Http404
        profile = user.userprofile

    return user, profile