partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

LoginManager.js (4035B)


      1 /**
      2  * The login manager is responsible for handling logins. Depending on the configuration, the user may be
      3  * pre-authenticated or the system needs to display a login dialog.
      4  */
      5 Ext.define('PartKeepr.Auth.LoginManager', {
      6 
      7     mixins: ['Ext.mixin.Observable'],
      8 
      9     /**
     10      * @var {Object} An instance of the login dialog
     11      */
     12     loginDialog: null,
     13 
     14     /**
     15      * @var {Object} The authentication provider
     16      */
     17     provider: null,
     18 
     19     /**
     20      * @var {Boolean} If the user is logged in or not
     21      */
     22     loggedIn: false,
     23 
     24     config: {
     25         /**
     26          * @var {Boolean} True if auto-login is required
     27          */
     28         autoLogin: false,
     29 
     30         /**
     31          * @var {String} The username to use for auto-login
     32          */
     33         autoLoginUsername: null,
     34 
     35         /**
     36          * @var {String} The password to use for auto-login
     37          */
     38         autoLoginPassword: null
     39     },
     40 
     41     constructor: function (config)
     42     {
     43         this.mixins.observable.constructor.call(this, config);
     44         this.provider = PartKeepr.Auth.AuthenticationProvider.getAuthenticationProvider();
     45         this.provider.on("authenticate", this.onAuthenticate, this);
     46 
     47         if (!this.config.autoLogin)
     48         {
     49             this.loginDialog = Ext.create("PartKeepr.LoginDialog");
     50             this.loginDialog.on("login", this.onLoginDialog, this);
     51         }
     52     },
     53     /**
     54      * Triggers the login process. If auto-login is required, directly calls authenticate(). If not, the
     55      * login dialog is shown.
     56      */
     57     login: function ()
     58     {
     59         if (this.config.autoLogin) {
     60             this.provider.setUsername(this.config.autoLoginUsername);
     61             this.provider.setPassword(this.config.autoLoginPassword);
     62             this.provider.authenticate();
     63         } else {
     64             this.loginDialog.show();
     65         }
     66     },
     67     /**
     68      * Triggers the logout process by calling the backend's logout function, which in turn
     69      * clears the session information and de-authenticates the user.
     70      */
     71     logout: function ()
     72     {
     73         PartKeepr.AuthBundle.Entity.User.callGetCollectionAction("logout",
     74             {},
     75             Ext.bind(this.onLogout, this),
     76             true
     77         );
     78     },
     79     /**
     80      * Callback for the logout action. Fires the logout event, which destroys
     81      * all windows.
     82      */
     83     onLogout: function () {
     84         this.loggedIn = false;
     85         this.fireEvent("logout");
     86     },
     87     /**
     88      * Callback when the authentication has completed. Fires the "login" event if the authentication was successful.
     89      * Displays an error message if the authentication was not successful.
     90      *
     91      * @param {Boolean} success If the authentication was successful or not
     92      */
     93     onAuthenticate: function (success)
     94     {
     95         if (success) {
     96             if (!this.config.autoLogin)
     97             {
     98                 this.loginDialog.hide();
     99             }
    100             this.fireEvent("login");
    101             this.loggedIn = true;
    102         } else {
    103             Ext.Msg.alert(i18n("Error"), i18n('Username or password invalid.'),
    104                 function ()
    105                 {
    106                     this.loginDialog.show();
    107                 },
    108                 this
    109             );
    110         }
    111     },
    112     /**
    113      * Returns the authenticated user
    114      *
    115      * @return {Object} The user object
    116      */
    117     getUser: function ()
    118     {
    119         return this.provider.getUser();
    120     },
    121     /**
    122      * Callback when the login dialog fired the "login" event. Passes the login data to the authentication provider
    123      * and starts the authentication process
    124      *
    125      * @param {String} username The username
    126      * @param {String} password The password
    127      */
    128     onLoginDialog: function (username, password)
    129     {
    130         this.provider.setUsername(username);
    131         this.provider.setPassword(password);
    132         this.provider.authenticate();
    133     },
    134     /**
    135      * Returns if the user is logged in or not
    136      *
    137      * @return {Boolean}
    138      */
    139     isLoggedIn: function ()
    140     {
    141         return this.loggedIn;
    142     }
    143 
    144 });