Implemented User-Groups integration for mcs (#62)
Implemented user-groups integration for mcs, this allows to store the user groups during the user creation. Co-authored-by: Benjamin Perez <benjamin@bexsoft.net>
This commit is contained in:
@@ -65,6 +65,15 @@ func registerUsersHandlers(api *operations.McsAPI) {
|
||||
|
||||
return admin_api.NewUpdateUserGroupsOK().WithPayload(userUpdateResponse)
|
||||
})
|
||||
// Get User
|
||||
api.AdminAPIGetUserInfoHandler = admin_api.GetUserInfoHandlerFunc(func(params admin_api.GetUserInfoParams, principal *models.Principal) middleware.Responder {
|
||||
userInfoResponse, err := getUserInfoResponse(params)
|
||||
if err != nil {
|
||||
return admin_api.NewGetUserDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())})
|
||||
}
|
||||
|
||||
return admin_api.NewGetUserOK().WithPayload(userInfoResponse)
|
||||
})
|
||||
}
|
||||
|
||||
func listUsers(ctx context.Context, client MinioAdmin) ([]*models.User, error) {
|
||||
@@ -116,18 +125,28 @@ func getListUsersResponse() (*models.ListUsersResponse, error) {
|
||||
}
|
||||
|
||||
// addUser invokes adding a users on `MinioAdmin` and builds the response `models.User`
|
||||
func addUser(ctx context.Context, client MinioAdmin, accessKey, secretKey *string) (*models.User, error) {
|
||||
func addUser(ctx context.Context, client MinioAdmin, accessKey, secretKey *string, groups []string) (*models.User, error) {
|
||||
// Calls into MinIO to add a new user if there's an error return it
|
||||
err := client.addUser(ctx, *accessKey, *secretKey)
|
||||
if err != nil {
|
||||
if err := client.addUser(ctx, *accessKey, *secretKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userElem := &models.User{
|
||||
AccessKey: *accessKey,
|
||||
if len(groups) > 0 {
|
||||
userElem, errUG := updateUserGroups(ctx, client, *accessKey, groups)
|
||||
|
||||
if errUG != nil {
|
||||
return nil, errUG
|
||||
}
|
||||
return userElem, nil
|
||||
}
|
||||
|
||||
return userElem, nil
|
||||
userRet := &models.User{
|
||||
AccessKey: *accessKey,
|
||||
MemberOf: nil,
|
||||
Policy: "",
|
||||
Status: "",
|
||||
}
|
||||
return userRet, nil
|
||||
}
|
||||
|
||||
func getUserAddResponse(params admin_api.AddUserParams) (*models.User, error) {
|
||||
@@ -141,7 +160,7 @@ func getUserAddResponse(params admin_api.AddUserParams) (*models.User, error) {
|
||||
// defining the client to be used
|
||||
adminClient := adminClient{client: mAdmin}
|
||||
|
||||
user, err := addUser(ctx, adminClient, params.Body.AccessKey, params.Body.SecretKey)
|
||||
user, err := addUser(ctx, adminClient, params.Body.AccessKey, params.Body.SecretKey, params.Body.Groups)
|
||||
if err != nil {
|
||||
log.Println("error adding user:", err)
|
||||
return nil, err
|
||||
@@ -189,6 +208,35 @@ func getUserInfo(ctx context.Context, client MinioAdmin, accessKey string) (*mad
|
||||
return &userInfo, nil
|
||||
}
|
||||
|
||||
func getUserInfoResponse(params admin_api.GetUserInfoParams) (*models.User, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
mAdmin, err := newMAdminClient()
|
||||
if err != nil {
|
||||
log.Println("error creating Madmin Client:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create a minioClient interface implementation
|
||||
// defining the client to be used
|
||||
adminClient := adminClient{client: mAdmin}
|
||||
|
||||
user, err := getUserInfo(ctx, adminClient, params.Name)
|
||||
if err != nil {
|
||||
log.Println("error getting user:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userInformation := &models.User{
|
||||
AccessKey: params.Name,
|
||||
MemberOf: user.MemberOf,
|
||||
Policy: user.PolicyName,
|
||||
Status: string(user.Status),
|
||||
}
|
||||
|
||||
return userInformation, nil
|
||||
}
|
||||
|
||||
// updateUserGroups invokes getUserInfo() to get the old groups from the user,
|
||||
// then we merge the list with the new groups list to have a shorter iteration between groups and we do a comparison between the current and old groups.
|
||||
// We delete or update the groups according the location in each list and send the user with the new groups from `MinioAdmin` to the client
|
||||
|
||||
Reference in New Issue
Block a user