Nicer implementation of the handling of administrative rights

remotes/origin/HEAD
markuspg 10 years ago
parent ae5ae01800
commit 3441f032a5

@ -76,11 +76,12 @@ lc::Lablib::~Lablib () {
delete webcams; delete webcams;
} }
bool lc::Lablib::CheckIfUserIsAdmin( const QString &argUserName ) const { bool lc::Lablib::CheckIfUserIsAdmin() const {
for ( const auto &s : adminUsers ) { for ( const auto &s : adminUsers ) {
if ( s == argUserName ) { if ( s == settings->localUserName ) {
debugMessagesTextEdit->appendPlainText( tr( "[DEBUG] User '%1' has administrative" debugMessagesTextEdit->appendPlainText( tr( "[DEBUG] User '%1' has administrative"
" rights." ).arg( argUserName ) ); " rights." )
.arg( settings->localUserName ) );
return true; return true;
} }
} }
@ -295,11 +296,6 @@ void lc::Lablib::SetPrintReceiptsForLocalClients( const bool &argPrintReceiptsFo
debugMessagesTextEdit->appendPlainText( tr( "[DEBUG] Set print_receipts_for_local_clients to : '%1'" ).arg( QString::number( PrintReceiptsForLocalClients ) ) ); debugMessagesTextEdit->appendPlainText( tr( "[DEBUG] Set print_receipts_for_local_clients to : '%1'" ).arg( QString::number( PrintReceiptsForLocalClients ) ) );
} }
void lc::Lablib::SetUserNameOnServer( const QString &argUserName ) {
userNameOnServer = argUserName;
debugMessagesTextEdit->appendPlainText( tr( "[DEBUG] user_name_on_server set to: '%1'" ).arg( userNameOnServer ) );
}
void lc::Lablib::ShowOrsee() { void lc::Lablib::ShowOrsee() {
QProcess showOrseeProcess; QProcess showOrseeProcess;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); QProcessEnvironment env = QProcessEnvironment::systemEnvironment();

@ -71,7 +71,7 @@ public:
* \param argUserName The account name which shall checked for administrative rights * \param argUserName The account name which shall checked for administrative rights
* \return True, if the account has administrative rights; false, otherwise * \return True, if the account has administrative rights; false, otherwise
*/ */
bool CheckIfUserIsAdmin( const QString &argUserName ) const; bool CheckIfUserIsAdmin() const;
/*! Returns the currently set port number of zTree /*! Returns the currently set port number of zTree
* *
* @return The currently set port number for zTree * @return The currently set port number for zTree
@ -107,11 +107,6 @@ public:
* @return True if receipts for local clients shall be printed * @return True if receipts for local clients shall be printed
*/ */
bool GetPrintReceiptsForLocalClients() const { return PrintReceiptsForLocalClients; } bool GetPrintReceiptsForLocalClients() const { return PrintReceiptsForLocalClients; }
/** Returns a pointer to server's user's name
*
* @return A pointer to the server's user's name
*/
QString GetUserNameOnServer () const { return userNameOnServer; }
/** Returns a QStringList containing all available LaTeX headers of this system /** Returns a QStringList containing all available LaTeX headers of this system
* *
* @return A pointer to a QStringList containing all available LaTeX headers * @return A pointer to a QStringList containing all available LaTeX headers
@ -135,7 +130,6 @@ public:
*/ */
void SetLocalZLeafDefaultName( const QString &argName ); void SetLocalZLeafDefaultName( const QString &argName );
void SetPrintReceiptsForLocalClients( const bool &argPrintReceiptsForLocalClients ); void SetPrintReceiptsForLocalClients( const bool &argPrintReceiptsForLocalClients );
void SetUserNameOnServer( const QString &argUserName );
void ShowOrsee(); void ShowOrsee();
void ShowPreprints(); void ShowPreprints();
@ -176,7 +170,6 @@ private:
QVector< int > *occupiedPorts = nullptr; QVector< int > *occupiedPorts = nullptr;
bool PrintReceiptsForLocalClients = true; bool PrintReceiptsForLocalClients = true;
SessionsModel *sessionsModel = nullptr; //! A derivation from QAbstractTableModel used to store the single Session instances SessionsModel *sessionsModel = nullptr; //! A derivation from QAbstractTableModel used to store the single Session instances
QString userNameOnServer; //! The user name on the server as extracted from the environment variables
QStringList *webcams = nullptr; //! A QStringList containing all available stationary webcams in the laboratory QStringList *webcams = nullptr; //! A QStringList containing all available stationary webcams in the laboratory
}; };

@ -1,5 +1,6 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QProcessEnvironment>
#include "settings.h" #include "settings.h"
@ -20,6 +21,7 @@ lc::Settings::Settings( const QSettings &argSettings, QObject *argParent ) :
lcInstDir{ ReadSettingsItem( "labcontrol_installation_directory", lcInstDir{ ReadSettingsItem( "labcontrol_installation_directory",
"Labcontrol will missbehave with high propability.", "Labcontrol will missbehave with high propability.",
argSettings, true ) }, argSettings, true ) },
localUserName{ GetLocalUserName() },
lprCmd{ ReadSettingsItem( "lpr_command", lprCmd{ ReadSettingsItem( "lpr_command",
"Receipts printing will not work.", "Receipts printing will not work.",
argSettings, true ) }, argSettings, true ) },
@ -103,6 +105,22 @@ bool lc::Settings::CheckPathAndComplain( const QString &argPath, const QString &
return true; return true;
} }
QString lc::Settings::GetLocalUserName() {
const QProcessEnvironment env{ QProcessEnvironment::systemEnvironment() };
QString userName;
// For Linux
if ( env.contains( "USER" ) ) {
userName = env.value( "USER", "" );
qDebug() << "The local user name is" << userName;
} else if ( env.contains( "USERNAME" ) ) { // For Windows
userName = env.value( "USERNAME", "" );
qDebug() << "The local user name is" << userName;
} else {
qWarning() << "The local user name could not be queried";
}
return userName;
}
QString lc::Settings::ReadSettingsItem( const QString &argVariableName, QString lc::Settings::ReadSettingsItem( const QString &argVariableName,
const QString &argMessage, const QString &argMessage,
const QSettings &argSettings, const QSettings &argSettings,

@ -25,6 +25,7 @@ public:
const QString fileMngr; const QString fileMngr;
const QString latexCmd; const QString latexCmd;
const QString lcInstDir; const QString lcInstDir;
const QString localUserName;
const QString lprCmd; const QString lprCmd;
const QString netstatCmd; const QString netstatCmd;
const QString netwBrdAddr; const QString netwBrdAddr;
@ -51,6 +52,7 @@ public:
private: private:
static bool CheckPathAndComplain( const QString &argPath, const QString &argVariableName, static bool CheckPathAndComplain( const QString &argPath, const QString &argVariableName,
const QString &argMessage ); const QString &argMessage );
static QString GetLocalUserName();
static QString ReadSettingsItem( const QString &argVariableName, static QString ReadSettingsItem( const QString &argVariableName,
const QString &argMessage, const QString &argMessage,
const QSettings &argSettings, const QSettings &argSettings,

@ -52,17 +52,7 @@ lc::MainWindow::~MainWindow() {
} }
bool lc::MainWindow::CheckIfUserIsAdmin() { bool lc::MainWindow::CheckIfUserIsAdmin() {
// Query the current user's name or give an error if this fails if ( settings->localUserName.isEmpty() ) {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString userName;
// For Linux
if ( env.contains( "USER" ) ) {
userName = env.value( "USER", "" );
} else if ( env.contains( "USERNAME" ) ) { // For Windows
userName = env.value( "USERNAME", "" );
}
if ( userName.isEmpty() ) {
QMessageBox messageBox{ QMessageBox::Warning, tr( "User not detectable" ), QMessageBox messageBox{ QMessageBox::Warning, tr( "User not detectable" ),
tr( "Your user name could not be queryed. The admin tab will be" tr( "Your user name could not be queryed. The admin tab will be"
" disabled. You won't be able to perform administrative" " disabled. You won't be able to perform administrative"
@ -73,10 +63,9 @@ bool lc::MainWindow::CheckIfUserIsAdmin() {
} }
ui->PTEDebugMessages->appendPlainText( tr( "[DEBUG] The user's name is %1." ) ui->PTEDebugMessages->appendPlainText( tr( "[DEBUG] The user's name is %1." )
.arg( userName ) ); .arg( settings->localUserName ) );
lablib->SetUserNameOnServer( userName );
return lablib->CheckIfUserIsAdmin( userName ); return lablib->CheckIfUserIsAdmin();
} }
void lc::MainWindow::DisableDisfunctionalWidgets() { void lc::MainWindow::DisableDisfunctionalWidgets() {
@ -695,7 +684,7 @@ void lc::MainWindow::SetupWidgets() {
} else { } else {
ui->LAdministrativeRights->setText( tr( "You don't have administrative rights." ) ); ui->LAdministrativeRights->setText( tr( "You don't have administrative rights." ) );
} }
ui->LUserName->setText( tr( "You are user %1" ).arg( lablib->GetUserNameOnServer() ) ); ui->LUserName->setText( tr( "You are user %1" ).arg( settings->localUserName ) );
if ( !settings->userNameOnClients.isEmpty() ) { if ( !settings->userNameOnClients.isEmpty() ) {
ui->RBUseLocalUser->setText( settings->userNameOnClients ); ui->RBUseLocalUser->setText( settings->userNameOnClients );
} else { } else {

Loading…
Cancel
Save