Version: current

      1 <?php
      2 /**
      3  * Abstract base class for an authentication method
      4  *
      5  * This class is used as a base class for the different authentication
      6  * methods like {@link MemberAuthenticator} or {@link OpenIDAuthenticator}.
      7  *
      8  * @author Markus Lanthaler <markus@silverstripe.com>
      9  * @package sapphire
     10  * @subpackage security
     11  */
     12 abstract class Authenticator extends Object {
     13
     14   /**
     15    * This variable holds all authenticators that should be used
     16    *
     17    * @var array
     18    */
     19   private static $authenticators = array('MemberAuthenticator');
     20
     21   /**
     22    * Used to influence the order of authenticators on the login-screen
     23    * (default shows first).
     24    *
     25    * @var string
     26    */
     27   private static $default_authenticator = 'MemberAuthenticator';
     28
     29
     30   /**
     31    * Method to authenticate an user
     32    *
     33    * @param array $RAW_data Raw data to authenticate the user
     34    * @param Form $form Optional: If passed, better error messages can be
     35    *                             produced by using
     36    *                             {@link Form::sessionMessage()}
     37    * @return bool|Member Returns FALSE if authentication fails, otherwise
     38    *                     the member object
     39    */
     40   public abstract static function authenticate($RAW_data,
     41                                                Form $form = null);
     42
     43
     44   /**
     45    * Method that creates the login form for this authentication method
     46    *
     47    * @param Controller The parent controller, necessary to create the
     48    *                   appropriate form action tag
     49    * @return Form Returns the login form to use with this authentication
     50    *              method
     51    */
     52   public abstract static function get_login_form(Controller $controller);
     53
     54
     55   /**
     56    * Get the name of the authentication method
     57    *
     58    * @return string Returns the name of the authentication method.
     59    */
     60   public abstract static function get_name();
     61
     62   public static function register($authenticator) {
     63     self::register_authenticator($authenticator);
     64   }
     65
     66
     67   /**
     68    * Register a new authenticator
     69    *
     70    * The new authenticator has to exist and to be derived from the
     71    * {@link Authenticator}.
     72    * Every authenticator can be registered only once.
     73    *
     74    * @param string $authenticator Name of the authenticator class to
     75    *                              register
     76    * @return bool Returns TRUE on success, FALSE otherwise.
     77    */
     78   public static function register_authenticator($authenticator) {
     79     $authenticator = trim($authenticator);
     80
     81     if(class_exists($authenticator) == false)
     82       return false;
     83
     84     if(is_subclass_of($authenticator, 'Authenticator') == false)
     85       return false;
     86
     87     if(in_array($authenticator, self::$authenticators) == false) {
     88       if(call_user_func(array($authenticator, 'on_register')) === true) {
     89         array_push(self::$authenticators, $authenticator);
     90       } else {
     91         return false;
     92       }
     93     }
     94
     95     return true;
     96   }
     97
     98   public static function unregister($authenticator) {
     99     self::unregister_authenticator($authenticator);
    100   }
    101
    102     /**
    103      * Remove a previously registered authenticator
    104      *
    105      * @param string $authenticator Name of the authenticator class to register
    106      * @return bool Returns TRUE on success, FALSE otherwise.
    107      */
    108     public static function unregister_authenticator($authenticator) {
    109         if(call_user_func(array($authenticator, 'on_unregister')) === true) {
    110             if(in_array($authenticator, self::$authenticators)) {
    111                 unset(self::$authenticators[array_search($authenticator, self::$authenticators)]);
    112             }
    113         };
    114     }
    115
    116
    117   /**
    118    * Check if a given authenticator is registered
    119    *
    120    * @param string $authenticator Name of the authenticator class to check
    121    * @return bool Returns TRUE if the authenticator is registered, FALSE
    122    *              otherwise.
    123    */
    124   public static function is_registered($authenticator) {
    125     return in_array($authenticator, self::$authenticators);
    126   }
    127
    128
    129   /**
    130    * Get all registered authenticators
    131    *
    132    * @return array Returns an array with the class names of all registered
    133    *               authenticators.
    134    */
    135   public static function get_authenticators() {
    136     // put default authenticator first (mainly for tab-order on loginform)
    137     if($key = array_search(self::$default_authenticator,self::$authenticators)) {
    138         unset(self::$authenticators[$key]);
    139         array_unshift(self::$authenticators, self::$default_authenticator);
    140     }
    141
    142     return self::$authenticators;
    143   }
    144
    145   /**
    146    * Set a default authenticator (shows first in tabs)
    147    *
    148    * @param string
    149    */
    150   public static function set_default_authenticator($authenticator) {
    151     self::$default_authenticator = $authenticator;
    152
    153
    154   }
    155
    156   /**
    157    * @return string
    158    */
    159   public static function get_default_authenticator() {
    160     return self::$default_authenticator;
    161   }
    162
    163
    164   /**
    165    * Callback function that is called when the authenticator is registered
    166    *
    167    * Use this method for initialization of a newly registered authenticator.
    168    * Just overload this method and it will be called when the authenticator
    169    * is registered.
    170    * <b>If the method returns FALSE, the authenticator won't be
    171    * registered!</b>
    172    *
    173    * @return bool Returns TRUE on success, FALSE otherwise.
    174    */
    175   protected static function on_register() {
    176     return true;
    177   }
    178
    179   /**
    180    * Callback function that is called when an authenticator is removed.
    181    *
    182    * @return bool
    183    */
    184   protected static function on_unregister() {
    185     return true;
    186   }
    187 }
    188
    189 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -