-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
114 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package adminController | ||
|
||
import ( | ||
"errors" | ||
|
||
"4u-go/app/apiException" | ||
"4u-go/app/models" | ||
"4u-go/app/services/adminService" | ||
"4u-go/app/utils" | ||
"4u-go/config/config" | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type createAdminByKeyData struct { | ||
Username string `json:"username" binding:"required"` | ||
Password string `json:"password" binding:"required"` | ||
Key string `json:"key" binding:"required"` | ||
} | ||
|
||
// CreateAdminByKey 通过密钥创建普通管理员 | ||
func CreateAdminByKey(c *gin.Context) { | ||
var data createAdminByKeyData | ||
err := c.ShouldBindJSON(&data) | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ParamError, err) | ||
return | ||
} | ||
|
||
key := config.Config.GetString("admin.key") | ||
if data.Key != key { | ||
apiException.AbortWithException(c, apiException.AdminKeyError, err) | ||
return | ||
} | ||
|
||
_, err = adminService.GetUserByUsername(data.Username) | ||
if err == nil { | ||
apiException.AbortWithException(c, apiException.AdminAlreadyExisted, err) | ||
return | ||
} else if !errors.Is(err, gorm.ErrRecordNotFound) { | ||
apiException.AbortWithException(c, apiException.ServerError, err) | ||
return | ||
} | ||
|
||
_, err = adminService.CreateAdminUser(data.Username, data.Password, models.ForU, "") | ||
if err != nil { | ||
apiException.AbortWithException(c, apiException.ServerError, err) | ||
return | ||
} | ||
utils.JsonSuccessResponse(c, gin.H{ | ||
"username": data.Username, | ||
"password": data.Password, | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package adminService | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"time" | ||
|
||
"4u-go/app/models" | ||
"4u-go/config/database" | ||
) | ||
|
||
// CreateAdminUser 创建管理员用户 | ||
func CreateAdminUser(username string, password string, userType models.UserType, college string) (*models.User, error) { | ||
h := sha256.New() | ||
if _, err := h.Write([]byte(password)); err != nil { | ||
return nil, fmt.Errorf("failed to hash password: %w", err) | ||
} | ||
pass := hex.EncodeToString(h.Sum(nil)) | ||
user := &models.User{ | ||
Type: userType, | ||
StudentID: username, | ||
Password: pass, | ||
College: college, | ||
CreateTime: time.Now(), | ||
} | ||
res := database.DB.Create(&user) | ||
|
||
return user, res.Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package adminService | ||
|
||
import ( | ||
"4u-go/app/models" | ||
"4u-go/config/database" | ||
) | ||
|
||
// GetUserByUsername 通过用户名获取用户 | ||
func GetUserByUsername(username string) (*models.User, error) { | ||
user := &models.User{} | ||
res := database.DB.Where("student_id = ?", username).First(user) | ||
return user, res.Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters