Skip to content

Commit

Permalink
do not crash hard on admin if no permissions to write guiSettings obj…
Browse files Browse the repository at this point in the history
…ect (#2424)

- closes #2329
  • Loading branch information
foxriver76 authored Apr 8, 2024
1 parent 7a4a925 commit d94e898
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 75 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ The icons may not be reused in other projects without the proper flaticon licens
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**
* (foxriver76) avoid crash case if user has no permission to write objects

### 6.17.1 (2024-04-06)
* (bluefox) support of includes in JSONConfig files

Expand Down
160 changes: 85 additions & 75 deletions packages/admin/src/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -606,59 +606,69 @@ class App extends Router {
this.setState({ noTranslation: !this.state.noTranslation });
};

getGUISettings() {
return this.socket.getObject(`system.adapter.${this.adminInstance}.guiSettings`).then(async obj => {
if (!obj) {
obj = JSON.parse(JSON.stringify(DEFAULT_GUI_SETTINGS_OBJECT));
await this.socket.setObject(`system.adapter.${this.adminInstance}.guiSettings`, obj);
}
async getGUISettings() {
let obj;

try {
obj = await this.socket.getObject(`system.adapter.${this.adminInstance}.guiSettings`);
} catch (e) {
console.warn(`Could not get "system.adapter.${this.adminInstance}.guiSettings": ${e.message}`);
}

let state;
if (!obj) {
obj = JSON.parse(JSON.stringify(DEFAULT_GUI_SETTINGS_OBJECT));
try {
state = await this.socket.getState(`system.adapter.${this.adminInstance}.guiSettings`);
await this.socket.setObject(`system.adapter.${this.adminInstance}.guiSettings`, obj);
} catch (e) {
state = { val: false };
console.warn(`Could not update "system.adapter.${this.adminInstance}.guiSettings": ${e}`);
}
if (state && state.val) {
this.guiSettings = obj;
this.guiSettings.native = this.guiSettings.native || { localStorage: {}, sessionStorage: {} };
if (!this.guiSettings.native.localStorage) {
this.guiSettings.native = { localStorage: this.guiSettings.native, sessionStorage: {} };
}
}

window._localStorage = {
getItem: this.localStorageGetItem,
setItem: this.localStorageSetItem,
removeItem: this.localStorageRemoveItem,
};
window._sessionStorage = {
getItem: this.sessionStorageGetItem,
setItem: this.sessionStorageSetItem,
removeItem: this.sessionStorageRemoveItem,
};

// this is only settings that initialized before connection was established
let drawerState = this.guiSettings.native['App.drawerState'];
if (drawerState) {
drawerState = parseInt(drawerState, 10);
} else {
drawerState = this.props.width === 'xs' ? DrawerStates.closed : DrawerStates.opened;
}
const noTranslation =
(window._localStorage || window.localStorage).getItem('App.noTranslation') !== 'false';
let state;
try {
state = await this.socket.getState(`system.adapter.${this.adminInstance}.guiSettings`);
} catch (e) {
state = { val: false };
}
if (state?.val) {
this.guiSettings = obj;
this.guiSettings.native = this.guiSettings.native || { localStorage: {}, sessionStorage: {} };
if (!this.guiSettings.native.localStorage) {
this.guiSettings.native = { localStorage: this.guiSettings.native, sessionStorage: {} };
}

this.setState({ guiSettings: true, drawerState, noTranslation }, () => {
if (Utils.getThemeName() !== this.state.theme.name) {
this.toggleTheme(Utils.getThemeName());
}
});
} else if (this.state.guiSettings) {
window._localStorage = null;
window._sessionStorage = null;
window._localStorage = {
getItem: this.localStorageGetItem,
setItem: this.localStorageSetItem,
removeItem: this.localStorageRemoveItem,
};
window._sessionStorage = {
getItem: this.sessionStorageGetItem,
setItem: this.sessionStorageSetItem,
removeItem: this.sessionStorageRemoveItem,
};

this.setState({ guiSettings: false });
// this is only settings that initialized before connection was established
let drawerState = this.guiSettings.native['App.drawerState'];
if (drawerState) {
drawerState = parseInt(drawerState, 10);
} else {
drawerState = this.props.width === 'xs' ? DrawerStates.closed : DrawerStates.opened;
}
});
const noTranslation =
(window._localStorage || window.localStorage).getItem('App.noTranslation') !== 'false';

this.setState({ guiSettings: true, drawerState, noTranslation }, () => {
if (Utils.getThemeName() !== this.state.theme.name) {
this.toggleTheme(Utils.getThemeName());
}
});
} else if (this.state.guiSettings) {
window._localStorage = null;
window._sessionStorage = null;

this.setState({ guiSettings: false });
}
}

enableGuiSettings(enabled, ownSettings) {
Expand Down Expand Up @@ -943,39 +953,39 @@ class App extends Router {

// Read user and show him
if (this.socket.isSecure || this.socket.systemConfig.native?.vendor) {
this.socket
.getCurrentUser()
.then(user => {
this.socket.getObject(`system.user.${user}`).then(userObj => {
if (userObj.native?.vendor) {
Object.assign(this.adminGuiConfig, userObj.native.vendor);
}
try {
const user = await this.socket
.getCurrentUser();

if (this.socket.isSecure) {
this.setState({
user: {
id: userObj._id,
name: Utils.getObjectNameFromObj(
userObj,
this.socket.systemLang,
),
color: userObj.common.color,
icon: userObj.common.icon,
invertBackground: this.mustInvertBackground(
userObj.common.color,
),
},
});
const userObj = await this.socket.getObject(`system.user.${user}`);

// start ping interval
this.makePingAuth();
}
if (userObj.native?.vendor) {
Object.assign(this.adminGuiConfig, userObj.native.vendor);
}

if (this.socket.isSecure) {
this.setState({
user: {
id: userObj._id,
name: Utils.getObjectNameFromObj(
userObj,
this.socket.systemLang,
),
color: userObj.common.color,
icon: userObj.common.icon,
invertBackground: this.mustInvertBackground(
userObj.common.color,
),
},
});
})
.catch(error => {
console.error(error);
this.showAlert(error, 'error');
});

// start ping interval
this.makePingAuth();
}
} catch (e) {
console.error(`Could not determine user to show: ${e}`);
this.showAlert(e, 'error');
}
}

this.setState(newState, () => this.setCurrentTabTitle());
Expand Down

0 comments on commit d94e898

Please sign in to comment.