2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								package  api  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  (  
						 
					
						
							
								
									
										
										
										
											2021-11-08 22:53:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-19 20:34:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"errors" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"fmt" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"net/http" 
							 
						 
					
						
							
								
									
										
										
										
											2022-01-15 00:55:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"strconv" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/api/response" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/bus" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/infra/log" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/login" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/models" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/services/ldap" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/services/multildap" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/util" 
							 
						 
					
						
							
								
									
										
										
										
											2021-10-11 20:30:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/grafana/grafana/pkg/web" 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									getLDAPConfig  =  multildap . GetConfig 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									newLDAP        =  multildap . New 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-15 16:32:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapLogger  =  log . New ( "LDAP.debug" ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									errOrganizationNotFound  =  func ( orgId  int64 )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-05 20:07:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "unable to find organization with ID '%d'" ,  orgId ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// LDAPAttribute is a serializer for user attributes mapped from LDAP. Is meant to display both the serialized value and the LDAP key we received it from.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  LDAPAttribute  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ConfigAttributeValue  string  ` json:"cfgAttrValue" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									LDAPAttributeValue    string  ` json:"ldapValue" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// RoleDTO is a serializer for mapped roles from LDAP
  
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  LDAPRoleDTO  struct  {  
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									OrgId    int64            ` json:"orgId" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									OrgName  string           ` json:"orgName" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									OrgRole  models . RoleType  ` json:"orgRole" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									GroupDN  string           ` json:"groupDN" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// LDAPUserDTO is a serializer for users mapped from LDAP
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  LDAPUserDTO  struct  {  
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Name            * LDAPAttribute            ` json:"name" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Surname         * LDAPAttribute            ` json:"surname" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Email           * LDAPAttribute            ` json:"email" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Username        * LDAPAttribute            ` json:"login" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									IsGrafanaAdmin  * bool                     ` json:"isGrafanaAdmin" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									IsDisabled      bool                      ` json:"isDisabled" ` 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									OrgRoles        [ ] LDAPRoleDTO             ` json:"roles" ` 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									Teams           [ ] models . TeamOrgGroupDTO  ` json:"teams" ` 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// LDAPServerDTO is a serializer for LDAP server statuses
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  LDAPServerDTO  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Host       string  ` json:"host" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Port       int     ` json:"port" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Available  bool    ` json:"available" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Error      string  ` json:"error" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// FetchOrgs fetches the organization(s) information by executing a single query to the database. Then, populating the DTO with the information retrieved.
  
						 
					
						
							
								
									
										
										
										
											2021-11-25 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( user  * LDAPUserDTO )  FetchOrgs ( ctx  context . Context )  error  {  
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									orgIds  :=  [ ] int64 { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  or  :=  range  user . OrgRoles  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										orgIds  =  append ( orgIds ,  or . OrgId ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									q  :=  & models . SearchOrgsQuery { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									q . Ids  =  orgIds 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-29 00:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  bus . Dispatch ( ctx ,  q ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									orgNamesById  :=  map [ int64 ] string { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  org  :=  range  q . Result  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										orgNamesById [ org . Id ]  =  org . Name 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  i ,  orgDTO  :=  range  user . OrgRoles  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  orgDTO . OrgId  <  1  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										orgName  :=  orgNamesById [ orgDTO . OrgId ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  orgName  !=  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											user . OrgRoles [ i ] . OrgName  =  orgName 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  errOrganizationNotFound ( orgDTO . OrgId ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// ReloadLDAPCfg reloads the LDAP configuration
  
						 
					
						
							
								
									
										
										
										
											2021-11-29 17:18:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( hs  * HTTPServer )  ReloadLDAPCfg ( c  * models . ReqContext )  response . Response  {  
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  ! ldap . IsEnabled ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "LDAP is not enabled" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									err  :=  ldap . ReloadConfig ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusInternalServerError ,  "Failed to reload LDAP config" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  response . Success ( "LDAP config reloaded" ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-30 03:37:21 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// GetLDAPStatus attempts to connect to all the configured LDAP servers and returns information on whenever they're available or not.
  
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( hs  * HTTPServer )  GetLDAPStatus ( c  * models . ReqContext )  response . Response  {  
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! ldap . IsEnabled ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "LDAP is not enabled" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-11 18:44:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapConfig ,  err  :=  getLDAPConfig ( hs . Cfg ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Failed to obtain the LDAP configuration. Please verify the configuration and try again" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ldap  :=  newLDAP ( ldapConfig . Servers ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-16 23:56:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ldap  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusInternalServerError ,  "Failed to find the LDAP server" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-16 23:56:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									statuses ,  err  :=  ldap . Ping ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Failed to connect to the LDAP server(s)" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									serverDTOs  :=  [ ] * LDAPServerDTO { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  status  :=  range  statuses  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										s  :=  & LDAPServerDTO { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Host :       status . Host , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Available :  status . Available , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Port :       status . Port , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  status . Error  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											s . Error  =  status . Error . Error ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										serverDTOs  =  append ( serverDTOs ,  s ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  response . JSON ( http . StatusOK ,  serverDTOs ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// PostSyncUserWithLDAP enables a single Grafana user to be synchronized against LDAP
  
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( hs  * HTTPServer )  PostSyncUserWithLDAP ( c  * models . ReqContext )  response . Response  {  
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! ldap . IsEnabled ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "LDAP is not enabled" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-11 18:44:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapConfig ,  err  :=  getLDAPConfig ( hs . Cfg ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Failed to obtain the LDAP configuration. Please verify the configuration and try again" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-15 00:55:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									userId ,  err  :=  strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] ,  10 ,  64 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "id is invalid" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									query  :=  models . GetUserByIdQuery { Id :  userId } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-29 00:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  bus . Dispatch ( c . Req . Context ( ) ,  & query ) ;  err  !=  nil  {  // validate the userId exists
 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-19 20:34:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  errors . Is ( err ,  models . ErrUserNotFound )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  response . Error ( 404 ,  models . ErrUserNotFound . Error ( ) ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( 500 ,  "Failed to get user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									authModuleQuery  :=  & models . GetAuthInfoQuery { UserId :  query . Result . Id ,  AuthModule :  models . AuthModuleLDAP } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-29 00:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  bus . Dispatch ( c . Req . Context ( ) ,  authModuleQuery ) ;  err  !=  nil  {  // validate the userId comes from LDAP
 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-19 20:34:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  errors . Is ( err ,  models . ErrUserNotFound )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  response . Error ( 404 ,  models . ErrUserNotFound . Error ( ) ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( 500 ,  "Failed to get user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ldapServer  :=  newLDAP ( ldapConfig . Servers ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									user ,  _ ,  err  :=  ldapServer . User ( query . Result . Login ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-19 20:34:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  errors . Is ( err ,  multildap . ErrDidNotFindUser )  {  // User was not in the LDAP server - we need to take action:
 
							 
						 
					
						
							
								
									
										
										
										
											2020-12-16 02:09:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  hs . Cfg . AdminUser  ==  query . Result . Login  {  // User is *the* Grafana Admin. We cannot disable it.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												errMsg  :=  fmt . Sprintf ( ` Refusing to sync grafana super admin "%s" - it would be disabled ` ,  query . Result . Login ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-12-15 16:32:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												ldapLogger . Error ( errMsg ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  response . Error ( http . StatusBadRequest ,  errMsg ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// Since the user was not in the LDAP server. Let's disable it.
 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-25 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											err  :=  login . DisableExternalUser ( c . Req . Context ( ) ,  query . Result . Login ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  response . Error ( http . StatusInternalServerError ,  "Failed to disable the user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-05 22:37:11 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											err  =  hs . AuthTokenService . RevokeAllUserTokens ( c . Req . Context ( ) ,  userId ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  response . Error ( http . StatusInternalServerError ,  "Failed to remove session tokens for the user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											return  response . Error ( http . StatusBadRequest ,  "User not found in LDAP. Disabled the user without updating information" ,  nil )  // should this be a success?
 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-11-07 21:31:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-15 16:32:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ldapLogger . Debug ( "Failed to sync the user with LDAP" ,  "err" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Something went wrong while finding the user in LDAP" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									upsertCmd  :=  & models . UpsertUserCommand { 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-06 14:49:58 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ReqContext :     c , 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ExternalUser :   user , 
							 
						 
					
						
							
								
									
										
										
										
											2020-12-11 18:44:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										SignupAllowed :  hs . Cfg . LDAPAllowSignup , 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-12-29 00:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  bus . Dispatch ( c . Req . Context ( ) ,  upsertCmd ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusInternalServerError ,  "Failed to update the user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  response . Success ( "User synced successfully" ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-13 23:26:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// GetUserFromLDAP finds an user based on a username in LDAP. This helps illustrate how would the particular user be mapped in Grafana when synced.
  
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( hs  * HTTPServer )  GetUserFromLDAP ( c  * models . ReqContext )  response . Response  {  
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  ! ldap . IsEnabled ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "LDAP is not enabled" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 22:29:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-11 18:44:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapConfig ,  err  :=  getLDAPConfig ( hs . Cfg ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Failed to obtain the LDAP configuration" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ldap  :=  newLDAP ( ldapConfig . Servers ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-10-11 20:30:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									username  :=  web . Params ( c . Req ) [ ":username" ] 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  len ( username )  ==  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Validation error. You must specify an username" ,  nil ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									user ,  serverConfig ,  err  :=  ldap . User ( username ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  user  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusNotFound ,  "No user was found in the LDAP server(s) with that username" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-15 16:32:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapLogger . Debug ( "user found" ,  "user" ,  user ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									name ,  surname  :=  splitName ( user . Name ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									u  :=  & LDAPUserDTO { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Name :            & LDAPAttribute { serverConfig . Attr . Name ,  name } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Surname :         & LDAPAttribute { serverConfig . Attr . Surname ,  surname } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Email :           & LDAPAttribute { serverConfig . Attr . Email ,  user . Email } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Username :        & LDAPAttribute { serverConfig . Attr . Username ,  user . Login } , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										IsGrafanaAdmin :  user . IsGrafanaAdmin , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										IsDisabled :      user . IsDisabled , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									orgRoles  :=  [ ] LDAPRoleDTO { } 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-11-01 22:42:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Need to iterate based on the config groups as only the first match for an org is used
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// We are showing all matches as that should help in understanding why one match wins out
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// over another.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  configGroup  :=  range  serverConfig . Groups  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  userGroup  :=  range  user . Groups  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  configGroup . GroupDN  ==  userGroup  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												r  :=  & LDAPRoleDTO { GroupDN :  configGroup . GroupDN ,  OrgId :  configGroup . OrgId ,  OrgRole :  configGroup . OrgRole } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												orgRoles  =  append ( orgRoles ,  * r ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												break 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-11-01 22:42:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										//}
 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Then, we find what we did not match by inspecting the list of groups returned from
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// LDAP against what we have already matched above.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									for  _ ,  userGroup  :=  range  user . Groups  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-11-01 22:42:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										var  matched  bool 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  _ ,  orgRole  :=  range  orgRoles  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  orgRole . GroupDN  ==  userGroup  {  // we already matched it
 
							 
						 
					
						
							
								
									
										
										
										
											2019-11-01 22:42:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												matched  =  true 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												break 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-11-01 22:42:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  ! matched  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-19 23:13:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											r  :=  & LDAPRoleDTO { GroupDN :  userGroup } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											orgRoles  =  append ( orgRoles ,  * r ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									u . OrgRoles  =  orgRoles 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-12-15 16:32:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ldapLogger . Debug ( "mapping org roles" ,  "orgsRoles" ,  u . OrgRoles ) 
							 
						 
					
						
							
								
									
										
										
										
											2021-11-25 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  u . FetchOrgs ( c . Req . Context ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "An organization was not found - Please verify your LDAP configuration" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cmd  :=  & models . GetTeamsForLDAPGroupCommand { Groups :  user . Groups } 
							 
						 
					
						
							
								
									
										
										
										
											2021-12-29 00:36:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									err  =  bus . Dispatch ( c . Req . Context ( ) ,  cmd ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-19 20:34:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  &&  ! errors . Is ( err ,  bus . ErrHandlerNotFound )  { 
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  response . Error ( http . StatusBadRequest ,  "Unable to find the teams for this user" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-08 18:48:47 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									u . Teams  =  cmd . Result 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-01-15 21:43:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  response . JSON ( 200 ,  u ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-09-04 01:34:44 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// splitName receives the full name of a user and splits it into two parts: A name and a surname.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  splitName ( name  string )  ( string ,  string )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									names  :=  util . SplitString ( name ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									switch  len ( names )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									case  0 : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  "" ,  "" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									case  1 : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  names [ 0 ] ,  "" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  names [ 0 ] ,  names [ 1 ] 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}