2007-07-19 12:40:28 +02:00
< ? php
/**
* Implements a basic security model
2008-02-25 03:10:37 +01:00
* @ package sapphire
* @ subpackage security
2007-07-19 12:40:28 +02:00
*/
class Security extends Controller {
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-10-02 23:57:12 +02:00
* Default user name . Only used in dev - mode by { @ link setDefaultAdmin ()}
*
* @ var string
* @ see setDefaultAdmin ()
2007-07-19 12:40:28 +02:00
*/
2007-09-16 19:39:41 +02:00
protected static $default_username ;
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-10-02 23:57:12 +02:00
* Default password . Only used in dev - mode by { @ link setDefaultAdmin ()}
*
* @ var string
* @ see setDefaultAdmin ()
2007-07-19 12:40:28 +02:00
*/
2007-09-16 19:39:41 +02:00
protected static $default_password ;
2007-09-14 05:12:21 +02:00
2007-09-15 23:51:37 +02:00
/**
* If set to TRUE to prevent sharing of the session across several sites
* in the domain .
*
* @ var bool
*/
2007-07-19 12:40:28 +02:00
protected static $strictPathChecking = false ;
2007-09-14 05:12:21 +02:00
2007-09-15 23:51:37 +02:00
/**
* Should passwords be stored encrypted ?
2009-11-14 02:14:32 +01:00
* @ deprecated 2.4 Please use 'none' as the default $encryptionAlgorithm instead
2007-09-15 23:51:37 +02:00
*
* @ var bool
*/
protected static $encryptPasswords = true ;
/**
2009-11-14 02:14:32 +01:00
* The password encryption algorithm to use by default .
* This is an arbitrary code registered through { @ link PasswordEncryptor } .
2007-09-15 23:51:37 +02:00
*
* @ var string
*/
2009-11-14 02:14:32 +01:00
protected static $encryptionAlgorithm = 'sha1_v2.4' ;
2007-09-15 23:51:37 +02:00
/**
* Should a salt be used for the password encryption ?
2009-11-14 02:14:32 +01:00
* @ deprecated 2.4 Please use a custom { @ link PasswordEncryptor } instead
2007-09-15 23:51:37 +02:00
*
* @ var bool
*/
protected static $useSalt = true ;
2007-10-25 03:51:53 +02:00
2008-10-08 04:00:12 +02:00
/**
* Showing " Remember me " - checkbox
* on loginform , and saving encrypted credentials to a cookie .
*
* @ var bool
*/
public static $autologin_enabled = true ;
2007-10-25 03:51:53 +02:00
/**
* Location of word list to use for generating passwords
*
* @ var string
*/
2009-09-04 01:36:45 +02:00
protected static $wordlist = './wordlist.txt' ;
2007-10-25 03:51:53 +02:00
2008-04-05 01:04:16 +02:00
/**
* Template thats used to render the pages .
*
* @ var string
*/
public static $template_main = 'Page' ;
2008-06-25 06:05:28 +02:00
/**
* Default message set used in permission failures .
*
* @ var array | string
*/
protected static $default_message_set = '' ;
2007-10-25 03:51:53 +02:00
/**
* Get location of word list file
*/
static function get_word_list () {
return Security :: $wordlist ;
}
2008-08-11 02:14:48 +02:00
/**
* Enable or disable recording of login attempts
* through the { @ link LoginRecord } object .
*
* @ var boolean $login_recording
*/
protected static $login_recording = false ;
2007-10-25 03:51:53 +02:00
/**
* Set location of word list file
*
* @ param string $wordListFile Location of word list file
*/
static function set_word_list ( $wordListFile ) {
Security :: $wordlist = $wordListFile ;
}
2008-06-25 06:05:28 +02:00
/**
* Set the default message set used in permissions failures .
*
* @ param string | array $messageSet
*/
static function set_default_message_set ( $messageSet ) {
self :: $default_message_set = $messageSet ;
}
2007-09-15 23:51:37 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-14 21:10:18 +02:00
* Register that we ' ve had a permission failure trying to view the given page
*
2007-07-19 12:40:28 +02:00
* This will redirect to a login page .
* If you don ' t provide a messageSet , a default will be used .
2007-09-14 21:10:18 +02:00
*
2007-11-07 03:33:09 +01:00
* @ param Controller $controller The controller that you were on to cause the permission
2007-09-14 21:10:18 +02:00
* failure .
* @ param string | array $messageSet The message to show to the user . This
* can be a string , or a map of different
* messages for different contexts .
* If you pass an array , you can use the
* following keys :
* - default : The default message
* - logInAgain : The message to show
* if the user has just
* logged out and the
* - alreadyLoggedIn : The message to
* show if the user
* is already logged
* in and lacks the
* permission to
* access the item .
2009-07-09 05:20:32 +02:00
*
* The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link
* to log in .
2007-07-19 12:40:28 +02:00
*/
2007-11-09 04:42:04 +01:00
static function permissionFailure ( $controller = null , $messageSet = null ) {
2009-07-09 05:20:32 +02:00
if ( ! $controller ) $controller = Controller :: curr ();
2008-11-03 14:50:06 +01:00
if ( Director :: is_ajax ()) {
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
$response = ( $controller ) ? $controller -> getResponse () : new SS_HTTPResponse ();
2008-11-03 14:50:06 +01:00
$response -> setStatusCode ( 403 );
2010-01-12 23:48:03 +01:00
if ( ! Member :: currentUser ()) $response -> setBody ( 'NOTLOGGEDIN:' );
2008-11-03 14:50:06 +01:00
return $response ;
} else {
// Prepare the messageSet provided
if ( ! $messageSet ) {
if ( self :: $default_message_set ) {
$messageSet = self :: $default_message_set ;
} else {
$messageSet = array (
'default' => _t (
'Security.NOTEPAGESECURED' ,
2009-07-09 05:20:32 +02:00
" That page is secured. Enter your credentials below and we will send "
. " you right along. "
2008-11-03 14:50:06 +01:00
),
'alreadyLoggedIn' => _t (
'Security.ALREADYLOGGEDIN' ,
2009-07-09 05:20:32 +02:00
" You don't have access to this page. If you have another account that "
. " can access that page, you can <a href= \" %s \" >log in again</a>. " ,
PR_MEDIUM ,
" %s will be replaced with a link to log in. "
2008-11-03 14:50:06 +01:00
),
'logInAgain' => _t (
'Security.LOGGEDOUT' ,
2009-07-09 05:20:32 +02:00
" You have been logged out. If you would like to log in again, enter "
. " your credentials below. "
2008-11-03 14:50:06 +01:00
)
);
}
2008-06-25 06:05:28 +02:00
}
2007-09-14 05:12:21 +02:00
2008-11-03 14:50:06 +01:00
if ( ! is_array ( $messageSet )) {
$messageSet = array ( 'default' => $messageSet );
}
2007-09-14 05:12:21 +02:00
2008-11-03 14:50:06 +01:00
// Work out the right message to show
2009-07-09 05:20:32 +02:00
if ( Member :: currentUser ()) {
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
$response = ( $controller ) ? $controller -> getResponse () : new SS_HTTPResponse ();
2009-07-09 05:20:32 +02:00
$response -> setStatusCode ( 403 );
2009-12-21 01:12:08 +01:00
//If 'alreadyLoggedIn' is not specified in the array, then use the default
//which should have been specified in the lines above
if ( isset ( $messageSet [ 'alreadyLoggedIn' ]))
$message = $messageSet [ 'alreadyLoggedIn' ];
else
$message = $messageSet [ 'default' ];
2009-07-09 05:20:32 +02:00
// Replace %s with the log in link
2009-12-21 01:12:08 +01:00
$body = sprintf ( $message ,
2009-07-09 05:20:32 +02:00
Controller :: join_links ( Director :: baseURL (), 'Security/login' ,
'?BackURL=' . urlencode ( $_SERVER [ 'REQUEST_URI' ])));
$response -> setBody ( $body );
return $response ;
2008-11-03 14:50:06 +01:00
} else if ( substr ( Director :: history (), 0 , 15 ) == 'Security/logout' ) {
2009-02-02 00:49:53 +01:00
$message = $messageSet [ 'logInAgain' ] ? $messageSet [ 'logInAgain' ] : $messageSet [ 'default' ];
2008-11-03 14:50:06 +01:00
} else {
$message = $messageSet [ 'default' ];
}
Session :: set ( " Security.Message.message " , $message );
Session :: set ( " Security.Message.type " , 'warning' );
Session :: set ( " BackURL " , $_SERVER [ 'REQUEST_URI' ]);
// TODO AccessLogEntry needs an extension to handle permission denied errors
// Audit logging hook
if ( $controller ) $controller -> extend ( 'permissionDenied' , $member );
2008-11-28 06:29:52 +01:00
2009-03-10 23:17:26 +01:00
Director :: redirect ( " Security/login?BackURL= " . urlencode ( $_SERVER [ 'REQUEST_URI' ]));
2007-07-19 12:40:28 +02:00
}
2007-08-17 05:09:46 +02:00
return ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 19:04:11 +02:00
/**
* Get the login form to process according to the submitted data
*/
2007-09-16 02:32:48 +02:00
protected function LoginForm () {
2008-04-26 08:31:52 +02:00
if ( isset ( $this -> requestParams [ 'AuthenticationMethod' ])) {
2007-09-15 02:08:23 +02:00
$authenticator = trim ( $_REQUEST [ 'AuthenticationMethod' ]);
2007-09-16 02:44:30 +02:00
$authenticators = Authenticator :: get_authenticators ();
2007-09-15 02:08:23 +02:00
if ( in_array ( $authenticator , $authenticators )) {
2008-10-08 04:00:12 +02:00
return call_user_func ( array ( $authenticator , 'get_login_form' ), $this );
2007-09-14 19:04:11 +02:00
}
}
2007-09-15 02:08:23 +02:00
user_error ( 'Passed invalid authentication method' , E_USER_ERROR );
2007-07-19 12:40:28 +02:00
}
2007-09-14 19:04:11 +02:00
/**
* Get the login forms for all available authentication methods
*
2007-09-14 21:10:18 +02:00
* @ return array Returns an array of available login forms ( array of Form
* objects ) .
*
2007-09-14 19:04:11 +02:00
* @ todo Check how to activate / deactivate authentication methods
*/
2007-09-16 02:32:48 +02:00
protected function GetLoginForms ()
2007-09-14 19:04:11 +02:00
{
$forms = array ();
2007-09-15 02:08:23 +02:00
2007-09-16 02:44:30 +02:00
$authenticators = Authenticator :: get_authenticators ();
2007-09-15 02:08:23 +02:00
foreach ( $authenticators as $authenticator ) {
array_push ( $forms ,
2007-09-16 02:44:30 +02:00
call_user_func ( array ( $authenticator , 'get_login_form' ),
2007-09-15 02:08:23 +02:00
$this ));
}
2007-09-14 19:04:11 +02:00
return $forms ;
}
/**
* Get a link to a security action
*
* @ param string $action Name of the action
2007-09-14 21:10:18 +02:00
* @ return string Returns the link to the given action
2007-09-14 19:04:11 +02:00
*/
2007-09-16 02:32:48 +02:00
public static function Link ( $action = null ) {
2007-07-19 12:40:28 +02:00
return " Security/ $action " ;
}
2007-09-14 19:04:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-14 19:04:11 +02:00
* Log the currently logged in user out
*
2007-09-14 05:12:21 +02:00
* @ param bool $redirect Redirect the user back to where they came .
2007-09-14 19:04:11 +02:00
* - If it ' s false , the code calling logout () is
* responsible for sending the user where - ever
* they should go .
2007-07-19 12:40:28 +02:00
*/
2007-09-16 02:32:48 +02:00
public function logout ( $redirect = true ) {
2007-09-14 21:10:18 +02:00
if ( $member = Member :: currentUser ())
$member -> logOut ();
if ( $redirect )
Director :: redirectBack ();
2007-07-19 12:40:28 +02:00
}
2007-09-14 05:12:21 +02:00
2007-09-14 19:04:11 +02:00
/**
2007-09-14 21:10:18 +02:00
* Show the " login " page
2007-09-14 19:04:11 +02:00
*
2007-09-14 21:10:18 +02:00
* @ return string Returns the " login " page as HTML code .
2007-09-14 19:04:11 +02:00
*/
2007-09-16 02:32:48 +02:00
public function login () {
2009-03-04 04:44:11 +01:00
// Event handler for pre-login, with an option to let it break you out of the login form
$eventResults = $this -> extend ( 'onBeforeSecurityLogin' );
// If there was a redirection, return
if ( Director :: redirected_to ()) return ;
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
// If there was an SS_HTTPResponse object returned, then return that
2009-03-04 04:44:11 +01:00
else if ( $eventResults ) {
foreach ( $eventResults as $result ) {
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
if ( $result instanceof SS_HTTPResponse ) return $result ;
2009-03-04 04:44:11 +01:00
}
}
2007-09-14 21:13:12 +02:00
$customCSS = project () . '/css/tabs.css' ;
if ( Director :: fileExists ( $customCSS )) {
Requirements :: css ( $customCSS );
}
$tmpPage = new Page ();
2008-02-25 03:10:37 +01:00
$tmpPage -> Title = _t ( 'Security.LOGIN' , 'Log in' );
2007-07-19 12:40:28 +02:00
$tmpPage -> URLSegment = " Security " ;
2007-12-02 22:19:54 +01:00
$tmpPage -> ID = - 1 ; // Set the page ID to -1 so we dont get the top level pages as its children
2007-07-19 12:40:28 +02:00
2008-08-09 05:29:30 +02:00
$controller = new Page_Controller ( $tmpPage );
2007-07-19 12:40:28 +02:00
$controller -> init ();
2007-09-15 04:05:23 +02:00
//Controller::$currentController = $controller;
2007-07-19 12:40:28 +02:00
2007-09-27 23:13:59 +02:00
$content = '' ;
$forms = $this -> GetLoginForms ();
if ( ! count ( $forms )) {
user_error ( 'No login-forms found, please use Authenticator::register_authenticator() to add one' , E_USER_ERROR );
}
// only display tabs when more than one authenticator is provided
// to save bandwidth and reduce the amount of custom styling needed
if ( count ( $forms ) > 1 ) {
2009-11-26 06:08:08 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/loader.js' );
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/prototype/prototype.js " );
Requirements :: javascript ( SAPPHIRE_DIR . " /thirdparty/behaviour/behaviour.js " );
Requirements :: javascript ( SAPPHIRE_DIR . " /javascript/prototype_improvements.js " );
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
Requirements :: javascript ( THIRDPARTY_DIR . " /scriptaculous/effects.js " );
Requirements :: css ( SAPPHIRE_DIR . " /css/Form.css " );
2007-09-27 23:13:59 +02:00
2007-09-16 19:30:12 +02:00
// Needed because the <base href=".."> in the template makes problems
// with the tabstrip library otherwise
$link_base = Director :: absoluteURL ( $this -> Link ( " login " ));
2008-11-18 02:48:37 +01:00
Requirements :: javascript ( THIRDPARTY_DIR . " /jquery/jquery.js " );
2009-11-26 06:08:08 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . " /javascript/jquery_improvements.js " );
Requirements :: javascript ( THIRDPARTY_DIR . '/jquery-livequery/jquery.livequery.js' );
2008-11-18 02:48:37 +01:00
Requirements :: javascript ( THIRDPARTY_DIR . " /tabstrip/tabstrip.js " );
Requirements :: css ( THIRDPARTY_DIR . " /tabstrip/tabstrip.css " );
2007-09-16 19:30:12 +02:00
$content = '<div id="Form_EditForm">' ;
$content .= '<ul class="tabstrip">' ;
$content_forms = '' ;
2007-09-14 19:04:11 +02:00
2007-09-16 02:44:30 +02:00
foreach ( $forms as $form ) {
$content .= " <li><a href= \" $link_base # { $form -> FormName () } _tab \" > { $form -> getAuthenticator () -> get_name () } </a></li> \n " ;
$content_forms .= '<div class="tab" id="' . $form -> FormName () . '_tab">' . $form -> forTemplate () . " </div> \n " ;
}
$content .= " </ul> \n " . $content_forms . " \n </div> \n " ;
2007-09-27 23:13:59 +02:00
} else {
$content .= $forms [ 0 ] -> forTemplate ();
}
if ( strlen ( $message = Session :: get ( 'Security.Message.message' )) > 0 ) {
$message_type = Session :: get ( 'Security.Message.type' );
if ( $message_type == 'bad' ) {
$message = " <p class= \" message $message_type\ " > $message </ p > " ;
2007-09-14 21:10:18 +02:00
} else {
2007-09-27 23:13:59 +02:00
$message = " <p> $message </p> " ;
2007-09-14 21:10:18 +02:00
}
2007-07-19 12:40:28 +02:00
2007-09-27 23:13:59 +02:00
$customisedController = $controller -> customise ( array (
" Content " => $message ,
2007-12-02 22:19:54 +01:00
" Form " => $content ,
2007-09-27 23:13:59 +02:00
));
} else {
$customisedController = $controller -> customise ( array (
2009-06-18 10:22:27 +02:00
" Form " => $content ,
2007-09-27 23:13:59 +02:00
));
}
2008-11-10 04:51:35 +01:00
Session :: clear ( 'Security.Message' );
2007-09-27 23:13:59 +02:00
// custom processing
2009-02-02 00:49:53 +01:00
return $customisedController -> renderWith ( array ( 'Security_login' , 'Security' , $this -> stat ( 'template_main' )));
2007-07-19 12:40:28 +02:00
}
2007-10-02 06:32:11 +02:00
function basicauthlogin () {
$member = BasicAuth :: requireLogin ( " SilverStripe login " , 'ADMIN' );
$member -> LogIn ();
}
2008-09-24 06:17:33 +02:00
2007-09-14 19:04:11 +02:00
/**
2007-09-14 21:10:18 +02:00
* Show the " lost password " page
2007-09-14 19:04:11 +02:00
*
2007-09-16 02:32:48 +02:00
* @ return string Returns the " lost password " page as HTML code .
2007-09-14 19:04:11 +02:00
*/
2007-09-16 02:32:48 +02:00
public function lostpassword () {
2009-11-26 06:08:08 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/loader.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/prototype_improvements.js' );
2008-09-29 20:49:55 +02:00
Requirements :: javascript ( THIRDPARTY_DIR . '/scriptaculous/effects.js' );
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
$tmpPage = new Page ();
2007-10-25 04:47:45 +02:00
$tmpPage -> Title = _t ( 'Security.LOSTPASSWORDHEADER' , 'Lost Password' );
2007-09-16 02:32:48 +02:00
$tmpPage -> URLSegment = 'Security' ;
2007-07-19 12:40:28 +02:00
$controller = new Page_Controller ( $tmpPage );
2007-11-06 06:23:00 +01:00
$controller -> init ();
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
$customisedController = $controller -> customise ( array (
2007-10-25 04:47:45 +02:00
'Content' =>
'<p>' .
_t (
'Security.NOTERESETPASSWORD' ,
'Enter your e-mail address and we will send you a link with which you can reset your password'
) .
'</p>' ,
2007-09-16 02:32:48 +02:00
'Form' => $this -> LostPasswordForm (),
2007-07-19 12:40:28 +02:00
));
2007-08-23 08:43:40 +02:00
//Controller::$currentController = $controller;
2009-02-02 00:49:53 +01:00
return $customisedController -> renderWith ( array ( 'Security_lostpassword' , 'Security' , $this -> stat ( 'template_main' )));
2007-07-19 12:40:28 +02:00
}
2007-09-14 19:04:11 +02:00
2007-09-16 02:32:48 +02:00
/**
* Factory method for the lost password form
*
* @ return Form Returns the lost password form
*/
public function LostPasswordForm () {
2008-10-16 13:08:52 +02:00
return new MemberLoginForm (
$this ,
'LostPasswordForm' ,
new FieldSet (
2009-02-02 00:49:53 +01:00
new EmailField ( 'Email' , _t ( 'Member.EMAIL' , 'Email' ))
2008-10-16 13:08:52 +02:00
),
new FieldSet (
new FormAction (
'forgotPassword' ,
_t ( 'Security.BUTTONSEND' , 'Send me the password reset link' )
)
),
false
);
2007-09-16 02:32:48 +02:00
}
2007-09-14 19:04:11 +02:00
/**
2008-10-24 04:23:53 +02:00
* Show the " password sent " page , after a user has requested
* to reset their password .
2007-09-14 19:04:11 +02:00
*
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
* @ param SS_HTTPRequest $request The SS_HTTPRequest for this action .
2007-09-14 21:10:18 +02:00
* @ return string Returns the " password sent " page as HTML code .
2007-09-14 19:04:11 +02:00
*/
2008-09-19 01:00:36 +02:00
public function passwordsent ( $request ) {
2009-11-26 06:08:08 +01:00
Requirements :: javascript ( SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/loader.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js' );
Requirements :: javascript ( SAPPHIRE_DIR . '/javascript/prototype_improvements.js' );
2008-09-29 20:49:55 +02:00
Requirements :: javascript ( THIRDPARTY_DIR . '/scriptaculous/effects.js' );
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
$tmpPage = new Page ();
2007-10-25 04:47:45 +02:00
$tmpPage -> Title = _t ( 'Security.LOSTPASSWORDHEADER' );
2007-09-16 02:32:48 +02:00
$tmpPage -> URLSegment = 'Security' ;
2009-03-10 23:17:26 +01:00
$tmpPage -> ID = - 1 ; // Set the page ID to -1 so we dont get the top level pages as its children
2007-07-19 12:40:28 +02:00
$controller = new Page_Controller ( $tmpPage );
2007-11-06 06:23:00 +01:00
$controller -> init ();
2007-09-14 05:12:21 +02:00
2008-12-04 23:38:32 +01:00
$email = Convert :: raw2xml ( $request -> param ( 'ID' ) . '.' . $request -> getExtension ());
2008-10-24 04:23:53 +02:00
2007-07-19 12:40:28 +02:00
$customisedController = $controller -> customise ( array (
2007-10-25 04:47:45 +02:00
'Title' => sprintf ( _t ( 'Security.PASSWORDSENTHEADER' , " Password reset link sent to '%s' " ), $email ),
2007-09-16 02:32:48 +02:00
'Content' =>
2007-10-25 04:47:45 +02:00
" <p> " .
2009-09-10 05:01:46 +02:00
sprintf ( _t ( 'Security.PASSWORDSENTTEXT' , " Thank you! A reset link has been sent to '%s', provided an account exists for this email address. " ), $email ) .
2007-10-25 04:47:45 +02:00
" </p> " ,
2007-07-19 12:40:28 +02:00
));
2007-08-23 08:43:40 +02:00
//Controller::$currentController = $controller;
2009-02-02 00:49:53 +01:00
return $customisedController -> renderWith ( array ( 'Security_passwordsent' , 'Security' , $this -> stat ( 'template_main' )));
2007-07-19 12:40:28 +02:00
}
2007-09-14 05:12:21 +02:00
2007-09-16 02:32:48 +02:00
/**
* Create a link to the password reset form
*
* @ param string $autoLoginHash The auto login hash
*/
public static function getPasswordResetLink ( $autoLoginHash ) {
$autoLoginHash = urldecode ( $autoLoginHash );
return self :: Link ( 'changepassword' ) . " ?h= $autoLoginHash " ;
}
2007-09-16 19:39:41 +02:00
2007-09-16 02:32:48 +02:00
/**
* Show the " change password " page
*
* @ return string Returns the " change password " page as HTML code .
*/
public function changepassword () {
$tmpPage = new Page ();
2007-10-25 04:47:45 +02:00
$tmpPage -> Title = _t ( 'Security.CHANGEPASSWORDHEADER' , 'Change your password' );
2007-09-16 02:32:48 +02:00
$tmpPage -> URLSegment = 'Security' ;
2009-03-10 23:17:26 +01:00
$tmpPage -> ID = - 1 ; // Set the page ID to -1 so we dont get the top level pages as its children
2007-09-16 02:32:48 +02:00
$controller = new Page_Controller ( $tmpPage );
2007-11-06 06:23:00 +01:00
$controller -> init ();
2007-09-16 02:32:48 +02:00
2008-08-11 07:18:18 +02:00
if ( isset ( $_REQUEST [ 'h' ]) && Member :: member_from_autologinhash ( $_REQUEST [ 'h' ])) {
2007-09-16 02:32:48 +02:00
// The auto login hash is valid, store it for the change password form
Session :: set ( 'AutoLoginHash' , $_REQUEST [ 'h' ]);
$customisedController = $controller -> customise ( array (
'Content' =>
2007-10-25 04:47:45 +02:00
'<p>' .
_t ( 'Security.ENTERNEWPASSWORD' , 'Please enter a new password.' ) .
'</p>' ,
2007-09-16 02:32:48 +02:00
'Form' => $this -> ChangePasswordForm (),
));
} elseif ( Member :: currentUser ()) {
// let a logged in user change his password
$customisedController = $controller -> customise ( array (
2007-10-25 04:47:45 +02:00
'Content' => '<p>' . _t ( 'Security.CHANGEPASSWORDBELOW' , 'You can change your password below.' ) . '</p>' ,
2007-09-16 02:32:48 +02:00
'Form' => $this -> ChangePasswordForm ()));
} else {
// show an error message if the auto login hash is invalid and the
// user is not logged in
if ( isset ( $_REQUEST [ 'h' ])) {
2007-10-25 04:47:45 +02:00
$customisedController = $controller -> customise (
array ( 'Content' =>
sprintf (
_t ( 'Security.NOTERESETLINKINVALID' ,
2008-10-17 17:20:31 +02:00
'<p>The password reset link is invalid or expired.</p><p>You can request a new one <a href="%s">here</a> or change your password after you <a href="%s">logged in</a>.</p>'
2007-10-25 04:47:45 +02:00
),
$this -> Link ( 'lostpassword' ),
$this -> link ( 'login' )
)
)
);
2007-09-16 02:32:48 +02:00
} else {
2007-10-25 04:47:45 +02:00
self :: permissionFailure (
$this ,
_t ( 'Security.ERRORPASSWORDPERMISSION' , 'You must be logged in in order to change your password!' )
);
2007-09-16 17:31:44 +02:00
return ;
2007-09-16 02:32:48 +02:00
}
}
2007-10-25 04:38:35 +02:00
//Controller::$currentController = $controller;
2009-02-02 00:49:53 +01:00
return $customisedController -> renderWith ( array ( 'Security_changepassword' , 'Security' , $this -> stat ( 'template_main' )));
2007-09-16 02:32:48 +02:00
}
2008-09-24 06:17:33 +02:00
/**
* Security / ping can be visited with ajax to keep a session alive .
* This is used in the CMS .
*/
function ping () {
return 1 ;
}
2007-09-16 02:32:48 +02:00
2007-09-14 19:04:11 +02:00
/**
2007-09-14 21:10:18 +02:00
* Factory method for the lost password form
2007-09-14 19:04:11 +02:00
*
2007-09-14 21:10:18 +02:00
* @ return Form Returns the lost password form
2007-09-14 19:04:11 +02:00
*/
2007-09-16 02:32:48 +02:00
public function ChangePasswordForm () {
return new ChangePasswordForm ( $this , 'ChangePasswordForm' );
2007-07-19 12:40:28 +02:00
}
/**
2007-09-14 21:10:18 +02:00
* Authenticate using the given email and password , returning the
* appropriate member object if
*
* @ return bool | Member Returns FALSE if authentication fails , otherwise
* the member object
2007-10-02 23:57:12 +02:00
* @ see setDefaultAdmin ()
2007-07-19 12:40:28 +02:00
*/
2007-09-16 02:32:48 +02:00
public static function authenticate ( $RAW_email , $RAW_password ) {
2007-07-19 12:40:28 +02:00
$SQL_email = Convert :: raw2sql ( $RAW_email );
$SQL_password = Convert :: raw2sql ( $RAW_password );
// Default login (see {@setDetaultAdmin()})
2007-09-16 19:39:41 +02:00
if (( $RAW_email == self :: $default_username ) && ( $RAW_password == self :: $default_password )
&& ! empty ( self :: $default_username ) && ! empty ( self :: $default_password )) {
2007-07-19 12:40:28 +02:00
$member = self :: findAnAdministrator ();
} else {
2010-01-20 21:17:25 +01:00
$member = DataObject :: get_one ( " Member " , " \" " . Member :: get_unique_identifier_field () . " \" = ' $SQL_email ' AND \" Password \" IS NOT NULL " );
2007-12-02 22:35:30 +01:00
if ( $member && ( $member -> checkPassword ( $RAW_password ) == false )) {
$member = null ;
}
2007-07-19 12:40:28 +02:00
}
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
return $member ;
}
2007-09-14 19:04:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2010-01-25 06:18:17 +01:00
* Return an existing member with administrator privileges , or create one of necessary .
*
* Will create a default 'Administrators' group if no group is found
* with an ADMIN permission . Will create a new 'Admin' member with administrative permissions
* if no existing Member with these permissions is found .
*
* Important : Any newly created administrator accounts will NOT have valid
* login credentials ( Email / Password properties ), which means they can ' t be used for login
* purposes outside of any default credentials set through { @ link Security :: setDefaultAdmin ()} .
*
* @ return Member
2007-07-19 12:40:28 +02:00
*/
2010-01-25 06:18:17 +01:00
static function findAnAdministrator () {
2009-12-10 02:00:08 +01:00
// coupling to subsites module
2009-12-03 22:32:46 +01:00
$subsiteCheck = class_exists ( 'GroupSubsites' ) ? ' AND "Group"."SubsiteID" = 0' : '' ;
2007-07-19 12:40:28 +02:00
2009-12-10 02:00:08 +01:00
// find a group with ADMIN permission
$adminGroup = DataObject :: get ( 'Group' ,
" \" Permission \" . \" Code \" = 'ADMIN' $subsiteCheck " ,
" \" Group \" . \" ID \" " ,
" JOIN \" Permission \" ON \" Group \" . \" ID \" = \" Permission \" . \" GroupID \" " ,
'1' );
if ( $adminGroup ) {
$adminGroup = $adminGroup -> First ();
2007-07-19 12:40:28 +02:00
if ( $adminGroup -> Members () -> First ()) {
$member = $adminGroup -> Members () -> First ();
}
}
2007-09-14 05:12:21 +02:00
2007-07-19 12:40:28 +02:00
if ( ! $adminGroup ) {
$adminGroup = Object :: create ( 'Group' );
$adminGroup -> Title = 'Administrators' ;
$adminGroup -> Code = " administrators " ;
$adminGroup -> write ();
Permission :: grant ( $adminGroup -> ID , " ADMIN " );
}
2007-07-20 01:15:05 +02:00
if ( ! isset ( $member )) {
2010-01-25 06:18:17 +01:00
// Leave 'Email' and 'Password' are not set to avoid creating
// persistent logins in the database. See Security::setDefaultAdmin().
2007-07-19 12:40:28 +02:00
$member = Object :: create ( 'Member' );
2010-01-25 06:18:17 +01:00
$member -> FirstName = 'Default Admin' ;
2007-07-19 12:40:28 +02:00
$member -> write ();
$member -> Groups () -> add ( $adminGroup );
}
2007-09-14 05:12:21 +02:00
return $member ;
2007-07-19 12:40:28 +02:00
}
2007-09-14 05:12:21 +02:00
2007-09-14 19:04:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-10-02 23:57:12 +02:00
* Set a default admin in dev - mode
*
2010-01-25 06:18:17 +01:00
* This will set a static default - admin which is not existing
2007-09-14 21:10:18 +02:00
* as a database - record . By this workaround we can test pages in dev - mode
* with a unified login . Submitted login - credentials are first checked
2010-01-25 06:18:17 +01:00
* against this static information in { @ link Security :: authenticate ()} .
2007-09-14 05:12:21 +02:00
*
2007-10-02 23:57:12 +02:00
* @ param string $username The user name
2010-01-25 06:18:17 +01:00
* @ param string $password The password ( in cleartext )
2007-07-19 12:40:28 +02:00
*/
2007-09-16 02:32:48 +02:00
public static function setDefaultAdmin ( $username , $password ) {
2007-09-16 19:39:41 +02:00
// don't overwrite if already set
2007-10-02 23:57:12 +02:00
if ( self :: $default_username || self :: $default_password ) {
return false ;
}
2007-09-14 05:12:21 +02:00
2007-09-16 19:39:41 +02:00
self :: $default_username = $username ;
self :: $default_password = $password ;
2007-07-19 12:40:28 +02:00
}
2007-09-17 23:51:42 +02:00
/**
* Checks if the passed credentials are matching the default - admin .
* Compares cleartext - password set through Security :: setDefaultAdmin () .
*
* @ param string $username
* @ param string $password
* @ return bool
*/
2007-09-27 23:13:59 +02:00
public static function check_default_admin ( $username , $password ) {
2007-09-17 23:51:42 +02:00
return (
2007-10-23 03:18:39 +02:00
self :: $default_username === $username
&& self :: $default_password === $password
&& self :: has_default_admin ()
2007-09-17 23:51:42 +02:00
);
}
2007-10-23 03:18:39 +02:00
/**
* Check that the default admin account has been set .
*/
public static function has_default_admin () {
2007-10-24 03:15:53 +02:00
return ! empty ( self :: $default_username ) && ! empty ( self :: $default_password );
2007-10-23 03:18:39 +02:00
}
2007-09-14 19:04:11 +02:00
2007-07-19 12:40:28 +02:00
/**
2007-09-14 21:10:18 +02:00
* Set strict path checking
*
2007-09-16 02:44:30 +02:00
* This prevents sharing of the session across several sites in the
* domain .
2007-09-14 05:12:21 +02:00
*
2007-09-14 21:10:18 +02:00
* @ param boolean $strictPathChecking To enable or disable strict patch
* checking .
2007-07-19 12:40:28 +02:00
*/
2007-09-16 02:32:48 +02:00
public static function setStrictPathChecking ( $strictPathChecking ) {
2007-07-19 12:40:28 +02:00
self :: $strictPathChecking = $strictPathChecking ;
}
2007-09-14 05:12:21 +02:00
2007-09-14 19:04:11 +02:00
/**
2007-09-14 21:10:18 +02:00
* Get strict path checking
2007-09-14 19:04:11 +02:00
*
2007-09-14 21:10:18 +02:00
* @ return boolean Status of strict path checking
2007-09-14 19:04:11 +02:00
*/
2007-09-16 02:32:48 +02:00
public static function getStrictPathChecking () {
2007-07-19 12:40:28 +02:00
return self :: $strictPathChecking ;
}
2007-09-15 23:51:37 +02:00
/**
* Set if passwords should be encrypted or not
*
2009-11-14 02:14:32 +01:00
* @ deprecated 2.4 Use PasswordEncryptor_None instead .
*
2007-09-15 23:51:37 +02:00
* @ param bool $encrypt Set to TRUE if you want that all ( new ) passwords
* will be stored encrypted , FALSE if you want to
* store the passwords in clear text .
*/
public static function encrypt_passwords ( $encrypt ) {
self :: $encryptPasswords = ( bool ) $encrypt ;
}
/**
2009-11-14 02:14:32 +01:00
* Get a list of all available encryption algorithms .
* Note : These are arbitrary codes , and not callable methods .
*
* @ deprecated 2.4 Use PasswordEncryptor :: get_encryptors ()
2007-09-15 23:51:37 +02:00
*
2009-11-14 02:14:32 +01:00
* @ return array Returns an array of strings containing all supported encryption algorithms .
2007-09-15 23:51:37 +02:00
*/
public static function get_encryption_algorithms () {
2009-11-14 02:14:32 +01:00
return array_keys ( PasswordEncryptor :: get_encryptors ());
2007-09-15 23:51:37 +02:00
}
/**
* Set the password encryption algorithm
*
* @ param string $algorithm One of the available password encryption
2009-11-14 02:14:32 +01:00
* algorithms determined by { @ link Security :: get_encryption_algorithms ()}
* @ return bool Returns TRUE if the passed algorithm was valid , otherwise FALSE .
*/
public static function set_password_encryption_algorithm ( $algorithm ) {
if ( ! array_key_exists ( $algorithm , PasswordEncryptor :: get_encryptors ())) return false ;
2007-09-15 23:51:37 +02:00
self :: $encryptionAlgorithm = $algorithm ;
return true ;
}
2009-11-14 02:14:32 +01:00
2007-09-15 23:51:37 +02:00
/**
2009-11-14 02:14:32 +01:00
* @ return String
2007-09-15 23:51:37 +02:00
*/
2009-11-14 02:14:32 +01:00
public static function get_password_encryption_algorithm () {
return self :: $encryptionAlgorithm ;
2007-09-15 23:51:37 +02:00
}
/**
2009-11-14 02:14:32 +01:00
* Encrypt a password according to the current password encryption settings .
2007-09-15 23:51:37 +02:00
* If the settings are so that passwords shouldn ' t be encrypted , the
* result is simple the clear text password with an empty salt except when
* a custom algorithm ( $algorithm parameter ) was passed .
*
* @ param string $password The password to encrypt
* @ param string $salt Optional : The salt to use . If it is not passed , but
2009-11-14 02:14:32 +01:00
* needed , the method will automatically create a
* random salt that will then be returned as return value .
2007-09-16 16:27:27 +02:00
* @ param string $algorithm Optional : Use another algorithm to encrypt the
2009-11-14 02:14:32 +01:00
* password ( so that the encryption algorithm can be changed over the time ) .
* @ param Member $member Optional
2007-09-15 23:51:37 +02:00
* @ return mixed Returns an associative array containing the encrypted
2009-11-14 02:14:32 +01:00
* password and the used salt in the form :
* < code >
* array (
* 'password' => string ,
* 'salt' => string ,
* 'algorithm' => string ,
* 'encryptor' => PasswordEncryptor instance
* )
* </ code >
* If the passed algorithm is invalid , FALSE will be returned .
2007-09-15 23:51:37 +02:00
*
* @ see encrypt_passwords ()
* @ see set_password_encryption_algorithm ()
2009-11-14 02:14:32 +01:00
*/
static function encrypt_password ( $password , $salt = null , $algorithm = null , $member = null ) {
if (
// if the password is empty, don't encrypt
strlen ( trim ( $password )) == 0
// if no algorithm is provided and no default is set, don't encrypt
|| ( ! $algorithm && self :: $encryptPasswords == false )
) {
$algorithm = 'none' ;
2007-09-15 23:51:37 +02:00
} else {
2009-11-14 02:14:32 +01:00
// Fall back to the default encryption algorithm
if ( ! $algorithm ) $algorithm = self :: $encryptionAlgorithm ;
}
2008-03-11 02:30:49 +01:00
2009-11-14 02:14:32 +01:00
$e = PasswordEncryptor :: create_for_algorithm ( $algorithm );
2007-09-15 23:51:37 +02:00
2009-11-14 02:14:32 +01:00
// New salts will only need to be generated if the password is hashed for the first time
$salt = ( $salt ) ? $salt : $e -> salt ( $password );
return array (
2010-01-20 21:17:25 +01:00
'password' => $e -> encrypt ( $password , $salt , $member ),
2009-11-14 02:14:32 +01:00
'salt' => $salt ,
'algorithm' => $algorithm ,
'encryptor' => $e
);
2007-09-15 23:51:37 +02:00
}
2007-11-15 23:29:10 +01:00
/**
* Checks the database is in a state to perform security checks .
* @ return bool
*/
public static function database_is_ready () {
2009-01-08 00:00:54 +01:00
$requiredTables = ClassInfo :: dataClassesFor ( 'Member' );
$requiredTables [] = 'Group' ;
$requiredTables [] = 'Permission' ;
foreach ( $requiredTables as $table ) if ( ! ClassInfo :: hasTable ( $table )) return false ;
return (( $permissionFields = DB :: fieldList ( 'Permission' )) && isset ( $permissionFields [ 'Type' ])) &&
2007-11-15 23:29:10 +01:00
(( $memberFields = DB :: fieldList ( 'Member' )) && isset ( $memberFields [ 'RememberLoginToken' ]));
}
2008-08-11 02:14:48 +02:00
/**
* Enable or disable recording of login attempts
* through the { @ link LoginRecord } object .
*
* @ param boolean $bool
*/
public static function set_login_recording ( $bool ) {
self :: $login_recording = ( bool ) $bool ;
}
/**
* @ return boolean
*/
public static function login_recording () {
return self :: $login_recording ;
}
2008-09-18 05:53:36 +02:00
protected static $default_login_dest = " " ;
/**
* Set the default login dest
* This is the URL that users will be redirected to after they log in ,
* if they haven ' t logged in en route to access a secured page .
*
* By default , this is set to the homepage
*/
public static function set_default_login_dest ( $dest ) {
self :: $default_login_dest = $dest ;
}
/**
* Get the default login dest
*/
public static function default_login_dest () {
return self :: $default_login_dest ;
}
2007-09-15 23:51:37 +02:00
2007-07-19 12:40:28 +02:00
}
2009-12-03 22:32:46 +01:00
?>