1
1
import localforage from 'localforage' ;
2
- import { config , translations } from 'stripes-config' ;
2
+ import { config , okapi , translations } from 'stripes-config' ;
3
3
import rtlDetect from 'rtl-detect' ;
4
4
import moment from 'moment' ;
5
5
import { loadDayJSLocale } from '@folio/stripes-components' ;
@@ -400,12 +400,12 @@ function loadResources(store, tenant, userId) {
400
400
// in mod-configuration so we can only retrieve them if the user has
401
401
// read-permission for configuration entries.
402
402
if ( canReadConfig ( store ) ) {
403
- const okapi = store . getState ( ) ?. okapi ;
403
+ const okapiObject = store . getState ( ) ?. okapi ;
404
404
promises = [
405
- getLocale ( okapi . url , store , tenant ) ,
406
- getUserLocale ( okapi . url , store , tenant , userId ) ,
407
- getPlugins ( okapi . url , store , tenant ) ,
408
- getBindings ( okapi . url , store , tenant ) ,
405
+ getLocale ( okapiObject . url , store , tenant ) ,
406
+ getUserLocale ( okapiObject . url , store , tenant , userId ) ,
407
+ getPlugins ( okapiObject . url , store , tenant ) ,
408
+ getBindings ( okapiObject . url , store , tenant ) ,
409
409
] ;
410
410
}
411
411
@@ -594,7 +594,7 @@ export function createOkapiSession(store, tenant, token, data) {
594
594
rtExpires : data . tokenExpiration ?. refreshTokenExpiration ? new Date ( data . tokenExpiration . refreshTokenExpiration ) . getTime ( ) : Date . now ( ) + ( 10 * 60 * 1000 ) ,
595
595
} ;
596
596
597
- const sessionTenant = data . tenant || tenant ;
597
+ const sessionTenant = data . originalTenantId || data . tenant || tenant ;
598
598
const okapiSess = {
599
599
token,
600
600
isAuthenticated : true ,
@@ -891,17 +891,16 @@ export function requestLogin(okapiUrl, store, tenant, data) {
891
891
892
892
/**
893
893
* fetchUserWithPerms
894
- * retrieve currently-authenticated user
894
+ * retrieve currently-authenticated user data after switching affiliation
895
895
* @param {string } okapiUrl
896
896
* @param {string } tenant
897
897
* @param {string } token
898
898
* @param {boolean } rtrIgnore
899
- * @param {boolean } isKeycloak
900
899
*
901
900
* @returns {Promise } Promise resolving to the response of the request
902
901
*/
903
- function fetchUserWithPerms ( okapiUrl , tenant , token , rtrIgnore = false , isKeycloak = false ) {
904
- const usersPath = isKeycloak ? 'users-keycloak' : 'bl-users' ;
902
+ function fetchUserWithPerms ( okapiUrl , tenant , token , rtrIgnore = false ) {
903
+ const usersPath = okapi . authnUrl ? 'users-keycloak' : 'bl-users' ;
905
904
return fetch (
906
905
`${ okapiUrl } /${ usersPath } /_self?expandPermissions=true&fullPermissions=true` ,
907
906
{
@@ -911,6 +910,29 @@ function fetchUserWithPerms(okapiUrl, tenant, token, rtrIgnore = false, isKeyclo
911
910
) ;
912
911
}
913
912
913
+ /**
914
+ * fetchOverriddenUserWithPerms
915
+ * Query string parameter overrideUser is set to true the retrieved shadow user will be used to fetch the real user together with its permissions and service points.
916
+ * After fetching process begin a session.
917
+ * @param {string } okapiUrl
918
+ * @param {redux store } store
919
+ * @param {string } tenant
920
+ * @param {string } token
921
+ *
922
+ * @returns {Promise } Promise resolving to the response-body (JSON) of the request
923
+ */
924
+
925
+ function fetchOverriddenUserWithPerms ( okapiUrl , tenant , token , rtrIgnore = false ) {
926
+ const usersPath = okapi . authnUrl ? 'users-keycloak' : 'bl-users' ;
927
+ return fetch (
928
+ `${ okapiUrl } /${ usersPath } /_self?expandPermissions=true&fullPermissions=true&overrideUser=true` ,
929
+ {
930
+ headers : getHeaders ( tenant , token ) ,
931
+ rtrIgnore,
932
+ } ,
933
+ ) ;
934
+ }
935
+
914
936
/**
915
937
* requestUserWithPerms
916
938
* retrieve currently-authenticated user, then process the result to begin a session.
@@ -922,8 +944,7 @@ function fetchUserWithPerms(okapiUrl, tenant, token, rtrIgnore = false, isKeyclo
922
944
* @returns {Promise } Promise resolving to the response-body (JSON) of the request
923
945
*/
924
946
export function requestUserWithPerms ( okapiUrl , store , tenant , token ) {
925
- const isKeycloak = Boolean ( store . getState ( ) . okapi ?. authnUrl ) ;
926
- return fetchUserWithPerms ( okapiUrl , tenant , token , ! token , isKeycloak )
947
+ return fetchOverriddenUserWithPerms ( okapiUrl , tenant , token , ! token )
927
948
. then ( ( resp ) => {
928
949
if ( resp . ok ) {
929
950
return processOkapiSession ( store , tenant , resp , token ) ;
@@ -989,7 +1010,7 @@ export function updateUser(store, data) {
989
1010
*/
990
1011
export async function updateTenant ( okapiConfig , tenant ) {
991
1012
const okapiSess = await getOkapiSession ( ) ;
992
- const userWithPermsResponse = await fetchUserWithPerms ( okapiConfig . url , tenant , okapiConfig . token , false , Boolean ( okapiConfig . authnUrl ) ) ;
1013
+ const userWithPermsResponse = await fetchUserWithPerms ( okapiConfig . url , tenant , okapiConfig . token , false ) ;
993
1014
const userWithPerms = await userWithPermsResponse . json ( ) ;
994
1015
await localforage . setItem ( SESSION_NAME , { ...okapiSess , tenant, ...spreadUserWithPerms ( userWithPerms ) } ) ;
995
1016
}
0 commit comments