Repository URL to install this package:
|
Version:
3.12.11 ▾
|
var _excluded = ["loading"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import { createSlice } from '@reduxjs/toolkit';
import checkPermissions from '@filerobot/utils/lib/checkPermissions';
import { AUTH_STATE, PERMISSIONS, PLUGINS_IDS } from '@filerobot/utils/lib/constants';
import { slicePropName as coreCommonSliceName } from './common.slice';
import createThunk from '@filerobot/utils/lib/createThunk';
/**
* The order of the code in slice file is:
* 1. initialState (*No* export needed)
* 2. automated thunks functions -- uses createThunk API-- (export needed)
* 3. createSlice (*No* export needed)
* 4. actions exports
* 5. selectors (export needed)
* 6. default reducer export
*
*/
var slicePropName = 'user';
var sliceName = "".concat(PLUGINS_IDS.CORE, "/").concat(slicePropName);
var initialState = {
sassKey: '',
authState: AUTH_STATE.ANONYMOUS,
permissions: {},
// userCheckedPermissions
securityData: {}
};
export var generateUserKey = createThunk(async function (_, thunkApi) {
var _userAuthInfo$key_val, _userAuthInfo$key_val2;
var _thunkApi$extra = thunkApi.extra,
filerobot = _thunkApi$extra.filerobot,
apiClient = _thunkApi$extra.apiClient;
var url = "".concat(filerobot.opts.apiEndpoint, "/").concat(filerobot.opts.container, "/key/").concat(filerobot.opts.securityTemplateId);
var sassKeyExpireDuration = 1170000;
// Retrieve sassKey from server through security template id
var userAuthInfo = await apiClient.get(url, {
signal: thunkApi.signal,
noDefaultHeaders: true
});
var userAuthState = await thunkApi.dispatch(fetchUserAuthState({
userSassKey: userAuthInfo.key,
skipDispatch: true
}));
// Re-generate a new sassKey before the current one expires.
setTimeout(function () {
thunkApi.dispatch(generateUserKey());
}, (_userAuthInfo$key_val = (_userAuthInfo$key_val2 = userAuthInfo.key_validity) === null || _userAuthInfo$key_val2 === void 0 ? void 0 : _userAuthInfo$key_val2.expire_duration) !== null && _userAuthInfo$key_val !== void 0 ? _userAuthInfo$key_val : sassKeyExpireDuration);
filerobot.emit('sass-key-obtained', userAuthInfo.key);
// Removed permissions from here, as we will always check the permissions in the userAuthState request.
return _objectSpread(_objectSpread({}, userAuthState), {}, {
sassKey: userAuthInfo.key,
securityData: userAuthInfo
});
}, {
actionType: "".concat(sliceName, "/generateUserKey")
});
export var fetchUserAuthState = createThunk(async function (_ref, thunkApi) {
var userSassKey = _ref.userSassKey,
skipDispatch = _ref.skipDispatch;
// We will have skipDispatch: true, when we call the fetchUserAuthState from the generateUserKey thunk,
// as we'll wait for the generateUserKey thunk & fetchUserAuthState thunk to finish then update the user state once,
// otherwise the user state should have skipDispatch: false, to update the user state after finishing its request.
if (skipDispatch) {
thunkApi.skipDispatch();
}
var _thunkApi$extra2 = thunkApi.extra,
filerobot = _thunkApi$extra2.filerobot,
apiClient = _thunkApi$extra2.apiClient;
var _await$apiClient$get = await apiClient.get("".concat(filerobot.opts.apiEndpoint, "/").concat(filerobot.opts.container, "/v4/key/info?key=").concat(userSassKey), {
signal: thunkApi.signal
}),
isValidate = _await$apiClient$get.is_validate,
isUserInfoIncluded = _await$apiClient$get.is_user_info_included,
permissions = _await$apiClient$get.permissions;
var userAuthState = AUTH_STATE.ANONYMOUS;
if (isValidate) {
userAuthState = isUserInfoIncluded ? AUTH_STATE.AUTH : AUTH_STATE.UNAUTH;
}
// If no skipDispatch then emit the event, as this means no request is triggered for generating the sass key and sass key is provided from opts.
if (!skipDispatch) {
filerobot.emit('sass-key-obtained', userSassKey);
}
return {
sassKey: userSassKey,
authState: userAuthState,
permissions: checkPermissions(Array.isArray(permissions) && permissions.length > 0 ? permissions : filerobot.opts.userPermissions || permissions || [], Object.values(PERMISSIONS), true)
};
}, {
actionType: "".concat(sliceName, "/fetchUserAuthState")
});
var userSlice = createSlice({
name: sliceName,
initialState: initialState,
reducers: {
userUpdated: function userUpdated(state, action) {
var _action$payload = action.payload,
loading = _action$payload.loading,
newUserData = _objectWithoutProperties(_action$payload, _excluded);
return _objectSpread(_objectSpread({}, state), newUserData);
}
},
extraReducers: function extraReducers(builder) {
builder.addCase(generateUserKey.fulfilled, function (state, action) {
return _objectSpread(_objectSpread({}, state), action.payload);
}).addCase(fetchUserAuthState.fulfilled, function (state, action) {
return _objectSpread(_objectSpread({}, state), action.payload);
});
}
});
var userUpdated = userSlice.actions.userUpdated;
export { userUpdated };
export var selectUser = function selectUser(state) {
return state[PLUGINS_IDS.CORE][slicePropName];
};
export var selectUserPermissions = function selectUserPermissions(state) {
return selectUser(state).permissions;
};
// `isDev` is used for enabling any feature requires user auth on dev. environment (in-case it is needed for some checking/improvements) and anyways it won't work from BE side as it requires the user auth.
export var selectIsUserAuth = function selectIsUserAuth(state) {
var _state$PLUGINS_IDS$CO, _state$PLUGINS_IDS$CO2;
return ((_state$PLUGINS_IDS$CO = state[PLUGINS_IDS.CORE]) === null || _state$PLUGINS_IDS$CO === void 0 ? void 0 : (_state$PLUGINS_IDS$CO2 = _state$PLUGINS_IDS$CO[coreCommonSliceName]) === null || _state$PLUGINS_IDS$CO2 === void 0 ? void 0 : _state$PLUGINS_IDS$CO2.isDevEnv) || selectUser(state).authState === AUTH_STATE.AUTH;
};
export var selectUserSecurityData = function selectUserSecurityData(state) {
return selectUser(state).securityData;
};
export var selectUserSassKey = function selectUserSassKey(state) {
return selectUser(state).sassKey;
};
export var selectIsUserPermittedTo = function selectIsUserPermittedTo(state, permissionToCheck) {
var userMustAuth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var userPermissions = selectUserPermissions(state);
if (userMustAuth) {
var isUserAuth = selectIsUserAuth(state);
if (!isUserAuth) {
return false;
}
}
return Boolean(userPermissions[permissionToCheck]);
};
export var selectIsUserPermittedToShare = function selectIsUserPermittedToShare(state) {
return selectIsUserPermittedTo(state, PERMISSIONS.OBJECTS_SHARE_MANAGE, true);
};
export default userSlice.reducer;