zhuhao commited on
Commit
41d18bc
·
1 Parent(s): b994dc1

add password reset function by extending the Flask command (#1632)

Browse files

### What problem does this PR solve?
add password reset function by extending the Flask command. #1200

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

Files changed (2) hide show
  1. api/apps/__init__.py +2 -1
  2. api/utils/commands.py +48 -0
api/apps/__init__.py CHANGED
@@ -25,7 +25,7 @@ from flask_cors import CORS
25
  from api.db import StatusEnum
26
  from api.db.db_models import close_connection
27
  from api.db.services import UserService
28
- from api.utils import CustomJSONEncoder
29
 
30
  from flask_session import Session
31
  from flask_login import LoginManager
@@ -60,6 +60,7 @@ Session(app)
60
  login_manager = LoginManager()
61
  login_manager.init_app(app)
62
 
 
63
 
64
 
65
  def search_pages_path(pages_dir):
 
25
  from api.db import StatusEnum
26
  from api.db.db_models import close_connection
27
  from api.db.services import UserService
28
+ from api.utils import CustomJSONEncoder, commands
29
 
30
  from flask_session import Session
31
  from flask_login import LoginManager
 
60
  login_manager = LoginManager()
61
  login_manager.init_app(app)
62
 
63
+ commands.register_commands(app)
64
 
65
 
66
  def search_pages_path(pages_dir):
api/utils/commands.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
17
+ import base64
18
+ import click
19
+
20
+ from flask import Flask
21
+ from werkzeug.security import generate_password_hash
22
+
23
+ from api.db.services import UserService
24
+
25
+
26
+ @click.command('reset-password', help='Reset the account password.')
27
+ @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset')
28
+ @click.option('--new-password', prompt=True, help='the new password.')
29
+ @click.option('--password-confirm', prompt=True, help='the new password confirm.')
30
+ def reset_password(email, new_password, password_confirm):
31
+ if str(new_password).strip() != str(password_confirm).strip():
32
+ click.echo(click.style('sorry. The two passwords do not match.', fg='red'))
33
+ return
34
+ user = UserService.query(email=email)
35
+ if not user:
36
+ click.echo(click.style('sorry. The Email is not registered!.', fg='red'))
37
+ return
38
+ encode_password = base64.b64encode(new_password.encode('utf-8')).decode('utf-8')
39
+ password_hash = generate_password_hash(encode_password)
40
+ user_dict = {
41
+ 'password': password_hash
42
+ }
43
+ UserService.update_user(user[0].id,user_dict)
44
+ click.echo(click.style('Congratulations! Password has been reset.', fg='green'))
45
+
46
+
47
+ def register_commands(app: Flask):
48
+ app.cli.add_command(reset_password)