Repository navigation
[dev] feat: 用户支持多分组(group0,group1) #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package common | ||
|
|
||
| import "strings" | ||
|
|
||
| const ( | ||
| // MaxGroupNameLength is the maximum length of a single group name in a | ||
| // comma-separated group list. | ||
| MaxGroupNameLength = 64 | ||
| // MaxUserGroupColumnLength matches the users.group column width | ||
| // (varchar(1024)) that stores the comma-separated multi-group list. | ||
| MaxUserGroupColumnLength = 1024 | ||
| ) | ||
|
|
||
| // SplitGroupList splits a comma-separated group list (e.g. a multi-group user | ||
| // or channel) into trimmed, non-empty, de-duplicated group names, preserving | ||
| // the original order. | ||
| func SplitGroupList(s string) []string { | ||
| if strings.TrimSpace(s) == "" { | ||
| return nil | ||
| } | ||
| parts := strings.Split(s, ",") | ||
| groups := make([]string, 0, len(parts)) | ||
| seen := make(map[string]struct{}, len(parts)) | ||
| for _, part := range parts { | ||
| group := strings.TrimSpace(part) | ||
| if group == "" { | ||
| continue | ||
| } | ||
| if _, ok := seen[group]; ok { | ||
| continue | ||
| } | ||
| seen[group] = struct{}{} | ||
| groups = append(groups, group) | ||
| } | ||
| return groups | ||
| } | ||
|
|
||
| // PrimaryGroup returns the first group of a comma-separated list, falling back | ||
| // to "default" when the list contains no valid group. | ||
| func PrimaryGroup(s string) string { | ||
| if groups := SplitGroupList(s); len(groups) > 0 { | ||
| return groups[0] | ||
| } | ||
| return "default" | ||
| } | ||
|
|
||
| // NormalizeGroupList canonicalizes a comma-separated group list by trimming | ||
| // whitespace and removing empty duplicates. It returns "default" when nothing | ||
| // valid remains so a user always keeps at least one group. | ||
| func NormalizeGroupList(s string) string { | ||
| groups := SplitGroupList(s) | ||
| if len(groups) == 0 { | ||
| return "default" | ||
| } | ||
| return strings.Join(groups, ",") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSplitGroupList(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want []string | ||
| }{ | ||
| {"empty", "", nil}, | ||
| {"spaces only", " , ", []string{}}, | ||
| {"single", "default", []string{"default"}}, | ||
| {"multi", "group0,group1", []string{"group0", "group1"}}, | ||
| {"trims whitespace", " group0 , group1 ", []string{"group0", "group1"}}, | ||
| {"drops empties", "group0,,group1,", []string{"group0", "group1"}}, | ||
| {"dedupes preserving order", "b,a,b", []string{"b", "a"}}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| assert.Equal(t, tt.want, SplitGroupList(tt.input)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestPrimaryGroup(t *testing.T) { | ||
| assert.Equal(t, "group0", PrimaryGroup("group0,group1")) | ||
| assert.Equal(t, "group0", PrimaryGroup(" group0 , group1 ")) | ||
| assert.Equal(t, "group0", PrimaryGroup("group0")) | ||
| assert.Equal(t, "default", PrimaryGroup("")) | ||
| assert.Equal(t, "default", PrimaryGroup(" , ")) | ||
| } | ||
|
|
||
| func TestNormalizeGroupList(t *testing.T) { | ||
| assert.Equal(t, "group0,group1", NormalizeGroupList(" group0 , , group1 ")) | ||
| assert.Equal(t, "group0", NormalizeGroupList("group0,group0")) | ||
| assert.Equal(t, "default", NormalizeGroupList("")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func insertUsersForGroupFilterTest(t *testing.T) { | ||
| t.Helper() | ||
| truncateTables(t) | ||
| users := []*User{ | ||
| {Username: "single", Password: "password123", Group: "group0", AffCode: "aff-s0"}, | ||
| {Username: "multi", Password: "password123", Group: "group0,group1", AffCode: "aff-m0"}, | ||
| {Username: "other", Password: "password123", Group: "group10", AffCode: "aff-o0"}, | ||
| } | ||
| for _, user := range users { | ||
| require.NoError(t, DB.Create(user).Error) | ||
| } | ||
| } | ||
|
|
||
| func TestSearchUsersGroupFilterMatchesMultiGroupUsers(t *testing.T) { | ||
| insertUsersForGroupFilterTest(t) | ||
|
|
||
| users, total, err := SearchUsers("", "group0", nil, nil, 0, 10) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, int64(2), total) | ||
| assert.ElementsMatch(t, []string{"single", "multi"}, usernames(users)) | ||
|
|
||
| // 按列表中任意一个分组过滤都能命中多分组用户; | ||
| // 同时验证不会用子串误匹配:group1 不命中 group10 | ||
| users, total, err = SearchUsers("", "group1", nil, nil, 0, 10) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, int64(1), total) | ||
| require.Len(t, users, 1) | ||
| assert.Equal(t, "multi", users[0].Username) | ||
|
|
||
| _, total, err = SearchUsers("", "group10", nil, nil, 0, 10) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, int64(1), total) | ||
| } | ||
|
|
||
| func usernames(users []*User) []string { | ||
| names := make([]string, 0, len(users)) | ||
| for _, user := range users { | ||
| names = append(names, user.Username) | ||
| } | ||
| return names | ||
| } | ||
|
|
||
| func TestEditWithTxNormalizesGroupList(t *testing.T) { | ||
| insertUsersForGroupFilterTest(t) | ||
| var multi User | ||
| require.NoError(t, DB.Where("username = ?", "multi").First(&multi).Error) | ||
|
|
||
| multi.Group = " group1 , , group0 , group1 " | ||
| require.NoError(t, multi.EditWithTx(DB, false)) | ||
| assert.Equal(t, "group1,group0", multi.Group) | ||
|
|
||
| var reloaded User | ||
| require.NoError(t, DB.Where("username = ?", "multi").First(&reloaded).Error) | ||
| assert.Equal(t, "group1,group0", reloaded.Group) | ||
|
|
||
| // 空分组规范化为 default | ||
| multi.Group = " , " | ||
| require.NoError(t, multi.EditWithTx(DB, false)) | ||
| assert.Equal(t, "default", multi.Group) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed in d52111b.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in d52111b: the ModelRequestRateLimit lookup now applies the same resolution — token group first, then common.PrimaryGroup(ContextKeyUserGroup) — so a multi-group user matches the configured per-group rate limit instead of silently falling back to global limits.