An ebook/comic library service and web client
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.

132 lines
4.1 KiB

  1. import base64
  2. import logging
  3. import random
  4. import string
  5. from os import path
  6. from typing import Optional
  7. import click
  8. from click import Context
  9. from atheneum import atheneum
  10. from atheneum.model import User
  11. from atheneum.service import user_service
  12. logging.basicConfig()
  13. @click.group()
  14. def main():
  15. pass
  16. @click.group(name='user')
  17. def user_command_group():
  18. pass
  19. @click.command(name='delete')
  20. @click.argument('name')
  21. def delete_user(name: str):
  22. logging.info('Deleting user with name \'%s\'', name)
  23. existing_user = User.query.filter_by(name=name).first()
  24. if existing_user is not None:
  25. successful = user_service.delete(existing_user)
  26. if successful:
  27. logging.warning('Deleted user with name \'%s\'', name)
  28. else:
  29. logging.error('Failed to delete user with name \'%s\'', name)
  30. else:
  31. logging.warning('User with name \'%s\' doesn\'t exist', name)
  32. @click.command('register')
  33. @click.argument('name')
  34. @click.argument('password', required=False)
  35. @click.option('--role',
  36. default=User.ROLE_USER,
  37. envvar='ROLE',
  38. help='Role to assign to the user. default=[USER]')
  39. def register_user(
  40. name: str,
  41. role: str,
  42. password: Optional[str] = None):
  43. logging.info('Registering user with name \'%s\'', name)
  44. existing_user = User.query.filter_by(name=name).first()
  45. if existing_user is None:
  46. user_password = password if password else ''.join(
  47. random.choices(string.ascii_letters + string.digits, k=24))
  48. new_user = user_service.register(name, user_password, role, False)
  49. logging.warning(
  50. 'Created new user: \'%s\' with password \'%s\' and role %s',
  51. new_user.name,
  52. user_password,
  53. new_user.role)
  54. else:
  55. logging.warning('User \'%s\' already exists. Did you mean to update?',
  56. name)
  57. @click.command(name='register-admin')
  58. @click.pass_context
  59. def register_admin_user(ctx: Context):
  60. admin_users = User.query.filter_by(role=User.ROLE_ADMIN).all()
  61. if len(admin_users) == 0:
  62. name = 'atheneum_administrator'
  63. password = ''.join(
  64. random.choices(string.ascii_letters + string.digits, k=32))
  65. ctx.invoke(
  66. register_user,
  67. name=name,
  68. role=User.ROLE_ADMIN,
  69. password=password)
  70. admin_credential_file = '.admin_credentials'
  71. with open(admin_credential_file, 'w') as f:
  72. f.write('{}:{}'.format(name, password))
  73. logging.info(
  74. 'These credentials can also be retrieved from {}'.format(
  75. path.abspath(admin_credential_file)))
  76. @click.command(name='reset-password')
  77. @click.argument('name')
  78. @click.argument('password', required=False)
  79. def reset_user_password(name: str, password: Optional[str] = None):
  80. logging.info('Resetting user password for \'%s\'', name)
  81. existing_user = User.query.filter_by(name=name).first()
  82. if existing_user is not None:
  83. user_password = password if password else ''.join(
  84. random.choices(string.ascii_letters + string.digits, k=24))
  85. user_service.update_password(existing_user, user_password)
  86. logging.warning(
  87. 'Updated user: \'%s\' with password \'%s\'',
  88. name,
  89. user_password)
  90. else:
  91. logging.warning('User with name \'%s\' doesn\'t exist', name)
  92. @click.command(name='list')
  93. def list_users():
  94. all_users = User.query.all()
  95. [click.echo(user.name) for user in all_users]
  96. @click.command(name='base64')
  97. @click.argument('text')
  98. def convert_to_base64(text: str):
  99. print(base64.b64encode(text.encode('utf8')).decode('utf8'))
  100. main.add_command(user_command_group)
  101. main.add_command(convert_to_base64)
  102. user_command_group.add_command(register_user)
  103. user_command_group.add_command(register_admin_user)
  104. user_command_group.add_command(delete_user)
  105. user_command_group.add_command(reset_user_password)
  106. user_command_group.add_command(list_users)
  107. if __name__ == '__main__':
  108. logging.debug('Managing: %s', atheneum.name)
  109. with atheneum.app_context():
  110. main()