user.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # -*- coding: utf-8 -*-
  2. from tracim import model as pm
  3. from sprox.tablebase import TableBase
  4. from sprox.formbase import EditableForm, AddRecordForm
  5. from sprox.fillerbase import TableFiller, EditFormFiller
  6. from tw2 import forms as tw2f
  7. import tg
  8. from tg import tmpl_context
  9. from tg.i18n import ugettext as _, lazy_ugettext as l_
  10. from sprox.widgets import PropertyMultipleSelectField
  11. from sprox._compat import unicode_text
  12. from formencode import Schema
  13. from formencode.validators import FieldsMatch
  14. from tracim.controllers import TIMRestController
  15. from tracim.lib import helpers as h
  16. from tracim.lib.user import UserApi
  17. from tracim.lib.group import GroupApi
  18. from tracim.lib.user import UserStaticApi
  19. from tracim.lib.userworkspace import RoleApi
  20. from tracim.lib.workspace import WorkspaceApi
  21. from tracim.model import DBSession
  22. from tracim.model.auth import Group, User
  23. from tracim.model.serializers import Context, CTX, DictLikeClass
  24. class UserWorkspaceRestController(TIMRestController):
  25. def _before(self, *args, **kw):
  26. """
  27. Instantiate the current workspace in tg.tmpl_context
  28. :param args:
  29. :param kw:
  30. :return:
  31. """
  32. super(self.__class__, self)._before(args, kw)
  33. api = UserApi(tg.tmpl_context.current_user)
  34. user_id = tmpl_context.current_user_id
  35. user = tmpl_context.current_user
  36. @tg.expose()
  37. def enable_notifications(self, workspace_id):
  38. workspace_id = int(workspace_id)
  39. api = WorkspaceApi(tg.tmpl_context.current_user)
  40. workspace = api.get_one(workspace_id)
  41. api.enable_notifications(tg.tmpl_context.current_user, workspace)
  42. tg.flash(_('Notification enabled for workspace {}').format(workspace.label))
  43. tg.redirect(self.parent_controller.url(None, 'me'))
  44. @tg.expose()
  45. def disable_notifications(self, workspace_id):
  46. workspace_id = int(workspace_id)
  47. api = WorkspaceApi(tg.tmpl_context.current_user)
  48. workspace = api.get_one(workspace_id)
  49. api.disable_notifications(tg.tmpl_context.current_user, workspace)
  50. tg.flash(_('Notification disabled for workspace {}').format(workspace.label))
  51. tg.redirect(self.parent_controller.url(None, 'me'))
  52. class UserPasswordRestController(TIMRestController):
  53. """
  54. CRUD Controller allowing to manage password of a given user
  55. TODO: do not duplicate this controller between admin and "standard user" interfaces
  56. """
  57. def _before(self, *args, **kw):
  58. """
  59. Instantiate the current workspace in tg.tmpl_context
  60. :param args:
  61. :param kw:
  62. :return:
  63. """
  64. super(self.__class__, self)._before(args, kw)
  65. api = UserApi(tg.tmpl_context.current_user)
  66. user_id = tmpl_context.current_user_id
  67. user = tmpl_context.current_user
  68. @tg.expose('tracim.templates.user_password_edit_me')
  69. def edit(self):
  70. dictified_user = Context(CTX.USER).toDict(tmpl_context.current_user, 'user')
  71. return DictLikeClass(result = dictified_user)
  72. @tg.expose()
  73. def put(self, current_password, new_password1, new_password2):
  74. # FIXME - Allow only self password or operation for managers
  75. current_user = tmpl_context.current_user
  76. redirect_url = tg.lurl('/user/me')
  77. if not current_password or not new_password1 or not new_password2:
  78. tg.flash(_('Empty password is not allowed.'))
  79. tg.redirect(redirect_url)
  80. if current_user.validate_password(current_password) is False:
  81. tg.flash(_('The current password you typed is wrong'))
  82. tg.redirect(redirect_url)
  83. if new_password1!=new_password2:
  84. tg.flash(_('New passwords do not match.'))
  85. tg.redirect(redirect_url)
  86. current_user.password = new_password1
  87. pm.DBSession.flush()
  88. tg.flash(_('Your password has been changed'))
  89. tg.redirect(redirect_url)
  90. class UserRestController(TIMRestController):
  91. """
  92. CRUD Controller allowing to manage Users
  93. """
  94. password = UserPasswordRestController()
  95. workspaces = UserWorkspaceRestController()
  96. @classmethod
  97. def current_item_id_key_in_context(cls):
  98. return 'user_id'
  99. @tg.expose('tracim.templates.user_get_all')
  100. def get_all(self, *args, **kw):
  101. tg.redirect(self.url(None, 'me'))
  102. pass
  103. @tg.expose()
  104. def post(self, name, email, password, is_tracim_manager='off', is_pod_admin='off'):
  105. pass
  106. @tg.expose('tracim.templates.user_get_me')
  107. def get_one(self, user_id):
  108. user_id = tmpl_context.current_user.user_id
  109. current_user = tmpl_context.current_user
  110. assert user_id==current_user.user_id
  111. api = UserApi(current_user)
  112. current_user = api.get_one(current_user.user_id)
  113. dictified_user = Context(CTX.USER).toDict(current_user, 'user')
  114. current_user_content = Context(CTX.CURRENT_USER).toDict(tmpl_context.current_user)
  115. fake_api_content = DictLikeClass(current_user=current_user_content)
  116. fake_api = Context(CTX.WORKSPACE).toDict(fake_api_content)
  117. return DictLikeClass(result = dictified_user, fake_api=fake_api)
  118. @tg.expose('tracim.templates.user_edit_me')
  119. def edit(self, id):
  120. id = tmpl_context.current_user.user_id
  121. current_user = tmpl_context.current_user
  122. assert id==current_user.user_id
  123. dictified_user = Context(CTX.USER).toDict(current_user, 'user')
  124. return DictLikeClass(result = dictified_user)
  125. @tg.expose('tracim.templates.workspace_edit')
  126. def put(self, user_id, name, email):
  127. user_id = tmpl_context.current_user.user_id
  128. current_user = tmpl_context.current_user
  129. assert user_id==current_user.user_id
  130. api = UserApi(tmpl_context.current_user)
  131. api.update(current_user, name, email, True)
  132. tg.flash(_('profile updated.'))
  133. tg.redirect(self.url())
  134. return