| 1 |
##parameters=came_from=None |
|---|
| 2 |
"""Prepare user login |
|---|
| 3 |
|
|---|
| 4 |
$Id$ |
|---|
| 5 |
""" |
|---|
| 6 |
|
|---|
| 7 |
from urllib import unquote |
|---|
| 8 |
|
|---|
| 9 |
def checkRedirect(portal, mtool): |
|---|
| 10 |
to_member_home = False |
|---|
| 11 |
to_workspaces = False |
|---|
| 12 |
has_home = mtool.getHomeFolder() |
|---|
| 13 |
if has_home: |
|---|
| 14 |
to_member_home = True |
|---|
| 15 |
if not has_home and mtool.checkPermission('View', portal.workspaces): |
|---|
| 16 |
to_workspaces = True |
|---|
| 17 |
return to_member_home, to_workspaces |
|---|
| 18 |
|
|---|
| 19 |
utool = context.portal_url |
|---|
| 20 |
mtool = context.portal_membership |
|---|
| 21 |
portal = utool.getPortalObject() |
|---|
| 22 |
portal_absolute_url = portal.absolute_url() |
|---|
| 23 |
|
|---|
| 24 |
redirect_url = came_from |
|---|
| 25 |
redirect_to_portal = False |
|---|
| 26 |
to_member_home = False |
|---|
| 27 |
to_workspaces = False |
|---|
| 28 |
|
|---|
| 29 |
is_anon = mtool.isAnonymousUser() |
|---|
| 30 |
member = mtool.getAuthenticatedMember() |
|---|
| 31 |
|
|---|
| 32 |
if not redirect_url or redirect_url.endswith('/logged_out'): |
|---|
| 33 |
if not is_anon: |
|---|
| 34 |
to_member_home, to_workspaces = checkRedirect(portal, mtool) |
|---|
| 35 |
if (not to_member_home) and (not to_workspaces): |
|---|
| 36 |
redirect_to_portal = True |
|---|
| 37 |
else: |
|---|
| 38 |
redirect_url = unquote(redirect_url) |
|---|
| 39 |
# One can be redirected from an http page while the login is done from an |
|---|
| 40 |
# https page. This is a fix for #1205. |
|---|
| 41 |
# A better option here would be to replace the previous portal_absolute_url |
|---|
| 42 |
# prefix in the redirect_url by the current portal absolute URL. |
|---|
| 43 |
if not redirect_url.startswith(portal_absolute_url): |
|---|
| 44 |
if not is_anon: |
|---|
| 45 |
to_member_home, to_workspaces = checkRedirect(portal, mtool) |
|---|
| 46 |
if (not to_member_home) and (not to_workspaces): |
|---|
| 47 |
redirect_to_portal = True |
|---|
| 48 |
|
|---|
| 49 |
if to_member_home: |
|---|
| 50 |
redirect_url = mtool.getHomeFolder().absolute_url() |
|---|
| 51 |
elif to_workspaces: |
|---|
| 52 |
redirect_url = portal.workspaces.absolute_url() |
|---|
| 53 |
elif redirect_to_portal: |
|---|
| 54 |
redirect_url = portal_absolute_url |
|---|
| 55 |
|
|---|
| 56 |
REQUEST = context.REQUEST |
|---|
| 57 |
RESPONSE = REQUEST.RESPONSE |
|---|
| 58 |
|
|---|
| 59 |
# Setup skins |
|---|
| 60 |
if (getattr(utool, 'updateSkinCookie', False) and |
|---|
| 61 |
utool.updateSkinCookie()): |
|---|
| 62 |
context.setupCurrentSkin() |
|---|
| 63 |
|
|---|
| 64 |
# Anonymous |
|---|
| 65 |
if is_anon: |
|---|
| 66 |
RESPONSE.expireCookie('__ac', path='/') |
|---|
| 67 |
return context.user_logged_in_failed() |
|---|
| 68 |
|
|---|
| 69 |
login_time = member.getProperty('last_login_time', None) |
|---|
| 70 |
|
|---|
| 71 |
# We don't want to create a member area for the Zope admin, |
|---|
| 72 |
# nor can we setProperties on it. |
|---|
| 73 |
if member.has_role('Member'): |
|---|
| 74 |
# This create the member area only if needed |
|---|
| 75 |
mtool.createMemberArea() |
|---|
| 76 |
now = context.ZopeTime() |
|---|
| 77 |
member.setProperties(login_time=now, last_login_time=login_time) |
|---|
| 78 |
|
|---|
| 79 |
if to_member_home or to_workspaces: |
|---|
| 80 |
redirect_url = '%s/?%s' % (redirect_url, 'portal_status_message=psm_logged_in') |
|---|
| 81 |
RESPONSE.redirect(redirect_url) |
|---|