Results 1 to 10 of 10

Thread: Remember logged user information

  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Remember logged user information

    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?
    ...یه مرد هیچوقت زمین نمیخوره

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Remember logged user information

    So which do you want to use, a database or a file?

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

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remember logged user information

    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.

  4. #4
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Remember logged user information

    Quote Originally Posted by squidge View Post
    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:


    Quote Originally Posted by Santosh Reddy View Post
    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
    Last edited by Alir3z4; 11th July 2011 at 16:31.
    ...یه مرد هیچوقت زمین نمیخوره

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remember logged user information

    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..

  6. #6
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Remember logged user information

    Quote Originally Posted by Santosh Reddy View Post
    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!
    ...یه مرد هیچوقت زمین نمیخوره

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remember logged user information

    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:

    Qt Code:
    1. class SessionManager : public QObject
    2. {
    3. Q_Object;
    4. public:
    5. bool startUser(QString user_name, QString password)
    6. {
    7. if(isCorrectPassword(user_name, password)) //check the username and password from database
    8. {
    9. userNamer = user_name;
    10. timer = startTimer(5 * 60 * 1000); //Auto Log out in 5 mins
    11. return true;
    12. }
    13. return false;
    14. }
    15.  
    16. void stopUser(void)
    17. {
    18. userName = "";
    19. }
    20.  
    21. bool isUserActive(void)
    22. {
    23. return !userName.isEmpty();
    24. }
    25.  
    26. QString activeUser(void)
    27. {
    28. return userName;
    29. }
    30.  
    31. QString getData(QString database_name, QString table_name)
    32. {
    33. if(!userNmae.isEmpty())
    34. {
    35. timer = startTimer(5 * 60 * 1000); //Auto Log out in 5 mins
    36. return getDataFromDataBase(database_name, table_name)
    37. }
    38. return QString();
    39. }
    40.  
    41. protected:
    42. void timerEvent(QTimerEvent * event)
    43. {
    44. if(timer == event->timerId())
    45. {
    46. stopUser(); //Logout user
    47. }
    48. killTimer(event->timerId());
    49. }
    50.  
    51. private:
    52. QString userName;
    53. int timer ;
    54. }
    To copy to clipboard, switch view to plain text mode 

    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.

  8. #8
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Question Re: Remember logged user information

    Quote Originally Posted by Santosh Reddy View Post
    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!
    ...یه مرد هیچوقت زمین نمیخوره

  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Remember logged user information

    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)

  10. The following user says thank you to Santosh Reddy for this useful post:

    Alir3z4 (13th July 2011)

  11. #10
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Remember logged user information

    Yes Singleton Pattern !
    My bad Santosh, i got it completely.
    tnx
    ...یه مرد هیچوقت زمین نمیخوره

Similar Threads

  1. Remember code folding
    By giantdragon in forum Qt Tools
    Replies: 1
    Last Post: 19th June 2011, 07:17
  2. Replies: 2
    Last Post: 6th May 2011, 23:32
  3. Replies: 1
    Last Post: 5th May 2011, 04:47
  4. remember file path
    By eric in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2008, 17:52
  5. Determining Logged In State
    By TheGrimace in forum Qt Programming
    Replies: 2
    Last Post: 22nd October 2007, 16:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.