| 
				
			 | 
			
			
				@@ -1,22 +1,37 @@ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				1
			 | 
			
			
				+import typing 
			 | 
		
	
		
			
			| 
				1
			 | 
			
				2
			 | 
			
			
				 from pyramid.security import ALL_PERMISSIONS 
			 | 
		
	
		
			
			| 
				2
			 | 
			
				3
			 | 
			
			
				 from pyramid.security import Allow 
			 | 
		
	
		
			
			| 
				3
			 | 
			
				4
			 | 
			
			
				 from pyramid.security import Authenticated 
			 | 
		
	
		
			
			| 
				
			 | 
			
				5
			 | 
			
			
				+from tracim.lib.core.user import UserApi 
			 | 
		
	
		
			
			| 
				
			 | 
			
				6
			 | 
			
			
				+from tracim.models.auth import Group 
			 | 
		
	
		
			
			| 
				
			 | 
			
				7
			 | 
			
			
				+from tracim.lib.core.workspace import WorkspaceApi 
			 | 
		
	
		
			
			| 
				4
			 | 
			
				8
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				
			 | 
			
				9
			 | 
			
			
				+# INFO - G.M - 06-04-2018 - Auth for pyramid 
			 | 
		
	
		
			
			| 
				
			 | 
			
				10
			 | 
			
			
				+# based on this tutorial : https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/auth/basic.html  # nopep8 
			 | 
		
	
		
			
			| 
				5
			 | 
			
				11
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				6
			 | 
			
				
			 | 
			
			
				-def check_credentials(username, password, request): 
			 | 
		
	
		
			
			| 
				7
			 | 
			
				
			 | 
			
			
				-    if username == 'admin' and password == 'admin': 
			 | 
		
	
		
			
			| 
				8
			 | 
			
				
			 | 
			
			
				-        # an empty list is enough to indicate logged-in... watch how this 
			 | 
		
	
		
			
			| 
				9
			 | 
			
				
			 | 
			
			
				-        # affects the principals returned in the home view if you want to 
			 | 
		
	
		
			
			| 
				10
			 | 
			
				
			 | 
			
			
				-        # expand ACLs later 
			 | 
		
	
		
			
			| 
				11
			 | 
			
				
			 | 
			
			
				-        return ['g:admin'] 
			 | 
		
	
		
			
			| 
				12
			 | 
			
				
			 | 
			
			
				-    if username == 'user' and password == 'user': 
			 | 
		
	
		
			
			| 
				13
			 | 
			
				
			 | 
			
			
				-        return [] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				12
			 | 
			
			
				+ 
			 | 
		
	
		
			
			| 
				
			 | 
			
				13
			 | 
			
			
				+def check_credentials(username, password, request) -> typing.Optional[dict]: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				14
			 | 
			
			
				+    permissions = None 
			 | 
		
	
		
			
			| 
				
			 | 
			
				15
			 | 
			
			
				+    app_config = request.registry.settings['CFG'] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				16
			 | 
			
			
				+    uapi = UserApi(None, session=request.dbsession, config=app_config) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				17
			 | 
			
			
				+    try: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				18
			 | 
			
			
				+        user = uapi.get_one_by_email(username) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				19
			 | 
			
			
				+        if user.validate_password(password): 
			 | 
		
	
		
			
			| 
				
			 | 
			
				20
			 | 
			
			
				+            permissions = [] 
			 | 
		
	
		
			
			| 
				
			 | 
			
				21
			 | 
			
			
				+            for group in user.groups: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				22
			 | 
			
			
				+                permissions.append(group.group_name) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				23
			 | 
			
			
				+            # TODO - G.M - 06-04-2018 - Add workspace specific permission ? 
			 | 
		
	
		
			
			| 
				
			 | 
			
				24
			 | 
			
			
				+    # TODO - G.M - 06-04-2018 - Better catch for exception of bad password, bad 
			 | 
		
	
		
			
			| 
				
			 | 
			
				25
			 | 
			
			
				+    # user 
			 | 
		
	
		
			
			| 
				
			 | 
			
				26
			 | 
			
			
				+    except: 
			 | 
		
	
		
			
			| 
				
			 | 
			
				27
			 | 
			
			
				+        pass 
			 | 
		
	
		
			
			| 
				
			 | 
			
				28
			 | 
			
			
				+    return permissions 
			 | 
		
	
		
			
			| 
				14
			 | 
			
				29
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				15
			 | 
			
				30
			 | 
			
			
				  
			 | 
		
	
		
			
			| 
				16
			 | 
			
				31
			 | 
			
			
				 class Root: 
			 | 
		
	
		
			
			| 
				17
			 | 
			
				
			 | 
			
			
				-    # dead simple, give everyone who is logged in any permission 
			 | 
		
	
		
			
			| 
				18
			 | 
			
				
			 | 
			
			
				-    # (see the home_view for an example permission) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				32
			 | 
			
			
				+    # root 
			 | 
		
	
		
			
			| 
				19
			 | 
			
				33
			 | 
			
			
				     __acl__ = ( 
			 | 
		
	
		
			
			| 
				20
			 | 
			
				
			 | 
			
			
				-        (Allow, 'g:admin', ALL_PERMISSIONS), 
			 | 
		
	
		
			
			| 
				21
			 | 
			
				
			 | 
			
			
				-        (Allow, Authenticated, 'user'), 
			 | 
		
	
		
			
			| 
				22
			 | 
			
				
			 | 
			
			
				-    ) 
			 | 
		
	
		
			
			| 
				
			 | 
			
				34
			 | 
			
			
				+        (Allow, Group.TIM_ADMIN_GROUPNAME, ALL_PERMISSIONS), 
			 | 
		
	
		
			
			| 
				
			 | 
			
				35
			 | 
			
			
				+        (Allow, Group.TIM_MANAGER_GROUPNAME, 'manager'), 
			 | 
		
	
		
			
			| 
				
			 | 
			
				36
			 | 
			
			
				+        (Allow, Group.TIM_USER_GROUPNAME, 'user'), 
			 | 
		
	
		
			
			| 
				
			 | 
			
				37
			 | 
			
			
				+    ) 
			 |