fix issues in groups members management
Build and Push Docker Image / build_and_push (push) Failing after 1m26s Details

This commit is contained in:
Arnaud Delcasse 2024-11-21 23:11:59 +01:00
parent dd30d7959b
commit d46f62d92a
5 changed files with 136 additions and 149 deletions

View File

@ -100,7 +100,6 @@ func (h *APIHandler) CalendarOrganizations(w http.ResponseWriter, r *http.Reques
for _, e := range events { for _, e := range events {
for _, g := range e.Owners { for _, g := range e.Owners {
log.Debug().Str("groupid", groupid).Str("g", g).Msg("check identical")
if g == groupid { if g == groupid {
filteredEvents = append(filteredEvents, e) filteredEvents = append(filteredEvents, e)
continue continue

View File

@ -224,162 +224,141 @@ func (h *ApplicationHandler) AdministrationGroupInviteAdmin(w http.ResponseWrite
accountresp *accounts.GetAccountUsernameResponse accountresp *accounts.GetAccountUsernameResponse
err error err error
) )
go func() { groupresp, err = h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
groupresp, err = h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ Id: groupid,
Id: groupid, Namespace: "parcoursmob_organizations",
Namespace: "parcoursmob_organizations", })
}) if err != nil {
log.Error().Err(err).Msg("Issue in Groups management service - GetGroup")
w.WriteHeader(http.StatusInternalServerError)
return
}
r.ParseForm()
if err != nil { accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
log.Error().Err(err).Msg("Issue in Groups management service - GetGroup") Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
log.Print("account exists")
// Account already exists : adding the existing account to admin list
account := accountresp.Account.ToStorageType()
// account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")
as, _ := accounts.AccountFromStorageType(&account)
if _, err = h.services.GRPC.MobilityAccounts.UpdateData(context.TODO(), &accounts.UpdateDataRequest{Account: as}); err != nil {
log.Error().Err(err).Msg("could not set groups to user account")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
}()
r.ParseForm()
go func() { data := map[string]any{
accountresp, err = h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{ "group": groupresp.Group.ToStorageType().Data["name"],
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
// Account already exists : adding the existing account to admin list
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid)
account.Data["groups"] = append(account.Data["groups"].([]any), groupid, groupid+":admin")
as, _ := accounts.AccountFromStorageType(&account)
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
}
if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
log.Error().Err(err).Msg("Cannot send email")
}
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": groupid,
"admin": true,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
log.Error().Err(err).Msg("Issue creating random bytes")
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
log.Error().Err(err).Msg("Cannot send email")
w.WriteHeader(http.StatusInternalServerError)
return
}
} }
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound) if err := h.emailing.Send("onboarding.existing_administrator", r.FormValue("username"), data); err != nil {
return log.Error().Err(err).Msg("Cannot send email")
}() }
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": groupid,
"admin": true,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
log.Error().Err(err).Msg("Issue creating random bytes")
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": groupresp.Group.ToStorageType().Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_administrator", r.FormValue("username"), data); err != nil {
log.Error().Err(err).Msg("Cannot send email")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, fmt.Sprintf("/app/administration/groups/%s", groupid), http.StatusFound)
} }
func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) { func (h *ApplicationHandler) AdministrationGroupInviteMember(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
groupid := vars["groupid"] groupid := vars["groupid"]
var ( group, err := h.services.GetGroup(groupid)
group storage.Group if err != nil {
) log.Error().Err(err).Msg("Issue in Groups management service - GetGroup")
groupCh := make(chan storage.Group) w.WriteHeader(http.StatusInternalServerError)
go func() { return
groupresp, err := h.services.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{ }
Id: groupid, r.ParseForm()
Namespace: "parcoursmob_organizations", accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
}) Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)
if err != nil { as, _ := accounts.AccountFromStorageType(&account)
log.Error().Err(err).Msg("Issue in Groups management service - GetGroup")
_, err = h.services.GRPC.MobilityAccounts.UpdateData(
context.TODO(),
&accounts.UpdateDataRequest{
Account: as,
},
)
data := map[string]any{
"group": group.Data["name"],
}
if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
log.Error().Err(err).Msg("error sending email onboarding.existing_member")
}
// http.Redirect(w, r, "/app/group/settings", http.StatusFound)
// return
} else {
// Onboard new administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": group.ID,
"admin": false,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
group := groupresp.Group.ToStorageType() key := base64.RawURLEncoding.EncodeToString(b)
groupCh <- group
}()
r.ParseForm()
go func() {
group = <-groupCh
accountresp, err := h.services.GRPC.MobilityAccounts.GetAccountUsername(context.TODO(), &accounts.GetAccountUsernameRequest{
Username: r.FormValue("username"),
Namespace: "parcoursmob",
})
if err == nil {
account := accountresp.Account.ToStorageType()
account.Data["groups"] = append(account.Data["groups"].([]any), group.ID)
as, _ := accounts.AccountFromStorageType(&account) h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
_, err = h.services.GRPC.MobilityAccounts.UpdateData( "group": group.Data["name"],
context.TODO(), "key": key,
&accounts.UpdateDataRequest{
Account: as,
},
)
data := map[string]any{
"group": group.Data["name"],
}
if err := h.emailing.Send("onboarding.existing_member", r.FormValue("username"), data); err != nil {
}
http.Redirect(w, r, "/app/group/settings", http.StatusFound)
return
} else {
// Onboard now administrator
onboarding := map[string]any{
"username": r.FormValue("username"),
"group": group.ID,
"admin": false,
}
b := make([]byte, 16)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
key := base64.RawURLEncoding.EncodeToString(b)
h.cache.PutWithTTL("onboarding/"+key, onboarding, 168*time.Hour) // 1 week TTL
data := map[string]any{
"group": group.Data["name"],
"key": key,
}
if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
} }
}()
if err := h.emailing.Send("onboarding.new_member", r.FormValue("username"), data); err != nil {
log.Error().Err(err).Msg("error sending email onboarding.new_member")
w.WriteHeader(http.StatusInternalServerError)
return
}
}
http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound) http.Redirect(w, r, "/app/administration/groups/"+group.ID, http.StatusFound)
return
} }
func filteVehicle(r *http.Request, v *fleets.Vehicle) bool { func filteVehicle(r *http.Request, v *fleets.Vehicle) bool {
@ -400,7 +379,6 @@ func filteVehicle(r *http.Request, v *fleets.Vehicle) bool {
} }
func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) { func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Request) {
bookings := []fleetsstorage.Booking{} bookings := []fleetsstorage.Booking{}
administrators := []string{} administrators := []string{}
reequest := &fleets.GetVehiclesRequest{ reequest := &fleets.GetVehiclesRequest{
@ -462,7 +440,6 @@ func (h ApplicationHandler) AdminStatVehicles(w http.ResponseWriter, r *http.Req
} }
func (h ApplicationHandler) AdminStatBookings(w http.ResponseWriter, r *http.Request) { func (h ApplicationHandler) AdminStatBookings(w http.ResponseWriter, r *http.Request) {
vehicles := map[string]fleetsstorage.Vehicle{} vehicles := map[string]fleetsstorage.Vehicle{}
bookings := []fleetsstorage.Booking{} bookings := []fleetsstorage.Booking{}
@ -578,7 +555,6 @@ func (h ApplicationHandler) AdminStatEvents(w http.ResponseWriter, r *http.Reque
resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{ resp, err := h.services.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"}, Namespaces: []string{"parcoursmob_dispositifs"},
}) })
if err != nil { if err != nil {
log.Error().Err(err).Msg("Issue in Agenda service - GetEvents") log.Error().Err(err).Msg("Issue in Agenda service - GetEvents")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)

View File

@ -42,7 +42,7 @@ func (h ApplicationHandler) VehiclesSearch(w http.ResponseWriter, r *http.Reques
administrators := []string{} administrators := []string{}
if r.FormValue("beneficiaryid") != "" { if r.FormValue("beneficiaryid") != "" && startdate.After(time.Now()) && enddate.After(startdate) {
// Handler form // Handler form
searched = true searched = true
@ -247,7 +247,7 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
return return
} }
//NOTIFY GROUP MEMBERS // NOTIFY GROUP MEMBERS
members, _, err := h.groupmembers(vehicle.Vehicle.Administrators[0]) members, _, err := h.groupmembers(vehicle.Vehicle.Administrators[0])
if err != nil { if err != nil {
log.Error().Err(err).Msg("") log.Error().Err(err).Msg("")
@ -262,7 +262,6 @@ func (h ApplicationHandler) Book(w http.ResponseWriter, r *http.Request) {
} }
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", booking.Id), http.StatusFound) http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", booking.Id), http.StatusFound)
} }
func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http.Request) { func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http.Request) {
@ -310,7 +309,6 @@ func (h ApplicationHandler) VehicleBookingDisplay(w http.ResponseWriter, r *http
} }
func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.Request) { func (h ApplicationHandler) VehiclesBookingsList(w http.ResponseWriter, r *http.Request) {
g := r.Context().Value(identification.GroupKey) g := r.Context().Value(identification.GroupKey)
if g == nil { if g == nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -371,5 +369,4 @@ func (h *ApplicationHandler) BookingDocumentDownload(w http.ResponseWriter, r *h
} }
http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", bookingid), http.StatusFound) http.Redirect(w, r, fmt.Sprintf("/app/vehicles/bookings/%s", bookingid), http.StatusFound)
} }

View File

@ -20,11 +20,12 @@ import (
) )
func main() { func main() {
// zerolog.TimeFieldFormat = zerolog.TimeFormatUnix zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
cfg, err := ReadConfig() cfg, err := ReadConfig()
if err != nil { if err != nil {
panic(err) log.Fatal().Err(err).Msg("cannot read config")
return
} }
var ( var (

View File

@ -57,6 +57,20 @@ func (s *ServicesHandler) GetGroupsMemberMap(id string) (groups map[string]any,
return return
} }
func (s *ServicesHandler) GetGroup(groupid string) (*storage.Group, error) {
groupresp, err := s.GRPC.GroupsManagement.GetGroup(context.TODO(), &groupsmanagement.GetGroupRequest{
Id: groupid,
Namespace: "parcoursmob_organizations",
})
if err != nil {
return nil, err
}
group := groupresp.Group.ToStorageType()
return &group, nil
}
// Enriched types // Enriched types
type GroupsManagementGroup struct { type GroupsManagementGroup struct {