##// END OF EJS Templates
events: add event to catch permission changed so we can flush affected users permission caches
marcink -
r2849:21c71a09 default
parent child Browse files
Show More
@@ -0,0 +1,38 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2016-2018 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import logging
22
23 from rhodecode import events
24
25 log = logging.getLogger(__name__)
26
27
28 def trigger_user_permission_flush(event):
29 """
30 Subscriber to the `UserPermissionChange`. This triggers the
31 automatic flush of permission caches, so the users affected receive new permissions
32 Right Away
33 """
34 affected_user_ids = event.user_ids
35
36
37 def includeme(config):
38 config.add_subscriber(trigger_user_permission_flush, events.UserPermissionsChange)
@@ -420,5 +420,7 b' def includeme(config):'
420 config.add_route(name='admin_home', pattern=ADMIN_PREFIX)
420 config.add_route(name='admin_home', pattern=ADMIN_PREFIX)
421 config.include(admin_routes, route_prefix=ADMIN_PREFIX)
421 config.include(admin_routes, route_prefix=ADMIN_PREFIX)
422
422
423 config.include('.subscribers')
424
423 # Scan module for configuration decorators.
425 # Scan module for configuration decorators.
424 config.scan('.views', ignore='.tests')
426 config.scan('.views', ignore='.tests')
@@ -23,6 +23,7 b' import logging'
23 from pyramid.view import view_config
23 from pyramid.view import view_config
24 from pyramid.httpexceptions import HTTPFound
24 from pyramid.httpexceptions import HTTPFound
25
25
26 from rhodecode import events
26 from rhodecode.apps._base import RepoGroupAppView
27 from rhodecode.apps._base import RepoGroupAppView
27 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import audit_logger
29 from rhodecode.lib import audit_logger
@@ -95,6 +96,14 b' class RepoGroupPermissionsView(RepoGroup'
95
96
96 Session().commit()
97 Session().commit()
97 h.flash(_('Repository Group permissions updated'), category='success')
98 h.flash(_('Repository Group permissions updated'), category='success')
99
100 affected_user_ids = []
101 for change in changes['added'] + changes['updated'] + changes['deleted']:
102 if change['type'] == 'user':
103 affected_user_ids.append(change['id'])
104
105 events.trigger(events.UserPermissionsChange(affected_user_ids))
106
98 raise HTTPFound(
107 raise HTTPFound(
99 h.route_path('edit_repo_group_perms',
108 h.route_path('edit_repo_group_perms',
100 repo_group_name=self.db_repo_group_name))
109 repo_group_name=self.db_repo_group_name))
@@ -23,6 +23,7 b' import logging'
23 from pyramid.httpexceptions import HTTPFound
23 from pyramid.httpexceptions import HTTPFound
24 from pyramid.view import view_config
24 from pyramid.view import view_config
25
25
26 from rhodecode import events
26 from rhodecode.apps._base import RepoAppView
27 from rhodecode.apps._base import RepoAppView
27 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import audit_logger
29 from rhodecode.lib import audit_logger
@@ -39,8 +40,6 b' class RepoSettingsPermissionsView(RepoAp'
39
40
40 def load_default_context(self):
41 def load_default_context(self):
41 c = self._get_local_tmpl_context()
42 c = self._get_local_tmpl_context()
42
43
44 return c
43 return c
45
44
46 @LoginRequired()
45 @LoginRequired()
@@ -85,5 +84,12 b' class RepoSettingsPermissionsView(RepoAp'
85 Session().commit()
84 Session().commit()
86 h.flash(_('Repository permissions updated'), category='success')
85 h.flash(_('Repository permissions updated'), category='success')
87
86
87 affected_user_ids = []
88 for change in changes['added'] + changes['updated'] + changes['deleted']:
89 if change['type'] == 'user':
90 affected_user_ids.append(change['id'])
91
92 events.trigger(events.UserPermissionsChange(affected_user_ids))
93
88 raise HTTPFound(
94 raise HTTPFound(
89 h.route_path('edit_repo_perms', repo_name=self.db_repo_name))
95 h.route_path('edit_repo_perms', repo_name=self.db_repo_name))
@@ -28,6 +28,7 b' from pyramid.view import view_config'
28 from pyramid.response import Response
28 from pyramid.response import Response
29 from pyramid.renderers import render
29 from pyramid.renderers import render
30
30
31 from rhodecode import events
31 from rhodecode.lib.exceptions import (
32 from rhodecode.lib.exceptions import (
32 RepoGroupAssignmentError, UserGroupAssignedException)
33 RepoGroupAssignmentError, UserGroupAssignedException)
33 from rhodecode.model.forms import (
34 from rhodecode.model.forms import (
@@ -183,6 +184,12 b' class UserGroupsView(UserGroupAppView):'
183
184
184 h.flash(_('Updated user group %s') % updated_user_group,
185 h.flash(_('Updated user group %s') % updated_user_group,
185 category='success')
186 category='success')
187
188 affected_user_ids = []
189 for user_id in added_members + removed_members:
190 affected_user_ids.append(user_id)
191 events.trigger(events.UserPermissionsChange(affected_user_ids))
192
186 Session().commit()
193 Session().commit()
187 except formencode.Invalid as errors:
194 except formencode.Invalid as errors:
188 defaults = errors.value
195 defaults = errors.value
@@ -354,6 +361,14 b' class UserGroupsView(UserGroupAppView):'
354
361
355 Session().commit()
362 Session().commit()
356 h.flash(_('User Group permissions updated'), category='success')
363 h.flash(_('User Group permissions updated'), category='success')
364
365 affected_user_ids = []
366 for change in changes['added'] + changes['updated'] + changes['deleted']:
367 if change['type'] == 'user':
368 affected_user_ids.append(change['id'])
369
370 events.trigger(events.UserPermissionsChange(affected_user_ids))
371
357 raise HTTPFound(
372 raise HTTPFound(
358 h.route_path('edit_user_group_perms', user_group_id=user_group_id))
373 h.route_path('edit_user_group_perms', user_group_id=user_group_id))
359
374
@@ -49,11 +49,13 b' def trigger(event, registry=None):'
49 if isinstance(event, RhodecodeEvent):
49 if isinstance(event, RhodecodeEvent):
50 integrations_event_handler(event)
50 integrations_event_handler(event)
51
51
52
52 from rhodecode.events.user import ( # noqa
53 from rhodecode.events.user import ( # noqa
53 UserPreCreate,
54 UserPreCreate,
54 UserPostCreate,
55 UserPostCreate,
55 UserPreUpdate,
56 UserPreUpdate,
56 UserRegistered
57 UserRegistered,
58 UserPermissionsChange,
57 )
59 )
58
60
59 from rhodecode.events.repo import ( # noqa
61 from rhodecode.events.repo import ( # noqa
@@ -83,3 +83,22 b' class UserPreUpdate(RhodecodeEvent):'
83 super(UserPreUpdate, self).__init__()
83 super(UserPreUpdate, self).__init__()
84 self.user = user
84 self.user = user
85 self.user_data = user_data
85 self.user_data = user_data
86
87
88 class UserPermissionsChange(RhodecodeEvent):
89 """
90 This event should be triggered on an event that permissions of user might changed.
91 Currently this should be triggered on:
92
93 - user added/removed from user group
94 - repo permissions changed
95 - repo group permissions changed
96 - user group permissions changed
97
98 """
99 name = 'user-permissions-change'
100 display_name = lazy_ugettext('user permissions change')
101
102 def __init__(self, user_ids):
103 super(UserPermissionsChange, self).__init__()
104 self.user_ids = user_ids
@@ -102,6 +102,7 b' def add_request_user_context(event):'
102 request.environ['rc_auth_user'] = auth_user
102 request.environ['rc_auth_user'] = auth_user
103 request.environ['rc_req_id'] = req_id
103 request.environ['rc_req_id'] = req_id
104
104
105
105 def inject_app_settings(event):
106 def inject_app_settings(event):
106 settings = event.app.registry.settings
107 settings = event.app.registry.settings
107 # inject info about available permissions
108 # inject info about available permissions
General Comments 0
You need to be logged in to leave comments. Login now