Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
9c529f96
authored
Apr 26, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: able to set initial quota for new user (close #22)
parent
5a9fb623
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
59 additions
and
1 deletions
+59
-1
common/constants.go
+2
-0
controller/token.go
+17
-0
model/main.go
+1
-0
model/option.go
+3
-0
model/user.go
+14
-0
web/src/components/SystemSetting.js
+22
-1
No files found.
common/constants.go
View file @
9c529f96
...
@@ -46,6 +46,8 @@ var WeChatAccountQRCodeImageURL = ""
...
@@ -46,6 +46,8 @@ var WeChatAccountQRCodeImageURL = ""
var
TurnstileSiteKey
=
""
var
TurnstileSiteKey
=
""
var
TurnstileSecretKey
=
""
var
TurnstileSecretKey
=
""
var
QuotaForNewUser
=
100
const
(
const
(
RoleGuestUser
=
0
RoleGuestUser
=
0
RoleCommonUser
=
1
RoleCommonUser
=
1
...
...
controller/token.go
View file @
9c529f96
...
@@ -104,6 +104,19 @@ func AddToken(c *gin.Context) {
...
@@ -104,6 +104,19 @@ func AddToken(c *gin.Context) {
if
isAdmin
{
if
isAdmin
{
cleanToken
.
RemainTimes
=
token
.
RemainTimes
cleanToken
.
RemainTimes
=
token
.
RemainTimes
cleanToken
.
UnlimitedTimes
=
token
.
UnlimitedTimes
cleanToken
.
UnlimitedTimes
=
token
.
UnlimitedTimes
}
else
{
userId
:=
c
.
GetInt
(
"id"
)
quota
,
err
:=
model
.
GetUserQuota
(
userId
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
if
quota
>
0
{
cleanToken
.
RemainTimes
=
quota
}
}
}
err
=
cleanToken
.
Insert
()
err
=
cleanToken
.
Insert
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -113,6 +126,10 @@ func AddToken(c *gin.Context) {
...
@@ -113,6 +126,10 @@ func AddToken(c *gin.Context) {
})
})
return
return
}
}
if
!
isAdmin
{
// update user quota
err
=
model
.
DecreaseUserQuota
(
c
.
GetInt
(
"id"
),
cleanToken
.
RemainTimes
)
}
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"success"
:
true
,
"message"
:
""
,
"message"
:
""
,
...
...
model/main.go
View file @
9c529f96
...
@@ -25,6 +25,7 @@ func createRootAccountIfNeed() error {
...
@@ -25,6 +25,7 @@ func createRootAccountIfNeed() error {
Role
:
common
.
RoleRootUser
,
Role
:
common
.
RoleRootUser
,
Status
:
common
.
UserStatusEnabled
,
Status
:
common
.
UserStatusEnabled
,
DisplayName
:
"Root User"
,
DisplayName
:
"Root User"
,
AccessToken
:
common
.
GetUUID
(),
}
}
DB
.
Create
(
&
rootUser
)
DB
.
Create
(
&
rootUser
)
}
}
...
...
model/option.go
View file @
9c529f96
...
@@ -46,6 +46,7 @@ func InitOptionMap() {
...
@@ -46,6 +46,7 @@ func InitOptionMap() {
common
.
OptionMap
[
"WeChatAccountQRCodeImageURL"
]
=
""
common
.
OptionMap
[
"WeChatAccountQRCodeImageURL"
]
=
""
common
.
OptionMap
[
"TurnstileSiteKey"
]
=
""
common
.
OptionMap
[
"TurnstileSiteKey"
]
=
""
common
.
OptionMap
[
"TurnstileSecretKey"
]
=
""
common
.
OptionMap
[
"TurnstileSecretKey"
]
=
""
common
.
OptionMap
[
"QuotaForNewUser"
]
=
strconv
.
Itoa
(
common
.
QuotaForNewUser
)
common
.
OptionMapRWMutex
.
Unlock
()
common
.
OptionMapRWMutex
.
Unlock
()
options
,
_
:=
AllOption
()
options
,
_
:=
AllOption
()
for
_
,
option
:=
range
options
{
for
_
,
option
:=
range
options
{
...
@@ -131,5 +132,7 @@ func updateOptionMap(key string, value string) {
...
@@ -131,5 +132,7 @@ func updateOptionMap(key string, value string) {
common
.
TurnstileSiteKey
=
value
common
.
TurnstileSiteKey
=
value
case
"TurnstileSecretKey"
:
case
"TurnstileSecretKey"
:
common
.
TurnstileSecretKey
=
value
common
.
TurnstileSecretKey
=
value
case
"QuotaForNewUser"
:
common
.
QuotaForNewUser
,
_
=
strconv
.
Atoi
(
value
)
}
}
}
}
model/user.go
View file @
9c529f96
...
@@ -2,6 +2,7 @@ package model
...
@@ -2,6 +2,7 @@ package model
import
(
import
(
"errors"
"errors"
"gorm.io/gorm"
"one-api/common"
"one-api/common"
"strings"
"strings"
)
)
...
@@ -21,6 +22,7 @@ type User struct {
...
@@ -21,6 +22,7 @@ type User struct {
VerificationCode
string
`json:"verification_code" gorm:"-:all"`
// this field is only for Email verification, don't save it to database!
VerificationCode
string
`json:"verification_code" gorm:"-:all"`
// this field is only for Email verification, don't save it to database!
Balance
int
`json:"balance" gorm:"type:int;default:0"`
Balance
int
`json:"balance" gorm:"type:int;default:0"`
AccessToken
string
`json:"access_token" gorm:"column:access_token;uniqueIndex"`
// this token is for system management
AccessToken
string
`json:"access_token" gorm:"column:access_token;uniqueIndex"`
// this token is for system management
Quota
int
`json:"quota" gorm:"type:int;default:0"`
}
}
func
GetMaxUserId
()
int
{
func
GetMaxUserId
()
int
{
...
@@ -69,6 +71,8 @@ func (user *User) Insert() error {
...
@@ -69,6 +71,8 @@ func (user *User) Insert() error {
return
err
return
err
}
}
}
}
user
.
Quota
=
common
.
QuotaForNewUser
user
.
AccessToken
=
common
.
GetUUID
()
err
=
DB
.
Create
(
user
)
.
Error
err
=
DB
.
Create
(
user
)
.
Error
return
err
return
err
}
}
...
@@ -202,3 +206,13 @@ func ValidateAccessToken(token string) (user *User) {
...
@@ -202,3 +206,13 @@ func ValidateAccessToken(token string) (user *User) {
}
}
return
nil
return
nil
}
}
func
GetUserQuota
(
id
int
)
(
quota
int
,
err
error
)
{
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
"quota"
)
.
Find
(
&
quota
)
.
Error
return
quota
,
err
}
func
DecreaseUserQuota
(
id
int
,
quota
int
)
(
err
error
)
{
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"quota"
,
gorm
.
Expr
(
"quota - ?"
,
quota
))
.
Error
return
err
}
web/src/components/SystemSetting.js
View file @
9c529f96
...
@@ -24,6 +24,7 @@ const SystemSetting = () => {
...
@@ -24,6 +24,7 @@ const SystemSetting = () => {
TurnstileSiteKey
:
''
,
TurnstileSiteKey
:
''
,
TurnstileSecretKey
:
''
,
TurnstileSecretKey
:
''
,
RegisterEnabled
:
''
,
RegisterEnabled
:
''
,
QuotaForNewUser
:
0
,
});
});
let
originInputs
=
{};
let
originInputs
=
{};
let
[
loading
,
setLoading
]
=
useState
(
false
);
let
[
loading
,
setLoading
]
=
useState
(
false
);
...
@@ -86,7 +87,8 @@ const SystemSetting = () => {
...
@@ -86,7 +87,8 @@ const SystemSetting = () => {
name
===
'WeChatServerToken'
||
name
===
'WeChatServerToken'
||
name
===
'WeChatAccountQRCodeImageURL'
||
name
===
'WeChatAccountQRCodeImageURL'
||
name
===
'TurnstileSiteKey'
||
name
===
'TurnstileSiteKey'
||
name
===
'TurnstileSecretKey'
name
===
'TurnstileSecretKey'
||
name
===
'QuotaForNewUser'
)
{
)
{
setInputs
((
inputs
)
=>
({
...
inputs
,
[
name
]:
value
}));
setInputs
((
inputs
)
=>
({
...
inputs
,
[
name
]:
value
}));
}
else
{
}
else
{
...
@@ -229,6 +231,25 @@ const SystemSetting = () => {
...
@@ -229,6 +231,25 @@ const SystemSetting = () => {
<
/Form.Group
>
<
/Form.Group
>
<
Divider
/>
<
Divider
/>
<
Header
as
=
'h3'
>
<
Header
as
=
'h3'
>
运营设置
<
/Header
>
<
Form
.
Group
widths
=
{
3
}
>
<
Form
.
Input
label
=
'新用户初始配额'
name
=
'QuotaForNewUser'
onChange
=
{
handleInputChange
}
autoComplete
=
'off'
value
=
{
inputs
.
QuotaForNewUser
}
type
=
'number'
min
=
'0'
placeholder
=
'例如:100'
/>
<
/Form.Group
>
<
Form
.
Button
onClick
=
{()
=>
{
updateOption
(
'QuotaForNewUser'
,
inputs
.
QuotaForNewUser
).
then
();
}}
>
保存运营设置
<
/Form.Button
>
<
Divider
/>
<
Header
as
=
'h3'
>
配置
SMTP
配置
SMTP
<
Header
.
Subheader
>
用以支持系统的邮件发送
<
/Header.Subheader
>
<
Header
.
Subheader
>
用以支持系统的邮件发送
<
/Header.Subheader
>
<
/Header
>
<
/Header
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment