users.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from __future__ import print_function
  17. import six
  18. from mediagoblin.gmg_commands import util as commands_util
  19. from mediagoblin import auth
  20. from mediagoblin import mg_globals
  21. def adduser_parser_setup(subparser):
  22. subparser.add_argument(
  23. '--username','-u',
  24. help="Username used to login")
  25. subparser.add_argument(
  26. '--password','-p',
  27. help="Your supersecret word to login, beware of storing it in bash history")
  28. subparser.add_argument(
  29. '--email','-e',
  30. help="Email to receive notifications")
  31. def adduser(args):
  32. #TODO: Lets trust admins this do not validate Emails :)
  33. commands_util.setup_app(args)
  34. args.username = six.text_type(commands_util.prompt_if_not_set(args.username, "Username:"))
  35. args.password = commands_util.prompt_if_not_set(args.password, "Password:",True)
  36. args.email = commands_util.prompt_if_not_set(args.email, "Email:")
  37. db = mg_globals.database
  38. users_with_username = \
  39. db.User.query.filter_by(
  40. username=args.username.lower()
  41. ).count()
  42. if users_with_username:
  43. print(u'Sorry, a user with that name already exists.')
  44. else:
  45. # Create the user
  46. entry = db.User()
  47. entry.username = six.text_type(args.username.lower())
  48. entry.email = six.text_type(args.email)
  49. entry.pw_hash = auth.gen_password_hash(args.password)
  50. default_privileges = [
  51. db.Privilege.query.filter(
  52. db.Privilege.privilege_name==u'commenter').one(),
  53. db.Privilege.query.filter(
  54. db.Privilege.privilege_name==u'uploader').one(),
  55. db.Privilege.query.filter(
  56. db.Privilege.privilege_name==u'reporter').one(),
  57. db.Privilege.query.filter(
  58. db.Privilege.privilege_name==u'active').one()
  59. ]
  60. entry.all_privileges = default_privileges
  61. entry.save()
  62. print(u"User created (and email marked as verified)")
  63. def makeadmin_parser_setup(subparser):
  64. subparser.add_argument(
  65. 'username',
  66. help="Username to give admin level")
  67. def makeadmin(args):
  68. commands_util.setup_app(args)
  69. db = mg_globals.database
  70. user = db.User.query.filter_by(
  71. username=six.text_type(args.username.lower())).one()
  72. if user:
  73. user.all_privileges.append(
  74. db.Privilege.query.filter(
  75. db.Privilege.privilege_name==u'admin').one()
  76. )
  77. user.save()
  78. print(u'The user is now Admin')
  79. else:
  80. print(u'The user doesn\'t exist')
  81. def changepw_parser_setup(subparser):
  82. subparser.add_argument(
  83. 'username',
  84. help="Username used to login")
  85. subparser.add_argument(
  86. 'password',
  87. help="Your NEW supersecret word to login")
  88. def changepw(args):
  89. commands_util.setup_app(args)
  90. db = mg_globals.database
  91. user = db.User.query.filter_by(
  92. username=six.text_type(args.username.lower())).one()
  93. if user:
  94. user.pw_hash = auth.gen_password_hash(args.password)
  95. user.save()
  96. print(u'Password successfully changed')
  97. else:
  98. print(u'The user doesn\'t exist')
  99. def deleteuser_parser_setup(subparser):
  100. subparser.add_argument(
  101. 'username',
  102. help="Username to delete",
  103. type=six.text_type)
  104. def deleteuser(args):
  105. commands_util.setup_app(args)
  106. db = mg_globals.database
  107. user = db.User.query.filter_by(username=args.username.lower()).first()
  108. if user:
  109. user.delete()
  110. print('The user %s has been deleted' % args.username)
  111. else:
  112. print('The user %s doesn\'t exist' % args.username)