-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
61 lines (48 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/MISW/portal-role-sync/config"
"github.com/MISW/portal-role-sync/infra/auth0"
"github.com/MISW/portal-role-sync/infra/portal"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
config, err := config.ReadConfig()
if err != nil {
log.Fatalln(err)
}
portalClient := portal.NewClient(config.Portal.API, config.Portal.Token)
auth0AuthConfig := auth0.NewConfig(config.Auth0.Domain, config.Auth0.ClientID, config.Auth0.ClientSecret)
auth0Client := auth0.NewClient(config.Auth0.Domain, auth0AuthConfig.TokenSource(ctx))
auth0MemberPortalRoles, err := auth0Client.GetUserPortalRoles(ctx)
if err != nil {
log.Fatalln(err)
}
portalMemberRoles, err := portalClient.GetAllMemberRoles(ctx)
if err != nil {
log.Fatalln(err)
}
i := 0
for k, v := range portalMemberRoles {
//auth0にユーザが存在しない場合、そもそもログインできしロール設定もできないので実行しない。
auth0MemberPortalRole, ok := auth0MemberPortalRoles[k]
if !ok {
log.Printf("user {%s} with role {%s} does not exist in auth0 users.\n", k, v.Role)
continue
}
// ロールが不一致の場合、ロールをアップデートする
if v.Role != auth0MemberPortalRole.Role {
if err := auth0Client.UpdateUserPortalRole(ctx, k, v.Role); err != nil {
log.Fatalln(err)
}
//成功
i++
log.Printf("updated user {%s} role to {%s}", k, v.Role)
}
}
log.Printf("Changed: %d\n", i)
}