fixed initial user
Some checks failed
Build and Push Docker Image / Build image (push) Has been cancelled
Some checks failed
Build and Push Docker Image / Build image (push) Has been cancelled
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.acooldomain.co/server-manager/backend/auth"
|
||||
"git.acooldomain.co/server-manager/backend/dbhandler"
|
||||
@@ -28,7 +32,6 @@ type UserResponse struct {
|
||||
|
||||
func (con UsersApi) GetUsers(ctx *gin.Context) {
|
||||
users, err := con.userPassAuthHandler.ListUsers(ctx)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@@ -65,18 +68,24 @@ type InviteUser struct {
|
||||
Permissions models.Permission `json:"Permissions"`
|
||||
}
|
||||
|
||||
func (con *UsersApi) inviteUser(ctx context.Context, email string, permissions models.Permission) error {
|
||||
token, err := con.tokenHandler.SaveInviteToken(ctx, email, permissions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = con.mailClient.SendMail(email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (con UsersApi) InviteUser(ctx *gin.Context) {
|
||||
var request InviteUser
|
||||
json.NewDecoder(ctx.Request.Body).Decode(&request)
|
||||
|
||||
token, err := con.tokenHandler.SaveInviteToken(ctx, request.Email, request.Permissions)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = con.mailClient.SendMail(request.Email, "You've been invited to join", "please open this link https://games.acooldomain.co/signup?token="+token)
|
||||
err := con.inviteUser(ctx, request.Email, request.Permissions)
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@@ -107,7 +116,6 @@ func (con UsersApi) DeleteUser(ctx *gin.Context) {
|
||||
username := ctx.Param("user_id")
|
||||
|
||||
err := con.userPassAuthHandler.RemoveUser(ctx, username)
|
||||
|
||||
if err != nil {
|
||||
ctx.AbortWithError(500, err)
|
||||
return
|
||||
@@ -123,6 +131,9 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
||||
}
|
||||
|
||||
inviteHandler, err := factories.GetInviteTokenDbHandler(config.Authentication.UserPass.InviteTokenDatabase)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
mailClient := *mail.NewMailClient(config.Email)
|
||||
|
||||
@@ -133,6 +144,20 @@ func LoadGroup(group *gin.RouterGroup, config models.GlobalConfig) {
|
||||
config: &config,
|
||||
}
|
||||
|
||||
if config.Authentication.Type == models.UserPass && config.Authentication.UserPass.InitialUser != nil {
|
||||
ctx, cancel := context.WithTimeoutCause(context.Background(), 5*time.Second, errors.New("Timeout"))
|
||||
defer cancel()
|
||||
|
||||
count, _ := connection.userPassAuthHandler.CountUsers(ctx)
|
||||
if count == 0 {
|
||||
log.Printf("Trying to create user %#v\n", config.Authentication.UserPass.InitialUser)
|
||||
err := connection.inviteUser(ctx, config.Authentication.UserPass.InitialUser.Email, models.Admin)
|
||||
if err != nil {
|
||||
log.Printf("Failed to create initial user %e\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
group.GET("", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUsers)
|
||||
group.GET("/@me", auth.AuthorizedTo(0), auth.AuthorizationEnforcer(), connection.GetUser)
|
||||
group.POST("", auth.AuthorizedTo(models.Admin), auth.AuthorizationEnforcer(), connection.InviteUser)
|
||||
|
Reference in New Issue
Block a user