|
@@ -0,0 +1,168 @@
|
|
1
|
+# -*- coding: utf-8 -*-
|
|
2
|
+import threading
|
|
3
|
+
|
|
4
|
+import transaction
|
|
5
|
+import typing as typing
|
|
6
|
+
|
|
7
|
+from tracim.models.auth import User
|
|
8
|
+
|
|
9
|
+# TODO - G.M -28-03-2018 - Check if "current user" stuff is always needed for tracimv2
|
|
10
|
+# CURRENT_USER_WEB = 'WEB'
|
|
11
|
+# CURRENT_USER_WSGIDAV = 'WSGIDAV'
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+class UserApi(object):
|
|
15
|
+
|
|
16
|
+ def __init__(self, current_user: User, session, config):
|
|
17
|
+ self._session = session
|
|
18
|
+ self._user = current_user
|
|
19
|
+ self._config = config
|
|
20
|
+
|
|
21
|
+ def get_all(self):
|
|
22
|
+ return self._session.query(User).order_by(User.display_name).all()
|
|
23
|
+
|
|
24
|
+ def _base_query(self):
|
|
25
|
+ return self._session.query(User)
|
|
26
|
+
|
|
27
|
+ def get_one(self, user_id: int):
|
|
28
|
+ return self._base_query().filter(User.user_id==user_id).one()
|
|
29
|
+
|
|
30
|
+ def get_one_by_email(self, email: str):
|
|
31
|
+ return self._base_query().filter(User.email==email).one()
|
|
32
|
+
|
|
33
|
+ def get_one_by_id(self, id: int) -> User:
|
|
34
|
+ return self._base_query().filter(User.user_id==id).one()
|
|
35
|
+
|
|
36
|
+ def update(
|
|
37
|
+ self,
|
|
38
|
+ user: User,
|
|
39
|
+ name: str=None,
|
|
40
|
+ email: str=None,
|
|
41
|
+ do_save=True,
|
|
42
|
+ timezone: str='',
|
|
43
|
+ ):
|
|
44
|
+ if name is not None:
|
|
45
|
+ user.display_name = name
|
|
46
|
+
|
|
47
|
+ if email is not None:
|
|
48
|
+ user.email = email
|
|
49
|
+
|
|
50
|
+ user.timezone = timezone
|
|
51
|
+
|
|
52
|
+ if do_save:
|
|
53
|
+ self.save(user)
|
|
54
|
+
|
|
55
|
+ if email and self._user and user.user_id==self._user.user_id:
|
|
56
|
+ pass
|
|
57
|
+ # this is required for the _session to keep on being up-to-date
|
|
58
|
+ # TODO - G.M - 28-03-2018 - Check for pyramid equivalent
|
|
59
|
+ # tg.request.identity['repoze.who.userid'] = email
|
|
60
|
+ # tg.auth_force_login(email)
|
|
61
|
+
|
|
62
|
+ def user_with_email_exists(self, email: str):
|
|
63
|
+ try:
|
|
64
|
+ self.get_one_by_email(email)
|
|
65
|
+ return True
|
|
66
|
+ except:
|
|
67
|
+ return False
|
|
68
|
+
|
|
69
|
+ def create_user(self, email=None, groups=[], save_now=False) -> User:
|
|
70
|
+ user = User()
|
|
71
|
+
|
|
72
|
+ if email:
|
|
73
|
+ user.email = email
|
|
74
|
+
|
|
75
|
+ for group in groups:
|
|
76
|
+ user.groups.append(group)
|
|
77
|
+
|
|
78
|
+ self._session.add(user)
|
|
79
|
+
|
|
80
|
+ if save_now:
|
|
81
|
+ self._session.flush()
|
|
82
|
+
|
|
83
|
+ return user
|
|
84
|
+
|
|
85
|
+ def save(self, user: User):
|
|
86
|
+ self._session.flush()
|
|
87
|
+
|
|
88
|
+ def execute_created_user_actions(self, created_user: User) -> None:
|
|
89
|
+ """
|
|
90
|
+ Execute actions when user just been created
|
|
91
|
+ :return:
|
|
92
|
+ """
|
|
93
|
+ # NOTE: Cyclic import
|
|
94
|
+ # TODO - G.M - 28-03-2018 - Reenable Calendar stuff
|
|
95
|
+ #from tracim.lib.calendar import CalendarManager
|
|
96
|
+ #from tracim.model.organisational import UserCalendar
|
|
97
|
+
|
|
98
|
+ created_user.ensure_auth_token(dbsession=self._session, validity_seconds=self._config.USER_AUTH_TOKEN_VALIDITY)
|
|
99
|
+
|
|
100
|
+ # Ensure database is up-to-date
|
|
101
|
+ self._session.flush()
|
|
102
|
+ transaction.commit()
|
|
103
|
+
|
|
104
|
+ # calendar_manager = CalendarManager(created_user)
|
|
105
|
+ # calendar_manager.create_then_remove_fake_event(
|
|
106
|
+ # calendar_class=UserCalendar,
|
|
107
|
+ # related_object_id=created_user.user_id,
|
|
108
|
+ # )
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+# class CurrentUserGetterInterface(object):
|
|
112
|
+# def get_current_user(self) -> typing.Union[None, User]:
|
|
113
|
+# raise NotImplementedError()
|
|
114
|
+#
|
|
115
|
+#
|
|
116
|
+# class BaseCurrentUserGetter(CurrentUserGetterInterface):
|
|
117
|
+# def __init__(self) -> None:
|
|
118
|
+# self.api = UserApi(None)
|
|
119
|
+
|
|
120
|
+# TODO - G.M - 28-03-2018 - Check for pyramid equivalent
|
|
121
|
+# class WebCurrentUserGetter(BaseCurrentUserGetter):
|
|
122
|
+# def get_current_user(self) -> typing.Union[None, User]:
|
|
123
|
+# # HACK - D.A. - 2015-09-02
|
|
124
|
+# # In tests, the tg.request.identity may not be set
|
|
125
|
+# # (this is a buggy case, but for now this is how the software is;)
|
|
126
|
+# if tg.request is not None:
|
|
127
|
+# if hasattr(tg.request, 'identity'):
|
|
128
|
+# if tg.request.identity is not None:
|
|
129
|
+# return self.api.get_one_by_email(
|
|
130
|
+# tg.request.identity['repoze.who.userid'],
|
|
131
|
+# )
|
|
132
|
+#
|
|
133
|
+# return None
|
|
134
|
+
|
|
135
|
+# TODO - G.M - 28-03-2018 - Reenable Webdav stuff
|
|
136
|
+# class WsgidavCurrentUserGetter(BaseCurrentUserGetter):
|
|
137
|
+# def get_current_user(self) -> typing.Union[None, User]:
|
|
138
|
+# if hasattr(cherrypy.request, 'current_user_email'):
|
|
139
|
+# return self.api.get_one_by_email(
|
|
140
|
+# cherrypy.request.current_user_email,
|
|
141
|
+# )
|
|
142
|
+#
|
|
143
|
+# return None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+# class CurrentUserGetterApi(object):
|
|
147
|
+# thread_local = threading.local()
|
|
148
|
+# matches = {
|
|
149
|
+# CURRENT_USER_WEB: WebCurrentUserGetter,
|
|
150
|
+# CURRENT_USER_WSGIDAV: WsgidavCurrentUserGetter,
|
|
151
|
+# }
|
|
152
|
+# default = CURRENT_USER_WEB
|
|
153
|
+#
|
|
154
|
+# @classmethod
|
|
155
|
+# def get_current_user(cls) -> User:
|
|
156
|
+# try:
|
|
157
|
+# return cls.thread_local.getter.get_current_user()
|
|
158
|
+# except AttributeError:
|
|
159
|
+# return cls.factory(cls.default).get_current_user()
|
|
160
|
+#
|
|
161
|
+# @classmethod
|
|
162
|
+# def set_thread_local_getter(cls, name) -> None:
|
|
163
|
+# if not hasattr(cls.thread_local, 'getter'):
|
|
164
|
+# cls.thread_local.getter = cls.factory(name)
|
|
165
|
+#
|
|
166
|
+# @classmethod
|
|
167
|
+# def factory(cls, name: str) -> CurrentUserGetterInterface:
|
|
168
|
+# return cls.matches[name]()
|