Creation of reusable components for mcs & implementation in users page (#63)
Creation of reusable componentes for mcs: - ModalWrapper => Modal box component with MinIO styles - InputBoxWrapper => Input box component with MinIO styles - RadioGroupSelector => Component that generates a Radio Group Selector combo with the requested options and MinIO styles Implementation of these new components in users creation / edit components Co-authored-by: Benjamin Perez <benjamin@bexsoft.net>
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package restapi
|
||||
|
||||
import (
|
||||
"github.com/go-openapi/errors"
|
||||
"github.com/go-openapi/runtime/middleware"
|
||||
"github.com/go-openapi/swag"
|
||||
"github.com/minio/mcs/restapi/operations"
|
||||
@@ -27,7 +28,6 @@ import (
|
||||
"github.com/minio/minio/pkg/madmin"
|
||||
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
)
|
||||
|
||||
@@ -74,6 +74,15 @@ func registerUsersHandlers(api *operations.McsAPI) {
|
||||
|
||||
return admin_api.NewGetUserOK().WithPayload(userInfoResponse)
|
||||
})
|
||||
// Update User
|
||||
api.AdminAPIUpdateUserInfoHandler = admin_api.UpdateUserInfoHandlerFunc(func(params admin_api.UpdateUserInfoParams, principal *models.Principal) middleware.Responder {
|
||||
userUpdateResponse, err := getUpdateUserResponse(params)
|
||||
if err != nil {
|
||||
return admin_api.NewUpdateUserInfoDefault(500).WithPayload(&models.Error{Code: 500, Message: swag.String(err.Error())})
|
||||
}
|
||||
|
||||
return admin_api.NewUpdateUserInfoOK().WithPayload(userUpdateResponse)
|
||||
})
|
||||
}
|
||||
|
||||
func listUsers(ctx context.Context, client MinioAdmin) ([]*models.User, error) {
|
||||
@@ -304,7 +313,7 @@ func updateUserGroups(ctx context.Context, client MinioAdmin, user string, group
|
||||
}
|
||||
|
||||
if channelHasError {
|
||||
errRt := errors.New("there was an error updating the groups")
|
||||
errRt := errors.New(500, "there was an error updating the groups")
|
||||
return nil, errRt
|
||||
}
|
||||
|
||||
@@ -345,3 +354,51 @@ func getUpdateUserGroupsResponse(params admin_api.UpdateUserGroupsParams) (*mode
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// setUserStatus invokes setUserStatus from madmin to update user status
|
||||
func setUserStatus(ctx context.Context, client MinioAdmin, user string, status string) error {
|
||||
var setStatus madmin.AccountStatus
|
||||
switch status {
|
||||
case "enabled":
|
||||
setStatus = madmin.AccountEnabled
|
||||
case "disabled":
|
||||
setStatus = madmin.AccountDisabled
|
||||
default:
|
||||
return errors.New(500, "status not valid")
|
||||
}
|
||||
|
||||
if err := client.setUserStatus(ctx, user, setStatus); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getUpdateUserResponse(params admin_api.UpdateUserInfoParams) (*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}
|
||||
|
||||
name := params.Name
|
||||
status := *params.Body.Status
|
||||
groups := params.Body.Groups
|
||||
|
||||
if err := setUserStatus(ctx, adminClient, name, status); err != nil {
|
||||
log.Println("error updating user status:", status)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userElem, errUG := updateUserGroups(ctx, adminClient, name, groups)
|
||||
|
||||
if errUG != nil {
|
||||
return nil, errUG
|
||||
}
|
||||
return userElem, nil
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ var minioListUsersMock func() (map[string]madmin.UserInfo, error)
|
||||
var minioAddUserMock func(accessKey, secreyKey string) error
|
||||
var minioRemoveUserMock func(accessKey string) error
|
||||
var minioGetUserInfoMock func(accessKey string) (madmin.UserInfo, error)
|
||||
var minioSetUserStatusMock func(accessKey string, status madmin.AccountStatus) error
|
||||
|
||||
// mock function of listUsers()
|
||||
func (ac adminClientMock) listUsers(ctx context.Context) (map[string]madmin.UserInfo, error) {
|
||||
@@ -54,6 +55,11 @@ func (ac adminClientMock) getUserInfo(ctx context.Context, accessKey string) (ma
|
||||
return minioGetUserInfoMock(accessKey)
|
||||
}
|
||||
|
||||
//mock function of setUserStatus()
|
||||
func (ac adminClientMock) setUserStatus(ctx context.Context, accessKey string, status madmin.AccountStatus) error {
|
||||
return minioSetUserStatusMock(accessKey, status)
|
||||
}
|
||||
|
||||
func TestListUsers(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
@@ -321,3 +327,44 @@ func TestGetUserInfo(t *testing.T) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetUserStatus(t *testing.T) {
|
||||
assert := asrt.New(t)
|
||||
adminClient := adminClientMock{}
|
||||
function := "setUserStatus()"
|
||||
userName := "userName123"
|
||||
ctx := context.Background()
|
||||
|
||||
// Test-1: setUserStatus() update valid disabled status
|
||||
expectedStatus := "disabled"
|
||||
minioSetUserStatusMock = func(accessKey string, status madmin.AccountStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setUserStatus(ctx, adminClient, userName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-2: setUserStatus() update valid enabled status
|
||||
expectedStatus = "enabled"
|
||||
minioSetUserStatusMock = func(accessKey string, status madmin.AccountStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setUserStatus(ctx, adminClient, userName, expectedStatus); err != nil {
|
||||
t.Errorf("Failed on %s:, error occurred: %s", function, err.Error())
|
||||
}
|
||||
// Test-3: setUserStatus() update invalid status, should send error
|
||||
expectedStatus = "invalid"
|
||||
minioSetUserStatusMock = func(accessKey string, status madmin.AccountStatus) error {
|
||||
return nil
|
||||
}
|
||||
if err := setUserStatus(ctx, adminClient, userName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("status not valid", err.Error())
|
||||
}
|
||||
// Test-4: setUserStatus() handler error correctly
|
||||
expectedStatus = "enabled"
|
||||
minioSetUserStatusMock = func(accessKey string, status madmin.AccountStatus) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
if err := setUserStatus(ctx, adminClient, userName, expectedStatus); assert.Error(err) {
|
||||
assert.Equal("error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ type MinioAdmin interface {
|
||||
addUser(ctx context.Context, acessKey, SecretKey string) error
|
||||
removeUser(ctx context.Context, accessKey string) error
|
||||
getUserInfo(ctx context.Context, accessKey string) (madmin.UserInfo, error)
|
||||
setUserStatus(ctx context.Context, accessKey string, status madmin.AccountStatus) error
|
||||
listGroups(ctx context.Context) ([]string, error)
|
||||
updateGroupMembers(ctx context.Context, greq madmin.GroupAddRemove) error
|
||||
getGroupDescription(ctx context.Context, group string) (*madmin.GroupDesc, error)
|
||||
@@ -105,6 +106,11 @@ func (ac adminClient) getUserInfo(ctx context.Context, accessKey string) (madmin
|
||||
return ac.client.GetUserInfo(ctx, accessKey)
|
||||
}
|
||||
|
||||
// implements madmin.SetUserStatus()
|
||||
func (ac adminClient) setUserStatus(ctx context.Context, accessKey string, status madmin.AccountStatus) error {
|
||||
return ac.client.SetUserStatus(ctx, accessKey, status)
|
||||
}
|
||||
|
||||
// implements madmin.ListGroups()
|
||||
func (ac adminClient) listGroups(ctx context.Context) ([]string, error) {
|
||||
return ac.client.ListGroups(ctx)
|
||||
|
||||
Reference in New Issue
Block a user