Big refactoring of PARCOURSMOB - Initial commit
This commit is contained in:
30
handlers/api/api.go
Normal file
30
handlers/api/api.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type APIHandler struct {
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
cache *cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewAPIHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*APIHandler, error) {
|
||||
return &APIHandler{
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
services: svc,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *APIHandler) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
51
handlers/api/cache.go
Normal file
51
handlers/api/cache.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
cacheid := vars["cacheid"]
|
||||
|
||||
d, err := h.cache.Get(cacheid)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
result := d
|
||||
|
||||
if data, ok := d.([]any); ok {
|
||||
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
|
||||
min, _ := strconv.Atoi(limitsmin[0])
|
||||
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
|
||||
max, _ := strconv.Atoi(limitsmax[0])
|
||||
if max > len(data) {
|
||||
result = data[min:]
|
||||
} else {
|
||||
result = data[min:max]
|
||||
}
|
||||
} else {
|
||||
result = data[min:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
j, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(j)
|
||||
|
||||
}
|
||||
54
handlers/api/geo.go
Normal file
54
handlers/api/geo.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *APIHandler) GeoAutocomplete(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
pelias := h.config.GetString("geo.pelias.url")
|
||||
|
||||
t, ok := r.URL.Query()["text"]
|
||||
|
||||
if !ok || len(t[0]) < 1 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
text := t[0]
|
||||
|
||||
resp, err := http.Get(fmt.Sprintf("%s/autocomplete?text=%s", pelias, text))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var response map[string]any
|
||||
jsonErr := json.Unmarshal(body, &response)
|
||||
if jsonErr != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
j, err := json.Marshal(response["features"])
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(j)
|
||||
}
|
||||
46
handlers/api/oidc.go
Normal file
46
handlers/api/oidc.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h APIHandler) OAuth2Callback(w http.ResponseWriter, r *http.Request) {
|
||||
oauth2Token, err := h.idp.OAuth2Config.Exchange(context.Background(), r.URL.Query().Get("code"))
|
||||
if err != nil {
|
||||
fmt.Println("Exchange error")
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract the ID Token from OAuth2 token.
|
||||
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = h.idp.TokenVerifier.Verify(context.Background(), rawIDToken)
|
||||
if err != nil {
|
||||
fmt.Println("not able to verify token")
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
session.Values["idtoken"] = rawIDToken
|
||||
|
||||
redirect := "/app/"
|
||||
|
||||
if session.Values["redirect"] != nil && session.Values["redirect"] != "" {
|
||||
redirect = session.Values["redirect"].(string)
|
||||
delete(session.Values, "redirect")
|
||||
}
|
||||
|
||||
session.Save(r, w)
|
||||
|
||||
http.Redirect(w, r, redirect, http.StatusFound)
|
||||
}
|
||||
123
handlers/application/administration.go
Normal file
123
handlers/application/administration.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
func (h *ApplicationHandler) Administration(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
request := &groupsmanagement.GetGroupsRequest{
|
||||
Namespaces: []string{"parcoursmob_organizations"},
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.GroupsManagement.GetGroups(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var groups = []any{}
|
||||
|
||||
for _, group := range resp.Groups {
|
||||
g := group.ToStorageType()
|
||||
groups = append(groups, g)
|
||||
}
|
||||
|
||||
h.Renderer.Administration(w, r, groups)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) AdministrationCreateGroup(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
r.ParseForm()
|
||||
|
||||
if r.FormValue("name") == "" {
|
||||
|
||||
fmt.Println("invalid name")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
modules := map[string]any{
|
||||
"beneficiaries": r.FormValue("modules.beneficiaries") == "on",
|
||||
"journeys": r.FormValue("modules.journeys") == "on",
|
||||
"vehicles": r.FormValue("modules.vehicles") == "on",
|
||||
"vehicles_management": r.FormValue("modules.vehicles_management") == "on",
|
||||
"events": r.FormValue("modules.events") == "on",
|
||||
"administration": r.FormValue("modules.administration") == "on",
|
||||
}
|
||||
|
||||
groupid := uuid.NewString()
|
||||
|
||||
dataMap := map[string]any{
|
||||
"name": r.FormValue("name"),
|
||||
"modules": modules,
|
||||
}
|
||||
|
||||
data, err := structpb.NewValue(dataMap)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
request_organization := &groupsmanagement.AddGroupRequest{
|
||||
Group: &groupsmanagement.Group{
|
||||
Id: groupid,
|
||||
Namespace: "parcoursmob_organizations",
|
||||
Data: data.GetStructValue(),
|
||||
},
|
||||
}
|
||||
|
||||
request_role := &groupsmanagement.AddGroupRequest{
|
||||
Group: &groupsmanagement.Group{
|
||||
Id: groupid + ":admin",
|
||||
Namespace: "parcoursmob_roles",
|
||||
},
|
||||
}
|
||||
|
||||
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_organization)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Create the admin role for the organization
|
||||
_, err = h.services.GRPC.GroupsManagement.AddGroup(context.TODO(), request_role)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
|
||||
return
|
||||
}
|
||||
h.Renderer.AdministrationCreateGroup(w, r)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) AdministrationGroupDisplay(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
groupid := vars["groupid"]
|
||||
|
||||
request := &groupsmanagement.GetGroupRequest{
|
||||
Id: groupid,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
h.Renderer.AdministrationGroupDisplay(w, r, resp.Group.ToStorageType())
|
||||
}
|
||||
36
handlers/application/application.go
Normal file
36
handlers/application/application.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ApplicationHandler struct {
|
||||
config *viper.Viper
|
||||
Renderer *renderer.Renderer
|
||||
services *services.ServicesHandler
|
||||
cache *cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewApplicationHandler(cfg *viper.Viper, svc *services.ServicesHandler, cache *cache.CacheHandler) (*ApplicationHandler, error) {
|
||||
templates_root := cfg.GetString("templates.root")
|
||||
renderer := renderer.NewRenderer(cfg, templates_root)
|
||||
return &ApplicationHandler{
|
||||
config: cfg,
|
||||
Renderer: renderer,
|
||||
services: svc,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) templateFile(file string) string {
|
||||
return h.config.GetString("templates.root") + file
|
||||
}
|
||||
305
handlers/application/beneficiaries.go
Normal file
305
handlers/application/beneficiaries.go
Normal file
@@ -0,0 +1,305 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
formvalidators "git.coopgo.io/coopgo-apps/parcoursmob/utils/form-validators"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
profilepictures "git.coopgo.io/coopgo-apps/parcoursmob/utils/profile-pictures"
|
||||
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
type BeneficiariesForm struct {
|
||||
FirstName string `json:"first_name" validate:"required"`
|
||||
LastName string `json:"last_name" validate:"required"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Birthdate *time.Time `json:"birthdate"`
|
||||
PhoneNumber string `json:"phone_number" validate:"required,phoneNumber"`
|
||||
Address any `json:"address,omitempty"`
|
||||
Gender string `json:"gender"`
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiariesList(w http.ResponseWriter, r *http.Request) {
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
request := &mobilityaccounts.GetAccountsBatchRequest{
|
||||
Accountids: group.Members,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var accounts = []any{}
|
||||
|
||||
for _, account := range resp.Accounts {
|
||||
if filterAccount(r, account) {
|
||||
a := account.ToStorageType()
|
||||
accounts = append(accounts, a)
|
||||
}
|
||||
}
|
||||
|
||||
cacheid := uuid.NewString()
|
||||
h.cache.PutWithTTL(cacheid, accounts, 1*time.Hour)
|
||||
|
||||
h.Renderer.BeneficiariesList(w, r, accounts, cacheid)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiaryCreate(w http.ResponseWriter, r *http.Request) {
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
fmt.Println(group)
|
||||
|
||||
if r.Method == "POST" {
|
||||
|
||||
dataMap, err := parseForm(r)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := structpb.NewValue(dataMap)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
request := &mobilityaccounts.RegisterRequest{
|
||||
Account: &mobilityaccounts.Account{
|
||||
Namespace: "parcoursmob_beneficiaries",
|
||||
Data: data.GetStructValue(),
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.Register(context.TODO(), request)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
subscribe := &groupsmanagement.SubscribeRequest{
|
||||
Groupid: group.ID,
|
||||
Memberid: resp.Account.Id,
|
||||
}
|
||||
|
||||
_, err = h.services.GRPC.GroupsManagement.Subscribe(context.TODO(), subscribe)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/beneficiaries/%s", resp.Account.Id), http.StatusFound)
|
||||
|
||||
return
|
||||
}
|
||||
h.Renderer.BeneficiaryCreate(w, r)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiaryDisplay(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
beneficiaryID := vars["beneficiaryid"]
|
||||
|
||||
request := &mobilityaccounts.GetAccountRequest{
|
||||
Id: beneficiaryID,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
//TODO filter namespaces
|
||||
//TODO filter groups
|
||||
|
||||
h.Renderer.BeneficiaryDisplay(w, r, resp.Account.ToStorageType())
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiaryUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
beneficiaryID := vars["beneficiaryid"]
|
||||
|
||||
if r.Method == "POST" {
|
||||
|
||||
dataMap, err := parseForm(r)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := structpb.NewValue(dataMap)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
request := &mobilityaccounts.UpdateDataRequest{
|
||||
Account: &mobilityaccounts.Account{
|
||||
Id: beneficiaryID,
|
||||
Namespace: "parcoursmob_beneficiaries",
|
||||
Data: data.GetStructValue(),
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.UpdateData(context.TODO(), request)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/app/beneficiaries/%s", resp.Account.Id), http.StatusFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
request := &mobilityaccounts.GetAccountRequest{
|
||||
Id: beneficiaryID,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
//TODO filter namespaces
|
||||
//TODO filter groups
|
||||
|
||||
h.Renderer.BeneficiaryUpdate(w, r, resp.Account.ToStorageType())
|
||||
}
|
||||
|
||||
func parseForm(r *http.Request) (map[string]any, error) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var date *time.Time
|
||||
|
||||
if r.PostFormValue("birthdate") != "" {
|
||||
d, err := time.Parse("2006-01-02", r.PostFormValue("birthdate"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
date = &d
|
||||
}
|
||||
|
||||
formData := BeneficiariesForm{
|
||||
FirstName: r.PostFormValue("first_name"),
|
||||
LastName: r.PostFormValue("last_name"),
|
||||
Email: r.PostFormValue("email"),
|
||||
Birthdate: date,
|
||||
PhoneNumber: r.PostFormValue("phone_number"),
|
||||
Gender: r.PostFormValue("gender"),
|
||||
}
|
||||
|
||||
if r.PostFormValue("address") != "" {
|
||||
var a any
|
||||
json.Unmarshal([]byte(r.PostFormValue("address")), &a)
|
||||
|
||||
formData.Address = a
|
||||
}
|
||||
|
||||
validate := formvalidators.New()
|
||||
if err := validate.Struct(formData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
d, err := json.Marshal(formData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var dataMap map[string]any
|
||||
err = json.Unmarshal(d, &dataMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dataMap, nil
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) BeneficiaryPicture(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
beneficiaryID := vars["beneficiaryid"]
|
||||
|
||||
request := &mobilityaccounts.GetAccountRequest{
|
||||
Id: beneficiaryID,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.GetAccount(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
account := resp.Account.ToStorageType()
|
||||
|
||||
firstName := account.Data["first_name"].(string)
|
||||
lastName := account.Data["last_name"].(string)
|
||||
picture := profilepictures.DefaultProfilePicture(strings.ToUpper(firstName[0:1] + lastName[0:1]))
|
||||
|
||||
buffer := new(bytes.Buffer)
|
||||
if err := png.Encode(buffer, picture); err != nil {
|
||||
log.Println("unable to encode image.")
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
|
||||
if _, err := w.Write(buffer.Bytes()); err != nil {
|
||||
log.Println("unable to write image.")
|
||||
}
|
||||
}
|
||||
|
||||
func filterAccount(r *http.Request, a *mobilityaccounts.Account) bool {
|
||||
searchFilter, ok := r.URL.Query()["search"]
|
||||
|
||||
if ok && len(searchFilter[0]) > 0 {
|
||||
name := a.Data.AsMap()["first_name"].(string) + " " + a.Data.AsMap()["last_name"].(string)
|
||||
if !strings.Contains(strings.ToLower(name), strings.ToLower(searchFilter[0])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
52
handlers/application/dashboard.go
Normal file
52
handlers/application/dashboard.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
)
|
||||
|
||||
func (h *ApplicationHandler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
g := r.Context().Value(identification.GroupKey)
|
||||
if g == nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
group := g.(storage.Group)
|
||||
|
||||
request := &mobilityaccounts.GetAccountsBatchRequest{
|
||||
Accountids: group.Members,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.MobilityAccounts.GetAccountsBatch(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var accounts = []any{}
|
||||
|
||||
// We only display the 10 last here
|
||||
count := len(resp.Accounts)
|
||||
min := count - 10
|
||||
if min < 0 {
|
||||
min = 0
|
||||
}
|
||||
|
||||
for _, account := range resp.Accounts[min:] {
|
||||
if filterAccount(r, account) {
|
||||
a := account.ToStorageType()
|
||||
accounts = append([]any{a}, accounts...)
|
||||
}
|
||||
}
|
||||
|
||||
h.Renderer.Dashboard(w, r, accounts, count)
|
||||
|
||||
}
|
||||
24
handlers/application/vehicles-management.go
Normal file
24
handlers/application/vehicles-management.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (h *ApplicationHandler) VehiclesManagementOverview(w http.ResponseWriter, r *http.Request) {
|
||||
h.Renderer.VehiclesManagementOverview(w, r)
|
||||
}
|
||||
|
||||
func (h *ApplicationHandler) VehiclesFleetAdd(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "POST" {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(r.Form)
|
||||
return
|
||||
}
|
||||
h.Renderer.VehiclesFleetAdd(w, r)
|
||||
}
|
||||
116
handlers/auth/groups.go
Normal file
116
handlers/auth/groups.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/renderer"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/services"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
groupsmanagement "git.coopgo.io/coopgo-platform/groups-management/grpcapi"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
Renderer *renderer.Renderer
|
||||
cache *cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewAuthHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*AuthHandler, error) {
|
||||
templates_root := cfg.GetString("templates.root")
|
||||
renderer := renderer.NewRenderer(cfg, templates_root)
|
||||
return &AuthHandler{
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
services: svc,
|
||||
Renderer: renderer,
|
||||
cache: cache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Groups(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
|
||||
if r.Method == "POST" {
|
||||
r.ParseForm()
|
||||
|
||||
groupid := r.FormValue("group")
|
||||
|
||||
session.Values["organization"] = groupid
|
||||
session.Save(r, w)
|
||||
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
tokenstring, ok := session.Values["idtoken"]
|
||||
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
idtoken, err := h.idp.TokenVerifier.Verify(context.Background(), tokenstring.(string))
|
||||
if err != nil {
|
||||
delete(session.Values, "idtoken")
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
var claims map[string]any
|
||||
|
||||
err = idtoken.Claims(&claims)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
g := claims["groups"]
|
||||
|
||||
groups_interface, ok := g.([]any)
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
groups := []string{}
|
||||
|
||||
for _, v := range groups_interface {
|
||||
groups = append(groups, v.(string))
|
||||
}
|
||||
|
||||
request := &groupsmanagement.GetGroupsBatchRequest{
|
||||
Groupids: groups,
|
||||
}
|
||||
|
||||
resp, err := h.services.GRPC.GroupsManagement.GetGroupsBatch(context.TODO(), request)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var groupsresponse = []any{}
|
||||
|
||||
for _, group := range resp.Groups {
|
||||
if group.Namespace != "parcoursmob_organizations" {
|
||||
continue
|
||||
}
|
||||
g := group.ToStorageType()
|
||||
groupsresponse = append(groupsresponse, g)
|
||||
}
|
||||
|
||||
h.Renderer.AuthGroups(w, r, groupsresponse)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) GroupSwitch(w http.ResponseWriter, r *http.Request) {
|
||||
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
|
||||
delete(session.Values, "organization")
|
||||
session.Save(r, w)
|
||||
http.Redirect(w, r, "/app/", http.StatusFound)
|
||||
}
|
||||
Reference in New Issue
Block a user