def reset_user_password(user: model.User) -> str: assert user password = auth.create_password() user.password_salt = auth.create_password() password_hash, revision = auth.get_password_hash( user.password_salt, password) user.password_hash = password_hash user.password_revision = revision return password
def reset_user_password(user: model.User) -> str: assert user password = auth.create_password() user.password_salt = auth.create_password() password_hash, revision = auth.get_password_hash(user.password_salt, password) user.password_hash = password_hash user.password_revision = revision return password
def update_user_password(user: model.User, password: str) -> None: assert user if not password: raise InvalidPasswordError('Password cannot be empty.') password_regex = config.config['password_regex'] if not re.match(password_regex, password): raise InvalidPasswordError('Password must satisfy regex %r.' % password_regex) user.password_salt = auth.create_password() user.password_hash = auth.get_password_hash(user.password_salt, password)
def update_user_password(user: model.User, password: str) -> None: assert user if not password: raise InvalidPasswordError('Password cannot be empty.') password_regex = config.config['password_regex'] if not re.match(password_regex, password): raise InvalidPasswordError( 'Password must satisfy regex %r.' % password_regex) user.password_salt = auth.create_password() password_hash, revision = auth.get_password_hash( user.password_salt, password) user.password_hash = password_hash user.password_revision = revision
def update_user_password(user: model.User, password: str) -> None: assert user if not password: raise InvalidPasswordError('비밀번호는 빈 값일 수 없습니다.') password_regex = config.config['password_regex'] if not re.match(password_regex, password): raise InvalidPasswordError('비밀번호는 다음의 정규식을 만족해야 합니다: %r' % password_regex) user.password_salt = auth.create_password() password_hash, revision = auth.get_password_hash(user.password_salt, password) user.password_hash = password_hash user.password_revision = revision
def update_user_password(user: model.User, password: str) -> None: assert user if not password: raise InvalidPasswordError("Password cannot be empty.") password_regex = config.config["password_regex"] if not re.match(password_regex, password): raise InvalidPasswordError("Password must satisfy regex %r." % password_regex) user.password_salt = auth.create_password() password_hash, revision = auth.get_password_hash(user.password_salt, password) user.password_hash = password_hash user.password_revision = revision
def update_user_email(user: model.User, email: str) -> None: assert user email = email.strip() if util.value_exceeds_column_size(email, model.User.email): raise InvalidEmailError('이메일이 너무 깁니다.') if not util.is_valid_email(email): raise InvalidEmailError('잘못된 이메일입니다.') user.email = email or None
def update_user_email(user: model.User, email: str) -> None: assert user email = email.strip() if util.value_exceeds_column_size(email, model.User.email): raise InvalidEmailError('Email is too long.') if not util.is_valid_email(email): raise InvalidEmailError('E-mail is invalid.') user.email = email or None
def update_user_avatar(user: model.User, avatar_style: str, avatar_content: Optional[bytes] = None) -> None: assert user if avatar_style == 'gravatar': user.avatar_style = user.AVATAR_GRAVATAR elif avatar_style == 'manual': user.avatar_style = user.AVATAR_MANUAL avatar_path = 'avatars/' + user.name.lower() + '.png' if not avatar_content: if files.has(avatar_path): return raise InvalidAvatarError('아바타 컨텐츠가 누락되었습니다.') image = images.Image(avatar_content) image.resize_fill(int(config.config['thumbnails']['avatar_width']), int(config.config['thumbnails']['avatar_height'])) files.save(avatar_path, image.to_png()) else: raise InvalidAvatarError('아바타 스타일 %r 은(는) 잘못된 값입니다. 올바른 스타일 값: %r.' % (avatar_style, ['gravatar', 'manual']))
def is_valid_password(user: model.User, password: str) -> bool: assert user salt, valid_hash = user.password_salt, user.password_hash try: return pwhash.verify( user.password_hash.encode('utf8'), (config.config['secret'] + salt + password).encode('utf8')) except InvalidkeyError: possible_hashes = [ get_sha256_legacy_password_hash(salt, password)[0], get_sha1_legacy_password_hash(salt, password)[0] ] if valid_hash in possible_hashes: # Convert the user password hash to the new hash new_hash, revision = get_password_hash(salt, password) user.password_hash = new_hash user.password_revision = revision db.session.commit() return True return False
def update_user_avatar( user: model.User, avatar_style: str, avatar_content: Optional[bytes] = None) -> None: assert user if avatar_style == 'gravatar': user.avatar_style = user.AVATAR_GRAVATAR elif avatar_style == 'manual': user.avatar_style = user.AVATAR_MANUAL avatar_path = 'avatars/' + user.name.lower() + '.png' if not avatar_content: if files.has(avatar_path): return raise InvalidAvatarError('Avatar content missing.') image = images.Image(avatar_content) image.resize_fill( int(config.config['thumbnails']['avatar_width']), int(config.config['thumbnails']['avatar_height'])) files.save(avatar_path, image.to_png()) else: raise InvalidAvatarError( 'Avatar style %r is invalid. Valid avatar styles: %r.' % ( avatar_style, ['gravatar', 'manual']))
def update_user_avatar(user: model.User, avatar_style: str, avatar_content: Optional[bytes] = None) -> None: assert user if avatar_style == "gravatar": user.avatar_style = user.AVATAR_GRAVATAR elif avatar_style == "manual": user.avatar_style = user.AVATAR_MANUAL avatar_path = "avatars/" + user.name.lower() + ".png" if not avatar_content: if files.has(avatar_path): return raise InvalidAvatarError("Avatar content missing.") image = images.Image(avatar_content) image.resize_fill( int(config.config["thumbnails"]["avatar_width"]), int(config.config["thumbnails"]["avatar_height"]), ) files.save(avatar_path, image.to_png()) else: raise InvalidAvatarError( "Avatar style %r is invalid. Valid avatar styles: %r." % (avatar_style, ["gravatar", "manual"]))
def update_user_rank(user: model.User, rank: str, auth_user: model.User) -> None: assert user if not rank: raise InvalidRankError('Rank cannot be empty.') rank = util.flip(auth.RANK_MAP).get(rank.strip(), None) all_ranks = list(auth.RANK_MAP.values()) if not rank: raise InvalidRankError('Rank can be either of %r.' % all_ranks) if rank in (model.User.RANK_ANONYMOUS, model.User.RANK_NOBODY): raise InvalidRankError('Rank %r cannot be used.' % auth.RANK_MAP[rank]) if all_ranks.index(auth_user.rank) \ < all_ranks.index(rank) and get_user_count() > 0: raise errors.AuthError('Trying to set higher rank than your own.') user.rank = rank
def update_user_rank(user: model.User, rank: str, auth_user: model.User) -> None: assert user if not rank: raise InvalidRankError('등급은 빈 값일 수 없습니다.') rank = util.flip(auth.RANK_MAP).get(rank.strip(), None) all_ranks = list(auth.RANK_MAP.values()) if not rank: raise InvalidRankError('등급은 다음중 하나여야 합니다: %r' % all_ranks) if rank in (model.User.RANK_ANONYMOUS, model.User.RANK_NOBODY): raise InvalidRankError('등급 %r 은(는) 사용할 수 없습니다.' % auth.RANK_MAP[rank]) if all_ranks.index(auth_user.rank) \ < all_ranks.index(rank) and get_user_count() > 0: raise errors.AuthError('당신보다 높은 등급을 지정할 수 없습니다.') user.rank = rank
def update_user_rank( user: model.User, rank: str, auth_user: model.User) -> None: assert user if not rank: raise InvalidRankError('Rank cannot be empty.') rank = util.flip(auth.RANK_MAP).get(rank.strip(), None) all_ranks = list(auth.RANK_MAP.values()) if not rank: raise InvalidRankError( 'Rank can be either of %r.' % all_ranks) if rank in (model.User.RANK_ANONYMOUS, model.User.RANK_NOBODY): raise InvalidRankError('Rank %r cannot be used.' % auth.RANK_MAP[rank]) if all_ranks.index(auth_user.rank) \ < all_ranks.index(rank) and get_user_count() > 0: raise errors.AuthError('Trying to set higher rank than your own.') user.rank = rank
def update_user_name(user: model.User, name: str) -> None: assert user if not name: raise InvalidUserNameError('Name cannot be empty.') if util.value_exceeds_column_size(name, model.User.name): raise InvalidUserNameError('User name is too long.') name = name.strip() name_regex = config.config['user_name_regex'] if not re.match(name_regex, name): raise InvalidUserNameError('User name %r must satisfy regex %r.' % (name, name_regex)) other_user = try_get_user_by_name(name) if other_user and other_user.user_id != user.user_id: raise UserAlreadyExistsError('User %r already exists.' % name) if user.name and files.has(get_avatar_path(user.name)): files.move(get_avatar_path(user.name), get_avatar_path(name)) user.name = name
def update_user_name(user: model.User, name: str) -> None: assert user if not name: raise InvalidUserNameError('닉네임(ID)은 빈 값일 수 없습니다.') if util.value_exceeds_column_size(name, model.User.name): raise InvalidUserNameError('닉네임(ID)이 너무 깁니다.') name = name.strip() name_regex = config.config['user_name_regex'] if not re.match(name_regex, name): raise InvalidUserNameError('닉네임(ID) %r 은(는) 다음의 정규식을 만족해야 합니다: %r' % (name, name_regex)) other_user = try_get_user_by_name(name) if other_user and other_user.user_id != user.user_id: raise UserAlreadyExistsError('사용자 %r 은(는) 이미 존재합니다.' % name) if user.name and files.has(get_avatar_path(user.name)): files.move(get_avatar_path(user.name), get_avatar_path(name)) user.name = name
def update_user_name(user: model.User, name: str) -> None: assert user if not name: raise InvalidUserNameError('Name cannot be empty.') if util.value_exceeds_column_size(name, model.User.name): raise InvalidUserNameError('User name is too long.') name = name.strip() name_regex = config.config['user_name_regex'] if not re.match(name_regex, name): raise InvalidUserNameError( 'User name %r must satisfy regex %r.' % (name, name_regex)) other_user = try_get_user_by_name(name) if other_user and other_user.user_id != user.user_id: raise UserAlreadyExistsError('User %r already exists.' % name) if user.name and files.has(get_avatar_path(user.name)): files.move(get_avatar_path(user.name), get_avatar_path(name)) user.name = name
def reset_user_password(user: model.User) -> str: assert user password = auth.create_password() user.password_salt = auth.create_password() user.password_hash = auth.get_password_hash(user.password_salt, password) return password
def bump_user_login_time(user: model.User) -> None: assert user user.last_login_time = datetime.utcnow()