func userLogin(req nano.Request) (*nano.Response, error) { var body struct { Username string Password string } err := json.Unmarshal(req.Body, &body) if err != nil { return nil, err } user, message, err := getUserFromEmailPassword(body.Username, body.Password) if err != nil { return nil, err } if user != nil { return nano.JSONResponse(200, hash{ "success": true, "user": user, }), nil } return nano.JSONResponse(400, hash{ "success": false, "error": message, }), nil }
func postUsers(req nano.Request) (*nano.Response, error) { var user struct { Email string FirstName string LastName string Password string } err := json.Unmarshal([]byte(req.Body), &user) if err != nil { module.Log.Error(err) return nil, err } newUser, err := CreateUser( true, user.Email, user.FirstName, user.LastName, user.Password, false, true, ) if err != nil { return nano.JSONResponse(409, hash{ "error": err.Error(), }), nil } return nano.JSONResponse(200, hash{ "Id": newUser.Id, }), nil }
func deleteUser(req nano.Request) (*nano.Response, error) { userId := req.Params["id"] if len(userId) == 0 { return nano.JSONResponse(400, hash{ "error": "User id needed for deletion", }), nil } rows, err := db.Query("SELECT is_admin FROM users WHERE id = $1::varchar", userId) if err != nil { module.Log.Error("select user from postgres failed: ", err) return nil, err } if !rows.Next() { rows.Close() return nano.JSONResponse(404, hash{ "error": "User not found", }), nil } isAdmin := false rows.Scan(&isAdmin) rows.Close() if isAdmin { return nano.JSONResponse(403, hash{ "error": "Admins cannot be deleted", }), nil } err = disableADUser(userId) if err != nil { module.Log.Error("Error while deleting user from Active Directory: ", err) return nano.JSONResponse(500, hash{ "error": "Error while deleting user from Active Directory", }), err } rows, err = db.Query("DELETE FROM users WHERE id = $1::varchar", userId) if err != nil { module.Log.Error("delete from postgres failed: ", err) return nil, err } rows.Close() // SendMsg(Message{Method: "Delete", Email: mail}) return nano.JSONResponse(200, hash{ "success": true, }), nil }
func getUsers(req nano.Request) (*nano.Response, error) { users, err := findUsers() if err != nil { return nil, err } return nano.JSONResponse(200, users), nil }
// Get a list of all the log entries of the database func ListCall(req nano.Request) (*nano.Response, error) { var histories []HistoryInfo rows, err := db.Query( `SELECT userid, connectionid, startdate, enddate FROM histories`, ) if err != nil { return nil, err } defer rows.Close() for rows.Next() { history := HistoryInfo{} rows.Scan( &history.UserId, &history.ConnectionId, &history.StartDate, &history.EndDate, ) histories = append(histories, history) } err = rows.Err() if err != nil { return nil, err } return nano.JSONResponse(200, histories), nil }
// Add a new log entry to the database func AddCall(req nano.Request) (*nano.Response, error) { var t HistoryInfo err := json.Unmarshal([]byte(req.Body), &t) if err != nil { return nil, err } rows, err := db.Query( `INSERT INTO histories (userid, connectionid, startdate, enddate) VALUES( $1::varchar, $2::varchar, $3::varchar, $4::varchar) `, t.UserId, t.ConnectionId, t.StartDate, t.EndDate) if err != nil { switch err.Error() { case "pq: duplicate key value violates unique constraint \"history_pkey\"": err = errors.New("history connection id exists already") } return nil, err } rows.Close() return nano.JSONResponse(200, hash{ "success": true, }), nil }
func getUser(req nano.Request) (*nano.Response, error) { userId := req.Params["id"] if userId == "" { return nano.JSONResponse(400, hash{ "error": "User id needed to retrieve account informations", }), nil } rows, err := db.Query( `SELECT id, first_name, last_name, email, is_admin, activated, sam, windows_password FROM users WHERE id = $1::varchar`, userId) if err != nil { return nil, err } defer rows.Close() if rows.Next() { var user nano.User rows.Scan( &user.Id, &user.FirstName, &user.LastName, &user.Email, &user.IsAdmin, &user.Activated, &user.Sam, &user.WindowsPassword, ) return nano.JSONResponse(200, user), nil } return nano.JSONResponse(404, hash{ "error": "User Not Found", }), nil }
func disableUser(req nano.Request) (*nano.Response, error) { userId := req.Params["id"] if userId == "" { return nano.JSONResponse(404, hash{ "error": "User id needed for desactivation", }), nil } rows, err := db.Query( `UPDATE users SET activated = false WHERE id = $1::varchar`, userId) if err != nil { return nil, err } rows.Close() return nano.JSONResponse(200, hash{ "success": true, }), nil }
func updateUserPassword(req nano.Request) (*nano.Response, error) { userId := req.Params["id"] if userId == "" { return nano.JSONResponse(400, hash{ "error": "Email needed to modify account", }), nil } var user struct { Password string } err := json.Unmarshal(req.Body, &user) if err != nil { return nil, err } pass, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost) if err != nil { return nil, err } rows, err := db.Query( `UPDATE users SET password = $1::varchar WHERE id = $2::varchar`, pass, userId) if err != nil { return nil, err } rows.Close() return nano.JSONResponse(200, hash{ "success": true, }), nil }
func ListRunningVm(req nano.Request) (*nano.Response, error) { var ( response struct { Result struct { DownloadingVmNames []string AvailableVMNames []string BootingVmNames []string RunningVmNames []string } Error string Id int } vmList []VmInfo icon string locked bool Status string displayName string ) jsonResponse, err := jsonRpcRequest( fmt.Sprintf("%s:%s", apiURL, apiPort), "Iaas.GetList", nil, ) if err != nil { module.Log.Error("RPC Call to Iaas API failed: ", err) return nil, err } err = json.Unmarshal([]byte(jsonResponse), &response) if err != nil { module.Log.Error("Failed to Unmarshal response from Iaas API: ", err) return nil, err } // TODO: Lots of Data aren't from iaas API for _, vmName := range response.Result.AvailableVMNames { locked = false if strings.Contains(vmName, "windows") { if strings.Contains(vmName, "winapps") { icon = "settings_applications" displayName = "Execution environment" } else { icon = "windows" displayName = "Windows Active Directory" } } else { if strings.Contains(vmName, "drive") { icon = "storage" displayName = "Drive" } else if strings.Contains(vmName, "licence") { icon = "vpn_lock" displayName = "Windows Licence service" } else { icon = "apps" locked = true displayName = "Haptic" } } if stringInSlice(vmName, response.Result.RunningVmNames) { Status = "running" } else if stringInSlice(vmName, response.Result.BootingVmNames) { Status = "booting" } else if stringInSlice(vmName, response.Result.DownloadingVmNames) { Status = "download" } else if stringInSlice(vmName, response.Result.AvailableVMNames) { Status = "available" } vmList = append(vmList, VmInfo{ Ico: icon, Name: vmName, DisplayName: displayName, Status: Status, Locked: locked, }) } return nano.JSONResponse(200, vmList), nil }