Skip to content

Commit

Permalink
Added screenshots and translations
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderHamaka committed Sep 13, 2022
1 parent dc06459 commit 1955a79
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 98 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Silverstripe CMS User Switcher

Allow Admin users to switch to other account through the CMS menu.
Allows administrators to assume the identity of another user.

Silverstripe 4+

Expand All @@ -10,9 +10,16 @@ composer require "sandervanscheepen/silverstripe-cms-userswitcher"
```

## Usage
Enabled for Members through the Security section in the CMS. Refresh of the interface required after activation.
Only Members in administrator group can be allowed to see the dropdown in the top left corner of the CMS to switch to other accounts.
All Members (admin or non admin) can be added to that dropdown through their record in Security.
The ability to assume the identity of another user can exclusively be enabled for Members with admin permissions.
All settings are managed through the Security admin at /admin/security
Admin users can enable/disable the userswitcher dropdown on their own account and add other accounts to the dropdown that will apear at the top left of the CMS interface (refresh needed after activation).

Options for Member that is part of Administrators group (viewed by administrator - also note the added dropdown in the top left corner):
![Settings for a Member record that is part of Administrators group](/docs/images/screen_security_admin.jpg?raw=true "Settings for a Member record that is part of Administrators group")

Options for Member that is not part of Administrators group (viewed by administrator):
![Settings for a Member record that is not part of Administrators group](/docs/images/screen_security_nonadmin.jpg?raw=true "Settings for a Member record that is not part of Administrators group")


## License
See [License](license.md)
Expand Down
3 changes: 3 additions & 0 deletions client/dist/css/LeftAndMain_UserSwitcher.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cms-menu__header .cms-userswitcher span {
color: #43536d;
}
Binary file added docs/images/screen_security_admin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/screen_security_nonadmin.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions lang/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
en:
SanderVanScheepen\SilverstripeCMSUserSwitcher\Extension\CMSUserSwitcherMemberExt:
HEADER_MAIN: 'Switching identities'
INTRODUCTION: 'This feature allows administrators after logging in to the CMS to assume the identity of another user.'
OPTION_ENABLE_FOR_ADMIN: 'Enable this administrator account to log in as other users.'
OPTION_ADD_MEMBER_TO_USERSWITCHER: 'Show this user in the list of accounts that administrators can assume the identity of'
6 changes: 6 additions & 0 deletions lang/nl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nl:
SanderVanScheepen\SilverstripeCMSUserSwitcher\Extension\CMSUserSwitcherMemberExt:
HEADER_MAIN: 'In kunnen loggen als andere gebruiker'
INTRODUCTION: 'Hiermee kunnen beheerders in het CMS de identiteit van een andere gebruiker aannemen.'
OPTION_ENABLE_FOR_ADMIN: 'Activeer inloggen als andere gebruiker voor dit administratoraccount'
OPTION_ADD_MEMBER_TO_USERSWITCHER: 'Toon deze gebruiker in de lijst van accounts waarvan administrators de identiteit van kunnen aannemen'
9 changes: 7 additions & 2 deletions src/Extension/CMSUserSwitcherMemberExt.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
use SilverStripe\Control\Controller;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\Requirements;
use function _t;
use function intval;

Expand Down Expand Up @@ -50,12 +53,14 @@ public function updateCMSFields(FieldList $oFields)
$oFields->removeByName('CMSUserSwitchCanBeImpersonatedByAdmin');

if (Permission::check('ADMIN') && ! Controller::curr() instanceof CMSProfileController) {
$oFields->push(HeaderField::create('HdrCMSUserSwitcherMain', _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.HEADER_MAIN', 'Switching identities'), 2));
$oFields->push(LiteralField::create('LitCMSUserSwitcherIntroduction', '<p>' . _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.INTRODUCTION', 'This feature allows administrators after logging in to the CMS to assume the identity of another user.') . '</p>'));

if (Permission::check('ADMIN', 'any', $this->owner)) {
$oFields->push(CheckboxField::create('CMSUserSwitchCanSwitch', _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.ENABLE_FOR_ADMIN', 'Enable CMS user switcher for this account.')));
$oFields->push(CheckboxField::create('CMSUserSwitchCanSwitch', _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.OPTION_ENABLE_FOR_ADMIN', 'Enable CMS user switcher for this account.')));
}

$oFields->push(CheckboxField::create('CMSUserSwitchCanBeImpersonatedByAdmin', _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.ADD_MEMBER_TO_USERSWITCHER', 'Show in CMS user switcher for admins')));
$oFields->push(CheckboxField::create('CMSUserSwitchCanBeImpersonatedByAdmin', _t('SanderVanScheepen\\SilverstripeCMSUserSwitcher\\Extension\\CMSUserSwitcherMemberExt.OPTION_ADD_MEMBER_TO_USERSWITCHER', 'Show in CMS user switcher for admins')));
}
}

Expand Down
178 changes: 87 additions & 91 deletions src/Extension/UserSwitcherDropdownLeftAndMainExt.php
Original file line number Diff line number Diff line change
@@ -1,106 +1,102 @@
<?php

namespace SanderVanScheepen\SilverstripeCMSUserSwitcher\Extension;

use SanderVanScheepen\SilverstripeCMSUserSwitcher\Controller\Admin\UserSwitcherController;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Security\Group;
use SilverStripe\Security\Permission;
use function in_array;
use function intval;
use function serialize;

class UserSwitcherDropdownLeftAndMainExt extends DataExtension
{

public function init()
{
Requirements::javascript('sandervanscheepen/silverstripe-cms-userswitcher:client/dist/js/LeftAndMain_UserSwitcher.js');
}

public function SwitchableMembers()
{
$output = ArrayList::create();
Requirements::javascript('sandervanscheepen/silverstripe-cms-userswitcher:client/dist/js/LeftAndMain_UserSwitcher.js');

$dlMembers = UserSwitcherDropdownLeftAndMainExt::getSwitchableMembers();

/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();

/** @var Member $oMember */
foreach ($dlMembers as $oMember) {
$sCurrentState = intval($oMember->ID) === intval($oCurrentMember->ID) ? 'selected' : '';

$sLabel = 'Member ' . $oMember->ID;
$sName = $oMember->getName();

if($sName && trim($sName) !== "") {
$sLabel = $sName;
}

$output->push(ArrayData::create([
'CurrentState' => $sCurrentState,
'ID' => $oMember->ID,
'Title' => $sLabel,
'MemberID' => $oMember->ID
]));
}

return $output;
}

protected static $oMemoizedCanUserSwitch = null;

public function canUserSwitch()
{
if (static::$oMemoizedCanUserSwitch === null) {
/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();

$oSession = Controller::curr()->getRequest()->getSession();

static::$oMemoizedCanUserSwitch = (
$oSession->get('CMSUserSwitched')
|| (Permission::check('ADMIN') && in_array($oCurrentMember->CMSUserSwitchCanSwitch, [true, 1, '1']) && static::getSwitchableMembers()->count() > 0)
);
namespace SanderVanScheepen\SilverstripeCMSUserSwitcher\Extension;

use SilverStripe\Control\Controller;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use function in_array;
use function intval;

class UserSwitcherDropdownLeftAndMainExt extends DataExtension
{

public function init()
{
Requirements::javascript('sandervanscheepen/silverstripe-cms-userswitcher:client/dist/js/LeftAndMain_UserSwitcher.js');
Requirements::css('sandervanscheepen/silverstripe-cms-userswitcher:client/dist/css/LeftAndMain_UserSwitcher.css');
}

public function SwitchableMembers()
{
$output = ArrayList::create();
Requirements::javascript('sandervanscheepen/silverstripe-cms-userswitcher:client/dist/js/LeftAndMain_UserSwitcher.js');

$dlMembers = UserSwitcherDropdownLeftAndMainExt::getSwitchableMembers();

/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();

/** @var Member $oMember */
foreach ($dlMembers as $oMember) {
$sCurrentState = intval($oMember->ID) === intval($oCurrentMember->ID) ? 'selected' : '';

$sLabel = 'Member ' . $oMember->ID;
$sName = $oMember->getName();

if ($sName && trim($sName) !== "") {
$sLabel = $sName;
}

return static::$oMemoizedCanUserSwitch;
$output->push(ArrayData::create([
'CurrentState' => $sCurrentState,
'ID' => $oMember->ID,
'Title' => $sLabel,
'MemberID' => $oMember->ID
]));
}

return $output;
}

protected static $oMemoizedCanUserSwitch = null;

public function canUserSwitch()
{
if (static::$oMemoizedCanUserSwitch === null) {
/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();

$oSession = Controller::curr()->getRequest()->getSession();

static::$oMemoizedCanUserSwitch = (
$oSession->get('CMSUserSwitched')
|| (Permission::check('ADMIN') && in_array($oCurrentMember->CMSUserSwitchCanSwitch, [true, 1, '1']) && static::getSwitchableMembers()->count() > 0)
);
}

protected static $oMemoizedSwitchableMembers = null;
return static::$oMemoizedCanUserSwitch;
}

public static function getSwitchableMembers()
{
if (static::$oMemoizedSwitchableMembers === null) {
/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();
$iCurrentMemberID = intval($oCurrentMember->ID);
protected static $oMemoizedSwitchableMembers = null;

$dlMembersThatCanBeImpersonated = Member::get()->filter([
'CMSUserSwitchCanBeImpersonatedByAdmin' => true
]);
public static function getSwitchableMembers()
{
if (static::$oMemoizedSwitchableMembers === null) {
/** @var Member $oCurrentMember */
$oCurrentMember = Security::getCurrentUser();
$iCurrentMemberID = intval($oCurrentMember->ID);

$aMemberIDs = $dlMembersThatCanBeImpersonated->column('ID');
$dlMembersThatCanBeImpersonated = Member::get()->filter([
'CMSUserSwitchCanBeImpersonatedByAdmin' => true
]);

if (in_array($iCurrentMemberID, $aMemberIDs) !== true) {
$aMemberIDs[] = $iCurrentMemberID;
}
$aMemberIDs = $dlMembersThatCanBeImpersonated->column('ID');

static::$oMemoizedSwitchableMembers = Member::get()->filter([
'ID' => $aMemberIDs
])->sort('FirstName ASC, Surname ASC');
if (in_array($iCurrentMemberID, $aMemberIDs) !== true) {
$aMemberIDs[] = $iCurrentMemberID;
}

return static::$oMemoizedSwitchableMembers;
static::$oMemoizedSwitchableMembers = Member::get()->filter([
'ID' => $aMemberIDs
])->sort('FirstName ASC, Surname ASC');
}

return static::$oMemoizedSwitchableMembers;
}
}
2 changes: 1 addition & 1 deletion templates/Includes/UserSwitcher.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% if $canUserSwitch %>
<div class="cms-subsites cms-userswitcher" data-pjax-fragment="CMSUserSwitcherMemberList">
<div class="field dropdown">
<div class="field dropdown" style="color: blue;">
<select id="UserSwitcherSelect">
<% loop $SwitchableMembers %>
<option value="$MemberID" $CurrentState>$Title</option>
Expand Down

0 comments on commit 1955a79

Please sign in to comment.