-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
UserManagement.js
92 lines (79 loc) · 3.89 KB
/
UserManagement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {isNil} from '../../Helper'
import {ApiService} from '../../Services'
import {ImpersonateButton} from '../../Templates/ImpersonateButton'
const BASE_PATH = '/neos/impersonate/'
export default class UserManagement {
constructor(_root) {
const userMenuElement = document.querySelector('.neos-user-menu[data-csrf-token]')
this._root = _root
this._csrfToken = !isNil(userMenuElement) ? userMenuElement.getAttribute('data-csrf-token') : ''
this._basePath = this._getBasePath(userMenuElement)
this._apiService = new ApiService(this._basePath, this._csrfToken)
if (!isNil(_root)) {
this._initialize()
}
}
_initialize() {
this._renderImpersonateButtons()
this._setupEventListeners()
}
_setupEventListeners() {
const impersonateButtons = this._root.querySelectorAll('button.impersonate-user')
impersonateButtons.forEach(_impersonateButton => {
_impersonateButton.addEventListener('click', this._impersonateUser.bind(this));
});
}
_getBasePath(element) {
let url = BASE_PATH
if (!isNil(element) && element.hasAttribute('data-module-basepath')) {
let moduleBasePath = element.getAttribute('data-module-basepath')
moduleBasePath += moduleBasePath.endsWith('/') ? '' : '/'
url = moduleBasePath + 'impersonate/'
}
return url
}
_renderImpersonateButtons() {
const userTableActionButtons = Array.from(this._root.querySelectorAll('.neos-table .neos-action'))
userTableActionButtons.forEach(_actionContainer => {
const deleteButton = _actionContainer.querySelector('button.neos-button-danger')
const showButton = _actionContainer.querySelector('a[href*="show"]')
if (isNil(showButton)) {
return false
}
const showButtonUri = new URL(decodeURIComponent(showButton.getAttribute('href')))
const showButtonUriParameter = new URLSearchParams(showButtonUri.search)
// user information from DOM
const userIdentifier = showButtonUriParameter.get('moduleArguments[user][__identity]')
const isCurrentUser = !isNil(deleteButton) && deleteButton.classList.contains('neos-disabled')
const impersonateButtonMarkup = ImpersonateButton(userIdentifier, isCurrentUser)
const temporaryContainer = document.createElement('div');
temporaryContainer.innerHTML = impersonateButtonMarkup;
showButton.parentElement.appendChild(temporaryContainer.firstChild);
})
}
_impersonateUser(event) {
event.preventDefault();
const button = event.currentTarget;
if (isNil(button)) {
return false
}
const identifier = button.getAttribute('data-user-identifier')
const response = this._apiService.callUserChange(identifier);
response
.then((data) => {
const {user, status} = data
const username = isNil(user) ? '' : user.accountIdentifier
const message = window.NeosCMS.I18n.translate('impersonate.success.impersonateUser', 'Switched to the new user {0}.', 'Neos.Neos', 'Main', {0: username})
window.NeosCMS.Notification.ok(message)
// load default backend, so we don't need to care for the module permissions.
// because in not every neos version the users have by default the content module or user module
window.location.pathname = '/neos'
})
.catch(function (error) {
if (window.NeosCMS) {
const message = window.NeosCMS.I18n.translate('impersonate.error.impersonateUser', 'Could not switch to the requested user.', 'Neos.Neos')
window.NeosCMS.Notification.error(message)
}
});
}
}