Back to index

plone3  3.1.7
login.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getMultiAdapter
00003 
00004 from plone.portlets.interfaces import IPortletDataProvider
00005 from plone.app.portlets.portlets import base
00006 
00007 from zope import schema
00008 from zope.formlib import form
00009 
00010 from plone.memoize.instance import memoize
00011 
00012 from Acquisition import aq_inner
00013 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00014 from Products.CMFCore.utils import getToolByName
00015 
00016 from Products.CMFPlone import PloneMessageFactory as _
00017 
00018 class ILoginPortlet(IPortletDataProvider):
00019     """A portlet which can render a login form.
00020     """
00021 
00022 class Assignment(base.Assignment):
00023     implements(ILoginPortlet)
00024 
00025     title = _(u'label_log_in', default=u'Log in')
00026 
00027 class Renderer(base.Renderer):
00028 
00029     def __init__(self, context, request, view, manager, data):
00030         base.Renderer.__init__(self, context, request, view, manager, data)
00031         
00032         self.membership = getToolByName(self.context, 'portal_membership')
00033         
00034         self.context_state = getMultiAdapter((context, request), name=u'plone_context_state')
00035         self.portal_state = getMultiAdapter((context, request), name=u'plone_portal_state')
00036         self.pas_info = getMultiAdapter((context, request), name=u'pas_info')
00037 
00038     def show(self):
00039         if not self.portal_state.anonymous():
00040             return False
00041         if not self.pas_info.hasLoginPasswordExtractor():
00042             return False
00043         page = self.request.get('URL', '').split('/')[-1]
00044         return page not in ('login_form', 'join_form')
00045 
00046     @property
00047     def available(self):
00048         return self.auth() is not None and self.show()
00049 
00050     def login_form(self):
00051         return '%s/login_form' % self.portal_state.portal_url()
00052 
00053     def mail_password_form(self):
00054         return '%s/mail_password_form' % self.portal_state.portal_url()
00055 
00056     def login_name(self):
00057         auth = self.auth()
00058         name = None
00059         if auth is not None:
00060             name = getattr(auth, 'name_cookie', None)
00061         if not name:
00062             name = '__ac_name'
00063         return name
00064 
00065     def login_password(self):
00066         auth = self.auth()
00067         passwd = None
00068         if auth is not None:
00069             passwd = getattr(auth, 'pw_cookie', None)
00070         if not passwd:
00071             passwd = '__ac_password'
00072         return passwd
00073 
00074     def join_action(self):
00075         userActions = self.context_state.actions()['user']
00076         joinAction = [a['url'] for a in userActions if a['id'] == 'join']
00077         if len(joinAction) > 0:
00078             return joinAction.pop()
00079         else:
00080             return None
00081 
00082     def can_register(self):
00083         if getToolByName(self.context, 'portal_registration', None) is None:
00084             return False
00085         return self.membership.checkPermission('Add portal member', self.context)
00086 
00087     def can_request_password(self):
00088         return self.membership.checkPermission('Mail forgotten password', self.context)
00089 
00090     @memoize
00091     def auth(self, _marker=[]):
00092         acl_users = getToolByName(self.context, 'acl_users')
00093         return getattr(acl_users, 'credentials_cookie_auth', None)
00094 
00095     def update(self):
00096         pass
00097 
00098     render = ViewPageTemplateFile('login.pt')
00099 
00100 class AddForm(base.NullAddForm):
00101 
00102     def create(self):
00103         return Assignment()