This question has been flagged
1 Reply
5321 Views

Before login, I want to check a condition. Where should i wrote the code?

I will give a value in res.users table. When i click login button, it should first check the condition then login.

 

Avatar
Discard
Best Answer

Refer the link http://stackoverflow.com/questions/16028848/openerp-7-login-flow Openerp Login Methods and Files Path mentioned in the LInk.

Before Login to check the condition to override Method def login(self, db, login, password) in openerp/addons/base/res/res_users.py

 

from openerp import SUPERUSER_ID

class res_users(osv.Model):
    _inherit = 'res.users'
  
    def login(self, db, login, password):
        try:
            users_obj = self.pool.get('res.users')
            cr = pooler.get_db(db).cursor()
            user_id = super(res_users, self).login(db, login, password)
            login_date = None
            try:            
                if user_id:
                    val = users_obj.browse(cr, SUPERUSER_ID, user_id)
                    login_date = val.login_date
                    current_date = datetime.date.today().strftime('%Y-%m-%d')
            except:
                pass
            if login_date == current_date:
               raise osv.except_osv(_('Message!'), _("Restrict Login Only one in a day"))
               _logger.info("Login failed for db:%s login:%s", db, login)
               user_id = False                
        except openerp.exceptions.AccessDenied:
            _logger.info("Login failed for db:%s login:%s", db, login)
            user_id = False
        return user_id 

 

 

 

Avatar
Discard
Author

Thanks for the answer

Author

I want to check login_date with past_date. If(login_date = past_date) restrict login. otherwise he can login to the account. Did u have any idea?

updated code default res_users table login_date (last login data) details available and check with current date in login method.

Author

This function, i dont want to check for admin. Then how can i give that? Except admin all other users should check this function

Author

i replace the code in original base module. But it is not working. Will it work, when i create an inherit module??

Hi Prakash, Can you explain below code breifly.... i have create a module ,and installed it.But at what condition the error message will shows like------- raise osv.except_osv(_('Message!'), _("Restrict Login Only one in a day"))

In the same day second time Login the error message shows...

Author

@prakash: i created a module and inherit the res.users. but its not working. I also want to check all users except admin. is it possible??

@rosey: yes it is possible if login_date == current_date and user_id != 1: # Default admin id 1. But i not tested the code.