Skip to content

Commit

Permalink
mciam to keycloak user control added
Browse files Browse the repository at this point in the history
  • Loading branch information
CliffSynn committed May 2, 2024
1 parent 7db859a commit 7824077
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
5 changes: 3 additions & 2 deletions actions/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ func App() *buffalo.App {
auth.DELETE("/user/{userId}", UnRegistUser)
auth.GET("/user", GetUserList)
auth.GET("/user/{userId}", GetUser)
auth.PATCH("/user/{userId}", UpdateUserProfile)

auth.POST("/usergroup", CreateUserGroup)
auth.PATCH("/usergroup/{groupId}", UpdateUserGroup)
auth.GET("/usergroup/{groupId}", GetUserGroupList)
auth.GET("/usergroup", GetUserGroup)
auth.GET("/usergroup", GetUserGroupList)
auth.GET("/usergroup/{groupId}", GetUserGroup)
auth.DELETE("/usergroup/{groupId}", DeleteUserGroup)

// manage := app.Group(apiPath + "manage")
Expand Down
2 changes: 1 addition & 1 deletion actions/usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ func GetUserGroup(c buffalo.Context) error {
func UpdateUserGroup(c buffalo.Context) error {
userGroupInfo := &iammodels.UserGroupInfo{}
c.Bind(userGroupInfo)

cblogger.Info("GroupInfo : ", userGroupInfo)
return c.Render(http.StatusOK, r.JSON(handler.UpdateUserGroup(c, *userGroupInfo)))
}
2 changes: 1 addition & 1 deletion actions/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func GetUserList(c buffalo.Context) error {
func RegistUser(c buffalo.Context) error {
userInfo := &iammodels.UserReq{}
c.Bind(userInfo)

cblogger.Info(userInfo)
user, err := handler.CreateUser(c, userInfo)
if err != nil {
cblogger.Error(err)
Expand Down
16 changes: 13 additions & 3 deletions handler/kcUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ var KCAdminRealm = os.Getenv("keycloakAdminRealm")
var KCRealm = os.Getenv("keycloakRealm")
var KCClient = gocloak.NewClient(KCUri)

var adminToken gocloak.JWT

func GetKeycloakAdminToken(c buffalo.Context) *gocloak.JWT {
token, kcLoginErr := KCClient.LoginAdmin(c, KCAdmin, KCPwd, KCAdminRealm)
//todo
// 1. admintoken expire chk
// 1-1. if expired
// 2-1. admin token refresh
// 3-1. return token
// 1-2. if not expired
// 2-2. return admin token

token, kcLoginErr := KCClient.LoginAdmin(c, KCAdmin, KCPwd, KCAdminRealm)
adminToken = *token
if kcLoginErr != nil {
cblogger.Info(kcLoginErr)
}

cblogger.Info("Tokens : " + token.AccessToken)
//cblogger.Info("Tokens : " + token.AccessToken)

return token
return &adminToken
}
4 changes: 2 additions & 2 deletions handler/userHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func DeleteUser(ctx buffalo.Context, userId string) error {

func UpdateUser(ctx buffalo.Context, userInfo iammodels.UserInfo) error {
adminAccessToken := GetKeycloakAdminToken(ctx).AccessToken
user, err := KCClient.GetUserByID(ctx, adminAccessToken, KCRealm, userInfo.UserId)
user, err := KCClient.GetUserByID(ctx, adminAccessToken, KCRealm, userInfo.Id)
if err != nil {
cblogger.Error(err)
return err
Expand All @@ -43,7 +43,7 @@ func UpdateUser(ctx buffalo.Context, userInfo iammodels.UserInfo) error {
To-do
User Update 항목 logic 추가
*/
user.Username = &userInfo.UserName
user.Email = &userInfo.Email

return KCClient.UpdateUser(ctx, adminAccessToken, KCRealm, *user)
}
8 changes: 2 additions & 6 deletions handler/usergroupHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ func UpdateUserGroup(ctx buffalo.Context, userGroupInfo iammodels.UserGroupInfo)
return err
}

/**
To-do
User Update 항목 logic 추가
*/
group.Name = &userGroupInfo.GroupName
updateGroup := iammodels.UpdateUserGroupByInfoToGroup(userGroupInfo, *group)

return KCClient.UpdateGroup(ctx, adminAccessToken, KCRealm, *group)
return KCClient.UpdateGroup(ctx, adminAccessToken, KCRealm, updateGroup)
}
24 changes: 22 additions & 2 deletions iammodels/model_user_group_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,27 @@
*/
package iammodels

import "github.com/Nerzal/gocloak/v13"

type UserGroupInfo struct {
GroupId string `json:"groupId,omitempty"`
GroupName string `json:"groupName,omitempty"`
GroupId string `json:"Id,omitempty"`
ParentId string `json:"parentGroupId,omitempty"`
GroupName string `json:"groupName,omitempty"`
Path string `json:"path,omitempty"`
SubGroups []string `json:"subGroups,omitempty"`
Attributes map[string][]string `json:"attributes,omitempty"`
Access map[string]bool `json:"access,omitempty"`
ClientRoles map[string][]string `json:"clientRoles,omitempty"`
RealmRoles []string `json:"realmRoles,omitempty"`
}

func UpdateUserGroupByInfoToGroup(userGroupInfo UserGroupInfo, group gocloak.Group) gocloak.Group {
group.Name = &userGroupInfo.GroupName
//group.Path = &userGroupInfo.Path
group.Attributes = &userGroupInfo.Attributes
//group.Access = &userGroupInfo.Access
group.ClientRoles = &userGroupInfo.ClientRoles
group.RealmRoles = &userGroupInfo.RealmRoles

return group
}
2 changes: 2 additions & 0 deletions iammodels/model_user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
package iammodels

type UserInfo struct {
Id string `json:"Id,omitempty"`
UserId string `json:"userId,omitempty"`
UserName string `json:"userName,omitempty"`
Email string `json:"email,omitempty"`
UserRoleList []UserRoleInfo `json:"userRoleList,omitempty"`
WorkspaceList []UserWorkspaceInfo `json:"workspaceList,omitempty"`
}

0 comments on commit 7824077

Please sign in to comment.