Skip to content
Snippets Groups Projects
Commit 29b94005 authored by Odin Aas's avatar Odin Aas :cry:
Browse files

Merge branch 'system-config' into 'dev-branch'

System config

See merge request !53
parents 66404e71 6fe2f65a
No related branches found
No related tags found
2 merge requests!55Dev branch,!53System config
Pipeline #25994 passed
......@@ -53,6 +53,7 @@ func main() {
http.HandleFunc("/new-department", handlers.NewDepartment)
http.HandleFunc("/new-process", handlers.NewProcess)
http.HandleFunc("/new-enoek", handlers.NewEnoek)
http.HandleFunc("/new-company", handlers.NewCompany)
if os.Getenv("DEP") == "FALSE" {
fmt.Println("Started server listening on port 9090 on development mode")
......
package handlers
import (
"API/other"
"encoding/json"
"log"
"net/http"
)
func NewCompany(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Access-Control-Allow-Origin", ""+other.ADMIN_WEB_URL)
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
if r.Method == http.MethodPost {
data := struct {
SessionToken string `json:"sessionToken"`
CompanyName string `json:"companyName"`
Email string `json:"email"`
Password string `json:"password"`
}{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&data)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
perm, err := other.GetPermissionFromToken(data.SessionToken)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusUnauthorized)
return
}
//check if they are allowed to do request
if perm != -1 {
w.WriteHeader(http.StatusUnauthorized)
return
}
if data.CompanyName == "" || data.Email == "" || data.Password == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
err = other.InsertCompany(data.CompanyName, data.Email, data.Password)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
} else if r.Method == http.MethodOptions {
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
......@@ -31,7 +31,6 @@ func NewGatewayDevice(eui string, name string) OutputGateway {
func DoNewRequest(body io.Reader, url string, method string) error {
//create request
//TODO: replace url with a environment variable
request, err := http.NewRequest(method, url, body)
if err != nil {
return err
......
package other
import (
"golang.org/x/crypto/bcrypt"
"time"
)
......@@ -129,7 +130,7 @@ func DeleteBuilding(id int) error {
}
for _, eui := range euis {
_, err := DB.Exec("DELETE FROM machine_processes WHERE machine_id IN (SELECT id FROM machine WHERE eui = ?)", eui)
_, err := DB.Exec("DELETE FROM machine_processes WHERE machine_id IN (SELECT eui FROM machine WHERE eui = ?)", eui)
if err != nil {
return err
}
......@@ -180,7 +181,7 @@ func EditDepartment(id int, name string) error {
return nil
}
func DeleteDepartment(id int) error {
_, err := DB.Exec("UPDATE machine set department_id=NULL WHERE id=?", id)
_, err := DB.Exec("UPDATE machine set department_id=NULL WHERE department_id=?", id)
if err != nil {
return err
}
......@@ -239,11 +240,25 @@ func DeleteEnoek(id int) error {
}
/*COMPANY FUNCTIONS*/
func InsertCompany(name string) error {
func InsertCompany(name string, email string, password string) error {
_, err := DB.Exec("INSERT INTO company (name) VALUES (?)", name)
if err != nil {
return err
}
row := DB.QueryRow("SELECT id FROM company WHERE name=?", name)
var id = 0
err = row.Scan(&id)
if err != nil {
return err
}
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
if err != nil {
return err
}
_, err = DB.Exec("INSERT INTO users (email, first_name, last_name, password, permission, company_id) VALUES (?, 'admin', 'admin', ?, 0, ?)", email, string(hash), id)
if err != nil {
return err
}
return nil
}
func EditCompany(id int, name string) error {
......@@ -338,6 +353,8 @@ func InsertSensorData(data SensorData) error {
}
return nil
}
// clears ALL sensordata, from all companies, scary!
func ClearSensorData() error {
_, err := DB.Exec("DELETE FROM sensorData")
if err != nil {
......
import axios from "axios"
import { useState } from "react"
import { redirect } from "react-router-dom"
var IngressAPI: String = import.meta.env.VITE_IAPI_URL
function Adminpage() {
const [companyName, setCompanyName] = useState("")
const [email, setEmail] = useState("")
const [password, setPassword] = useState("")
return(
<>
<form>
<div>
<form onSubmit={e => newCompany(e, companyName, email, password)}>
<h1>Add company</h1>
<input type='text'>boob</input>
<a>This creates a company with 1 single admin account, password admin and username admin.</a>
<div>
<label>
Company name
<input type='text' value={companyName} onChange={e => setCompanyName(e.target.value)}/>
</label>
<label>
Email for admin user
<input type='text' value={email} onChange={e => setEmail(e.target.value)} />
</label>
<label>
Password for admin user
<input type='password' value={password} onChange={e => setPassword(e.target.value)} />
</label>
<input type='submit' value='Submit Company'/>
</div>
</form>
</>
</div>
)
}
function newCompany(e: React.FormEvent<HTMLFormElement>, CompName: string, email: string, password: string){
e.preventDefault()
var token: string = ""
var tokenBool = sessionStorage.getItem("TOKEN")
if (tokenBool == null) {
redirect('/')
} else {
token = tokenBool
}
axios.post(IngressAPI + "/new-company", {
sessionToken: token,
companyName: CompName,
email: email,
password: password
})
.catch(error => {
console.log(error)
})
}
export default Adminpage
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment