Refactor scope loading to dynamically retrieve first-level subdirectories

- Introduced a new function to get first-level subdirectories in the scopes directory, enhancing the flexibility of scope definition loading.
- Updated the loadScopeDefinitions method to utilize this new function, replacing hardcoded subdirectory names with a dynamic approach.
- Improved the description of the invite verification scope in the user scope initialization for clarity.
This commit is contained in:
Max 2025-11-04 18:07:22 +08:00
parent e1405a5fa2
commit f6aead62bb
3 changed files with 58 additions and 28 deletions

View file

@ -223,22 +223,53 @@ func (m *ScopeManager) expandAlias(alias string, visited map[string]bool) ([]str
return expanded, nil return expanded, nil
} }
// getFirstLevelSubdirs returns all first-level subdirectories in a given directory
func getFirstLevelSubdirs(baseDir string) ([]string, error) {
var subdirs []string
err := application.App.Walk(baseDir, func(root, filename string, isdir bool) error {
// Only process directories
if !isdir {
return nil
}
// Skip the root directory itself
if filename == baseDir {
return nil
}
// Get relative path from baseDir
relPath := strings.TrimPrefix(filename, baseDir)
relPath = strings.TrimPrefix(relPath, string(filepath.Separator))
// Only include first-level directories (no nested paths)
if !strings.Contains(relPath, string(filepath.Separator)) && relPath != "" {
subdirs = append(subdirs, relPath)
}
return nil
}, "")
if err != nil {
return nil, err
}
return subdirs, nil
}
// loadScopeDefinitions loads scope definitions from subdirectories // loadScopeDefinitions loads scope definitions from subdirectories
func (m *ScopeManager) loadScopeDefinitions() error { func (m *ScopeManager) loadScopeDefinitions() error {
scopesDir := filepath.Join("openapi", "scopes") scopesDir := filepath.Join("openapi", "scopes")
// Subdirectories to scan // Get all subdirectories in the scopes directory
subDirs := []string{"kb", "job", "file", "user"} subDirs, err := getFirstLevelSubdirs(scopesDir)
if err != nil {
return fmt.Errorf("failed to scan scopes directory: %w", err)
}
// Load scope definitions from each subdirectory
for _, subDir := range subDirs { for _, subDir := range subDirs {
dirPath := filepath.Join(scopesDir, subDir) dirPath := filepath.Join(scopesDir, subDir)
exists, err := application.App.Exists(dirPath)
if err != nil {
return err
}
if !exists {
continue
}
// Walk through all .yml files in the directory // Walk through all .yml files in the directory
err = application.App.Walk(dirPath, func(root, filename string, isdir bool) error { err = application.App.Walk(dirPath, func(root, filename string, isdir bool) error {

View file

@ -35,12 +35,11 @@ func init() {
"GET /file/:uploaderID/:fileID/content", "GET /file/:uploaderID/:fileID/content",
}, },
}, },
// Invite verification scope - allows users to accept team invitations // Invite verification scope - allows users to view invitation details before accepting
&acl.ScopeDefinition{ &acl.ScopeDefinition{
Name: ScopeInviteVerification, Name: ScopeInviteVerification,
Description: "Invite verification - temporary access for accepting team invitations", Description: "Invite verification - temporary access for viewing invitation details",
Endpoints: []string{ Endpoints: []string{
"POST /user/teams/invitations/:invitation_id/accept",
"GET /user/teams/invitations/:invitation_id", "GET /user/teams/invitations/:invitation_id",
}, },
}, },