PDA

View Full Version : Remember logged user information



Alir3z4
11th July 2011, 06:11
In my little project, user shoulda log in then have ability to do whatever....
[users kept in Database]
But i don't know how can i hold user information to recognize user when moving between files and dialogs.
In web developing we use SESSIONS to do things like this.
First i thought is better to using file to save logged user info or SETTINGS and ...
Any idea?

squidge
11th July 2011, 07:45
So which do you want to use, a database or a file?

Depending on your requirements, it might be worthwhile to look at QSqLite

Santosh Reddy
11th July 2011, 08:00
I don't think using QSetting / files is a good idea.

You can create user object and pass it around to dialogs (when creating them) or may be, create a kind of session manager (global), which keeps track of logged in user. All the widgets / dialogs can delegate with session manager for authorization.

Alir3z4
11th July 2011, 15:31
So which do you want to use, a database or a file?

Depending on your requirements, it might be worthwhile to look at QSqLite

I use PostgreSQL(QPSQL) for keeping users data and i want o Authorize them around the application.
tnx squidge

Added after 4 minutes:


I don't think using QSetting / files is a good idea.

You can create user object and pass it around to dialogs (when creating them) or may be, create a kind of session manager (global), which keeps track of logged in user. All the widgets / dialogs can delegate with session manager for authorization.

First thing came to my mind was that..
But pass the logged user Object around the dialogs i don't thing be good way or in other word best way!
Coz my project have a lot of dialog and actions on database, and i track user actions and keeps Activities history for them..
tnx Santosh

Santosh Reddy
11th July 2011, 15:37
then you can create session manager, which will hold the logged in user info, and also act as a gateway to access database. All the dialog can access database only using the session manager, and session manager will return data only if the logged in user is valid..

Alir3z4
11th July 2011, 15:45
then you can create session manager, which will hold the logged in user info, and also act as a gateway to access database. All the dialog can access database only using the session manager, and session manager will return data only if the logged in user is valid..

tnx for quick reply!
I don't what you mean exactly about session manager.
Coz even on the web we use file for keeping Sessions data [/tmp]

Of course your idea is acceptable way, But can you add more details about it?
tnx Santosh!

Santosh Reddy
11th July 2011, 20:15
Basically I was suggesting to create a session manager class, which provies a consistant interface / APIs for user authintacation, which can be used by all the widgets / dialogs.

Example:


class SessionManager : public QObject
{
Q_Object;
public:
bool startUser(QString user_name, QString password)
{
if(isCorrectPassword(user_name, password)) //check the username and password from database
{
userNamer = user_name;
timer = startTimer(5 * 60 * 1000); //Auto Log out in 5 mins
return true;
}
return false;
}

void stopUser(void)
{
userName = "";
}

bool isUserActive(void)
{
return !userName.isEmpty();
}

QString activeUser(void)
{
return userName;
}

QString getData(QString database_name, QString table_name)
{
if(!userNmae.isEmpty())
{
timer = startTimer(5 * 60 * 1000); //Auto Log out in 5 mins
return getDataFromDataBase(database_name, table_name)
}
return QString();
}

protected:
void timerEvent(QTimerEvent * event)
{
if(timer == event->timerId())
{
stopUser(); //Logout user
}
killTimer(event->timerId());
}

private:
QString userName;
int timer ;
}

So all access to database is done only by the session manager class, also if you wish, you can make session manage a singleton class.

Alir3z4
13th July 2011, 07:21
Basically I was suggesting to create a session manager class, which provies a consistant interface / APIs for user authintacation, which can be used by all the widgets / dialogs.
So all access to database is done only by the session manager class, also if you wish, you can make session manage a singleton class.


If i'm wrong you correct me..
With this story, when we need access to user info, shoulda initiale a instance of SessionManager, and it's mean everything will be destroyed, coz this class is in memory!
yeah?
sorry for my persistence behavior!

Santosh Reddy
13th July 2011, 07:28
There should be only one instance (Global Instance / Global Variable) of SessionManager, You should not create it at any other time. This is very reason (as I said in my earlier post) SessionManager will an ideal candidate for a Singleton class pattern.

So when you need to access user info you should use the global instance of SessionManager (not create a new one)

Alir3z4
13th July 2011, 18:31
Yes Singleton Pattern (http://en.wikipedia.org/wiki/Singleton_pattern) :) !
My bad Santosh, i got it completely.
tnx