Skip to content

Commit e944e47

Browse files
Raderpulltheflower
andauthored
Add command to write org name to casdoor user (#1252)
Co-authored-by: Zhang ZeHua <pulltheflower@163.com>
1 parent 4f23c4f commit e944e47

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package trigger
2+
3+
import (
4+
"errors"
5+
"log/slog"
6+
7+
"github.com/google/uuid"
8+
"github.com/spf13/cobra"
9+
"opencsg.com/csghub-server/builder/rpc"
10+
"opencsg.com/csghub-server/builder/store/database"
11+
"opencsg.com/csghub-server/common/config"
12+
)
13+
14+
var fixOrgCasdoorUserCmd = &cobra.Command{
15+
Use: "fix-org-casdoor-user",
16+
Short: "scan all organizations and create missing casdoor users",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
cfg, err := config.LoadConfig()
19+
if err != nil {
20+
return err
21+
}
22+
ctx := cmd.Context()
23+
24+
if cfg.SSOType != rpc.SSOTypeCasdoor {
25+
return errors.New("sso type is not casdoor, cannot fix org casdoor user")
26+
}
27+
28+
sso, err := rpc.NewSSOClient(cfg)
29+
if err != nil {
30+
return err
31+
}
32+
33+
orgStore := database.NewOrgStore()
34+
orgs, _, err := orgStore.GetUserOwnOrgs(ctx, "")
35+
if err != nil {
36+
return err
37+
}
38+
39+
var (
40+
joinErr error
41+
created int
42+
skipped int
43+
failed int
44+
)
45+
for _, org := range orgs {
46+
exists, checkErr := sso.IsExistByName(ctx, org.Name)
47+
if checkErr != nil {
48+
slog.Error("check casdoor user exist failed",
49+
slog.String("org", org.Name),
50+
slog.String("error", checkErr.Error()),
51+
)
52+
joinErr = errors.Join(joinErr, checkErr)
53+
failed++
54+
continue
55+
}
56+
if exists {
57+
skipped++
58+
continue
59+
}
60+
61+
createErr := sso.CreateUser(ctx, &rpc.SSOCreateUserInfo{
62+
Name: org.Name,
63+
Nickname: org.Nickname,
64+
UUID: org.UUID.String(),
65+
Password: uuid.New().String(),
66+
})
67+
if createErr != nil {
68+
slog.Error("create casdoor user failed",
69+
slog.String("org", org.Name),
70+
slog.String("error", createErr.Error()),
71+
)
72+
joinErr = errors.Join(joinErr, createErr)
73+
failed++
74+
continue
75+
}
76+
slog.Info("casdoor user created", slog.String("org", org.Name))
77+
created++
78+
}
79+
80+
slog.Info("fix org casdoor user done",
81+
slog.Int("created", created),
82+
slog.Int("skipped", skipped),
83+
slog.Int("failed", failed),
84+
)
85+
return joinErr
86+
},
87+
}

cmd/csghub-server/cmd/trigger/init.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func init() {
1414
Cmd.AddCommand(
1515
gitCallbackCmd,
1616
fixOrgDataCmd,
17+
fixOrgCasdoorUserCmd,
1718
fixUserDataCmd,
1819
updateRepoCmd,
1920
fixRepoSourceCmd,

0 commit comments

Comments
 (0)