Upgrade to material ui 4

Add user management and roles - TBA
Menu Label Renames - TBA
This commit is contained in:
Rick Watson
2019-05-24 12:19:27 +01:00
parent 685420aaed
commit 0c630f0f93
16 changed files with 799 additions and 469 deletions

View File

@ -1,9 +1,6 @@
#include <SecurityManager.h>
SecurityManager::SecurityManager(AsyncWebServer* server, FS* fs) : SettingsPersistence(fs, SECURITY_SETTINGS_FILE) {
server->on(USERS_PATH, HTTP_GET, std::bind(&SecurityManager::fetchUsers, this, std::placeholders::_1));
}
SecurityManager::SecurityManager(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, USERS_PATH, SECURITY_SETTINGS_FILE) {}
SecurityManager::~SecurityManager() {}
void SecurityManager::readFromJsonObject(JsonObject& root) {
@ -22,16 +19,19 @@ void SecurityManager::readFromJsonObject(JsonObject& root) {
// users
_users.clear();
if (root["users"].is<JsonArray>()) {
JsonArray users = root["users"];
for (JsonVariant user : users) {
String username = user["username"];
String password = user["password"];
String role = user["role"];
_users.push_back(User(username, password, role));
for (JsonVariant user : root["users"].as<JsonArray>()) {
std::list<String> roles;
if (user["roles"].is<JsonArray>()) {
for (JsonVariant role : user["roles"].as<JsonArray>()) {
roles.push_back(role.as<String>());
}
}
_users.push_back(User(user["username"], user["password"], roles));
}
}
}
void SecurityManager::writeToJsonObject(JsonObject& root) {
// secret
root["jwt_secret"] = _jwtSecret;
@ -48,18 +48,13 @@ void SecurityManager::writeToJsonObject(JsonObject& root) {
JsonObject user = users.createNestedObject();
user["username"] = _user.getUsername();
user["password"] = _user.getPassword();
user["role"] = _user.getRole();
JsonArray roles = user.createNestedArray("roles");
for (String _role : _user.getRoles()){
roles.add(_role);
}
}
}
void SecurityManager::fetchUsers(AsyncWebServerRequest *request) {
AsyncJsonResponse * response = new AsyncJsonResponse(MAX_USERS_SIZE);
JsonObject jsonObject = response->getRoot();
writeToJsonObject(jsonObject);
response->setLength();
request->send(response);
}
void SecurityManager::begin() {
// read config
readFromFS();
@ -105,7 +100,10 @@ Authentication SecurityManager::authenticate(String username, String password) {
inline void populateJWTPayload(JsonObject &payload, User *user) {
payload["username"] = user->getUsername();
payload["role"] = user->getRole();
JsonArray roles = payload.createNestedArray("roles");
for (String _role : user->getRoles()){
roles.add(_role);
}
}
boolean SecurityManager::validatePayload(JsonObject &parsedPayload, User *user) {

View File

@ -28,17 +28,17 @@ class User {
private:
String _username;
String _password;
String _role;
std::list<String> _roles;
public:
User(String username, String password, String role): _username(username), _password(password), _role(role) {}
User(String username, String password, std::list<String> roles): _username(username), _password(password), _roles(roles) {}
String getUsername() {
return _username;
}
String getPassword() {
return _password;
}
String getRole() {
return _role;
std::list<String> getRoles() {
return _roles;
}
};
@ -62,7 +62,7 @@ class Authentication {
}
};
class SecurityManager : public SettingsPersistence {
class SecurityManager : public SettingsService {
public: