aligned users to new design
This commit is contained in:
132
users/users.go
132
users/users.go
@@ -1,49 +1,49 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/auth"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/db_handler/mongo"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/dbhandler"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/mail"
|
||||
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
connection *dbhandler.UsersDBHandler
|
||||
config *models.GlobalConfig
|
||||
userPassAuthHandler dbhandler.UserPassAuthanticationDbHandler
|
||||
tokenHandler dbhandler.InviteTokenDbHandler
|
||||
mailClient mail.MailClient
|
||||
config *models.GlobalConfig
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
Username string
|
||||
Email string
|
||||
Permissions models.Permission
|
||||
Username string `json:"Username"`
|
||||
Email string `json:"Email"`
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con Connection) GetUsers(c *gin.Context) {
|
||||
users, err := (*con.connection).ListUsers()
|
||||
func (con Connection) GetUsers(ctx *gin.Context) {
|
||||
users, err := con.userPassAuthHandler.ListUsers(ctx)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func (con Connection) GetUser(c *gin.Context) {
|
||||
claims, exists := c.Get("claims")
|
||||
if !exists {
|
||||
c.AbortWithStatus(505)
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.IndentedJSON(http.StatusOK, UserResponse{
|
||||
ctx.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func (con Connection) GetUser(ctx *gin.Context) {
|
||||
claims, exists := ctx.Get("claims")
|
||||
if !exists {
|
||||
ctx.AbortWithStatus(403)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.IndentedJSON(http.StatusOK, UserResponse{
|
||||
Username: claims.(*auth.AuthClaims).Username,
|
||||
Permissions: claims.(*auth.AuthClaims).Permissions,
|
||||
})
|
||||
@@ -54,63 +54,87 @@ type InviteUser struct {
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con Connection) InviteUser(c *gin.Context) {
|
||||
func (con Connection) InviteUser(ctx *gin.Context) {
|
||||
var request InviteUser
|
||||
json.NewDecoder(c.Request.Body).Decode(&request)
|
||||
token := uuid.NewString()
|
||||
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
|
||||
token, err := con.tokenHandler.SaveInviteToken(ctx, request.Email, request.Permissions)
|
||||
|
||||
err := mail.SendMail(request.Email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
con.connection.Database("Backend").Collection("Tokens").InsertOne(context.TODO(), auth.InviteToken{
|
||||
Email: request.Email,
|
||||
Permissions: request.Permissions,
|
||||
Token: token,
|
||||
})
|
||||
c.JSON(200, "OK")
|
||||
|
||||
err = con.mailClient.SendMail(request.Email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(200, "OK")
|
||||
}
|
||||
|
||||
type SetUserPermissionsRequest struct {
|
||||
Permissions models.Permission
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con Connection) SetUserPermissions(c *gin.Context) {
|
||||
func (con Connection) SetUserPermissions(ctx *gin.Context) {
|
||||
var request SetUserPermissionsRequest
|
||||
json.NewDecoder(c.Request.Body).Decode(&request)
|
||||
username := c.Param("user_id")
|
||||
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
username := ctx.Param("user_id")
|
||||
|
||||
_, err := con.connection.Database("Backend").Collection("Users").UpdateOne(
|
||||
context.TODO(),
|
||||
bson.D{{Key: "Username", Value: username}},
|
||||
bson.D{{Key: "$set", Value: bson.D{{Key: "Permissions", Value: request.Permissions}}}},
|
||||
)
|
||||
err := con.userPassAuthHandler.SetPermissions(ctx, username, request.Permissions)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "OK")
|
||||
ctx.JSON(200, "OK")
|
||||
}
|
||||
|
||||
func (con Connection) DeleteUser(c *gin.Context) {
|
||||
username := c.Param("user_id")
|
||||
func (con Connection) DeleteUser(ctx *gin.Context) {
|
||||
username := ctx.Param("user_id")
|
||||
|
||||
err := con.userPassAuthHandler.RemoveUser(ctx, username)
|
||||
|
||||
_, err := con.connection.Database("Backend").Collection("Users").DeleteOne(
|
||||
context.TODO(),
|
||||
bson.D{{Key: "Username", Value: username}},
|
||||
)
|
||||
if err != nil {
|
||||
c.AbortWithError(500, err)
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, "OK")
|
||||
ctx.JSON(200, "OK")
|
||||
}
|
||||
|
||||
func LoadGroup(group *gin.RouterGroup, client *mongo.Client, config models.GlobalConfig) {
|
||||
connection := Connection{connection: client}
|
||||
func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
||||
var userAuthHandler dbhandler.UserPassAuthanticationDbHandler
|
||||
var inviteHandler dbhandler.InviteTokenDbHandler
|
||||
var mailClient mail.MailClient
|
||||
|
||||
var err error
|
||||
|
||||
if config.Authentication.UserPass.Type == models.MONGO {
|
||||
userAuthHandler, err = mongo.NewUserPassAuthHandler(*config.Authentication.UserPass.Mongo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
if config.Authentication.UserPass.InviteTokenDatabase.Type == models.MONGO {
|
||||
inviteHandler, err = mongo.NewInviteTokenDbHandler(*config.Authentication.UserPass.Mongo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
mailClient = *mail.NewMailClient(config.Email)
|
||||
|
||||
connection := Connection{
|
||||
userPassAuthHandler: userAuthHandler,
|
||||
tokenHandler: inviteHandler,
|
||||
mailClient: mailClient,
|
||||
config: &config,
|
||||
}
|
||||
|
||||
group.GET("", auth.AuthorizedTo(0), connection.GetUsers)
|
||||
group.GET("/@me", auth.AuthorizedTo(0), connection.GetUser)
|
||||
group.POST("", auth.AuthorizedTo(models.Admin), connection.InviteUser)
|
||||
|
Reference in New Issue
Block a user