added ability to change other uses

This commit is contained in:
frikky
2020-06-22 13:39:51 +02:00
parent 7c940e57d4
commit ec9254ffa2
2 changed files with 150 additions and 11 deletions
+85 -2
View File
@@ -1285,6 +1285,88 @@ func generateApikey(ctx context.Context, userInfo User) (User, error) {
return userInfo, nil return userInfo, nil
} }
func handleUpdateUser(resp http.ResponseWriter, request *http.Request) {
cors := handleCors(resp, request)
if cors {
return
}
userInfo, err := handleApiAuthentication(resp, request)
if err != nil {
log.Printf("Api authentication failed in apigen: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(`{"success": false}`))
return
}
body, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Println("Failed reading body")
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Missing field: user_id"}`)))
return
}
type newUserStruct struct {
Role string `json:"role"`
UserId string `json:"user_id"`
}
ctx := context.Background()
var t newUserStruct
err = json.Unmarshal(body, &t)
if err != nil {
log.Printf("Failed unmarshaling userId: %s", err)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed unmarshaling. Missing field: user_id"}`)))
return
}
if userInfo.Role != "admin" {
log.Printf("%s tried to update user %s", userInfo.Username, t.UserId)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "You need to be admin to change other users"}`)))
return
}
foundUser, err := getUser(ctx, t.UserId)
if err != nil {
log.Printf("Can't find user %s (apikey gen)", t.UserId, err)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
return
}
if t.Role != "admin" && t.Role != "user" {
log.Printf("%s tried and failed to update user %s", userInfo.Username, t.UserId)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can only change to role user and admin"}`)))
return
} else {
// Same user - can't edit yourself
if userInfo.Id == t.UserId {
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Can't update the role of your own user"}`)))
return
}
log.Printf("Updated user %s from %s to %s", foundUser.Username, foundUser.Role, t.Role)
foundUser.Role = t.Role
foundUser.Roles = []string{t.Role}
}
err = setUser(ctx, foundUser)
if err != nil {
log.Printf("Error patching user %s: %s", foundUser.Username, err)
resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
return
}
resp.WriteHeader(200)
resp.Write([]byte(fmt.Sprintf(`{"success": true}`)))
}
func handleApiGeneration(resp http.ResponseWriter, request *http.Request) { func handleApiGeneration(resp http.ResponseWriter, request *http.Request) {
cors := handleCors(resp, request) cors := handleCors(resp, request)
if cors { if cors {
@@ -1343,8 +1425,8 @@ func handleApiGeneration(resp http.ResponseWriter, request *http.Request) {
foundUser, err := getUser(ctx, t.UserId) foundUser, err := getUser(ctx, t.UserId)
if err != nil { if err != nil {
log.Printf("Can't find user %s (apikey gen)", t.UserId, err) log.Printf("Can't find user %s (apikey gen)", t.UserId, err)
resp.WriteHeader(200) resp.WriteHeader(401)
resp.Write([]byte(fmt.Sprintf(`{"success": true}`))) resp.Write([]byte(fmt.Sprintf(`{"success": false}`)))
return return
} }
@@ -6158,6 +6240,7 @@ func init() {
r.HandleFunc("/api/v1/users/getinfo", handleInfo).Methods("GET", "OPTIONS") r.HandleFunc("/api/v1/users/getinfo", handleInfo).Methods("GET", "OPTIONS")
r.HandleFunc("/api/v1/users/getsettings", handleSettings).Methods("GET", "OPTIONS") r.HandleFunc("/api/v1/users/getsettings", handleSettings).Methods("GET", "OPTIONS")
r.HandleFunc("/api/v1/users/generateapikey", handleApiGeneration).Methods("GET", "OPTIONS") r.HandleFunc("/api/v1/users/generateapikey", handleApiGeneration).Methods("GET", "OPTIONS")
r.HandleFunc("/api/v1/users/updateuser", handleUpdateUser).Methods("PUT", "OPTIONS")
r.HandleFunc("/api/v1/users/{user}", deleteUser).Methods("DELETE", "OPTIONS") r.HandleFunc("/api/v1/users/{user}", deleteUser).Methods("DELETE", "OPTIONS")
// General - duplicates and old. // General - duplicates and old.
+65 -9
View File
@@ -2,6 +2,8 @@ import React, { useEffect} from 'react';
import {Link} from 'react-router-dom'; import {Link} from 'react-router-dom';
import Paper from '@material-ui/core/Paper'; import Paper from '@material-ui/core/Paper';
import Select from '@material-ui/core/Select';
import MenuItem from '@material-ui/core/MenuItem';
import List from '@material-ui/core/List'; import List from '@material-ui/core/List';
import Divider from '@material-ui/core/Divider'; import Divider from '@material-ui/core/Divider';
import TextField from '@material-ui/core/TextField'; import TextField from '@material-ui/core/TextField';
@@ -319,6 +321,43 @@ const Admin = (props) => {
modalUser[field] = value modalUser[field] = value
} }
const setUser = (userId, field, value) => {
const data = {"user_id": userId}
data[field] = value
console.log("DATA: ", data)
fetch(globalUrl+"/api/v1/users/updateuser", {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(data),
credentials: "include",
})
.then((response) => {
if (response.status !== 200) {
console.log("Status not 200 for WORKFLOW EXECUTION :O!")
} else {
getUsers()
}
return response.json()
})
.then((responseJson) => {
if (!responseJson.success && responseJson.reason !== undefined) {
alert.error("Failed setting user: "+responseJson.reason)
} else {
alert.success("Set the user field "+field+" to "+value)
}
})
.catch(error => {
console.log(error)
});
}
const generateApikey = (userId) => { const generateApikey = (userId) => {
const data = {"user_id": userId} const data = {"user_id": userId}
@@ -543,10 +582,6 @@ const Admin = (props) => {
primary="API key" primary="API key"
style={{minWidth: 350, maxWidth: 350, overflow: "hidden"}} style={{minWidth: 350, maxWidth: 350, overflow: "hidden"}}
/> />
<ListItemText
primary="Password"
style={{minWidth: 180, maxWidth: 180}}
/>
<ListItemText <ListItemText
primary="Role" primary="Role"
style={{minWidth: 150, maxWidth: 150}} style={{minWidth: 150, maxWidth: 150}}
@@ -572,11 +607,32 @@ const Admin = (props) => {
style={{maxWidth: 350, minWidth: 350,}} style={{maxWidth: 350, minWidth: 350,}}
/> />
<ListItemText <ListItemText
primary="**************" primary=
style={{minWidth: 180, maxWidth: 180}} <Select
/> PaperProps={{
<ListItemText style: {
primary={data.role} }
}}
SelectDisplayProps={{
style: {
marginLeft: 10,
}
}}
value={data.role}
fullWidth
onChange={(e) => {
console.log("VALUE: ", e.target.value)
setUser(data.id, "role", e.target.value)
}}
style={{backgroundColor: surfaceColor, color: "white", height: "50px"}}
>
<MenuItem style={{backgroundColor: inputColor, color: "white"}} value={"admin"}>
Admin
</MenuItem>
<MenuItem style={{backgroundColor: inputColor, color: "white"}} value={"user"}>
User
</MenuItem>
</Select>
style={{minWidth: 150, maxWidth: 150}} style={{minWidth: 150, maxWidth: 150}}
/> />
<ListItemText <ListItemText