You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.4 KiB

  1. # -*- encoding: utf-8 -*-
  2. ################################################################################
  3. # See __openerp__.py file for Copyright and Licence Informations.
  4. ################################################################################
  5. import datetime
  6. from ast import literal_eval
  7. from openerp import SUPERUSER_ID
  8. from openerp import pooler
  9. from openerp import exceptions
  10. from openerp.osv.orm import Model
  11. class res_users(Model):
  12. _inherit = "res.users"
  13. ### Private Function section
  14. def _send_email_passkey(self, cr, user_id, user_agent_env):
  15. """ Send a email to the admin of the system and / or the user
  16. to inform passkey use """
  17. mail_obj = self.pool.get('mail.mail')
  18. icp_obj = self.pool.get('ir.config_parameter')
  19. admin_user = self.browse(cr, SUPERUSER_ID, SUPERUSER_ID)
  20. login_user = self.browse(cr, SUPERUSER_ID, user_id)
  21. send_to_admin = literal_eval(icp_obj.get_param(cr, SUPERUSER_ID,
  22. 'auth_admin_passkey.send_to_admin', 'True'))
  23. send_to_user = literal_eval(icp_obj.get_param(cr, SUPERUSER_ID,
  24. 'auth_admin_passkey.send_to_user', 'True'))
  25. emails_to = []
  26. if send_to_admin and admin_user.email:
  27. emails_to.append(admin_user.email)
  28. if send_to_user and login_user.email:
  29. emails_to.append(login_user.email)
  30. if emails_to:
  31. body = "Admin user used his passkey to login with '%s'.\n\n" %(login_user.login)
  32. body += "\n\nTechnicals informations belows : \n\n"
  33. body += "- Login date : %s\n\n" %(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
  34. for key, value in user_agent_env.iteritems():
  35. body +=("- %s : %s\n\n") % (key, value)
  36. for email_to in emails_to:
  37. mail_obj.create(cr, SUPERUSER_ID, {
  38. 'email_to': email_to,
  39. 'subject': "Passkey used",
  40. 'body_html': '<pre>%s</pre>' % body})
  41. def _send_email_same_password(self, cr, login_user):
  42. """ Send a email to the admin user to inform that another user has the
  43. same password as him"""
  44. mail_obj = self.pool.get('mail.mail')
  45. admin_user = self.browse(cr, SUPERUSER_ID, SUPERUSER_ID)
  46. if admin_user.email:
  47. mail_obj.create(cr, SUPERUSER_ID, {
  48. 'email_to': admin_user.email,
  49. 'subject': "[WARNING] OpenERP Security Risk",
  50. 'body_html': """<pre>User with login '%s' has the same """\
  51. """password as you.</pre>""" %(login_user)
  52. })
  53. ### Overload Section
  54. def authenticate(self, db, login, password, user_agent_env):
  55. """ Authenticate the user 'login' is password is ok
  56. or if is admin password. In the second case, send mail to user and admin."""
  57. user_id = super(res_users, self).authenticate(db, login, password, user_agent_env)
  58. if user_id != SUPERUSER_ID:
  59. same_password = False
  60. cr = pooler.get_db(db).cursor()
  61. try:
  62. # directly use parent 'check_credentials' function
  63. # to really know if credentials are ok or if it was admin password
  64. super(res_users, self).check_credentials(cr, SUPERUSER_ID, password)
  65. try:
  66. # Test now if the user has the same password as admin user
  67. super(res_users, self).check_credentials(cr, user_id, password)
  68. same_password = True
  69. except exceptions.AccessDenied:
  70. pass
  71. if not same_password:
  72. self._send_email_passkey(cr, user_id, user_agent_env)
  73. else:
  74. self._send_email_same_password(cr, login)
  75. cr.commit()
  76. except exceptions.AccessDenied:
  77. pass
  78. finally:
  79. cr.close()
  80. return user_id
  81. def check_credentials(self, cr, uid, password):
  82. """ Return now True if credentials are good OR if password is admin password"""
  83. if uid != SUPERUSER_ID:
  84. try:
  85. self.check_credentials(cr, SUPERUSER_ID, password)
  86. return True
  87. except exceptions.AccessDenied:
  88. return super(res_users, self).check_credentials(cr, uid, password)
  89. else:
  90. return super(res_users, self).check_credentials(cr, uid, password)