Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

  1. #ifndef AdminSettingsService_h
  2. #define AdminSettingsService_h
  3. #include <SettingsService.h>
  4. template <class T>
  5. class AdminSettingsService : public SettingsService<T> {
  6. public:
  7. AdminSettingsService(AsyncWebServer* server,
  8. FS* fs,
  9. SecurityManager* securityManager,
  10. char const* servicePath,
  11. char const* filePath) :
  12. SettingsService<T>(server, fs, servicePath, filePath),
  13. _securityManager(securityManager) {
  14. }
  15. protected:
  16. // will validate the requests with the security manager
  17. SecurityManager* _securityManager;
  18. void fetchConfig(AsyncWebServerRequest* request) {
  19. // verify the request against the predicate
  20. Authentication authentication = _securityManager->authenticateRequest(request);
  21. if (!getAuthenticationPredicate()(authentication)) {
  22. request->send(401);
  23. return;
  24. }
  25. // delegate to underlying implemetation
  26. SettingsService<T>::fetchConfig(request);
  27. }
  28. void updateConfig(AsyncWebServerRequest* request, JsonDocument& jsonDocument) {
  29. // verify the request against the predicate
  30. Authentication authentication = _securityManager->authenticateRequest(request);
  31. if (!getAuthenticationPredicate()(authentication)) {
  32. request->send(401);
  33. return;
  34. }
  35. // delegate to underlying implemetation
  36. SettingsService<T>::updateConfig(request, jsonDocument);
  37. }
  38. // override this to replace the default authentication predicate, IS_ADMIN
  39. AuthenticationPredicate getAuthenticationPredicate() {
  40. return AuthenticationPredicates::IS_ADMIN;
  41. }
  42. };
  43. #endif // end AdminSettingsService