2014-10-13 10:52:19 +02:00
title: Members
2011-02-07 07:48:44 +01:00
# Member
## Introduction
2017-07-03 03:22:12 +02:00
The [Member ](api:SilverStripe\Security\Member ) class is used to represent user accounts on a SilverStripe site (including newsletter recipients).
2011-02-07 07:48:44 +01:00
## Testing For Logged In Users
2017-05-20 06:32:25 +02:00
**Security::getCurrentUser()**
2011-02-07 07:48:44 +01:00
Returns the full *Member* Object for the current user, returns *null* if user is not logged in.
2017-08-03 02:51:32 +02:00
```php
2017-05-20 06:32:25 +02:00
if( $member = Security::getCurrentUser() ) {
2011-02-07 07:48:44 +01:00
// Work with $member
} else {
// Do non-member stuff
}
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
## Subclassing
< div class = "warning" markdown = "1" >
2017-07-03 03:22:12 +02:00
This is the least desirable way of extending the [Member ](api:SilverStripe\Security\Member ) class. It's better to use [DataExtension ](api:SilverStripe\ORM\DataExtension )
2011-03-08 22:05:51 +01:00
(see below).
2011-02-07 07:48:44 +01:00
< / div >
2017-07-03 03:22:12 +02:00
You can define subclasses of [Member ](api:SilverStripe\Security\Member ) to add extra fields or functionality to the built-in membership system.
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-05 00:45:24 +02:00
use SilverStripe\Security\Member;
2017-08-03 05:57:29 +02:00
class MyMember extends Member
{
2017-08-03 05:35:09 +02:00
private static $db = [
2011-02-07 07:48:44 +01:00
"Age" => "Int",
"Address" => "Text",
2017-08-03 05:35:09 +02:00
];
2011-02-07 07:48:44 +01:00
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2017-08-07 01:46:19 +02:00
To ensure that all new members are created using this class, put it in `Injector` .
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
2017-08-07 01:46:19 +02:00
```yaml
SilverStripe\Core\Injector\Injector:
SilverStripe\Security\Member:
class: My\Project\MyMember
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
## Overloading getCMSFields()
2012-01-30 23:13:42 +01:00
If you overload the built-in public function getCMSFields(), then you can change the form that is used to view & edit member
2017-07-03 03:22:12 +02:00
details in the newsletter system. This function returns a [FieldList ](api:SilverStripe\Forms\FieldList ) object. You should generally start by calling
parent::getCMSFields() and manipulate the [FieldList ](api:SilverStripe\Forms\FieldList ) from there.
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-03 06:25:49 +02:00
public function getCMSFields()
{
2011-02-07 07:48:44 +01:00
$fields = parent::getCMSFields();
2014-01-07 17:02:43 +01:00
$fields->insertBefore("HTMLEmail", new TextField("Age"));
2011-02-07 07:48:44 +01:00
$fields->removeByName("JobTitle");
$fields->removeByName("Organisation");
return $fields;
}
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
## Extending Member or DataObject?
2017-07-03 03:22:12 +02:00
Basic rule: Class [Member ](api:SilverStripe\Security\Member ) should just be extended for entities who have some kind of login.
If you have different types of [Member ](api:SilverStripe\Security\Member )s in the system, you have to make sure that those with login-capabilities have
2011-02-07 07:48:44 +01:00
unique email-addresses (as this is used for login-credentials).
2017-07-03 03:22:12 +02:00
For persons without login-capabilities (e.g. for an address-database), you shouldn't extend [Member ](api:SilverStripe\Security\Member ) to avoid conflicts
with the Member-database. This enables us to have a different subclass of [Member ](api:SilverStripe\Security\Member ) for an email-address with login-data,
2011-02-07 07:48:44 +01:00
and another subclass for the same email-address in the address-database.
2011-04-15 11:35:30 +02:00
## Member Role Extension
2011-02-07 07:48:44 +01:00
Using inheritance to add extra behaviour or data fields to a member is limiting, because you can only inherit from 1
2013-03-27 12:06:57 +01:00
class. A better way is to use role extensions to add this behaviour. Add the following to your
2015-01-21 03:27:22 +01:00
`[config.yml](/developer_guides/configuration/configuration/#configuration-yaml-syntax-and-rules)` .
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```yml
2013-03-27 12:06:57 +01:00
Member:
extensions:
- MyMemberExtension
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2017-07-03 03:22:12 +02:00
A role extension is simply a subclass of [DataExtension ](api:SilverStripe\ORM\DataExtension ) that is designed to be used to add behaviour to [Member ](api:SilverStripe\Security\Member ).
2011-02-07 07:48:44 +01:00
The roles affect the entire class - all members will get the additional behaviour. However, if you want to restrict
2017-07-03 03:22:12 +02:00
things, you should add appropriate [Permission::checkMember() ](api:SilverStripe\Security\Permission::checkMember( )) calls to the role's methods.
2011-02-07 07:48:44 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-08-05 00:45:24 +02:00
use SilverStripe\Security\Permission;
use SilverStripe\ORM\DataExtension;
2017-08-03 05:57:29 +02:00
class MyMemberExtension extends DataExtension
{
2011-02-07 07:48:44 +01:00
/**
* Modify the field set to be displayed in the CMS detail pop-up
*/
2017-08-03 06:25:49 +02:00
public function updateCMSFields(FieldList $currentFields)
{
2011-02-07 07:48:44 +01:00
// Only show the additional fields on an appropriate kind of use
if(Permission::checkMember($this->owner->ID, "VIEW_FORUM")) {
2011-10-28 03:37:27 +02:00
// Edit the FieldList passed, adding or removing fields as necessary
2011-02-07 07:48:44 +01:00
}
}
2012-06-28 11:43:56 +02:00
// define additional properties
2017-08-03 05:35:09 +02:00
private static $db = [];
private static $has_one = [];
private static $has_many = [];
private static $many_many = [];
private static $belongs_many_many = [];
2011-02-07 07:48:44 +01:00
2017-08-03 06:25:49 +02:00
public function somethingElse()
{
2011-02-07 07:48:44 +01:00
// You can add any other methods you like, which you can call directly on the member object.
}
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
2016-02-04 23:21:06 +01:00
## Saved User Logins ##
Logins can be "remembered" across multiple devices when user checks the "Remember Me" box. By default, a new login token
will be created and associated with the device used during authentication. When user logs out, all previously saved tokens
2017-07-03 03:22:12 +02:00
for all devices will be revoked, unless `[RememberLoginHash::$logout_across_devices](api:SilverStripe\Security\RememberLoginHash::$logout_across_devices) is set to false. For extra security,
single tokens can be enforced by setting `[RememberLoginHash::$force_single_token](api:SilverStripe\Security\RememberLoginHash::$force_single_token) to true.
2016-02-04 23:21:06 +01:00
2017-01-13 04:11:20 +01:00
## Acting as another user ##
Occasionally, it may be necessary not only to check permissions of a particular member, but also to
temporarily assume the identity of another user for certain tasks. E.g. when running a CLI task,
it may be necessary to log in as an administrator to perform write operations.
You can use `Member::actAs()` method, which takes a member or member id to act as, and a callback
within which the current user will be assigned the given member. After this method returns
the current state will be restored to whichever current user (if any) was logged in.
If you pass in null as a first argument, you can also mock being logged out, without modifying
the current user.
Note: Take care not to invoke this method to perform any operation the current user should not
reasonably be expected to be allowed to do.
E.g.
2017-08-03 02:51:32 +02:00
```php
2017-08-05 00:45:24 +02:00
use SilverStripe\Control\Director;
use SilverStripe\Security\Security;
use SilverStripe\Security\Member;
use SilverStripe\Dev\BuildTask;
2017-01-13 04:11:20 +01:00
class CleanRecordsTask extends BuildTask
{
public function run($request)
{
if (!Director::is_cli()) {
throw new BadMethodCallException('This task only runs on CLI');
}
$admin = Security::findAnAdministrator();
Member::actAs($admin, function() {
DataRecord::get()->filter('Dirty', true)->removeAll();
});
}
2017-08-03 02:51:32 +02:00
```
2011-02-07 07:48:44 +01:00
## API Documentation
2017-07-03 03:22:12 +02:00
[Member ](api:SilverStripe\Security\Member )