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
512f9e3a
authored
Apr 26, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: limit the ability of common user to set the remaining usage times of token (#9)
parent
f3a7f812
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
7 deletions
+18
-7
controller/token.go
+8
-2
web/src/pages/Token/EditToken.js
+10
-5
No files found.
controller/token.go
View file @
512f9e3a
...
@@ -76,6 +76,7 @@ func GetToken(c *gin.Context) {
...
@@ -76,6 +76,7 @@ func GetToken(c *gin.Context) {
}
}
func
AddToken
(
c
*
gin
.
Context
)
{
func
AddToken
(
c
*
gin
.
Context
)
{
isAdmin
:=
c
.
GetInt
(
"role"
)
>=
common
.
RoleAdminUser
token
:=
model
.
Token
{}
token
:=
model
.
Token
{}
err
:=
c
.
ShouldBindJSON
(
&
token
)
err
:=
c
.
ShouldBindJSON
(
&
token
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -99,8 +100,10 @@ func AddToken(c *gin.Context) {
...
@@ -99,8 +100,10 @@ func AddToken(c *gin.Context) {
CreatedTime
:
common
.
GetTimestamp
(),
CreatedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
AccessedTime
:
common
.
GetTimestamp
(),
ExpiredTime
:
token
.
ExpiredTime
,
ExpiredTime
:
token
.
ExpiredTime
,
RemainTimes
:
token
.
RemainTimes
,
}
UnlimitedTimes
:
token
.
UnlimitedTimes
,
if
isAdmin
{
cleanToken
.
RemainTimes
=
token
.
RemainTimes
cleanToken
.
UnlimitedTimes
=
token
.
UnlimitedTimes
}
}
err
=
cleanToken
.
Insert
()
err
=
cleanToken
.
Insert
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -136,6 +139,7 @@ func DeleteToken(c *gin.Context) {
...
@@ -136,6 +139,7 @@ func DeleteToken(c *gin.Context) {
}
}
func
UpdateToken
(
c
*
gin
.
Context
)
{
func
UpdateToken
(
c
*
gin
.
Context
)
{
isAdmin
:=
c
.
GetInt
(
"role"
)
>=
common
.
RoleAdminUser
userId
:=
c
.
GetInt
(
"id"
)
userId
:=
c
.
GetInt
(
"id"
)
statusOnly
:=
c
.
Query
(
"status_only"
)
statusOnly
:=
c
.
Query
(
"status_only"
)
token
:=
model
.
Token
{}
token
:=
model
.
Token
{}
...
@@ -177,9 +181,11 @@ func UpdateToken(c *gin.Context) {
...
@@ -177,9 +181,11 @@ func UpdateToken(c *gin.Context) {
// If you add more fields, please also update token.Update()
// If you add more fields, please also update token.Update()
cleanToken
.
Name
=
token
.
Name
cleanToken
.
Name
=
token
.
Name
cleanToken
.
ExpiredTime
=
token
.
ExpiredTime
cleanToken
.
ExpiredTime
=
token
.
ExpiredTime
if
isAdmin
{
cleanToken
.
RemainTimes
=
token
.
RemainTimes
cleanToken
.
RemainTimes
=
token
.
RemainTimes
cleanToken
.
UnlimitedTimes
=
token
.
UnlimitedTimes
cleanToken
.
UnlimitedTimes
=
token
.
UnlimitedTimes
}
}
}
err
=
cleanToken
.
Update
()
err
=
cleanToken
.
Update
()
if
err
!=
nil
{
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
...
...
web/src/pages/Token/EditToken.js
View file @
512f9e3a
import
React
,
{
useEffect
,
useState
}
from
'react'
;
import
React
,
{
useEffect
,
useState
}
from
'react'
;
import
{
Button
,
Form
,
Header
,
Segment
}
from
'semantic-ui-react'
;
import
{
Button
,
Form
,
Header
,
Segment
}
from
'semantic-ui-react'
;
import
{
useParams
}
from
'react-router-dom'
;
import
{
useParams
}
from
'react-router-dom'
;
import
{
API
,
showError
,
showSuccess
,
timestamp2string
}
from
'../../helpers'
;
import
{
API
,
isAdmin
,
showError
,
showSuccess
,
timestamp2string
}
from
'../../helpers'
;
const
EditToken
=
()
=>
{
const
EditToken
=
()
=>
{
const
params
=
useParams
();
const
params
=
useParams
();
...
@@ -12,8 +12,9 @@ const EditToken = () => {
...
@@ -12,8 +12,9 @@ const EditToken = () => {
name
:
''
,
name
:
''
,
remain_times
:
0
,
remain_times
:
0
,
expired_time
:
-
1
,
expired_time
:
-
1
,
unlimited_times
:
false
,
unlimited_times
:
false
};
};
const
isAdminUser
=
isAdmin
();
const
[
inputs
,
setInputs
]
=
useState
(
originInputs
);
const
[
inputs
,
setInputs
]
=
useState
(
originInputs
);
const
{
name
,
remain_times
,
expired_time
,
unlimited_times
}
=
inputs
;
const
{
name
,
remain_times
,
expired_time
,
unlimited_times
}
=
inputs
;
...
@@ -38,7 +39,7 @@ const EditToken = () => {
...
@@ -38,7 +39,7 @@ const EditToken = () => {
const
setUnlimitedTimes
=
()
=>
{
const
setUnlimitedTimes
=
()
=>
{
setInputs
({
...
inputs
,
unlimited_times
:
!
unlimited_times
});
setInputs
({
...
inputs
,
unlimited_times
:
!
unlimited_times
});
}
}
;
const
loadToken
=
async
()
=>
{
const
loadToken
=
async
()
=>
{
let
res
=
await
API
.
get
(
`/api/token/
${
tokenId
}
`
);
let
res
=
await
API
.
get
(
`/api/token/
${
tokenId
}
`
);
...
@@ -93,7 +94,7 @@ const EditToken = () => {
...
@@ -93,7 +94,7 @@ const EditToken = () => {
return
(
return
(
<>
<>
<
Segment
loading
=
{
loading
}
>
<
Segment
loading
=
{
loading
}
>
<
Header
as
=
'h3'
>
{
isEdit
?
"更新令牌信息"
:
"创建新的令牌"
}
<
/Header
>
<
Header
as
=
'h3'
>
{
isEdit
?
'更新令牌信息'
:
'创建新的令牌'
}
<
/Header
>
<
Form
autoComplete
=
'off'
>
<
Form
autoComplete
=
'off'
>
<
Form
.
Field
>
<
Form
.
Field
>
<
Form
.
Input
<
Form
.
Input
...
@@ -106,6 +107,8 @@ const EditToken = () => {
...
@@ -106,6 +107,8 @@ const EditToken = () => {
required
=
{
!
isEdit
}
required
=
{
!
isEdit
}
/
>
/
>
<
/Form.Field
>
<
/Form.Field
>
{
isAdminUser
&&
<>
<
Form
.
Field
>
<
Form
.
Field
>
<
Form
.
Input
<
Form
.
Input
label
=
'剩余次数'
label
=
'剩余次数'
...
@@ -120,7 +123,9 @@ const EditToken = () => {
...
@@ -120,7 +123,9 @@ const EditToken = () => {
<
/Form.Field
>
<
/Form.Field
>
<
Button
type
=
{
'button'
}
onClick
=
{()
=>
{
<
Button
type
=
{
'button'
}
onClick
=
{()
=>
{
setUnlimitedTimes
();
setUnlimitedTimes
();
}}
>
{
unlimited_times
?
"取消无限次"
:
"设置为无限次"
}
<
/Button
>
}}
>
{
unlimited_times
?
'取消无限次'
:
'设置为无限次'
}
<
/Button
>
<
/
>
}
<
Form
.
Field
>
<
Form
.
Field
>
<
Form
.
Input
<
Form
.
Input
label
=
'过期时间'
label
=
'过期时间'
...
...
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