mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-11-01 17:34:14 +01:00
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"heckel.io/ntfy/user"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func (s *Server) handleAccessAllow(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
||
|
req, err := readJSONWithLimit[apiAccessAllowRequest](r.Body, jsonBodyBytesLimit, false)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
permission, err := user.ParsePermission(req.Permission)
|
||
|
if err != nil {
|
||
|
return errHTTPBadRequestPermissionInvalid
|
||
|
}
|
||
|
if err := s.userManager.AllowAccess(req.Username, req.Topic, permission); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return s.writeJSON(w, newSuccessResponse())
|
||
|
}
|
||
|
|
||
|
func (s *Server) handleAccessReset(w http.ResponseWriter, r *http.Request, v *visitor) error {
|
||
|
req, err := readJSONWithLimit[apiAccessResetRequest](r.Body, jsonBodyBytesLimit, false)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
u, err := s.userManager.User(req.Username)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := s.userManager.ResetAccess(req.Username, req.Topic); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if err := s.killUserSubscriber(u, req.Topic); err != nil { // This may be a pattern
|
||
|
return err
|
||
|
}
|
||
|
return s.writeJSON(w, newSuccessResponse())
|
||
|
}
|
||
|
|
||
|
func (s *Server) killUserSubscriber(u *user.User, topicPattern string) error {
|
||
|
topics, err := s.topicsFromPattern(topicPattern)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
for _, t := range topics {
|
||
|
t.CancelSubscriberUser(u.ID)
|
||
|
}
|
||
|
return nil
|
||
|
}
|