users.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.cli.users
  4. ~~~~~~~~~~~~~~~~~
  5. This module contains all user commands.
  6. :copyright: (c) 2016 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import sys
  10. import os
  11. import click
  12. from sqlalchemy.exc import IntegrityError
  13. from flaskbb.cli.main import flaskbb
  14. from flaskbb.cli.utils import FlaskBBCLIError, EmailType, prompt_save_user
  15. from flaskbb.user.models import User
  16. @flaskbb.group()
  17. def users():
  18. """Create, update or delete users."""
  19. pass
  20. @users.command("new")
  21. @click.option("--username", "-u", help="The username of the user.")
  22. @click.option("--email", "-e", type=EmailType(),
  23. help="The email address of the user.")
  24. @click.option("--password", "-p", help="The password of the user.")
  25. @click.option("--group", "-g", help="The group of the user.",
  26. type=click.Choice(["admin", "super_mod", "mod", "member"]))
  27. def new_user(username, email, password, group):
  28. """Creates a new user. Omit any options to use the interactive mode."""
  29. try:
  30. user = prompt_save_user(username, email, password, group)
  31. click.secho("[+] User {} with Email {} in Group {} created.".format(
  32. user.username, user.email, user.primary_group.name), fg="cyan"
  33. )
  34. except IntegrityError:
  35. raise FlaskBBCLIError("Couldn't create the user because the "
  36. "username or email address is already taken.",
  37. fg="red")
  38. @users.command("update")
  39. @click.option("--username", "-u", help="The username of the user.")
  40. @click.option("--email", "-e", type=EmailType(),
  41. help="The email address of the user.")
  42. @click.option("--password", "-p", help="The password of the user.")
  43. @click.option("--group", "-g", help="The group of the user.",
  44. type=click.Choice(["admin", "super_mod", "mod", "member"]))
  45. def change_user(username, password, email, group):
  46. """Updates an user. Omit any options to use the interactive mode."""
  47. user = prompt_save_user(username, password, email, group, only_update=True)
  48. if user is None:
  49. raise FlaskBBCLIError("The user with username {} does not exist."
  50. .format(username), fg="red")
  51. click.secho("[+] User {} updated.".format(user.username), fg="cyan")
  52. @users.command("delete")
  53. @click.option("--username", "-u", help="The username of the user.")
  54. @click.option("--force", "-f", default=False, is_flag=True,
  55. help="Removes the user without asking for confirmation.")
  56. def delete_user(username, force):
  57. """Deletes an user."""
  58. if not username:
  59. username = click.prompt(
  60. click.style("Username", fg="magenta"), type=str,
  61. default=os.environ.get("USER", "")
  62. )
  63. user = User.query.filter_by(username=username).first()
  64. if user is None:
  65. raise FlaskBBCLIError("The user with username {} does not exist."
  66. .format(username), fg="red")
  67. if not force and not \
  68. click.confirm(click.style("Are you sure?", fg="magenta")):
  69. sys.exit(0)
  70. user.delete()
  71. click.secho("[+] User {} deleted.".format(user.username), fg="cyan")