Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
fix: Authentication is added to all the interfaces related to user op…
Browse files Browse the repository at this point in the history
…eration.
  • Loading branch information
PBK-B committed Sep 1, 2021
1 parent be0f60a commit db2d426
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 26 additions & 4 deletions controllers/apis/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *UsersController) ApiGetMe() {
func (c *UsersController) ApiCreateUser() {

// 要求登陆助理函数
userAssistant(&c.Controller)
_, me, _ := userAssistant(&c.Controller)

u_name := c.GetString("name")
u_password := c.GetString("password")
Expand All @@ -79,6 +79,12 @@ func (c *UsersController) ApiCreateUser() {
return
}

if !isAdminUser(me) {
callBackResult(&c.Controller, 403, "权限不足", nil)
c.Finish()
return
}

// TODO: Bin BY 这里应该还可以做判断用户名和密码是否合法
user := models.Users{Name: u_name, Password: helper.StringToMd5(u_password), Status: 1}
user, err := models.AddUsers(&user)
Expand All @@ -99,7 +105,7 @@ func (c *UsersController) ApiCreateUser() {

func (c *UsersController) ApiUpStatusUser() {
// 要求登陆助理函数
userAssistant(&c.Controller)
_, me, _ := userAssistant(&c.Controller)

u_id, _ := c.GetInt("id", 0)

Expand All @@ -116,6 +122,12 @@ func (c *UsersController) ApiUpStatusUser() {
return
}

if !isAdminUser(me) {
callBackResult(&c.Controller, 403, "权限不足", nil)
c.Finish()
return
}

user, err := models.GetUserById(u_id)

if user == nil || err != nil {
Expand Down Expand Up @@ -150,6 +162,7 @@ func (c *UsersController) ApiUpStatusUser() {
c.Finish()
}

// 修改用户密码接口
func (c *UsersController) ApiUpdateUser() {
// 要求登陆助理函数
_, me, _ := userAssistant(&c.Controller)
Expand All @@ -163,8 +176,7 @@ func (c *UsersController) ApiUpdateUser() {
return
}

// TODO: 目前简单判断 ID 为 1 的用户为超级管理员
if me.Id != 1 && me.Id != u_id {
if !isAdminUser(me) && me.Id != u_id {
callBackResult(&c.Controller, 403, "权限不足", nil)
c.Finish()
return
Expand Down Expand Up @@ -228,3 +240,13 @@ func (c *UsersController) ApiUserList() {

callBackResult(&c.Controller, 200, "", new_users)
}

// 做一个简单的判断用户是否属于超级管理员
func isAdminUser(u models.Users) bool {
// TODO: 目前简单判断 ID 为 1 的用户为超级管理员
if u.Id == 1 {
return true
} else {
return false
}
}
6 changes: 1 addition & 5 deletions src/pages/admin/UserControll.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ function UserControll() {
<Button
appearance="link"
onClick={disableAction}
disabled={
rowData.id === UserStore?.me?.id || UserStore?.me?.id === 1
? false
: true
}
disabled={ UserStore?.me?.id !== 1}
>
{rowData.status === '启用' ? ' 禁用 ' : ' 启用 '}
</Button>
Expand Down

0 comments on commit db2d426

Please sign in to comment.