@@ -16,11 +16,9 @@ import (
16
16
"os"
17
17
"path/filepath"
18
18
"regexp"
19
- "strconv"
20
19
"strings"
21
20
"time"
22
21
"unicode/utf8"
23
- "xorm.io/xorm"
24
22
25
23
"code.gitea.io/gitea/modules/base"
26
24
"code.gitea.io/gitea/modules/git"
@@ -35,7 +33,9 @@ import (
35
33
"golang.org/x/crypto/bcrypt"
36
34
"golang.org/x/crypto/pbkdf2"
37
35
"golang.org/x/crypto/scrypt"
36
+
38
37
"xorm.io/builder"
38
+ "xorm.io/xorm"
39
39
)
40
40
41
41
// UserType defines the user type
@@ -1590,15 +1590,19 @@ func GetUser(user *User) (bool, error) {
1590
1590
// SearchUserOptions contains the options for searching
1591
1591
type SearchUserOptions struct {
1592
1592
ListOptions
1593
- Keyword string
1594
- StatusFilterMap map [string ]string // Admin can apply advanced search filters
1595
- Type UserType
1596
- UID int64
1597
- OrderBy SearchOrderBy
1598
- Visible []structs.VisibleType
1599
- Actor * User // The user doing the search
1600
- IsActive util.OptionalBool
1601
- SearchByEmail bool // Search by email as well as username/full name
1593
+ Keyword string
1594
+ Type UserType
1595
+ UID int64
1596
+ OrderBy SearchOrderBy
1597
+ Visible []structs.VisibleType
1598
+ Actor * User // The user doing the search
1599
+ SearchByEmail bool // Search by email as well as username/full name
1600
+
1601
+ IsActive util.OptionalBool
1602
+ IsAdmin util.OptionalBool
1603
+ IsRestricted util.OptionalBool
1604
+ IsTwoFactorEnabled util.OptionalBool
1605
+ IsProhibitLogin util.OptionalBool
1602
1606
}
1603
1607
1604
1608
func (opts * SearchUserOptions ) toConds () builder.Cond {
@@ -1640,6 +1644,7 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
1640
1644
accessCond = accessCond .Or (builder.Eq {"id" : opts .Actor .ID })
1641
1645
cond = cond .And (accessCond )
1642
1646
}
1647
+
1643
1648
} else {
1644
1649
// Force visibility for privacy
1645
1650
// Not logged in - only public users
@@ -1654,6 +1659,18 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
1654
1659
cond = cond .And (builder.Eq {"is_active" : opts .IsActive .IsTrue ()})
1655
1660
}
1656
1661
1662
+ if ! opts .IsAdmin .IsNone () {
1663
+ cond = cond .And (builder.Eq {"is_admin" : opts .IsAdmin .IsTrue ()})
1664
+ }
1665
+
1666
+ if ! opts .IsRestricted .IsNone () {
1667
+ cond = cond .And (builder.Eq {"is_restricted" : opts .IsRestricted .IsTrue ()})
1668
+ }
1669
+
1670
+ if ! opts .IsProhibitLogin .IsNone () {
1671
+ cond = cond .And (builder.Eq {"prohibit_login" : opts .IsProhibitLogin .IsTrue ()})
1672
+ }
1673
+
1657
1674
return cond
1658
1675
}
1659
1676
@@ -1662,38 +1679,14 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
1662
1679
func SearchUsers (opts * SearchUserOptions ) (users []* User , _ int64 , _ error ) {
1663
1680
cond := opts .toConds ()
1664
1681
1665
- searchFilterTwoFactorValue := ""
1666
- if opts .Actor != nil && opts .Actor .IsAdmin {
1667
- // Admin can apply advanced filters
1668
- for filterKey , filterValue := range opts .StatusFilterMap {
1669
- if filterValue == "" {
1670
- continue
1671
- }
1672
- parsedBool , _ := strconv .ParseBool (filterValue )
1673
- if filterKey == "is_active" {
1674
- cond = cond .And (builder.Eq {"is_active" : parsedBool })
1675
- } else if filterKey == "is_admin" {
1676
- cond = cond .And (builder.Eq {"is_admin" : parsedBool })
1677
- } else if filterKey == "is_restricted" {
1678
- cond = cond .And (builder.Eq {"is_restricted" : parsedBool })
1679
- } else if filterKey == "is_prohibit_login" {
1680
- cond = cond .And (builder.Eq {"prohibit_login" : parsedBool })
1681
- } else if filterKey == "is_2fa_enabled" {
1682
- searchFilterTwoFactorValue = filterValue
1683
- } else {
1684
- log .Error ("Unknown admin user search filter: %v=%v" , filterKey , filterValue )
1685
- }
1686
- }
1687
- }
1688
-
1689
- searchUserBaseQuery := func () (sess * xorm.Session ) {
1682
+ searchUserBaseQuery := func () (sess * xorm.Session ) {
1690
1683
sess = x .Where (cond )
1691
1684
1692
- if searchFilterTwoFactorValue != "" {
1685
+ if ! opts . IsTwoFactorEnabled . IsNone () {
1693
1686
// 2fa filter uses LEFT JOIN to check whether a user has a 2fa record
1694
1687
// TODO: bad performance here, maybe there will be a column "is_2fa_enabled" in the future
1695
1688
sess = sess .Join ("LEFT OUTER" , "two_factor" , "two_factor.uid = `user`.id" )
1696
- if searchFilterTwoFactorValue == "1" {
1689
+ if opts . IsTwoFactorEnabled . IsTrue () {
1697
1690
cond = cond .And (builder .Expr ("two_factor.uid IS NOT NULL" ))
1698
1691
} else {
1699
1692
cond = cond .And (builder .Expr ("two_factor.uid IS NULL" ))
0 commit comments