partkeepr

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

AuthenticationProvider.js (3896B)


      1 /**
      2  * Base class for authentication providers
      3  */
      4 Ext.define('PartKeepr.Auth.AuthenticationProvider', {
      5 
      6     mixins: ['Ext.mixin.Observable'],
      7 
      8     /**
      9      * @var {String} The username
     10      */
     11     username: null,
     12 
     13     /**
     14      * @var {String} The password
     15      */
     16     password: null,
     17 
     18     /**
     19      * @var {Object} The authenaticated user
     20      */
     21     user: null,
     22 
     23     constructor: function (config)
     24     {
     25         this.mixins.observable.constructor.call(this, config);
     26     },
     27 
     28     /**
     29      * Returns any additional headers for the requests.
     30      *
     31      * Must be overriden in the child classes.
     32      *
     33      * @return {Object} An object with all additional headers
     34      */
     35     getHeaders: function ()
     36     {
     37         return {};
     38     },
     39 
     40     /**
     41      * Sets the username for authentication
     42      *
     43      * @param {String} username The username
     44      */
     45     setUsername: function (username)
     46     {
     47         this.username = username;
     48     },
     49 
     50     /**
     51      * Returns the username for authentication
     52      *
     53      * @return {String} The username
     54      */
     55     getUsername: function ()
     56     {
     57         return this.username;
     58     },
     59 
     60     /**
     61      * Sets the password for authentication
     62      *
     63      * @param {String} password The password
     64      */
     65     setPassword: function (password)
     66     {
     67         this.password = password;
     68     },
     69 
     70     /**
     71      * Returns the password for authentication
     72      *
     73      * @return {String} The password
     74      */
     75     getPassword: function ()
     76     {
     77         return this.password;
     78     },
     79 
     80     /**
     81      * Triggers the authentication. By default, this simply calls the /api/users/login action, but
     82      * can be overriden in child classes to provide advanced logic.
     83      */
     84     authenticate: function ()
     85     {
     86         PartKeepr.AuthBundle.Entity.User.callPostCollectionAction("login",
     87             {},
     88             Ext.bind(this.onLogin, this),
     89             true
     90         );
     91     },
     92     /**
     93      * Sets the user object
     94      *
     95      * @var {Object} user The user object
     96      */
     97     setUser: function (user)
     98     {
     99         this.user = user;
    100     },
    101     /**
    102      * Returns the user object
    103      *
    104      * @return {Object} The user object
    105      */
    106     getUser: function ()
    107     {
    108         return this.user;
    109     },
    110     /**
    111      * Callback handler for the login action. Checks if the response contains a status code of 401.
    112      *
    113      * @param {Object} options The options object
    114      * @param {Boolean} success If the request was successful
    115      * @param {Object} response The response object
    116      */
    117     onLogin: function (options, success, response)
    118     {
    119         if (response.status == "401") {
    120             this.fireEvent("authenticate", false);
    121         } else {
    122             var records = PartKeepr.AuthBundle.Entity.User.getProxy().getReader().read(response);
    123             this.setUser(records.getRecords()[0]);
    124             this.fireEvent("authenticate", true);
    125         }
    126     },
    127     statics: {
    128         /**
    129          * @var {Object} The current authentication provider
    130          */
    131         authenticationProvider: null,
    132 
    133         /**
    134          * Retrieves the authentication provider. If no authentication provider is set, automatically
    135          * returns the base class, which doesn't have any functionality.
    136          *
    137          * @return {Object} The authentication provider
    138          */
    139         getAuthenticationProvider: function ()
    140         {
    141             if (!(this.authenticationProvider instanceof PartKeepr.Auth.AuthenticationProvider)) {
    142                 this.authenticationProvider = Ext.create("PartKeepr.Auth.AuthenticationProvider");
    143             }
    144 
    145             return this.authenticationProvider;
    146         },
    147 
    148         /**
    149          * Sets the authentication provider
    150          *
    151          * @param {Object} The authentication provider
    152          */
    153         setAuthenticationProvider: function (authenticationProvider)
    154         {
    155             this.authenticationProvider = authenticationProvider;
    156 
    157         }
    158     }
    159 });