Results 1 to 15 of 15

Thread: Log-in information available through whole app

  1. #1
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Log-in information available through whole app

    Hi!

    This one is probably dead simple by all of you, but I can't figure out what will be the best way to do it.
    I have an app that require log-in. What is the best and the safest way to make user name, user group and app state available through while application (main window and dialogs)?
    I figure out several ways, but do not know which one to choose and I need your help:
    - use global variables (obviously this would be my first choice, but I have found several posts that advise against globals),
    - use signal-slot to transfer information to a hidden field (good one, but I have to invoke signal-slot every time I do something),
    - use temporary file with that info (easily broken into).
    Is there any other way to do it? Please advise and/or comment on those three options.

    Regards; Luka

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Log-in information available through whole app

    As the current user can be seen as a property of the application, just subclass QApplication and store the user info as its member variable.
    (Preferably make a class for the user related info.)

  3. The following user says thank you to mvuori for this useful post:

    omci (19th November 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    67
    Thanks
    22
    Thanked 5 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Log-in information available through whole app

    If your application is not that complicated,you could insert the details of a user who successfully logged in a table and then use lastinsertedid function to know who is currently logged in and the rights that user has.

  5. #4
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    I'll describe our experience.
    We have a single-tone security object - which contains info about user and his permissions.
    Any action (login,logout, permission-request) perform through this object.
    As result of any action, which influence (or can) to permissions of current user (login, logout, permission changed)
    signal evCheckPermission() will be emitted. Appropriate recepient can check needed info (user login, name, permission and so on) through security object's interface.
    Interface of this object defined in header-file, which include anywhere, where it's needed.
    As result, we avoid duplication of data (and all available errors related with duplication of data).

    P.S. In our case this object is a wrapper over ldap =).
    Last edited by grin; 19th November 2011 at 17:57.

  6. #5
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    @mvuori: thanks, your reply about sub classing suits my case the most. Now I only have to figure out why my QApplication subclass doesn't work.
    @thefatladysingsopera: Unfortunately it is possible to use my app by several users at the same time. Your comment reminded me that I should create a way to log users and their actions.
    @grin: not really useful for my app, but interesting insight about more complex application for permissions. I have no need to push permission to all running instances at the moment. Will remember this for later use.

    Thanks to all!

  7. #6
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    @mvuori: Subclassing QApplication works perfectly. I would like to know, if there is a signal emitted when a variable changes? I can't find it in documents.
    This is my appl.h file with declaration of appl class as subclass of QApplication. If any of the variable (u_name, permission, state) changes, I would like to trigger a specific action, let say change the greeting message on main window. However, a user can log out and another log in at several dialogue windows as well as main window. I can't find a signal that would trigger that action. Do I have to manually create one or does it exists already in Qt?
    Qt Code:
    1. // // // appl.h // // //
    2. #ifndef APPL_H
    3. #define APPL_H
    4.  
    5. #include <QApplication>
    6. #define cApp ((appl *) qApp)
    7.  
    8. class appl : public QApplication {
    9.  
    10. public:
    11. appl(int &argc, char **argv);
    12. ~appl();
    13.  
    14. QString u_name; // user name
    15. QString permission; //user permission
    16. QString state; // state of the application
    17. void change_state(); // function that changes state from public to private
    18.  
    19. };
    20.  
    21. #endif // APPL_H
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Log-in information available through whole app

    Actually you should to use get/set functions (i.e. for user name userName()/setUserName()) and then emit needed signals inside setters. Having public vars isn't a good idea, as it can be accessed everywhere in code through qApp (QApplication::instance()).
    Oleg Shparber

  9. #8
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    @Oleg: I am an amateur programmer and everything I know I learned only from beginners books and on-line tutorials. I am lacking knowledge how to make a variable available through all application. Global vars are not good solution, they say, public vars are not good solution, they say. Private vars are available only to the private parts of code and frankly I have no idea at how get/set functions work. At PHP this is usually solved with $_SESSION[] variables. Can you please explain or give simple example for get/set functions to work across application. Thank you.

  10. #9
    Join Date
    Apr 2010
    Location
    Russian Federation, Kaluga
    Posts
    20
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    Standart practic:
    Qt Code:
    1. // // // appl.h // // //
    2. #ifndef APPL_H
    3. #define APPL_H
    4.  
    5. #include <QApplication>
    6. #define cApp ((appl *) qApp)
    7.  
    8. class appl : public QApplication {
    9.  
    10. public:
    11. appl(int &argc, char **argv);
    12. ~appl();
    13. void change_state(); // function that changes state from public to private
    14.  
    15. //get functions
    16. QString userName() const {return m_u_name;}
    17. QString permission() const {return m_permission;}
    18. QString state() const {return m_state;}
    19.  
    20. //set functions
    21. void setUserName(QString uname) {m_u_name = uname;}
    22. void setPermission(QString permission) {m_permission = permission;}
    23. void setState(QString state) {m_state = state;}
    24.  
    25.  
    26. private:
    27. QString m_u_name; // user name
    28. QString m_permission; //user permission
    29. QString m_state; // state of the application
    30.  
    31. };
    32. #endif // APPL_H
    To copy to clipboard, switch view to plain text mode 

    all get functions returns a COPY of you data - and through this get-functions nobody can change private data.
    So you avoid case, when your private data is changed unexpected.

  11. The following user says thank you to grin for this useful post:

    omci (21st November 2011)

  12. #10
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Log-in information available through whole app

    @omci: Here's an sample code with signals emitted on property change.

    application.h
    Qt Code:
    1. #ifndef APPLICATION_H
    2. #define APPLICATION_H
    3.  
    4. #include <QApplication>
    5.  
    6. class Application : public QApplication, public Settings
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit Application(int &argc, char *argv[]);
    11. ~Application();
    12.  
    13. // Getters
    14. QString userName() const;
    15. QString permission() const;
    16.  
    17. // Setters
    18. void setUserName(const QString & userName);
    19. void setPermission(const QString & permission);
    20.  
    21. signals:
    22. void userNameChanged();
    23. void permissionChanged();
    24.  
    25. private:
    26. QString m_userName;
    27. QString m_permission;
    28.  
    29. private:
    30. }
    31.  
    32. #endif // APPLICATION_H
    To copy to clipboard, switch view to plain text mode 

    application.cpp
    Qt Code:
    1. #include "application.h"
    2.  
    3. Application::Application(int &argc, char *argv[]) :
    4. QApplication(argc, argv)
    5. {
    6. }
    7.  
    8. Application::~Application()
    9. {
    10. }
    11.  
    12.  
    13. QString Application::userName() const
    14. {
    15. return m_userName;
    16. }
    17.  
    18. QString permission() const
    19. {
    20. return m_permission;
    21. }
    22.  
    23. void Application::setUserName(const QString & userName)
    24. {
    25. m_userName = userName;
    26. emit userNameChanged();
    27. }
    28.  
    29. void Application::setPermission(const QString & permission)
    30. {
    31. m_permission = permission;
    32. emit permissionChanged();
    33. }
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

  13. The following user says thank you to Oleg for this useful post:

    omci (21st November 2011)

  14. #11
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    How do I catch the emit signal?
    the signal is emitted in application.cpp as seen in post #10, while the setUserName() is called from login.cpp? I have tried
    Qt Code:
    1. // login.cpp //
    2. QObject::connect(Application, SIGNAL(userNameChanged()), this, SLOT(on_userNameChanged()));
    To copy to clipboard, switch view to plain text mode 
    The error I get is:
    error: expected primary-expression before ‘,’ token
    If I try
    Qt Code:
    1. // login.cpp //
    2. Application appl;
    3. QObject::connect(appl, SIGNAL(userNameChanged()), this, SLOT(on_userNameChanged()));
    To copy to clipboard, switch view to plain text mode 
    the error is:
    No matching function for call to 'Application::Application()'
    What am I doing wrong?

  15. #12
    Join Date
    Oct 2007
    Location
    Lake Forest, CA, USA
    Posts
    132
    Thanks
    10
    Thanked 27 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Log-in information available through whole app

    Qt Code:
    1. Application appl(argc, argv);
    To copy to clipboard, switch view to plain text mode 
    Oleg Shparber

  16. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Log-in information available through whole app

    Just as a small improvement: Signals should only be emitted when the data really changes. E.g.:
    Qt Code:
    1. void Application::setUserName(const QString & userName)
    2. {
    3. if (userName == m_userName)
    4. return;
    5.  
    6. m_userName = userName;
    7. Q_EMIT userNameChanged();
    8. }
    To copy to clipboard, switch view to plain text mode 

  17. #14
    Join Date
    Sep 2009
    Location
    Kranj, Slovenia
    Posts
    25
    Thanks
    10
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Log-in information available through whole app

    Just a couple more questions. Where do I put QObject::connect call? In main.cpp (where I start the application), application.cpp (the definition of the class), login.cpp (where the setUserName is changed, the signal is emitted and one of the places that are affected by the emitted signal)? Do I need to define QObject::connect at all or is it possible to just use something like on_UserName_changed() that would automatically listen to the change in username?

    Regards; Luka

  18. #15
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Log-in information available through whole app

    You can't use "on_UserName_changed()" because its syntax is on_<object name>_<signal name>()!

Similar Threads

  1. Information about curves
    By den in forum Qwt
    Replies: 1
    Last Post: 27th April 2010, 19:02
  2. Optaining information from a DLL
    By srohit24 in forum Qt Programming
    Replies: 2
    Last Post: 14th February 2009, 05:58
  3. Getting System Information
    By Ankitha Varsha in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2008, 10:37
  4. QT app information
    By CHeader in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2008, 11:07
  5. Getting MAC Information from qt?
    By vishal.chauhan in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2007, 09:31

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.