Create and implemented userManager class

This commit is contained in:
2025-05-22 19:33:56 +02:00
parent 6ad501ec50
commit 40d58ab011
6 changed files with 135 additions and 4 deletions

View File

@@ -1,2 +1,2 @@
all: all:
g++ -std=c++17 src/main.cpp -o ncsambawatcher g++ -std=c++17 src/main.cpp src/usermanager.cpp -o ncsambawatcher

8
src/locations.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _LOCATIONS_H
#define _LOCATIONS_H
#define LOGFILE "journalctl -u smbd --since now -f"
#define USER_LOG_LOCATION 3
#endif // _LOCATIONS_H

View File

@@ -1,4 +1,7 @@
#include <iostream> #include <iostream>
#include <array>
#include <string>
#include <map>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <syslog.h> #include <syslog.h>
@@ -7,10 +10,13 @@
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include "pipedata.h" #include "pipedata.h"
#include "locations.h"
#include "usermanager.h"
int main() int main()
{ {
openlog("ncsambawatcher", LOG_PID | LOG_CONS, LOG_USER);
int p1[2]; int p1[2];
pipe(p1); pipe(p1);
@@ -21,7 +27,13 @@ int main()
{ {
close(p1[0]); // read close(p1[0]); // read
FILE* logpipe = popen(LOGFILE, "r");
std::array<char, 256> buffer;
while(fgets(buffer.data(), buffer.size(), logpipe) != nullptr)
{
std::string line(buffer.data());
}
close(p1[1]); // write close(p1[1]); // write
} }
@@ -34,5 +46,7 @@ int main()
close(p1[0]); // read close(p1[0]); // read
} }
closelog();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@@ -5,7 +5,8 @@
struct pipestruct struct pipestruct
{ {
std::string s; std::string user;
}; };
#endif // _PIPDATA_H #endif // _PIPDATA_H

14
src/usermanager.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "usermanager.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter = '|')
{
std::vector<std::string> ret;
std::stringstream ss(input);
std::string token;
while (std::getline(ss, token, delimiter)) {
ret.push_back(token);
}
return ret;
}

94
src/usermanager.h Normal file
View File

@@ -0,0 +1,94 @@
#ifndef _USERMAN_H
#define _USERMAN_H
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include "locations.h"
std::vector<std::string> splitLogFile(const std::string& input, char delimiter);
class userManager
{
private:
std::map<std::string, bool> users;
public:
void addUserFromLogLine(std::string &line)
{
addUser(splitLogFile(line, '|').at(USER_LOG_LOCATION));
}
void addUser(std::string &user)
{
if (users.count(user) == 0)
{
users[user] = false;
}
}
void removeUser(std::string &user)
{
users.erase(user);
}
bool isContains(std::string &user)
{
return users.count(user) == 1;
}
void setUserFlagged(std::string &user)
{
if (users.count(user) == 1)
{
users[user] = true;
}
}
void setUserUnflagged(std::string &user)
{
if (users.count(user) == 1)
{
users[user] = false;
}
}
void unflagAllUsers()
{
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
it->second = false;
}
}
std::vector<std::string> getUsers()
{
std::vector<std::string> ret;
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
ret.push_back(it->first);
}
return ret;
}
std::vector<std::string> getFlaggedUsers()
{
std::vector<std::string> ret;
for (std::map<std::string, bool>::iterator it = users.begin(); it != users.end(); ++it)
{
if (it->second)
{
ret.push_back(it->first);
}
}
return ret;
}
};
#endif // _USERMAN_H