Skip to content

Commit

Permalink
allow unset admin
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Mar 20, 2024
1 parent 036b0f6 commit fd19b4b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client/src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ export default {
return await authClient().post("/announcement", { subject, announcement });
},

async setAdmin(email) {
async setAdmin(email, admin) {
await this.refresh_token();
return await authClient().put("/set_admin", { email });
return await authClient().put("/set_admin", { email, admin });
},

// notifications
Expand Down
2 changes: 1 addition & 1 deletion client/src/views/Admin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export default {
const setAdmin = (user) => {
userService
.setAdmin(user.email)
.setAdmin(user.email, true)
.then((response) => {
toast.value.toast(response.data.msg, "#388E3C");
})
Expand Down
11 changes: 9 additions & 2 deletions server/app/admin_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type UpdateMaintenanceInput struct {
// SetAdminInput struct for setting users as admins
type SetAdminInput struct {
Email string `json:"email" binding:"required"`
Admin bool `json:"admin" binding:"required"`
}

// GetAllUsersHandler returns all users
Expand Down Expand Up @@ -293,17 +294,23 @@ func (a *App) SetAdmin(req *http.Request) (interface{}, Response) {
return nil, InternalServerError(errors.New(internalServerErrorMsg))
}

if user.Admin {
if user.Admin && input.Admin {
return ResponseMsg{
Message: "User is already an admin",
}, Ok()
}

if !user.Admin && !input.Admin {
return ResponseMsg{
Message: "User is not already an admin",
}, Ok()
}

err = a.db.UpdateUserByID(
models.User{
ID: user.ID,
Email: input.Email,
Admin: true,
Admin: input.Admin,
UpdatedAt: time.Now(),
},
)
Expand Down

0 comments on commit fd19b4b

Please sign in to comment.