user creation added in main

This commit is contained in:
eleonore12345 2024-08-09 15:29:53 +02:00
parent 4ac02da07c
commit 78797df02c
5 changed files with 33 additions and 6 deletions

View File

@ -7,6 +7,7 @@
int BashManager::execute(string command) int BashManager::execute(string command)
{ {
cout << "command is " << command << endl;
FILE * p = popen(command.c_str(),"r"); FILE * p = popen(command.c_str(),"r");
if( p == NULL) if( p == NULL)
{ {
@ -28,6 +29,10 @@ string BashManager::executeAndReadResult(string command)
} }
while(fgets(lineBuffer,sizeof(lineBuffer),p)){ while(fgets(lineBuffer,sizeof(lineBuffer),p)){
result += lineBuffer; result += lineBuffer;
//removing newline character
if (!result.empty() && result[result.length()-1] == '\n') {
result.erase(result.length()-1);
}
} }
pclose(p); pclose(p);
return result; return result;

View File

@ -70,13 +70,16 @@ vector <Service> Services::readServicesFromCSV (string CSV) const
cout << "Invalid services.csv file." << endl; cout << "Invalid services.csv file." << endl;
}else{ }else{
string line; string line;
string tmpUserID; //used before converting to int string tmpUserID=""; //used before converting to int
int userID; int userID;
string username=""; string username;
string server=""; string server;
list <string> servers; list <string> servers;
while(getline(streamServices,line)){ while(getline(streamServices,line)){
servers.clear(); servers.clear();
tmpUserID="";
username="";
server="";
if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
continue; continue;
} }

View File

@ -34,8 +34,27 @@ int isServiceOnServer(string serviceUsername)
} }
int createUser(string serviceUsername){ int createUser(string serviceUsername){
//TO DO //this method creates a Unix user dedicated to the service
cout << "create user called" << endl; //get the User ID from servers.csv
int uidStart=2000; //so that the uids do not overlap with existing uids
Services services = Services("./services/services.csv");
const Service * service = services.findByUsername(serviceUsername);
int uid = (*service).getUserID()+uidStart;
//test if user already exists
string cmd = "id -u "+serviceUsername;
string res = BashManager::executeAndReadResult (cmd);
if(res==to_string(uid)){
cout << "user already existed" << endl;
return 0;
}
//create user
string cmd2 ="useradd -u " + to_string(uid) + " " + serviceUsername;
cout << cmd2 << endl;
int res2 = BashManager::execute(cmd2);
if (res2 != 0){
cerr << "Error when executing the bash command to create a user specific to the service." << endl;
return -1;
}
return 0; return 0;
} }

Binary file not shown.

View File

@ -1,4 +1,4 @@
ID,Name,Server #ID,Name,Server
1,test.sh8s.sh,ordinosaure 1,test.sh8s.sh,ordinosaure
2,truc.sh8s.sh,shlag_cluster 2,truc.sh8s.sh,shlag_cluster
3,other.sh8s.sh, 3,other.sh8s.sh,

1 ID #ID Name Server
2 1 1 test.sh8s.sh ordinosaure
3 2 2 truc.sh8s.sh shlag_cluster
4 3 3 other.sh8s.sh