forked from neos/neos-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Adds restore button for user impersonation
With Neos 8.0 we introduce the impersonation feature. So, it is possible to impersonate as a different user from the user management module. This change will add related endpoints to the neos-ui to be able to communicate with the Neos.Neos API. The impersonation state will be saved in the Redux store, and a saga handles the restore of the origin user. Besides the data handling stuff, we also add a new button to the user menu. Related Neos PR: neos/neos-development-collection#3648
- Loading branch information
1 parent
20c88da
commit ff7a17d
Showing
10 changed files
with
245 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/neos-ui-redux-store/src/User/Impersonate/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {produce} from 'immer'; | ||
import {action as createAction, ActionType} from 'typesafe-actions'; | ||
import {InitAction} from '@neos-project/neos-ui-redux-store/src/System'; | ||
import { WritableDraft } from 'immer/dist/internal'; | ||
|
||
export interface ImpersonateAccount extends WritableDraft<{ | ||
accountIdentifier: string; | ||
fullName: string; | ||
}> {} | ||
|
||
export interface State extends Readonly<{ | ||
status: boolean; | ||
user?: ImpersonateAccount; | ||
origin?: ImpersonateAccount; | ||
}> {} | ||
|
||
export const defaultState: State = { | ||
status: false | ||
}; | ||
|
||
|
||
// | ||
// Export the action types | ||
// | ||
export enum actionTypes { | ||
INIT = '@neos/neos-ui/User/Impersonate/INIT', | ||
FETCH_STATUS = '@neos/neos-ui/User/Impersonate/FETCH_STATUS', | ||
RESTORE = '@neos/neos-ui/User/Impersonate/RESTORE', | ||
} | ||
|
||
// | ||
// Export the actions | ||
// | ||
export const actions = { | ||
init: () => createAction(actionTypes.INIT), | ||
fetchStatus: (data: State) => createAction(actionTypes.FETCH_STATUS, data), | ||
restore: () => createAction(actionTypes.RESTORE), | ||
}; | ||
|
||
// | ||
// Export the union type of all actions | ||
// | ||
export type Action = ActionType<typeof actions>; | ||
|
||
export const reducer = (state: State = defaultState, action: Action | InitAction) => produce(state, draft => { | ||
switch (action.type) { | ||
case actionTypes.INIT: | ||
draft.status = false; | ||
break; | ||
case actionTypes.FETCH_STATUS: | ||
draft.status = action.payload.status; | ||
draft.user = action.payload.user; | ||
draft.origin = action.payload.origin; | ||
break; | ||
case actionTypes.RESTORE: | ||
draft.status = false; | ||
break; | ||
} | ||
}); | ||
|
||
// | ||
// Export the selectors | ||
// | ||
export const selectors = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {put, call, takeEvery} from 'redux-saga/effects'; | ||
|
||
import {actionTypes, actions} from '@neos-project/neos-ui-redux-store'; | ||
import backend from '@neos-project/neos-ui-backend-connector'; | ||
import {$get} from 'plow-js'; | ||
|
||
export function * impersonateRestore({globalRegistry}) { | ||
const {impersonateRestore} = backend.get().endpoints; | ||
const i18nRegistry = globalRegistry.get('i18n'); | ||
const errorMessage = i18nRegistry.translate( | ||
'impersonate.error.restoreUser', | ||
'Could not switch back to the original user.', | ||
{}, | ||
'Neos.Neos', | ||
'Main' | ||
); | ||
|
||
yield takeEvery(actionTypes.User.Impersonate.RESTORE, function * restore(action) { | ||
try { | ||
const feedback = yield call(impersonateRestore, action.payload); | ||
const originUser = $get('origin.accountIdentifier', feedback); | ||
const user = $get('impersonate.accountIdentifier', feedback); | ||
const status = $get('status', feedback); | ||
|
||
const restoreMessage = i18nRegistry.translate( | ||
'impersonate.success.restoreUser', | ||
'Switched back from {0} to the orginal user {1}.', | ||
{ | ||
0: user, | ||
1: originUser | ||
}, | ||
'Neos.Neos', | ||
'Main' | ||
); | ||
|
||
if (status) { | ||
yield put(actions.UI.FlashMessages.add('restoreUserImpersonateUser', restoreMessage, 'success')); | ||
} else { | ||
yield put(actions.UI.FlashMessages.add('restoreUserImpersonateUser', errorMessage, 'error')); | ||
} | ||
window.location.reload(); | ||
} catch (error) { | ||
yield put(actions.UI.FlashMessages.add('restoreUserImpersonateUser', errorMessage, 'error')); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/neos-ui/src/Containers/PrimaryToolbar/UserDropDown/RestoreButtonItem.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import {Icon} from '@neos-project/react-ui-components'; | ||
import {neos} from '@neos-project/neos-ui-decorators'; | ||
import {connect} from 'react-redux'; | ||
import {actions} from '@neos-project/neos-ui-redux-store'; | ||
import I18n from '@neos-project/neos-ui-i18n'; | ||
import {$transform, $get} from 'plow-js'; | ||
|
||
import buttonTheme from './style.css'; | ||
|
||
@connect( | ||
$transform({ | ||
originUser: $get('user.impersonate.origin'), | ||
user: $get('user.impersonate.user') | ||
}), | ||
{ | ||
addFlashMessage: actions.UI.FlashMessages.add, | ||
impersonateRestore: actions.User.Impersonate.restore | ||
} | ||
) | ||
@neos(globalRegistry => ({ | ||
i18nRegistry: globalRegistry.get('i18n') | ||
})) | ||
export default class RestoreButtonItem extends React.PureComponent { | ||
static propTypes = { | ||
originUser: PropTypes.object, | ||
user: PropTypes.object, | ||
addFlashMessage: PropTypes.func.isRequired, | ||
impersonateRestore: PropTypes.func.isRequired, | ||
i18nRegistry: PropTypes.object.isRequired | ||
}; | ||
|
||
render() { | ||
const {originUser, i18nRegistry, impersonateRestore} = this.props; | ||
const title = i18nRegistry.translate( | ||
'impersonate.title.restoreUserButton', | ||
'Switch back to the orginal user account', | ||
{}, | ||
'Neos.Neos', | ||
'Main' | ||
); | ||
|
||
return (originUser ? ( | ||
<li className={buttonTheme.dropDown__item}> | ||
<button | ||
title={title} | ||
onClick={ | ||
() => impersonateRestore() | ||
} | ||
> | ||
<Icon | ||
icon="random" | ||
aria-hidden="true" | ||
className={buttonTheme.dropDown__itemIcon} | ||
/> | ||
<I18n | ||
id="impersonate.label.restoreUserButton" | ||
sourceName="Main" | ||
packageKey="Neos.Neos" | ||
fallback={`Back to user "${originUser.fullName}"`} | ||
params={{0: originUser.fullName}} | ||
/> | ||
</button> | ||
</li> | ||
) : null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters