main.py~ 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import functools
  2. import logging
  3. from datetime import datetime, timedelta
  4. import time
  5. import simplejson
  6. import urlparse
  7. import werkzeug.utils
  8. from werkzeug.exceptions import BadRequest
  9. import openerp
  10. from openerp import SUPERUSER_ID
  11. from openerp import http
  12. from openerp.http import request
  13. from openerp.addons.web.controllers.main import db_monodb, ensure_db, set_cookie_and_redirect, login_and_redirect
  14. from openerp.addons.auth_signup.controllers.main import AuthSignupHome as Home
  15. from openerp.modules.registry import RegistryManager
  16. from openerp.tools.translate import _
  17. import openerp.pooler as pooler
  18. _logger = logging.getLogger(__name__)
  19. #----------------------------------------------------------
  20. # Controller
  21. #----------------------------------------------------------
  22. class LockoutSign(openerp.addons.web.controllers.main.Home):
  23. @http.route()
  24. def web_login(self, *args, **kw):
  25. ensure_db()
  26. dbname = request.session.db
  27. registry = RegistryManager.get(dbname)
  28. #cr = registry.cursor()
  29. cr = request.cr
  30. response = super(LockoutSign, self).web_login(*args, **kw)
  31. if response.is_qweb and response.qcontext.has_key('error'):
  32. error = response.qcontext['error']
  33. if error:
  34. if request.httprequest.method == 'POST':
  35. old_uid = request.uid
  36. company_ids = pooler.get_pool(request.session.db).get('res.company').search(cr, SUPERUSER_ID, [])
  37. company = pooler.get_pool(request.session.db).get('res.company').browse(cr, SUPERUSER_ID, company_ids[0])
  38. attempt_cnt = company.attempt_cnt
  39. unlock_after = company.lockouttime_id.value
  40. unlock_after_name = company.lockouttime_id.name
  41. uid = request.session.authenticate(request.session.db, request.params['login'], request.params['password'])
  42. if uid is False:
  43. uloginids = pooler.get_pool(request.session.db).get('res.users').search(cr, SUPERUSER_ID, [('login','=',request.params['login'])])
  44. for lid in pooler.get_pool(request.session.db).get('res.users').browse(cr, SUPERUSER_ID, uloginids):
  45. if lid.flg_userlocked:
  46. if unlock_after==0:
  47. error = 'Your Login is temporarily Locked. Please Contact Administrator to Unlock it.'
  48. else:
  49. error = 'Your Login is temporarily Locked. Please try after '+unlock_after_name
  50. else:
  51. wronglogin_cnt = lid.wronglogin_cnt and lid.wronglogin_cnt+1 or 1
  52. pooler.get_pool(request.session.db).get('res.users').write(cr, SUPERUSER_ID,[lid.id],{'wronglogin_cnt': wronglogin_cnt})
  53. if int(lid.wronglogin_cnt)>int(attempt_cnt):
  54. pooler.get_pool(request.session.db).get('res.users').write(cr, SUPERUSER_ID,[lid.id],{'flg_userlocked': True,'userlocked_datetime':time.strftime('%Y-%m-%d %H:%M:%S')})
  55. if unlock_after==0:
  56. error = 'Your Login is temporarily Locked. Please Contact Administrator to Unlock it.'
  57. else:
  58. error = 'Your Login is temporarily Locked. Please try after '+unlock_after_name
  59. response.qcontext['error'] = error
  60. return response
  61. # vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: