Skip to content

Commit

Permalink
allow deleting global evs
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jul 7, 2023
1 parent d8667bf commit a83492d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
4 changes: 2 additions & 2 deletions services/userlog/pkg/service/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (c *Converter) ConvertGlobalEvent(typ string, data json.RawMessage) (OC10No
return OC10Notification{}, err
}

return c.deprovisionMessage(PlatformDeprovision, dd.DeprovisionDate)
return c.deprovisionMessage(PlatformDeprovision, dd.DeprovisionDate.Format(dd.DeprovisionFormat))
}

}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (c *Converter) policiesMessage(eventid string, nt NotificationTemplate, exe
}, nil
}

func (c *Converter) deprovisionMessage(nt NotificationTemplate, deproDate time.Time) (OC10Notification, error) {
func (c *Converter) deprovisionMessage(nt NotificationTemplate, deproDate string) (OC10Notification, error) {
subj, subjraw, msg, msgraw, err := composeMessage(nt, c.locale, c.translationPath, map[string]interface{}{
"date": deproDate,
})
Expand Down
2 changes: 2 additions & 0 deletions services/userlog/pkg/service/globalevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var (
type DeprovisionData struct {
// The deprovision date
DeprovisionDate time.Time `json:"deprovision_date"`
// The Format of the deprvision date
DeprovisionFormat string
// The user who stored the deprovision message
Deprovisioner string
}
44 changes: 23 additions & 21 deletions services/userlog/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,31 +110,35 @@ func (ul *UserlogService) HandleSSE(w http.ResponseWriter, r *http.Request) {
ul.sse.ServeHTTP(w, r)
}

// HandlePostEvent is the POST handler for events
func (ul *UserlogService) HandlePostEvent(w http.ResponseWriter, r *http.Request) {
u, ok := ctx.ContextGetUser(r.Context())
if !ok {
ul.log.Error().Msg("post: no user in context")
w.WriteHeader(http.StatusInternalServerError)
// HandlePostGlobaelEvent is the POST handler for global events
func (ul *UserlogService) HandlePostGlobalEvent(w http.ResponseWriter, r *http.Request) {
var req PostEventsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
ul.log.Error().Err(err).Int("returned statuscode", http.StatusBadRequest).Msg("request body is malformed")
w.WriteHeader(http.StatusBadRequest)
return
}

uid := u.GetId().GetOpaqueId()
if uid == "" {
ul.log.Error().Msg("post: user in context is broken")
if err := ul.StoreGlobalEvent(req.Type, req.Data); err != nil {
ul.log.Error().Err(err).Msg("post: error storing global event")
w.WriteHeader(http.StatusInternalServerError)
return
}

var req PostEventsRequest
w.WriteHeader(http.StatusOK)
}

// HandleDeleteGlobalEvent is the DELETE handler for global events
func (ul *UserlogService) HandleDeleteGlobalEvent(w http.ResponseWriter, r *http.Request) {
var req DeleteEventsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
ul.log.Error().Err(err).Int("returned statuscode", http.StatusBadRequest).Msg("request body is malformed")
w.WriteHeader(http.StatusBadRequest)
return
}

if err := ul.StoreGlobalEvent(req.Type, req.Data); err != nil {
ul.log.Error().Err(err).Msg("post: error storing global event")
if err := ul.DeleteGlobalEvents(req.IDs); err != nil {
ul.log.Error().Err(err).Int("returned statuscode", http.StatusInternalServerError).Msg("delete events failed")
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -197,12 +201,9 @@ func RequireAdmin(rm *roles.Manager, logger log.Logger) func(next http.HandlerFu
return func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
u, ok := revactx.ContextGetUser(r.Context())
if !ok {
errorcode.AccessDenied.Render(w, r, http.StatusUnauthorized, "Unauthorized")
return
}
if u.Id == nil || u.Id.OpaqueId == "" {
errorcode.InvalidRequest.Render(w, r, http.StatusBadRequest, "user is missing an id")
if !ok || u.GetId().GetOpaqueId() == "" {
logger.Error().Str("userid", u.Id.OpaqueId).Msg("user not in context")
errorcode.AccessDenied.Render(w, r, http.StatusInternalServerError, "")
return
}
// get roles from context
Expand All @@ -213,11 +214,12 @@ func RequireAdmin(rm *roles.Manager, logger log.Logger) func(next http.HandlerFu
roleIDs, err = rm.FindRoleIDsForUser(r.Context(), u.Id.OpaqueId)
if err != nil {
logger.Err(err).Str("userid", u.Id.OpaqueId).Msg("failed to get roles for user")
errorcode.AccessDenied.Render(w, r, http.StatusUnauthorized, "Unauthorized")
errorcode.AccessDenied.Render(w, r, http.StatusInternalServerError, "")
return
}
if len(roleIDs) == 0 {
errorcode.AccessDenied.Render(w, r, http.StatusUnauthorized, "Unauthorized")
logger.Err(err).Str("userid", u.Id.OpaqueId).Msg("user has no roles")
errorcode.AccessDenied.Render(w, r, http.StatusInternalServerError, "")
return
}
}
Expand All @@ -228,7 +230,7 @@ func RequireAdmin(rm *roles.Manager, logger log.Logger) func(next http.HandlerFu
return
}

errorcode.AccessDenied.Render(w, r, http.StatusForbidden, "Forbidden")
errorcode.AccessDenied.Render(w, r, http.StatusNotFound, "Forbidden")
}
}
}
41 changes: 30 additions & 11 deletions services/userlog/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func NewUserlogService(opts ...Option) (*UserlogService, error) {

ul.m.Route("/ocs/v2.php/apps/notifications/api/v1/notifications", func(r chi.Router) {
r.Get("/", ul.HandleGetEvents)
r.Post("/", RequireAdmin(&m, ul.log)(ul.HandlePostEvent))
r.Delete("/", ul.HandleDeleteEvents)
r.Post("/global", RequireAdmin(&m, ul.log)(ul.HandlePostGlobalEvent))
r.Delete("/global", RequireAdmin(&m, ul.log)(ul.HandleDeleteGlobalEvent))

if !ul.cfg.DisableSSE {
r.Get("/sse", ul.HandleSSE)
Expand Down Expand Up @@ -266,11 +267,22 @@ func (ul *UserlogService) StoreGlobalEvent(typ string, data map[string]string) e
return fmt.Errorf("cannot parse time to format. time: '%s' format: '%s'", dps, format)
}

return ul.storeGlobalEvent(typ, DeprovisionData{
DeprovisionDate: date,
})
ev := DeprovisionData{
DeprovisionDate: date,
DeprovisionFormat: format,
}

b, err := json.Marshal(ev)
if err != nil {
return err
}

return ul.alterGlobalEvents(func(evs map[string]json.RawMessage) error {
evs[typ] = b
return nil
})
}

}

// GetGlobalEvents will return all global events
Expand All @@ -291,6 +303,16 @@ func (ul *UserlogService) GetGlobalEvents() (map[string]json.RawMessage, error)
return out, nil
}

// DeleteGlobalEvents will delete the specified event
func (ul *UserlogService) DeleteGlobalEvents(evnames []string) error {
return ul.alterGlobalEvents(func(evs map[string]json.RawMessage) error {
for _, name := range evnames {
delete(evs, name)
}
return nil
})
}

func (ul *UserlogService) addEventToUser(userid string, event events.Event) error {
if !ul.cfg.DisableSSE {
if err := ul.sendSSE(userid, event); err != nil {
Expand Down Expand Up @@ -368,18 +390,15 @@ func (ul *UserlogService) alterUserEventList(userid string, alter func([]string)
})
}

func (ul *UserlogService) storeGlobalEvent(typ string, ev interface{}) error {
b, err := json.Marshal(ev)
if err != nil {
return err
}

func (ul *UserlogService) alterGlobalEvents(alter func(map[string]json.RawMessage) error) error {
evs, err := ul.GetGlobalEvents()
if err != nil && err != store.ErrNotFound {
return err
}

evs[typ] = b
if err := alter(evs); err != nil {
return err
}

val, err := json.Marshal(evs)
if err != nil {
Expand Down

0 comments on commit a83492d

Please sign in to comment.