You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import base64
  17. import click
  18. import re
  19. from flask import Flask
  20. from werkzeug.security import generate_password_hash
  21. from api.db.services import UserService
  22. @click.command('reset-password', help='Reset the account password.')
  23. @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset')
  24. @click.option('--new-password', prompt=True, help='the new password.')
  25. @click.option('--password-confirm', prompt=True, help='the new password confirm.')
  26. def reset_password(email, new_password, password_confirm):
  27. if str(new_password).strip() != str(password_confirm).strip():
  28. click.echo(click.style('sorry. The two passwords do not match.', fg='red'))
  29. return
  30. user = UserService.query(email=email)
  31. if not user:
  32. click.echo(click.style('sorry. The Email is not registered!.', fg='red'))
  33. return
  34. encode_password = base64.b64encode(new_password.encode('utf-8')).decode('utf-8')
  35. password_hash = generate_password_hash(encode_password)
  36. user_dict = {
  37. 'password': password_hash
  38. }
  39. UserService.update_user(user[0].id,user_dict)
  40. click.echo(click.style('Congratulations! Password has been reset.', fg='green'))
  41. @click.command('reset-email', help='Reset the account email.')
  42. @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset')
  43. @click.option('--new-email', prompt=True, help='the new email.')
  44. @click.option('--email-confirm', prompt=True, help='the new email confirm.')
  45. def reset_email(email, new_email, email_confirm):
  46. if str(new_email).strip() != str(email_confirm).strip():
  47. click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red'))
  48. return
  49. if str(new_email).strip() == str(email).strip():
  50. click.echo(click.style('Sorry, new email and old email are the same.', fg='red'))
  51. return
  52. user = UserService.query(email=email)
  53. if not user:
  54. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  55. return
  56. if not re.match(r"^[\w\._-]+@([\w_-]+\.)+[\w-]{2,4}$", new_email):
  57. click.echo(click.style('sorry. {} is not a valid email. '.format(new_email), fg='red'))
  58. return
  59. new_user = UserService.query(email=new_email)
  60. if new_user:
  61. click.echo(click.style('sorry. the account: [{}] is exist .'.format(new_email), fg='red'))
  62. return
  63. user_dict = {
  64. 'email': new_email
  65. }
  66. UserService.update_user(user[0].id,user_dict)
  67. click.echo(click.style('Congratulations!, email has been reset.', fg='green'))
  68. def register_commands(app: Flask):
  69. app.cli.add_command(reset_password)
  70. app.cli.add_command(reset_email)