mirror of
https://github.com/versity/versitygw.git
synced 2026-01-07 04:06:23 +00:00
* feat: Added admin CLI, created api endpoint for creating new user, created action for admin CLI to create a new user, changed the authentication middleware to verify the users from db * feat: Added both single and multi user support, added caching layer for getting IAM users * fix: Added all the files
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
// Copyright 2023 Versity Software
|
|
// This file is licensed under the Apache License, Version 2.0
|
|
// (the "License"); you may not use this file except in compliance
|
|
// with the License. You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing,
|
|
// software distributed under the License is distributed on an
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
// KIND, either express or implied. See the License for the
|
|
// specific language governing permissions and limitations
|
|
// under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/versity/versitygw/backend/auth"
|
|
)
|
|
|
|
type AdminController struct {
|
|
IAMService auth.IAMService
|
|
}
|
|
|
|
func NewAdminController() AdminController {
|
|
return AdminController{IAMService: auth.New()}
|
|
}
|
|
|
|
func (c AdminController) CreateUser(ctx *fiber.Ctx) error {
|
|
access, secret, role, region := ctx.Query("access"), ctx.Query("secret"), ctx.Query("role"), ctx.Query("region")
|
|
requesterRole := ctx.Locals("role")
|
|
|
|
if requesterRole != "admin" {
|
|
return fmt.Errorf("access denied: only admin users have access to this resource")
|
|
}
|
|
|
|
user := auth.Account{Secret: secret, Role: role, Region: region}
|
|
|
|
err := c.IAMService.CreateAccount(access, &user)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create a user: %w", err)
|
|
}
|
|
|
|
ctx.SendString("The user has been created successfully")
|
|
return nil
|
|
}
|