@@ -7,6 +7,7 @@ package ldap
7
7
import (
8
8
"context"
9
9
"fmt"
10
+ "sort"
10
11
"strings"
11
12
12
13
"code.gitea.io/gitea/models"
@@ -17,7 +18,7 @@ import (
17
18
func (source * Source ) Sync (ctx context.Context , updateExisting bool ) error {
18
19
log .Trace ("Doing: SyncExternalUsers[%s]" , source .loginSource .Name )
19
20
20
- var existingUsers []int64
21
+ var existingUsers []int
21
22
isAttributeSSHPublicKeySet := len (strings .TrimSpace (source .AttributeSSHPublicKey )) > 0
22
23
var sshKeysNeedUpdate bool
23
24
@@ -34,6 +35,10 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
34
35
default :
35
36
}
36
37
38
+ sort .Slice (users , func (i , j int ) bool {
39
+ return users [i ].LowerName < users [j ].LowerName
40
+ })
41
+
37
42
sr , err := source .SearchEntries ()
38
43
if err != nil {
39
44
log .Error ("SyncExternalUsers LDAP source failure [%s], skipped" , source .loginSource .Name )
@@ -48,6 +53,12 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
48
53
log .Warn ("LDAP search found no entries but did not report an error. All users will be deactivated as per settings" )
49
54
}
50
55
56
+ sort .Slice (sr , func (i , j int ) bool {
57
+ return sr [i ].LowerName < sr [j ].LowerName
58
+ })
59
+
60
+ userPos := 0
61
+
51
62
for _ , su := range sr {
52
63
select {
53
64
case <- ctx .Done ():
@@ -71,12 +82,12 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
71
82
}
72
83
73
84
var usr * models.User
74
- // Search for existing user
75
- for _ , du := range users {
76
- if du . LowerName == strings . ToLower ( su . Username ) {
77
- usr = du
78
- break
79
- }
85
+ for userPos < len ( users ) && users [ userPos ]. LowerName < su . LowerName {
86
+ userPos ++
87
+ }
88
+ if userPos < len ( users ) && users [ userPos ]. LowerName == su . LowerName {
89
+ usr = users [ userPos ]
90
+ existingUsers = append ( existingUsers , userPos )
80
91
}
81
92
82
93
fullName := composeFullName (su .Name , su .Surname , su .Username )
@@ -85,7 +96,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
85
96
log .Trace ("SyncExternalUsers[%s]: Creating user %s" , source .loginSource .Name , su .Username )
86
97
87
98
usr = & models.User {
88
- LowerName : strings . ToLower ( su .Username ) ,
99
+ LowerName : su .LowerName ,
89
100
Name : su .Username ,
90
101
FullName : fullName ,
91
102
LoginType : source .loginSource .Type ,
@@ -108,8 +119,6 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
108
119
}
109
120
}
110
121
} else if updateExisting {
111
- existingUsers = append (existingUsers , usr .ID )
112
-
113
122
// Synchronize SSH Public Key if that attribute is set
114
123
if isAttributeSSHPublicKeySet && models .SynchronizePublicKeys (usr , source .loginSource , su .SSHPublicKey ) {
115
124
sshKeysNeedUpdate = true
@@ -161,15 +170,12 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error {
161
170
162
171
// Deactivate users not present in LDAP
163
172
if updateExisting {
164
- for _ , usr := range users {
165
- found := false
166
- for _ , uid := range existingUsers {
167
- if usr .ID == uid {
168
- found = true
169
- break
170
- }
173
+ existPos := 0
174
+ for i , usr := range users {
175
+ for existPos < len (existingUsers ) && i > existingUsers [existPos ] {
176
+ existPos ++
171
177
}
172
- if ! found {
178
+ if usr . IsActive && ( existPos >= len ( existingUsers ) || i < existingUsers [ existPos ]) {
173
179
log .Trace ("SyncExternalUsers[%s]: Deactivating user %s" , source .loginSource .Name , usr .Name )
174
180
175
181
usr .IsActive = false
0 commit comments