Advertisement
Guest User

Untitled

a guest
May 20th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.38 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from openerp import models, fields, api, exceptions, _
  4. from openerp.http import request
  5. from openerp.exceptions import ValidationError
  6. from Crypto.PublicKey import RSA
  7. import ast
  8. import pdb
  9.  
  10. # class lubon_partners(models.Model):
  11. #     _name = 'lubon_partners.lubon_partners'
  12.  
  13. #     name = fields.Char()
  14. # -*- coding: utf-8 -*-
  15. #from openerp import fields, models
  16.  
  17.  
  18. class Partner(models.Model):
  19.     _inherit = 'res.partner'
  20.     credential_ids = fields.One2many('lubon_credentials.credentials', 'partner_id', string='credentials')
  21.     masterkey = fields.Char()
  22.  
  23.     @api.one
  24.     def reveal_credentials(self, pin=None):
  25.  
  26.         require_pin = True
  27.  
  28.         def validate_retry():
  29.             retry_count = request.session.get('lubon_pin_retry', 1)
  30.             request.session['lubon_pin_retry'] = retry_count + 1
  31.             if retry_count >= 3:
  32.                 request.session.logout()
  33.                 return False
  34.             return True
  35.  
  36.         if require_pin and not pin:
  37.             b=1
  38.             raise ValidationError("PIN required!")
  39.  
  40.         if require_pin and pin != self.env.user.pin:
  41.             if not validate_retry():
  42.                 return -1
  43.             raise ValidationError("Incorrect PIN!")
  44.  
  45.         request.session['lubon_pin_retry'] = 1
  46.  
  47.         return [self.masterkey or '', self.env['ir.config_parameter'].get_param('lubon_credentials.reveal_credentials_timeout', '') or 15000]
  48.  
  49.  
  50. class Users(models.Model):
  51.     _inherit = 'res.users'
  52.     pin = fields.Char()
  53.  
  54.  
  55.  
  56. class lubon_qlan_credentials(models.Model):
  57.     _name = 'lubon_credentials.credentials'
  58.     _rec_name = 'description'
  59.     active=fields.Boolean(default=True)
  60.     description = fields.Char(string="Description", required=True)
  61.     user = fields.Char(string="User")
  62.     password = fields.Char()
  63.     hint = fields.Char()
  64.     password01 = fields.Char(string="Pass")
  65.     password02 = fields.Char(string="Conf")
  66.     encrypted=fields.Char(string="Encrypted")
  67.     is_saved=fields.Boolean(string="Saved")
  68.     partner_id = fields.Many2one('res.partner',  ondelete='set null', string="Partner", index=True)
  69.  #   credentials_type=fields.Selection([('tenant_admin','Tenant admin'),('wifi','Wifi'),('site_admin','Site admin'),('telephony','Telephony'),('general','General')], default='general')
  70.     @api.one
  71.     def show_password(self):
  72.         raise exceptions.ValidationError(self.password)
  73.         return True
  74.  
  75.     def _get_ipaddress(self, cr, uid, context=None):
  76.         return request.httprequest.environ['REMOTE_ADDR']
  77.     @api.one
  78.     def reveal_credentials(self, pin=None):
  79.  
  80.         require_pin = True
  81.  
  82.         def validate_retry():
  83.             retry_count = request.session.get('lubon_pin_retry', 1)
  84.             request.session['lubon_pin_retry'] = retry_count + 1
  85.             if retry_count >= 3:
  86.                 request.session.logout()
  87.                 return False
  88.             return True
  89.  
  90.         if require_pin and not pin:
  91.             raise ValidationError("PIN required!")
  92.  
  93.         if require_pin and pin != self.env.user.pin:
  94.             if not validate_retry():
  95.                 return -1
  96.             raise ValidationError("Incorrect PIN!")
  97.  
  98.         request.session['lubon_pin_retry'] = 1
  99.         password=""
  100.         if self.encrypted:
  101.             f= open('/home/odoo/.odoo/private_key.pem','r')
  102.             r = RSA.importKey(f.read())
  103.             f.close()
  104.             encrypted= ast.literal_eval(str(self.encrypted))
  105.             password=r.decrypt(encrypted)
  106.         return [password or '', self.env['ir.config_parameter'].get_param('lubon_credentials.reveal_credentials_timeout', '') or 15000]
  107.    
  108.     @api.one
  109.     def encrypt(self):
  110.         if not self.is_saved:
  111.             self.password01=self.password
  112.             self.password02=self.password
  113.             self.encrypt_password(True)
  114.             self.password=''
  115.         return True
  116.  
  117.  
  118.    
  119.     @api.one
  120.     @api.onchange('password01','password02')
  121.     def encrypt_password(self, force=False):
  122.         if (self.password01 and self.password02) or force:
  123.             if self.password01 == self.password02:
  124.                 f= open('/home/odoo/.odoo/public_key.pem','r')
  125.                 r = RSA.importKey(f.read())
  126.                 f.close()
  127.                 #pdb.set_trace()
  128.                 encrypted=""
  129.                 if self.password01:
  130.                     p=self.password01.encode('utf-8')
  131.                     encrypted=r.encrypt(p,32)
  132.                 self.encrypted=encrypted
  133.                 self.password01=''
  134.                 self.password02=''
  135.                 self.is_saved=1
  136.             else:
  137.                 raise ValidationError("Pls enter the same password twice")
  138.  
  139.  
  140.  
  141. class base_config_settings(models.TransientModel):
  142.     _name = 'lubon_credentials.config.settings'
  143.     _inherit = 'res.config.settings'
  144.  
  145.     def _get_default_reveal_credentials_timeout(self):
  146.         return self.env['ir.config_parameter'].get_param('lubon_credentials.reveal_credentials_timeout', '') or 15000
  147.  
  148.     reveal_credentials_timeout = fields.Integer('Reveal Credentials Timeout (ms)', required=True, default=_get_default_reveal_credentials_timeout)
  149.  
  150.     @api.model
  151.     def set_reveal_credentials_timeout(self, ids):
  152.         config = self.browse(ids[0])
  153.         icp = self.env['ir.config_parameter']
  154.         icp.set_param('lubon_credentials.reveal_credentials_timeout', config.reveal_credentials_timeout)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement