added pwd facility
This commit is contained in:
Executable
+48
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Set a L@pp user's password from the command line."""
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from lapp import create_app
|
||||
from models import User, db
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Set a new password for an existing L@pp user.",
|
||||
usage="%(prog)s <username> <new-pwd>",
|
||||
)
|
||||
parser.add_argument("username", help="name of the user to update")
|
||||
parser.add_argument("new_password", metavar="new-pwd", help="new password")
|
||||
return parser, parser.parse_args()
|
||||
|
||||
|
||||
def main():
|
||||
parser, args = parse_args()
|
||||
|
||||
if not args.username:
|
||||
parser.error("username must not be empty")
|
||||
if not args.new_password:
|
||||
parser.error("new-pwd must not be empty")
|
||||
|
||||
app = create_app()
|
||||
with app.app_context():
|
||||
user = db.session.scalar(
|
||||
sa.select(User).where(User.name == args.username)
|
||||
)
|
||||
if user is None:
|
||||
print(f"User {args.username!r} does not exist.", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
user.set_password(args.new_password)
|
||||
db.session.commit()
|
||||
|
||||
print(f"Password updated for user {args.username!r}.")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user