-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:vienthuong/Impersonation
- Loading branch information
Showing
10 changed files
with
149 additions
and
20 deletions.
There are no files selected for viewing
27 changes: 21 additions & 6 deletions
27
src/Resources/app/administration/src/extension/component/structure/sw-page/index.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
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
25 changes: 19 additions & 6 deletions
25
...tension/module/sw-users-permissions/components/sw-users-permissions-user-listing/index.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
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
11 changes: 11 additions & 0 deletions
11
src/Resources/app/administration/src/init/impersonation.init.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,11 @@ | ||
import { IMPERSONATING_ATTRIBUTE_KEY } from '../constant/impersonation.constant'; | ||
import ImpersonationService from '../service/impersonation.service'; | ||
|
||
const { State, Application } = Shopware; | ||
const initContainer = Application.getContainer('init'); | ||
const currentUser = State.get('session').currentUser; | ||
const httpClient = initContainer.httpClient; | ||
|
||
Application.addServiceProvider('impersonationService', (container) => { | ||
return new ImpersonationService(currentUser, container, httpClient); | ||
}); |
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
76 changes: 76 additions & 0 deletions
76
src/Resources/app/administration/src/service/impersonation.service.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,76 @@ | ||
import { initializeUserNotifications } from 'src/app/state/notification.store'; | ||
import {IMPERSONATING_ATTRIBUTE_KEY} from "../constant/impersonation.constant"; | ||
const { State } = Shopware; | ||
|
||
class ImpersonationService { | ||
constructor(impersonatingUser, container, httpClient) { | ||
this.name = 'impersonationService'; | ||
const {userService, localeHelper, loginService} = container; | ||
|
||
this.userService = userService; | ||
this.localeHelper = localeHelper; | ||
this.loginService = loginService; | ||
this.impersonatingUser = impersonatingUser; | ||
this.httpClient = httpClient; | ||
|
||
this._initializeService(); | ||
} | ||
|
||
async impersonate(userId) { | ||
localStorage.setItem(IMPERSONATING_ATTRIBUTE_KEY, userId); | ||
|
||
const response = await this.userService.getUser({}, { | ||
[IMPERSONATING_ATTRIBUTE_KEY]: userId | ||
}); | ||
|
||
const { data: { password, ...user } } = response; | ||
|
||
await this._initializeUser(user); | ||
} | ||
|
||
async leaveImpersonation() { | ||
localStorage.removeItem(IMPERSONATING_ATTRIBUTE_KEY); | ||
|
||
await this._initializeUser(this.impersonatingUser); | ||
} | ||
|
||
isImpersonating() { | ||
return !!localStorage.getItem(IMPERSONATING_ATTRIBUTE_KEY) | ||
} | ||
|
||
async _initializeUser(user) { | ||
State.commit('setCurrentUser', user); | ||
|
||
await this.localeHelper.setLocaleWithId(user.localeId); | ||
|
||
State.commit('context/setApiLanguageId', State.get('session').languageId); | ||
|
||
initializeUserNotifications(); | ||
} | ||
|
||
_initializeService() { | ||
this.httpClient.interceptors.request.use(config => { | ||
if (this.isImpersonating()) { | ||
config.headers = config.headers || {}; | ||
config.headers[IMPERSONATING_ATTRIBUTE_KEY] = localStorage.getItem(IMPERSONATING_ATTRIBUTE_KEY); | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
this._registerLogInListener(); | ||
|
||
if (this.isImpersonating()) { | ||
this.impersonate(localStorage.getItem(IMPERSONATING_ATTRIBUTE_KEY)); | ||
} | ||
} | ||
_registerLogInListener() { | ||
this.loginService.addOnLoginListener(async () => { | ||
if (this.isImpersonating()) { | ||
await this.leaveImpersonation(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default ImpersonationService; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.