source: WAeUP_SRP/trunk/skins/cps_custom/logged_in.py @ 5750

Last change on this file since 5750 was 5656, checked in by Henrik Bettermann, 14 years ago

Log failed logins.

File size: 12.6 KB
Line 
1## Script (Python) "logged_in"
2##bind container=container
3##bind context=context
4##bind namespace=
5##bind script=script
6##bind subpath=traverse_subpath
7##parameters=
8##title=
9##
10"""Prepare user login
11modified from cps_default/logged_in.py
12
13$Id: logged_in.py 1933 2007-06-19 05:05:29Z henrik $
14"""
15try:
16    from Products.zdb import set_trace
17except:
18    def set_trace():
19        pass
20import DateTime
21import os
22current = DateTime.DateTime()
23import logging
24logger = logging.getLogger('Skins.logged_in')
25
26
27from urllib import unquote
28
29request = context.REQUEST
30response = request.RESPONSE
31utool = context.portal_url
32mtool = context.portal_membership
33wftool = context.portal_workflow
34portal = utool.getPortalObject()
35
36redirect_to_portal = False
37is_anon = mtool.isAnonymousUser()
38member = mtool.getAuthenticatedMember()
39member_id = str(member)
40
41cred = context.waeup_tool.getCredentialFromAuthHeader(request)
42
43if context.isStaff():
44    is_unsecure = context.waeup_tool.checkGenericPassword(member_id)
45    if is_unsecure:
46        response.expireCookie('__ac', path='/')
47        return response.redirect("%s/user_logged_in_disabled" % context.portal_url())
48
49#load_passport = hasattr(context.waeup_tool,'loadStudentFoto')
50
51if not is_anon:
52    if "Student" in member.getRoles():
53        students = context.portal_url.getPortalObject().campus.students
54        student = getattr(students,member_id)
55        student_app = getattr(student,'application',None)
56        student_per = getattr(student,'personal',None)
57     
58
59        #########################################################
60
61        # perform makeStudentData for returning students who login for the first time
62        # the returning key comes from set_access_data
63
64        if request.has_key('returning') and  student_per is None:
65            email=request.get("email")
66            phone=request.get("phone_nr")
67            context.waeup_tool.makeStudentData(member_id,
68                                                   email=email,
69                                                   phone_nr=phone,
70                                                   )
71            #d = {}
72            #d['id'] = member_id
73            #d['email'] = email
74            #d['phone'] = phone
75            #context.students_catalog.modifyRecord(**d)
76        elif student_per is None:
77            context.waeup_tool.makeStudentData(member_id)
78
79        #########################################################
80
81        # add missing payments folder
82
83        if 'payments' not in student.objectIds():
84            student.invokeFactory('PaymentsFolder','payments')
85            payments = getattr(student,'payments')
86            wftool.doActionFor(payments,'open')
87            d = {}
88            d['Title'] = 'Online Payments'
89            payments.getContent().edit(mapping=d)
90
91        #########################################################
92
93        #student_pume = getattr(student,'pume',None)
94        s_review_state = context.getStudentReviewState(student.id)
95        a_review_state = wftool.getInfoFor(student_app,'review_state',None)
96        student_per = getattr(student,'personal',None)
97        p_review_state = wftool.getInfoFor(student_per,'review_state',None)
98        logger.info('%s logged in, review_state %s' % (member,s_review_state))
99        if student_app is None:
100            logger.info('%s logged in, without application object' % (member))
101        else:
102            app_doc = student_app.getContent()
103       
104        res = context.students_catalog(id = member_id)
105        matric_no = res[0].matric_no
106        jamb_reg_no = res[0].jamb_reg_no
107        entry_mode = res[0].entry_mode
108        verdict = res[0].verdict
109        level = res[0].level
110        has_level = level
111        has_verdict = verdict and not verdict == 'N/A'     
112
113        #########################################################
114
115        # save email and phone of returning students after all objects have been created
116
117        if request.has_key('returning') and student_app:
118            email=request.get("email")
119            phone=request.get("phone_nr")
120            # should be always closed, however ...
121            #if a_review_state != 'opened':
122            #    wftool.doActionFor(student_app,'open')
123            # should be always opened after import, however ...
124            if p_review_state != 'opened':
125                wftool.doActionFor(student_per,'open')
126            per_doc = student_per.getContent()
127            #app_doc.edit(mapping = {'app_email' : email})
128            per_doc.edit(mapping = {'email' : email, 'phone' : phone})
129            #wftool.doActionFor(student_app,'close')
130
131        #########################################################
132
133        # look for passport pictures of returning students
134
135        if s_review_state == 'returning' and\
136           not context.waeup_tool.picturesExist(('passport',), member_id):
137            folder = 'pictures_returning'
138            #res = context.students_catalog(id = member_id)
139            filename = res[0].matric_no.upper()
140            msg = context.waeup_tool.loadStudentFoto(student,filename,folder)
141            logger.info('%s (%s), %s' % (member,s_review_state,msg))
142
143        #########################################################
144       
145        # look for passport pictures of transfer students
146
147        if entry_mode == 'transfer' and\
148           s_review_state in ('returning','school_fee_paid',) and\
149           not context.waeup_tool.picturesExist(('passport',), member_id):
150            res_both = context.students_catalog(jamb_reg_no = jamb_reg_no)
151            #set_trace()
152            if len(res_both) == 2:
153                for ts in res_both:
154                    if ts.id != member_id:
155                        msg = context.waeup_tool.loadTransferStudentFoto(ts.id,member_id)
156                        logger.info('%s (%s on transfer), %s' % (member,s_review_state,msg))
157
158        #########################################################       
159
160        # look for passport pictures of new students
161
162        if s_review_state in ('admitted',
163                              'student_created',
164                              'clearance_pin_entered',
165                              'clearance_requested'
166                             )  and not context.waeup_tool.picturesExist(('passport',),
167                                                                         member_id):
168            folder = 'pictures_admitted_latest'
169            filename = app_doc.jamb_reg_no.replace('/','_')
170            msg = context.waeup_tool.loadStudentFoto(student,filename,folder)
171            logger.info('%s (%s), %s' % (member,s_review_state,msg))
172            if 'passport picture not found' in msg:
173                filename = app_doc.app_reg_pin.replace('-','')
174                msg = context.waeup_tool.loadStudentFoto(student,filename,folder)
175                logger.info('%s (%s), %s' % (member,s_review_state,msg))
176
177        #########################################################
178
179        # perform necessary updates for new students
180
181        # 1. add pin and application date to app_doc
182
183        if s_review_state == "student_created":
184            wftool.doActionFor(student,'admit')
185            s_review_state = 'admitted'
186
187        if s_review_state == "admitted" and a_review_state == 'created':
188            wftool.doActionFor(student_app,'open')
189            #if student_pume is not None:
190            #  wftool.doActionFor(student_pume,'close')
191            da = {}
192            pin = request.get('pin')
193
194            # if the student comes directly, add missing pin or pin with wrong syntax (fix)
195            if not pin:
196                jamb_reg_no = app_doc.jamb_reg_no
197                for reg_no in (jamb_reg_no,jamb_reg_no.lower(),jamb_reg_no.upper()):
198                    res = context.portal_pins(student=reg_no)
199                    if len(res) > 0:
200                        break
201                if len(res) > 0:
202                    p = res[0].pin
203                    if len(p) > 10:
204                        if p.startswith('IPTP'):
205                            pin = "%s-%s-%s" % (p[:4],p[4:5],p[5:])
206                        else:
207                            pin = "%s-%s-%s" % (p[:3],p[3:-10],p[-10:])
208                    else:
209                        pin = p
210            da['app_ac_pin'] = pin
211            da['app_ac_date'] = current
212            app_doc.edit(mapping = da)
213
214        # 2. same as 1 but without opening app_doc, assuming that the student already logged in
215        #    but the pin is missing in app_doc (fix)
216
217        elif s_review_state in ("admitted") and a_review_state == 'opened' and\
218                                            (not app_doc.app_ac_pin or app_doc.app_ac_pin.startswith('IPT-P')):
219            jamb_reg_no = app_doc.jamb_reg_no
220            for reg_no in (jamb_reg_no,jamb_reg_no.lower(),jamb_reg_no.upper()):
221                res = context.portal_pins(student=reg_no)
222                if len(res) > 0:
223                    break
224            if len(res) > 0:
225                p = res[0].pin
226                if len(p) > 10:
227                    if p.startswith('IPTP'):
228                        pin = "%s-%s-%s" % (p[:4],p[4:5],p[5:])
229                    else:
230                        pin = "%s-%s-%s" % (p[:3],p[3:-10],p[-10:])                 
231                else:
232                    pin = p
233                da = {}
234                da['app_ac_pin'] = pin
235                app_doc.edit(mapping = da)
236
237        # 3. open personal object (fix). This should be done in clearance_edit.
238
239        elif s_review_state in ("cleared_and_validated",) and\
240                             p_review_state == 'created' :
241            wftool.doActionFor(student_per,'open')
242
243
244        #########################################################
245
246        #if matric_no:
247        #    has_results = context.results_import(matric_no = matric_no)
248        #else:
249        #    has_results = ''
250
251        #########################################################
252
253        # fetch current verdict via getVerdict and level via getLevelFromResultsCosCode
254
255        #if s_review_state in ('returning','school_fee_paid') and not (has_verdict and has_level):
256        #    if has_results:
257        #        study_course = getattr(student,'study_course')
258        #        sc_review_state = wftool.getInfoFor(study_course,'review_state',None)
259        #        dsc = {}
260        #        if not has_verdict and s_review_state == 'returning':
261        #            dsc['current_verdict'] = context.getVerdict(has_results[0].Verdict)[0]
262        #        if not has_level and s_review_state == 'returning':
263        #            lnr = context.getLevelFromResultsCosCode(has_results)
264        #            dsc['current_level'] = "%d00" % lnr
265        #        elif not has_level and s_review_state == 'school_fee_paid':
266        #            lnr = context.getLevelFromResultsCosCode(has_results)
267        #            cv = context.getVerdict(has_results[0].Verdict)[0]
268        #            if cv in ('A','B',):
269        #                lnr += 1
270        #            dsc['current_level'] = "%d00" % lnr
271        #            dsc['previous_verdict'] = cv
272        #        if sc_review_state != 'opened':
273        #            wftool.doActionFor(study_course,'open')
274        #        study_course_doc =study_course.getContent()
275        #        study_course_doc.edit(mapping = dsc)
276        #        wftool.doActionFor(study_course,'close_for_edit')
277
278        #########################################################
279
280
281        # determine appropriate redirect url
282
283        if s_review_state == "application_pin_entered":
284            redirect_url = "%s/application_edit_form" % student.absolute_url()
285        elif s_review_state in ('admitted', 'objection_raised',):
286            redirect_url = "%s/admission_form" % student.absolute_url()
287        elif s_review_state == "clearance_pin_entered":
288            redirect_url = "%s/clearance_edit_form" % student.absolute_url()
289        elif s_review_state == "cleared_and_validated":
290            redirect_url = "%s/personal_edit_form" % student.absolute_url()
291        #elif s_review_state == "returning" and has_results:
292        #    redirect_url = "%s/session_results_view" % student.absolute_url()
293        elif s_review_state in ('school_fee_paid','courses_registered'):
294            redirect_url = "%s/study_course/study_course_view" % student.absolute_url()
295        else:
296            redirect_url = "%s/student_index" % student.absolute_url()
297
298
299    else:
300        logger.info('%s logged in' % (member))
301        redirect_url = portal.absolute_url()
302#Anonymous
303else:
304    logger.info('failed login with %s' % cred)
305    response.expireCookie('__ac', path='/')
306    return context.user_logged_in_failed()
307
308# Setup skins
309if (getattr(utool, 'updateSkinCookie', False) and
310    utool.updateSkinCookie()):
311    context.setupCurrentSkin()
312
313response.redirect(redirect_url)
314
Note: See TracBrowser for help on using the repository browser.