11import re
22
33from django .conf import settings
4- from django .shortcuts import render_to_response
4+ from django .contrib .auth .decorators import login_required
5+ from ietf .ietfauth .utils import has_role , role_required
56
6- from ietf .ietfauth .utils import has_role
7-
87
98
109class SecAuthMiddleware (object ):
1110 """
12- Middleware component that performs custom auth check for every
13- request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
14-
15- Since authentication is performed externally at the apache level
16- REMOTE_USER should contain the name of the authenticated
17- user. If the user is a secretariat than access is granted.
18- Otherwise return a 401 error page.
11+ Middleware component that performs custom auth check for secretariat
12+ apps. request except those excluded by SECR_AUTH_UNRESTRICTED_URLS.
1913
2014 To use, add the class to MIDDLEWARE_CLASSES and define
2115 SECR_AUTH_UNRESTRICTED_URLS in your settings.py.
@@ -28,30 +22,32 @@ class SecAuthMiddleware(object):
2822
2923 Also sets custom request attributes:
3024 user_is_secretariat
31- user_is_chair
32- user_is_ad
33- )
34-
3525 """
3626
3727 def __init__ (self ):
3828 self .unrestricted = [re .compile (pattern ) for pattern in
3929 settings .SECR_AUTH_UNRESTRICTED_URLS ]
4030
31+ def is_unrestricted_url (self ,path ):
32+ for pattern in self .unrestricted :
33+ if pattern .match (path ):
34+ return True
35+ return False
36+
4137 def process_view (self , request , view_func , view_args , view_kwargs ):
42- # need to initialize user, it doesn't get set when running tests for example
43-
4438 if request .path .startswith ('/secr/' ):
45- request .user_is_secretariat = False
46-
47- if request .user .is_anonymous ():
48- return render_to_response ('401.html' )
49-
50- # do custom check
39+ # set custom request attribute
5140 if has_role (request .user , 'Secretariat' ):
5241 request .user_is_secretariat = True
53-
42+ else :
43+ request .user_is_secretariat = False
44+
45+ if request .path .startswith ('/secr/announcement/' ):
46+ return login_required (view_func )(request ,* view_args ,** view_kwargs )
47+ elif self .is_unrestricted_url (request .path ):
48+ return role_required ('WG Chair' ,'Secretariat' )(view_func )(request ,* view_args ,** view_kwargs )
49+ else :
50+ return role_required ('Secretariat' )(view_func )(request ,* view_args ,** view_kwargs )
51+ else :
5452 return None
55-
56- return None
5753
0 commit comments