123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- # -*- coding: utf-8 -*-
- from webob.exc import HTTPForbidden
-
- from tracim import model as pm
-
- from sprox.tablebase import TableBase
- from sprox.formbase import EditableForm, AddRecordForm
- from sprox.fillerbase import TableFiller, EditFormFiller
- from tw2 import forms as tw2f
- import tg
- from tg import tmpl_context
- from tg.i18n import ugettext as _, lazy_ugettext as l_
-
- from sprox.widgets import PropertyMultipleSelectField
- from sprox._compat import unicode_text
-
- from formencode import Schema
- from formencode.validators import FieldsMatch
-
- from tracim.controllers import TIMRestController
- from tracim.lib import helpers as h
- from tracim.lib.user import UserApi
- from tracim.lib.group import GroupApi
- from tracim.lib.user import UserStaticApi
- from tracim.lib.userworkspace import RoleApi
- from tracim.lib.workspace import WorkspaceApi
-
- from tracim.model import DBSession
- from tracim.model.auth import Group, User
- from tracim.model.serializers import Context, CTX, DictLikeClass
-
-
- class UserWorkspaceRestController(TIMRestController):
-
- def _before(self, *args, **kw):
- """
- Instantiate the current workspace in tg.tmpl_context
- :param args:
- :param kw:
- :return:
- """
- super(self.__class__, self)._before(args, kw)
-
- api = UserApi(tg.tmpl_context.current_user)
- user_id = tmpl_context.current_user_id
- user = tmpl_context.current_user
-
- @tg.expose()
- def enable_notifications(self, workspace_id, next_url=None):
- workspace_id = int(workspace_id)
- api = WorkspaceApi(tg.tmpl_context.current_user)
-
- workspace = api.get_one(workspace_id)
- api.enable_notifications(tg.tmpl_context.current_user, workspace)
- tg.flash(_('Notification enabled for workspace {}').format(workspace.label))
-
- if next_url:
- tg.redirect(tg.url(next_url))
- tg.redirect(self.parent_controller.url(None, 'me'))
-
- @tg.expose()
- def disable_notifications(self, workspace_id, next_url=None):
- workspace_id = int(workspace_id)
- api = WorkspaceApi(tg.tmpl_context.current_user)
-
- workspace = api.get_one(workspace_id)
- api.disable_notifications(tg.tmpl_context.current_user, workspace)
- tg.flash(_('Notification disabled for workspace {}').format(workspace.label))
-
- if next_url:
- tg.redirect(tg.url(next_url))
- tg.redirect(self.parent_controller.url(None, 'me'))
-
-
- class UserPasswordRestController(TIMRestController):
- """
- CRUD Controller allowing to manage password of a given user
- TODO: do not duplicate this controller between admin and "standard user" interfaces
- """
-
- def _before(self, *args, **kw):
- """
- Instantiate the current workspace in tg.tmpl_context
- :param args:
- :param kw:
- :return:
- """
- super(self.__class__, self)._before(args, kw)
-
- api = UserApi(tg.tmpl_context.current_user)
- user_id = tmpl_context.current_user_id
- user = tmpl_context.current_user
-
-
- @tg.expose('tracim.templates.user_password_edit_me')
- def edit(self):
- if not tg.config.get('auth_is_internal'):
- raise HTTPForbidden()
-
- dictified_user = Context(CTX.USER).toDict(tmpl_context.current_user, 'user')
- return DictLikeClass(result = dictified_user)
-
- @tg.expose()
- def put(self, current_password, new_password1, new_password2):
- if not tg.config.get('auth_is_internal'):
- raise HTTPForbidden()
-
- # FIXME - Allow only self password or operation for managers
- current_user = tmpl_context.current_user
-
- redirect_url = tg.lurl('/home')
-
- if not current_password or not new_password1 or not new_password2:
- tg.flash(_('Empty password is not allowed.'))
- tg.redirect(redirect_url)
-
- if current_user.validate_password(current_password) is False:
- tg.flash(_('The current password you typed is wrong'))
- tg.redirect(redirect_url)
-
- if new_password1!=new_password2:
- tg.flash(_('New passwords do not match.'))
- tg.redirect(redirect_url)
-
- current_user.password = new_password1
- pm.DBSession.flush()
-
- tg.flash(_('Your password has been changed'))
- tg.redirect(redirect_url)
-
-
- class UserRestController(TIMRestController):
- """
- CRUD Controller allowing to manage Users
- """
-
- password = UserPasswordRestController()
- workspaces = UserWorkspaceRestController()
-
- @classmethod
- def current_item_id_key_in_context(cls):
- return 'user_id'
-
- @tg.expose('tracim.templates.user_get_all')
- def get_all(self, *args, **kw):
- tg.redirect(self.url(None, 'me'))
- pass
-
- @tg.expose()
- def post(self, name, email, password, is_tracim_manager='off', is_pod_admin='off'):
- pass
-
- @tg.expose('tracim.templates.user_get_me')
- def get_one(self, user_id):
- user_id = tmpl_context.current_user.user_id
-
- current_user = tmpl_context.current_user
- assert user_id==current_user.user_id
- api = UserApi(current_user)
- current_user = api.get_one(current_user.user_id)
- dictified_user = Context(CTX.USER).toDict(current_user, 'user')
- current_user_content = Context(CTX.CURRENT_USER).toDict(tmpl_context.current_user)
- fake_api_content = DictLikeClass(current_user=current_user_content)
- fake_api = Context(CTX.WORKSPACE).toDict(fake_api_content)
-
- return DictLikeClass(result=dictified_user, fake_api=fake_api)
-
- @tg.expose('tracim.templates.user_edit_me')
- def edit(self, id, next_url=None):
- id = tmpl_context.current_user.user_id
- current_user = tmpl_context.current_user
- assert id==current_user.user_id
-
- dictified_user = Context(CTX.USER).toDict(current_user, 'user')
- fake_api = DictLikeClass(next_url=next_url)
- return DictLikeClass(result=dictified_user, fake_api=fake_api)
-
- @tg.expose('tracim.templates.workspace.edit')
- def put(self, user_id, name, email, next_url=None):
- user_id = tmpl_context.current_user.user_id
- current_user = tmpl_context.current_user
- assert user_id==current_user.user_id
-
- # Only keep allowed field update
- updated_fields = self._clean_update_fields({
- 'name': name,
- 'email': email
- })
-
- api = UserApi(tmpl_context.current_user)
- api.update(current_user, do_save=True, **updated_fields)
- tg.flash(_('profile updated.'))
- if next_url:
- tg.redirect(tg.url(next_url))
- tg.redirect(self.url())
-
- def _clean_update_fields(self, fields: dict):
- """
- Remove field key who are not allowed to be updated
- :param fields: dict with field name key to be cleaned
- :rtype fields: dict
- :return:
- """
- auth_instance = tg.config.get('auth_instance')
- if not auth_instance.is_internal:
- externalized_fields_names = auth_instance.managed_fields
- for externalized_field_name in externalized_fields_names:
- if externalized_field_name in fields:
- fields.pop(externalized_field_name)
- return fields
|