33from sqlalchemy import Column , Integer , String , DateTime , LargeBinary
44from pbench .server .database .database import Database
55from sqlalchemy .orm import relationship
6- from sqlalchemy .orm import validates
76
87
98class User (Database .Base ):
@@ -17,6 +16,7 @@ class User(Database.Base):
1716 last_name = Column (String (255 ), unique = False , nullable = False )
1817 password = Column (LargeBinary (500 ), nullable = False )
1918 registered_on = Column (DateTime , nullable = False )
19+ bcrypt_log_rounds = Column (Integer , nullable = False )
2020 email = Column (String (255 ), unique = True , nullable = False )
2121 auth_tokens = relationship ("ActiveTokens" , backref = "users" )
2222
@@ -25,6 +25,7 @@ def __init__(self, bcrypt_log_rounds, **kwargs):
2525 self .username = kwargs .get ("username" )
2626 self .first_name = kwargs .get ("first_name" )
2727 self .last_name = kwargs .get ("last_name" )
28+ self .bcrypt_log_rounds = bcrypt_log_rounds
2829 self .password = generate_password_hash (
2930 kwargs .get ("password" ), bcrypt_log_rounds
3031 )
@@ -63,14 +64,6 @@ def add(self):
6364 Database .db_session .rollback ()
6465 raise e
6566
66- # Prevent update on "registered_on" field
67- @validates ("registered_on" )
68- def validates_user_registration (self , key , value ):
69- if self .registered_on : # Field already exists
70- raise ValueError ("registered_on cannot be modified." )
71-
72- return value
73-
7467 def update (self , ** kwargs ):
7568 """
7669 Update the current user object with given keyword arguments
@@ -81,6 +74,13 @@ def update(self, **kwargs):
8174 # Insert the auth token
8275 self .auth_tokens .append (value )
8376 Database .db_session .add (value )
77+ elif key == "password" :
78+ setattr (
79+ self , key , generate_password_hash (value , self .bcrypt_log_rounds )
80+ )
81+ # Prevent update on "registered_on" and "bcrypt_log_rounds" fields
82+ elif key in ["registered_on" , "bcrypt_log_rounds" ]:
83+ continue
8484 else :
8585 setattr (self , key , value )
8686 Database .db_session .commit ()
0 commit comments