Show More
@@ -1,98 +1,100 b'' | |||||
1 | # Copyright (C) 2016-2017 RhodeCode GmbH |
|
1 | # Copyright (C) 2016-2017 RhodeCode GmbH | |
2 | # |
|
2 | # | |
3 | # This program is free software: you can redistribute it and/or modify |
|
3 | # This program is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
4 | # it under the terms of the GNU Affero General Public License, version 3 | |
5 | # (only), as published by the Free Software Foundation. |
|
5 | # (only), as published by the Free Software Foundation. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU Affero General Public License |
|
12 | # You should have received a copy of the GNU Affero General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | # |
|
14 | # | |
15 | # This program is dual-licensed. If you wish to learn more about the |
|
15 | # This program is dual-licensed. If you wish to learn more about the | |
16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
16 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
18 | import logging |
|
18 | import logging | |
19 |
|
19 | |||
20 | from datetime import datetime |
|
20 | from datetime import datetime | |
21 | from pyramid.threadlocal import get_current_request |
|
21 | from pyramid.threadlocal import get_current_request | |
22 | from rhodecode.lib.utils2 import AttributeDict |
|
22 | from rhodecode.lib.utils2 import AttributeDict | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | # this is a user object to be used for events caused by the system (eg. shell) |
|
25 | # this is a user object to be used for events caused by the system (eg. shell) | |
26 | SYSTEM_USER = AttributeDict(dict( |
|
26 | SYSTEM_USER = AttributeDict(dict( | |
27 | username='__SYSTEM__' |
|
27 | username='__SYSTEM__' | |
28 | )) |
|
28 | )) | |
29 |
|
29 | |||
30 | log = logging.getLogger(__name__) |
|
30 | log = logging.getLogger(__name__) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class RhodecodeEvent(object): |
|
33 | class RhodecodeEvent(object): | |
34 | """ |
|
34 | """ | |
35 | Base event class for all Rhodecode events |
|
35 | Base event class for all Rhodecode events | |
36 | """ |
|
36 | """ | |
37 | name = "RhodeCodeEvent" |
|
37 | name = "RhodeCodeEvent" | |
38 |
|
38 | |||
39 | def __init__(self): |
|
39 | def __init__(self): | |
40 | self.request = get_current_request() |
|
40 | self.request = get_current_request() | |
41 | self.utc_timestamp = datetime.utcnow() |
|
41 | self.utc_timestamp = datetime.utcnow() | |
42 |
|
42 | |||
43 | @property |
|
43 | @property | |
44 | def auth_user(self): |
|
44 | def auth_user(self): | |
45 | if not self.request: |
|
45 | if not self.request: | |
46 | return |
|
46 | return | |
47 |
|
47 | |||
48 | user = getattr(self.request, 'user', None) |
|
48 | user = getattr(self.request, 'user', None) | |
49 | if user: |
|
49 | if user: | |
50 | return user |
|
50 | return user | |
51 |
|
51 | |||
52 | api_user = getattr(self.request, 'rpc_user', None) |
|
52 | api_user = getattr(self.request, 'rpc_user', None) | |
53 | if api_user: |
|
53 | if api_user: | |
54 | return api_user |
|
54 | return api_user | |
55 |
|
55 | |||
56 | @property |
|
56 | @property | |
57 | def actor(self): |
|
57 | def actor(self): | |
58 | auth_user = self.auth_user |
|
58 | auth_user = self.auth_user | |
|
59 | ||||
59 | if auth_user: |
|
60 | if auth_user: | |
60 | instance = auth_user.get_instance() |
|
61 | instance = auth_user.get_instance() | |
61 | if not instance: |
|
62 | if not instance: | |
62 | return AttributeDict(dict( |
|
63 | return AttributeDict(dict( | |
63 | username=auth_user.username |
|
64 | username=auth_user.username | |
64 | )) |
|
65 | )) | |
|
66 | return instance | |||
65 |
|
67 | |||
66 | return SYSTEM_USER |
|
68 | return SYSTEM_USER | |
67 |
|
69 | |||
68 | @property |
|
70 | @property | |
69 | def actor_ip(self): |
|
71 | def actor_ip(self): | |
70 | auth_user = self.auth_user |
|
72 | auth_user = self.auth_user | |
71 | if auth_user: |
|
73 | if auth_user: | |
72 | return auth_user.ip_addr |
|
74 | return auth_user.ip_addr | |
73 | return '<no ip available>' |
|
75 | return '<no ip available>' | |
74 |
|
76 | |||
75 | @property |
|
77 | @property | |
76 | def server_url(self): |
|
78 | def server_url(self): | |
77 | default = '<no server_url available>' |
|
79 | default = '<no server_url available>' | |
78 | if self.request: |
|
80 | if self.request: | |
79 | from rhodecode.lib import helpers as h |
|
81 | from rhodecode.lib import helpers as h | |
80 | try: |
|
82 | try: | |
81 | return h.url('home', qualified=True) |
|
83 | return h.url('home', qualified=True) | |
82 | except Exception: |
|
84 | except Exception: | |
83 | log.exception('Failed to fetch URL for server') |
|
85 | log.exception('Failed to fetch URL for server') | |
84 | return default |
|
86 | return default | |
85 |
|
87 | |||
86 | return default |
|
88 | return default | |
87 |
|
89 | |||
88 | def as_dict(self): |
|
90 | def as_dict(self): | |
89 | data = { |
|
91 | data = { | |
90 | 'name': self.name, |
|
92 | 'name': self.name, | |
91 | 'utc_timestamp': self.utc_timestamp, |
|
93 | 'utc_timestamp': self.utc_timestamp, | |
92 | 'actor_ip': self.actor_ip, |
|
94 | 'actor_ip': self.actor_ip, | |
93 | 'actor': { |
|
95 | 'actor': { | |
94 | 'username': self.actor.username |
|
96 | 'username': self.actor.username | |
95 | }, |
|
97 | }, | |
96 | 'server_url': self.server_url |
|
98 | 'server_url': self.server_url | |
97 | } |
|
99 | } | |
98 | return data |
|
100 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now