11from django .conf import settings
2- from django .http import HttpResponseForbidden
3- from django . shortcuts import render_to_response
2+ from django .contrib . auth . decorators import login_required
3+ from ietf . ietfauth . utils import has_role , role_required
44
5- from ietf .ietfauth .utils import has_role
6-
75import re
86
97class SecAuthMiddleware (object ):
108 """
11- Middleware component that performs custom auth check for every
12- request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
13-
14- Since authentication is performed externally at the apache level
15- REMOTE_USER should contain the name of the authenticated
16- user. If the user is a secretariat than access is granted.
17- Otherwise return a 401 error page.
9+ Middleware component that performs custom auth check for secretariat
10+ apps. request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
1811
1912 To use, add the class to MIDDLEWARE_CLASSES and define
2013 SECR_AUTH_UNRESTRICTED_URLS in your settings.py.
@@ -27,30 +20,32 @@ class SecAuthMiddleware(object):
2720
2821 Also sets custom request attributes:
2922 user_is_secretariat
30- user_is_chair
31- user_is_ad
32- )
33-
3423 """
3524
3625 def __init__ (self ):
3726 self .unrestricted = [re .compile (pattern ) for pattern in
3827 settings .SECR_AUTH_UNRESTRICTED_URLS ]
3928
29+ def is_unrestricted_url (self ,path ):
30+ for pattern in self .unrestricted :
31+ if pattern .match (path ):
32+ return True
33+ return False
34+
4035 def process_view (self , request , view_func , view_args , view_kwargs ):
41- # need to initialize user, it doesn't get set when running tests for example
42-
4336 if request .path .startswith ('/secr/' ):
44- request .user_is_secretariat = False
45-
46- if request .user .is_anonymous ():
47- return render_to_response ('401.html' )
48-
49- # do custom check
37+ # set custom request attribute
5038 if has_role (request .user , 'Secretariat' ):
5139 request .user_is_secretariat = True
52-
40+ else :
41+ request .user_is_secretariat = False
42+
43+ if request .path .startswith ('/secr/announcement/' ):
44+ return login_required (view_func )(request ,* view_args ,** view_kwargs )
45+ elif self .is_unrestricted_url (request .path ):
46+ return role_required ('WG Chair' ,'Secretariat' )(view_func )(request ,* view_args ,** view_kwargs )
47+ else :
48+ return role_required ('Secretariat' )(view_func )(request ,* view_args ,** view_kwargs )
49+ else :
5350 return None
54-
55- return None
5651
0 commit comments