def check_user(cls, email, password): dbuser = DBUser.get_user_by_email(email) if dbuser: if dbuser.password == secret_hash(password, salt=dbuser.password[:36]): return cls(dbuser) elif not dbuser.password: dbuser.update(password=secret_hash(app.config["DefaultPassword"], salt=None)) return None
def check_user(cls, email, password): dbuser = DBUser.get_user_by_email(email) if dbuser: if dbuser.password == secret_hash(password, salt=dbuser.password[:36]): return cls(dbuser) return None
def update(self, **profile): if "password" in profile: if not 4 <= len(profile["password"].strip()) <= 30: raise Exception( "password must be more than 4 and less than 30 characters") profile["password"] = secret_hash(profile["password"].strip(), salt=None) if "email" in profile: email = profile["email"].strip().lower() profile["email"] = email if self.email != email and User.check_exist(email=email): raise Exception("email address already exist") if "nickname" in profile: nickname = profile["nickname"].strip() profile["nickname"] = nickname if self.nickname != nickname and User.check_exist( nickname=nickname): raise Exception("nickname already exist") if "role" in profile: if profile["role"] not in DBUser.UserRoles: raise Exception("role must be in %s" % DBUser.UserRoles) self.db_object.update(**profile)
def create_user(cls, **settings): password = settings.pop("password") email = settings.pop("email").strip().lower() if User.check_exist(email=email): raise Exception("email address already exist") nickname = settings.get("nickname", "").strip() if User.check_exist(nickname=nickname): raise Exception("nickname already exist") dbuser = DBUser.create(email=email) dbuser.save() _ = dbuser.stats # init stats dbuser.password = secret_hash(password, salt=None) dbuser.update(**settings) return cls(dbuser)
def update(self, **profile): if "password" in profile: profile["password"] = secret_hash(profile["password"], salt=None) if "email" in profile: email = profile["email"].strip().lower() profile["email"] = email if self.email != email and User.check_exist(email=email): raise Exception("email address already exist") if "nickname" in profile: nickname = profile["nickname"].strip() profile["nickname"] = nickname if self.nickname != nickname and User.check_exist(nickname=nickname): raise Exception("nickname already exist") if "role" in profile: if profile["role"] not in DBUser.UserRoles: raise Exception("role must be in %s" % DBUser.UserRoles) self.db_object.update(**profile)