Browse Source

Enable admin to search by email (#2888)

Ethan Koenig 1 year ago
parent
commit
716ac1214f
2 changed files with 16 additions and 10 deletions
  1. 13 8
      models/user.go
  2. 3 2
      routers/admin/users.go

+ 13 - 8
models/user.go

@@ -1271,22 +1271,27 @@ func GetUser(user *User) (bool, error) {
1271 1271
 
1272 1272
 // SearchUserOptions contains the options for searching
1273 1273
 type SearchUserOptions struct {
1274
-	Keyword  string
1275
-	Type     UserType
1276
-	OrderBy  string
1277
-	Page     int
1278
-	PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
1279
-	IsActive util.OptionalBool
1274
+	Keyword       string
1275
+	Type          UserType
1276
+	OrderBy       string
1277
+	Page          int
1278
+	PageSize      int // Can be smaller than or equal to setting.UI.ExplorePagingNum
1279
+	IsActive      util.OptionalBool
1280
+	SearchByEmail bool // Search by email as well as username/full name
1280 1281
 }
1281 1282
 
1282 1283
 func (opts *SearchUserOptions) toConds() builder.Cond {
1283 1284
 	var cond builder.Cond = builder.Eq{"type": opts.Type}
1284 1285
 	if len(opts.Keyword) > 0 {
1285 1286
 		lowerKeyword := strings.ToLower(opts.Keyword)
1286
-		cond = cond.And(builder.Or(
1287
+		keywordCond := builder.Or(
1287 1288
 			builder.Like{"lower_name", lowerKeyword},
1288 1289
 			builder.Like{"LOWER(full_name)", lowerKeyword},
1289
-		))
1290
+		)
1291
+		if opts.SearchByEmail {
1292
+			keywordCond = keywordCond.Or(builder.Like{"LOWER(email)", lowerKeyword})
1293
+		}
1294
+		cond = cond.And(keywordCond)
1290 1295
 	}
1291 1296
 
1292 1297
 	if !opts.IsActive.IsNone() {

+ 3 - 2
routers/admin/users.go

@@ -31,8 +31,9 @@ func Users(ctx *context.Context) {
31 31
 	ctx.Data["PageIsAdminUsers"] = true
32 32
 
33 33
 	routers.RenderUserSearch(ctx, &models.SearchUserOptions{
34
-		Type:     models.UserTypeIndividual,
35
-		PageSize: setting.UI.Admin.UserPagingNum,
34
+		Type:          models.UserTypeIndividual,
35
+		PageSize:      setting.UI.Admin.UserPagingNum,
36
+		SearchByEmail: true,
36 37
 	}, tplUsers)
37 38
 }
38 39