account.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # encoding=utf-8
  2. import logging
  3. from sqlalchemy.exc import IntegrityError
  4. import tornado.gen
  5. from tornado.web import HTTPError
  6. import tornado.web
  7. from terroroftinytown.tracker.base import BaseHandler
  8. from terroroftinytown.tracker.form import (LoginForm, AddUserForm, ConfirmForm,
  9. ChangePasswordForm)
  10. from terroroftinytown.tracker.model import User, Session
  11. import terroroftinytown.tracker.util
  12. ACCOUNT_COOKIE_NAME = 'tottu'
  13. ACCOUNT_TOKEN_COOKIE_NAME = 'tottt'
  14. logger = logging.getLogger(__name__)
  15. class LoginHandler(BaseHandler):
  16. def get(self):
  17. form = LoginForm()
  18. self.render('admin/account/login.html', form=form)
  19. @tornado.gen.coroutine
  20. def post(self):
  21. form = LoginForm(self.request.arguments)
  22. if form.validate() \
  23. and self._login(form.username.data, form.password.data):
  24. logger.info(self.user_audit_text('User %s logged in.'),
  25. form.username.data)
  26. self.redirect(
  27. self.get_argument('next', self.reverse_url('admin.overview'))
  28. )
  29. return
  30. yield terroroftinytown.tracker.util.sleep(1)
  31. self.render('admin/account/login.html', form=form, message='Log in failed.')
  32. def _login(self, username, password):
  33. if User.no_users_exist():
  34. User.save_new_user(username, password)
  35. if User.check_account(username, password):
  36. self.set_secure_cookie(
  37. ACCOUNT_COOKIE_NAME, username, expires_days=30
  38. )
  39. self.set_secure_cookie(
  40. ACCOUNT_TOKEN_COOKIE_NAME, User.get_user_token(username),
  41. expires_days=30
  42. )
  43. return True
  44. class LogoutHandler(BaseHandler):
  45. def get(self):
  46. self.clear_cookie(ACCOUNT_COOKIE_NAME)
  47. self.clear_cookie(ACCOUNT_TOKEN_COOKIE_NAME)
  48. self.redirect('/')
  49. class AllUsersHandler(BaseHandler):
  50. @tornado.web.authenticated
  51. def get(self):
  52. add_user_form = AddUserForm()
  53. self.render(
  54. 'admin/account/all.html',
  55. usernames=User.all_usernames(),
  56. add_user_form=add_user_form
  57. )
  58. @tornado.web.authenticated
  59. def post(self):
  60. action = self.get_argument('action')
  61. message = None
  62. if action != 'add_user':
  63. raise HTTPError(400, 'Unknown action')
  64. add_user_form = AddUserForm(self.request.arguments)
  65. if add_user_form.validate():
  66. username = add_user_form.username.data
  67. password = add_user_form.password.data
  68. try:
  69. User.save_new_user(username, password)
  70. except IntegrityError:
  71. message = 'User already exists.'
  72. else:
  73. logger.info(self.user_audit_text('Added new user %s'),
  74. username)
  75. self.redirect(self.reverse_url('user.overview', username))
  76. return
  77. self.render(
  78. 'admin/account/all.html',
  79. add_user_form=add_user_form,
  80. usernames=User.all_usernames(),
  81. message=message
  82. )
  83. class UserHandler(BaseHandler):
  84. @tornado.web.authenticated
  85. def get(self, username):
  86. delete_form = ConfirmForm()
  87. password_form = ChangePasswordForm()
  88. self.render(
  89. 'admin/account/user.html',
  90. username=username, delete_form=delete_form,
  91. password_form=password_form
  92. )
  93. @tornado.web.authenticated
  94. def post(self, username):
  95. action = self.get_argument('action')
  96. delete_form = ConfirmForm(self.request.arguments)
  97. password_form = ChangePasswordForm(self.request.arguments)
  98. if action == 'delete':
  99. message = self._delete(username, delete_form)
  100. elif action == 'password':
  101. message = self._password(username, password_form)
  102. else:
  103. raise HTTPError(400, 'Unknown action')
  104. self.render(
  105. 'admin/account/user.html',
  106. username=username, delete_form=delete_form,
  107. password_form=password_form, message=message
  108. )
  109. def _delete(self, username, form):
  110. if form.validate():
  111. logger.info(self.user_audit_text('Deleted user %s'), username)
  112. User.delete_user(username)
  113. self.redirect(self.reverse_url('users.overview'))
  114. else:
  115. return 'Error'
  116. def _password(self, username, form):
  117. if form.validate():
  118. if form.username.data != username:
  119. return 'Wrong username'
  120. logger.info(self.user_audit_text('Updated user %s password'),
  121. username)
  122. User.update_password(username, form.password.data)
  123. self.redirect(self.reverse_url('users.overview'))
  124. else:
  125. return 'Error'