void UserManagerBackend::refreshUsers() { qDebug() << "Refreshing user list..."; userList.clear(); QFile userFile(chroot + "/etc/passwd"); if ( userFile.open(QIODevice::ReadOnly) ) { QTextStream stream(&userFile); stream.setCodec("UTF-8"); QString line; while ( !stream.atEnd() ) { line = stream.readLine(); if ((line.indexOf("#") != 0) && (! line.isEmpty())) { //Make sure it isn't a comment or blank QString username = line.section(":",0,0); int uid = line.section(":",2,2).toInt(); int gid = line.section(":",3,3).toInt(); QString home = line.section(":",5,5); QString shell = line.section(":",6,6); QString fullname = line.section(":",4,4); userList[username] = User(username, uid, gid, home, shell, fullname); } } } else { //Unable to open file error qWarning("Error! Unable to open /etc/passwd"); } }
SpellCheck::SpellCheck() : m_hunspell(0), m_codec(0), m_wordchars("") { // There is a considerable lag involved in loading the Spellcheck dictionaries QApplication::setOverrideCursor(Qt::WaitCursor); loadDictionaryNames(); // Create the user dictionary word list directiory if necessary. const QString user_directory = userDictionaryDirectory(); QDir userDir(user_directory); if (!userDir.exists()) { userDir.mkpath(user_directory); } // Create the configured file if necessary. QFile userFile(currentUserDictionaryFile()); if (!userFile.exists()) { if (userFile.open(QIODevice::WriteOnly)) { userFile.close(); } } // Load the dictionary the user has selected if one was saved. SettingsStore settings; setDictionary(settings.dictionary()); QApplication::restoreOverrideCursor(); }
void MainWindow::getUsers() { /* here is modifyed code from pc-usermanager */ mvUsers.clear(); QFile userFile("/etc/passwd"); if ( userFile.open(QIODevice::ReadOnly) ) { QTextStream stream(&userFile); stream.setCodec("UTF-8"); QString line; while ( !stream.atEnd() ) { line = stream.readLine(); if ((line.trimmed().indexOf("#") != 0) && (! line.isEmpty())) { //Make sure it isn't a comment or blank QString username = line.section(":",0,0); QString homedir = line.section(":",5,5); // Ignore PEFS encrypted users if ( QFile::exists(homedir + "/.pefs.db") ) continue; int uid = line.section(":",2,2).toInt(); if ((uid>1000)&&(uid<65534)) mvUsers.push_back(username); }//if gout user }//for all lines }//if can open file }
void User::load(void) { Deposit *deposit; Withdrawal *withdrawal; QFile userFile(fileName); userFile.open(QIODevice::ReadOnly | QIODevice::Text); QXmlStreamReader xml(&userFile); while (!xml.atEnd() && !xml.hasError()) { xml.readNext(); if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "id") id = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "first_name") firstName = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "last_name") lastName = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "email") email = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "pwd_hash") pwdHash = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "type") { QString typeStr; typeStr = User::getXmlString(&xml); if (typeStr == "normal") type = UT_NORMAL; else if (typeStr == "manager") type = UT_MANAGER; else if (typeStr == "admin") type = UT_ADMIN; else if (typeStr == "disabled") type = UT_DISABLED; } else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "reports_to") reportsTo = getXmlString(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "region") { QString regionID = getXmlString(&xml); if (server->hasRegion(regionID)) region = server->getRegion(regionID); } else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "annualAllotment") annualAllotment = getXmlInt(&xml); else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "deposit") { deposit = new Deposit; deposits.append(deposit); deposit->setID(deposits.indexOf(deposit)); deposit->load(&xml); } else if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "withdrawal") { withdrawal = new Withdrawal(this, withdrawals.size()); withdrawals.append(withdrawal); withdrawal->load(&xml); } } userFile.close(); }
void User::save(void) { qint32 i; QFile userFile(fileName); userFile.open(QIODevice::WriteOnly); QXmlStreamWriter xml(&userFile); xml.setAutoFormatting(true); xml.writeStartDocument(); xml.writeStartElement("user"); xml.writeStartElement("id"); xml.writeCharacters(id.toUtf8()); xml.writeEndElement(); xml.writeStartElement("first_name"); xml.writeCharacters(firstName.toUtf8()); xml.writeEndElement(); xml.writeStartElement("last_name"); xml.writeCharacters(lastName.toUtf8()); xml.writeEndElement(); xml.writeStartElement("email"); xml.writeCharacters(email.toUtf8()); xml.writeEndElement(); xml.writeStartElement("pwd_hash"); xml.writeCharacters(pwdHash.toUtf8()); xml.writeEndElement(); xml.writeStartElement("type"); switch (type) { case UT_NORMAL: xml.writeCharacters("normal"); break; case UT_MANAGER: xml.writeCharacters("manager"); break; case UT_ADMIN: xml.writeCharacters("admin"); break; case UT_DISABLED: xml.writeCharacters("disabled"); break; } xml.writeEndElement(); xml.writeStartElement("reports_to"); xml.writeCharacters(reportsTo.toUtf8()); xml.writeEndElement(); xml.writeStartElement("region"); if (region) xml.writeCharacters(region->getName().toUtf8()); xml.writeEndElement(); xml.writeStartElement("annualAllotment"); xml.writeCharacters(QString::number(annualAllotment)); xml.writeEndElement(); for (i=0; i<deposits.size(); i++) deposits.at(i)->save(&xml); for (i=0; i<withdrawals.size(); i++) withdrawals.at(i)->save(&xml); xml.writeEndElement(); // user xml.writeEndDocument(); userFile.close(); }
/*! Creates a new SQLITE database in the user's home/.configwizard directory, named according to the username input in the previous page. */ void NewPage::CreateDatabase() { QDir d( QDir::home() ); d.cd(".configwizard"); QString dbPath = d.path(); dbPath.append("/"); QString userName = nameLineEdit->text(); userName.replace(" ", ""); QString userEmail = emailLineEdit->text(); userEmail.replace(" ", ""); QString userPass = passLineEdit->text(); userPass.replace(" ", ""); dbPath.append(userName); dbPath.append(".db"); databasePath = dbPath; configdb = QSqlDatabase::addDatabase("QSQLITE"); configdb.setDatabaseName(dbPath); if( configdb.open() ) { qWarning("Database created"); } else { qWarning("Database not created"); } qWarning() << "Path to database is " << databasePath; QString userFileName = d.absolutePath(); userFileName.append("/userFileName"); QFile userFile(userFileName); userFile.open(QIODevice::WriteOnly); userFile.write(databasePath.toLatin1()); userFile.close(); // Create the database structure QSqlQuery createTables(configdb); QString createTablesSQL = "CREATE TABLE \"tuser\" (\"userid\", \"username\", \"useremail\", \"password\")"; createTables.exec(createTablesSQL); QString insertUserSQL = "INSERT INTO tuser (userid, username, useremail, password) VALUES (1, \'"; insertUserSQL.append(userName); insertUserSQL.append("\',\'"); insertUserSQL.append(userEmail); insertUserSQL.append("\',\'"); insertUserSQL.append(userPass); insertUserSQL.append("\')"); createTables.exec(insertUserSQL); qWarning() << insertUserSQL; }
/*! Launches a QFileDialog to select a user database then stores the path and filename in a config file. */ void LoadPage::setOpenFileName() { QFileDialog::Options options; QString selectedFilter; QDir d( QDir::home() ); QString homeDir = d.absolutePath(); homeDir.append("/.configwizard"); QString fileName = QFileDialog::getOpenFileName(this, "Select file to open", homeDir, tr("SQLite Databases (*.db)"), &selectedFilter, options); if (!fileName.isEmpty()) { fileToLoad->setText(fileName); QString userFileName = homeDir; userFileName.append("/userFileName"); QFile userFile(userFileName); userFile.open(QIODevice::WriteOnly); userFile.write(fileName.toLatin1()); userFile.close(); } }
int FixUsersCommand::Execute() { LOG << "Runnning FixUsersCommand::Execute"; initStatusDat(config()->config()->datadir()); File userFile(config()->config()->datadir(), USER_LST); if (!userFile.Exists()) { LOG << userFile.full_pathname() << " does not exist."; return 1; } UserManager userMgr(config()->config()->datadir(), sizeof(userrec), config()->config()->config()->maxusers); LOG << "Checking USER.LST... found " << userMgr.GetNumberOfUserRecords() << " user records."; LOG << "TBD: Check for trashed user recs."; if (userMgr.GetNumberOfUserRecords() > config()->config()->config()->maxusers) { LOG << "Might be too many."; if (!arg("exp").as_bool()) { return 1; } } else { LOG << "Reasonable number."; } std::vector<smalrec> smallrecords; std::set<std::string> names; const int num_user_records = userMgr.GetNumberOfUserRecords(); for(int i = 1; i <= num_user_records; i++) { User user; userMgr.ReadUser(&user, i); user.FixUp(); userMgr.WriteUser(&user, i); if (!user.IsUserDeleted() && !user.IsUserInactive()) { smalrec sr = { 0 }; strcpy((char*) sr.name, user.GetName()); sr.number = static_cast<uint16_t>(i); std::string namestring((char*) sr.name); if (names.find(namestring) == names.end()) { smallrecords.push_back(sr); names.insert(namestring); if (arg("verbose").as_bool()) { LOG << "Keeping user: "******" #" << sr.number ; } } else { LOG << "[skipping duplicate user: "******" #" << sr.number << "]"; } } }; std::sort(smallrecords.begin(), smallrecords.end(), [](const smalrec& a, const smalrec& b) -> bool { int equal = strcmp((char*)a.name, (char*)b.name); // Sort by user number if names match. if (equal == 0) { return a.number < b.number; } // Otherwise sort by name comparison. return equal < 0; }); printf("size=%lu %lu\n", smallrecords.size(), sizeof(smalrec) * smallrecords.size()); LOG << "Checking NAMES.LST"; File nameFile(config()->config()->datadir(), NAMES_LST); if (!nameFile.Exists()) { LOG << nameFile.full_pathname() << " does not exist, regenerating with " << smallrecords.size() << " names"; nameFile.Open(File::modeCreateFile | File::modeBinary | File::modeWriteOnly); nameFile.Write(&smallrecords[0], sizeof(smalrec) * smallrecords.size()); nameFile.Close(); } else { if (nameFile.Open(File::modeReadOnly | File::modeBinary)) { long size = nameFile.GetLength(); uint16_t recs = static_cast<uint16_t>(size / sizeof(smalrec)); if (recs != status.users) { status.users = recs; LOG << "STATUS.DAT contained an incorrect user count."; } else { LOG << "STATUS.DAT matches expected user count of " << status.users << " users."; } } nameFile.Close(); } return 0; }
int FixUsersCommand::Execute() { std::cout << "Runnning FixUsersCommand::Execute" << std::endl; File userFile(syscfg.datadir, USER_LST); if(!userFile.Exists()) { Print(NOK, true, "%s does not exist.", userFile.full_pathname().c_str()); giveUp(); } WUserManager userMgr; userMgr.InitializeUserManager(syscfg.datadir, sizeof(userrec), syscfg.maxusers); Print(OK, true, "Checking USER.LST... found %d user records.", userMgr.GetNumberOfUserRecords()); Print(OK, true, "TBD: Check for trashed user recs."); if(userMgr.GetNumberOfUserRecords() > syscfg.maxusers) { Print(OK, true, "Might be too many."); maybeGiveUp(); } else { Print(OK, true, "Reasonable number."); } std::vector<smalrec> smallrecords; std::set<std::string> names; const int num_user_records = userMgr.GetNumberOfUserRecords(); for(int i = 1; i <= num_user_records; i++) { WUser user; userMgr.ReadUser(&user, i); user.FixUp(); userMgr.WriteUser(&user, i); if (!user.IsUserDeleted() && !user.IsUserInactive()) { smalrec sr = { 0 }; strcpy((char*) sr.name, user.GetName()); sr.number = static_cast<unsigned short>(i); std::string namestring((char*) sr.name); if (names.find(namestring) == names.end()) { smallrecords.push_back(sr); names.insert(namestring); const std::string msg = StringPrintf("Keeping user: %s #%d", sr.name, sr.number); Print(OK, true, msg.c_str()); } else { std::cout << "[skipping duplicate user: "******" #" << sr.number << "]"; } } }; std::sort(smallrecords.begin(), smallrecords.end(), [](const smalrec& a, const smalrec& b) -> bool { int equal = strcmp((char*)a.name, (char*)b.name); // Sort by user number if names match. if (equal == 0) { return a.number < b.number; } // Otherwise sort by name comparison. return equal < 0; }); printf("size=%lu %lu\n", smallrecords.size(), sizeof(smalrec) * smallrecords.size()); Print(OK, true, "Checking NAMES.LST"); File nameFile(syscfg.datadir, NAMES_LST); if(!nameFile.Exists()) { Print(NOK, true, "%s does not exist, regenerating with %d names", nameFile.full_pathname().c_str(), smallrecords.size()); nameFile.Close(); nameFile.Open(File::modeCreateFile | File::modeBinary | File::modeWriteOnly); nameFile.Write(&smallrecords[0], sizeof(smalrec) * smallrecords.size()); nameFile.Close(); } else { if(nameFile.Open(File::modeReadOnly | File::modeBinary)) { unsigned long size = nameFile.GetLength(); unsigned short recs = static_cast<unsigned short>(size / sizeof(smalrec)); if (recs != status.users) { status.users = recs; Print(NOK, true, "STATUS.DAT contained an incorrect user count."); } else { Print(OK, true, "STATUS.DAT matches expected user count of %d users.", status.users); } } nameFile.Close(); } return 0; }
bool Database::genDatabase(const std::string path_src, const std::string path_dest, const string movieDatabase, const string userDatabase) { DIR *dp; struct dirent *fp; std::vector<std::string> fnames; if ((dp = opendir(path_src.c_str())) != NULL) { while ((fp = readdir(dp)) != NULL) { if(string(fp->d_name).find(".txt") != std::string::npos) { fnames.push_back(string(fp->d_name)); } } closedir(dp); } if((dp = opendir(path_dest.c_str())) == NULL){ if(mkdir(path_dest.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { cout << "mkdir failed!" << endl; return false; } } //for(vector<string>::iterator it = fnames.begin(); it != fnames.end(); ++ it) // cout << *it << endl; //cout << fnames.size() << std::endl; //cout << "In Database" << endl; //remove files if(remove((path_dest + movieDatabase).c_str()) == 0) cout << "Remove " << movieDatabase << endl; if(remove((path_dest + userDatabase).c_str()) == 0) cout << "Remove " << userDatabase << endl; vector<mRatings> mRs; UserRatings *users = new UserRatings(); //generate movie dataset for(vector<string>::iterator it = fnames.begin(); it != fnames.end(); ++ it) { ifstream myFile((path_src + *it).c_str()); string line; int movieId; if(myFile.is_open()) { getline(myFile, line); movieId = strToInt(line.substr(0, line.find(':') - 0)); cout << "MovieID " << movieId << endl; while(getline(myFile, line)) { //int start = 0; //cout << line.substr(start, line.find(',') - start) << " " << line.substr(line.find(',') + 1, line.find_last_of(',') - line.find(',') - 1) << endl; uInt uId = strToInt(line.substr(0, line.find(',') - 0)); uByte rating = uByte(strToInt(line.substr(line.find(',') + 1, line.find_last_of(',') - line.find(',') - 1))); mRatings mRat = mRatings(uId, rating); int index = binarySearch<mRatings>(mRs, mRat); insert<mRatings>(mRs, mRat, index); //initilize user vectors users->addRating(mRs.back().getId(), uRatings(movieId, mRs.back().getValue())); } myFile.close(); //generate user database ofstream movieWFile((path_dest + movieDatabase).c_str(), ios::out|ios::binary|ios::app); if(movieWFile.good()) { if(movieWFile.is_open()) { unsigned short uIdHi = 0; uByte rating = 0; uInt size = int(mRs.size()); movieWFile.write((char *)&size, sizeof(uInt)); cout << size << endl; for(vector<mRatings>::iterator it = mRs.begin(); it != mRs.end(); ++ it) { uIdHi = it->userIdHi; rating = ((it->userIdLo) << 4) + uByte(it->value); movieWFile.write((char *)&uIdHi, sizeof(unsigned short)); movieWFile.write((char *)&rating, sizeof(uByte)); cout << it->getId() << " " << it->getValue() << endl; } } mRs.clear(); movieWFile.close(); } else { cout << "Failed!" << endl; } } } //generate user dataset ofstream userFile((path_dest + userDatabase).c_str(), ios::out|ios::binary|ios::ate); vector<uRatings> uRs; for(uInt i = 1; i <= users->getUserNum(); i ++) { uRs.clear(); users->getRatings(i, uRs); if(userFile.good()) { if(userFile.is_open()) { unsigned short mId = 0; uByte rating = 0; uInt num = uInt(uRs.size()); userFile.write((char* )&num, sizeof(unsigned int)); for(vector<uRatings>::iterator it = uRs.begin(); it != uRs.end(); ++ it) { mId = it->movieId; rating = it->value; userFile.write((char *)&mId, sizeof(unsigned short)); userFile.write((char *)&rating, sizeof(uByte)); } } } } userFile.close(); return true; }
bool Database::loadDatabase(const std::string database_movie_path,const std::string database_user_path, MovieRatings &movies, UserRatings &users) { ifstream movieFile(database_movie_path.c_str(), ios::in|ios::binary); ifstream userFile(database_user_path.c_str(), ios::in|ios::binary); if(!movieFile.good() || !userFile.good()) { cerr << "File not exists!" << endl; //return false; //return genDatabase(DATASET_PATH, DATABASE_PATH, MOVIEDATABASE, USERDATABASE); if(genDatabase(DATASET_PATH, DATABASE_PATH, MOVIEDATABASE, USERDATABASE)) loadDatabase(database_movie_path, database_user_path, movies, users); } else { //load movie database if(movieFile.is_open()) { cout << "Loading movie Database..." << endl; uInt num = 0; uInt index = 1; unsigned short uIdHi = 0; uByte rating = 0; while(!movieFile.eof()) { num = 0; uIdHi = 0; rating = 0; movieFile.read((char *)&num, sizeof(int)); //cout << index << endl; //cout << "Num " << num << endl; for(int i = 0; i < num; i ++) { movieFile.read((char *)&uIdHi, sizeof(unsigned short)); movieFile.read((char *)&rating, sizeof(uByte)); movies.addRating(index, mRatings(uIdHi, rating)); } index ++; } movieFile.close(); } else { cerr << "File not open!" << endl; return false; } //load user database if(userFile.is_open()) { cout << "Loading user Database..." << endl; uInt num = 0; uInt index = 1; unsigned short mId = 0; uByte rating = 0; while(!userFile.eof()) { num = 0; mId = 0; rating = 0; userFile.read((char *)&num , sizeof(int)); //cout << index << endl; //cout << "Num " << num << endl; for(int i = 0; i < num; i ++) { userFile.read((char *)(&mId), sizeof(unsigned short)); userFile.read((char *)(&rating), sizeof(uByte)); users.addRating(index, uRatings(mId, rating)); } index ++; } } else { cerr << "File not open!" << endl; return false; } } movies.computeAllMeans(); //movies.dumpAllMeans(); movies.computeAllStdDevs(); //movies.dumpAllStdDevs(); users.computeAllMeans(); //users.dumpAllMeans(); users.computeAllStdDevs(); //users.dumpAllStdDevs(); movies.computeAllBias(); users.computeAllBias(&movies); return true; }