Skip to content

Commit

Permalink
fix: error user role
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Sep 24, 2024
1 parent 50eab6b commit 221894d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
4 changes: 4 additions & 0 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ const (
RoleRootUser = 100
)

func IsValidateRole(role int) bool {
return role == RoleGuestUser || role == RoleCommonUser || role == RoleAdminUser || role == RoleRootUser
}

var (
FileUploadPermission = RoleGuestUser
FileDownloadPermission = RoleGuestUser
Expand Down
8 changes: 5 additions & 3 deletions controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"one-api/common"
"one-api/model"
"strconv"
"strings"
"sync"

"github.com/gin-contrib/sessions"
Expand Down Expand Up @@ -616,6 +617,7 @@ func DeleteSelf(c *gin.Context) {
func CreateUser(c *gin.Context) {
var user model.User
err := json.NewDecoder(c.Request.Body).Decode(&user)
user.Username = strings.TrimSpace(user.Username)
if err != nil || user.Username == "" || user.Password == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
Expand Down Expand Up @@ -663,8 +665,8 @@ func CreateUser(c *gin.Context) {
}

type ManageRequest struct {
Username string `json:"username"`
Action string `json:"action"`
Id int `json:"id"`
Action string `json:"action"`
}

// ManageUser Only admin user can do this
Expand All @@ -680,7 +682,7 @@ func ManageUser(c *gin.Context) {
return
}
user := model.User{
Username: req.Username,
Id: req.Id,
}
// Fill attributes
model.DB.Unscoped().Where(&user).First(&user)
Expand Down
27 changes: 27 additions & 0 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"strings"
)

func validUserInfo(username string, role int) bool {
// check username is empty
if strings.TrimSpace(username) == "" {
return false
}
if !common.IsValidateRole(role) {
return false
}
return true
}

func authHelper(c *gin.Context, minRole int) {
session := sessions.Default(c)
username := session.Get("username")
Expand All @@ -30,6 +41,14 @@ func authHelper(c *gin.Context, minRole int) {
}
user := model.ValidateAccessToken(accessToken)
if user != nil && user.Username != "" {
if !validUserInfo(user.Username, user.Role) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权进行此操作,用户信息无效",
})
c.Abort()
return
}
// Token is valid
username = user.Username
role = user.Role
Expand Down Expand Up @@ -91,6 +110,14 @@ func authHelper(c *gin.Context, minRole int) {
c.Abort()
return
}
if !validUserInfo(username.(string), role.(int)) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权进行此操作,用户信息无效",
})
c.Abort()
return
}
c.Set("username", username)
c.Set("role", role)
c.Set("id", id)
Expand Down
5 changes: 3 additions & 2 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,12 @@ func (user *User) ValidateAndFill() (err error) {
// that means if your field’s value is 0, '', false or other zero values,
// it won’t be used to build query conditions
password := user.Password
if user.Username == "" || password == "" {
username := strings.TrimSpace(user.Username)
if username == "" || password == "" {
return errors.New("用户名或密码为空")
}
// find buy username or email
DB.Where("username = ? OR email = ?", user.Username, user.Username).First(user)
DB.Where("username = ? OR email = ?", username, username).First(user)
okay := common.ValidatePasswordAndHash(password, user.Password)
if !okay || user.Status != common.UserStatusEnabled {
return errors.New("用户名或密码错误,或用户已被封禁")
Expand Down
14 changes: 7 additions & 7 deletions web/src/components/UsersTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const UsersTable = () => {
title='确定?'
okType={'warning'}
onConfirm={() => {
manageUser(record.username, 'promote', record);
manageUser(record.id, 'promote', record);
}}
>
<Button theme='light' type='warning' style={{ marginRight: 1 }}>
Expand All @@ -162,7 +162,7 @@ const UsersTable = () => {
title='确定?'
okType={'warning'}
onConfirm={() => {
manageUser(record.username, 'demote', record);
manageUser(record.id, 'demote', record);
}}
>
<Button
Expand All @@ -179,7 +179,7 @@ const UsersTable = () => {
type='warning'
style={{ marginRight: 1 }}
onClick={async () => {
manageUser(record.username, 'disable', record);
manageUser(record.id, 'disable', record);
}}
>
禁用
Expand All @@ -190,7 +190,7 @@ const UsersTable = () => {
type='secondary'
style={{ marginRight: 1 }}
onClick={async () => {
manageUser(record.username, 'enable', record);
manageUser(record.id, 'enable', record);
}}
disabled={record.status === 3}
>
Expand All @@ -214,7 +214,7 @@ const UsersTable = () => {
okType={'danger'}
position={'left'}
onConfirm={() => {
manageUser(record.username, 'delete', record).then(() => {
manageUser(record.id, 'delete', record).then(() => {
removeRecord(record.id);
});
}}
Expand Down Expand Up @@ -303,9 +303,9 @@ const UsersTable = () => {
fetchGroups().then();
}, []);

const manageUser = async (username, action, record) => {
const manageUser = async (userId, action, record) => {
const res = await API.post('/api/user/manage', {
username,
id: userId,
action,
});
const { success, message } = res.data;
Expand Down

0 comments on commit 221894d

Please sign in to comment.