示例#1
0
def backends_data(user):
    """Return backends data for given user.

    Will return a dict with values:
        associated: UserSocialAuth model instances for currently
                    associated accounts
        not_associated: Not associated (yet) backend names.
        backends: All backend names.

    If user is not authenticated, then first list is empty, and there's no
    difference between the second and third lists.
    """
    available = get_backends().keys()
    values = {
        'associated': [],
        'not_associated': available,
        'backends': available
    }

    # user comes from request.user usually, on /admin/ it will be an instance
    # of auth.User and this code will fail if a custom User model was defined
    if hasattr(user, 'is_authenticated') and user.is_authenticated():
        associated = UserSocialAuth.get_social_auth_for_user(user)
        not_associated = list(
            set(available) - set(assoc.provider for assoc in associated))
        values['associated'] = associated
        values['not_associated'] = not_associated
    return values
示例#2
0
def backends_data(user):
    """Return backends data for given user.

    Will return a dict with values:
        associated: UserSocialAuth model instances for currently
                    associated accounts
        not_associated: Not associated (yet) backend names.
        backends: All backend names.

    If user is not authenticated, then first list is empty, and there's no
    difference between the second and third lists.
    """
    available = get_backends().keys()
    values = {'associated': [],
              'not_associated': available,
              'backends': available}

    # user comes from request.user usually, on /admin/ it will be an instance
    # of auth.User and this code will fail if a custom User model was defined
    if hasattr(user, 'is_authenticated') and user.is_authenticated():
        associated = UserSocialAuth.get_social_auth_for_user(user)
        not_associated = list(set(available) -
                              set(assoc.provider for assoc in associated))
        values['associated'] = associated
        values['not_associated'] = not_associated
    return values
示例#3
0
 def context_value():
     keys = get_backends().keys()
     accounts = dict(zip(keys, [None] * len(keys)))
     user = request.user
     if hasattr(user, 'is_authenticated') and user.is_authenticated():
         accounts.update((assoc.provider.replace('-', '_'), assoc)
                 for assoc in UserSocialAuth.get_social_auth_for_user(user))
     return accounts
示例#4
0
 def tokens(self):
     """Return access_token stored in extra_data or None"""
     # Make import here to avoid recursive imports :-/
     from core.social_auth.backends import get_backends
     backend = get_backends().get(self.provider)
     if backend:
         return backend.AUTH_BACKEND.tokens(self)
     else:
         return {}
示例#5
0
文件: base.py 项目: amedhat3/sharek
 def tokens(self):
     """Return access_token stored in extra_data or None"""
     # Make import here to avoid recursive imports :-/
     from core.social_auth.backends import get_backends
     backend = get_backends().get(self.provider)
     if backend:
         return backend.AUTH_BACKEND.tokens(self)
     else:
         return {}
示例#6
0
 def context_value():
     keys = get_backends().keys()
     accounts = dict(zip(keys, [None] * len(keys)))
     user = request.user
     if hasattr(user, 'is_authenticated') and user.is_authenticated():
         accounts.update(
             (assoc.provider.replace('-', '_'), assoc)
             for assoc in UserSocialAuth.get_social_auth_for_user(user))
     return accounts
示例#7
0
文件: utils.py 项目: espace/sharek
def group_backend_by_type(items, key=lambda x: x):
    """Group items by backend type."""

    # Beware of cyclical imports!
    from core.social_auth.backends import get_backends, OpenIdAuth, BaseOAuth, BaseOAuth2

    result = defaultdict(list)
    backends = get_backends()

    for item in items:
        backend = backends[key(item)]
        if issubclass(backend, OpenIdAuth):
            result["openid"].append(item)
        elif issubclass(backend, BaseOAuth2):
            result["oauth2"].append(item)
        elif issubclass(backend, BaseOAuth):
            result["oauth"].append(item)
    return dict(result)