Kaynağa Gözat

权限测试成功

robert 1 hafta önce
ebeveyn
işleme
aa07389464
2 değiştirilmiş dosya ile 34 ekleme ve 4 silme
  1. 15 1
      auth.go
  2. 19 3
      cli.go

+ 15 - 1
auth.go

@@ -653,7 +653,21 @@ func (am *AuthManager) ListUsers() []*User {
 	defer am.mu.RUnlock()
 	users := make([]*User, 0, len(am.users))
 	for _, u := range am.users {
-		users = append(users, u)
+		// Calculate IsOnline based on active sessions
+		isOnline := false
+		for _, s := range am.sessions {
+			if s.Username == u.Username {
+				if time.Now().Unix() <= s.Expiry {
+					isOnline = true
+					break
+				}
+			}
+		}
+		
+		// Create a copy to avoid race conditions and not modify cache
+		userCopy := *u
+		userCopy.IsOnline = isOnline
+		users = append(users, &userCopy)
 	}
 	return users
 }

+ 19 - 3
cli.go

@@ -626,10 +626,8 @@ func (c *CLI) registerDefaultCommands() {
 				}
 				
 			case "update":
-				// Simplistic update: user role update <name> parents=p1,p2
-				// Complex permissions editing is hard in CLI without JSON
 				if len(roleArgs) < 1 {
-					printBoxed("Usage: user role update <name> [parents=p1,p2]")
+					printBoxed("Usage: user role update <name> [parents=p1,p2] [perms=action:pattern,...]")
 					return
 				}
 				name := roleArgs[0]
@@ -653,6 +651,24 @@ func (c *CLI) registerDefaultCommands() {
 						targetRole.ParentRoles = strings.Split(pStr, ",")
 						updated = true
 					}
+					if strings.HasPrefix(arg, "perms=") {
+						permStr := strings.TrimPrefix(arg, "perms=")
+						rules := strings.Split(permStr, ",")
+						var newPerms []Permission
+						for _, r := range rules {
+							parts := strings.SplitN(r, ":", 2)
+							if len(parts) == 2 {
+								action := parts[0]
+								pattern := parts[1]
+								newPerms = append(newPerms, Permission{
+									KeyPattern: pattern,
+									Actions:    []string{action},
+								})
+							}
+						}
+						targetRole.Permissions = newPerms
+						updated = true
+					}
 				}
 				
 				if updated {