Skip to content

Commit

Permalink
fix unknown users issue (not sure why that could happen... let's go t…
Browse files Browse the repository at this point in the history
…hat way now)
  • Loading branch information
Garfonso committed Jan 2, 2024
1 parent 0f32b43 commit 06dcf9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
16 changes: 16 additions & 0 deletions lib/modules/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ class PersonModule {
return result;
}

/**
* Get the user id from the username
* @param {string} name
* @returns {string}
*/
getUserIDFromName(name) {
for (const userObj of Object.entries(this.usersCache)) {
if (userObj.name === name) {
return userObj.iobId;
}
}

this.adapter.log.warn(`Could not get user id for ${name} - Trying with username` + JSON.stringify(this.usersCache['system.user.' + name.toLowerCase()]));
return 'system.user.' + name.toLowerCase(); //hack and not correct since js-controller 3.2
}

onObjectChange(id, obj) {
if (id.startsWith('system.user.')) {
if (obj && obj.common && obj.common.enabled()) {
Expand Down
39 changes: 9 additions & 30 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class WebServer {
}

async _processSingleCall(ws, data, entity_id) {
const user = await this._getUserId(ws.__auth.username || this.config.defaultUser);
const user = this._modules.person.getUserIDFromName(ws.__auth.username || this.config.defaultUser);

const entity = entityData.entityId2Entity[entity_id];
const id = entity.context.STATE.setId;
Expand Down Expand Up @@ -681,7 +681,7 @@ class WebServer {
entity.state = 'unknown';
if (entity.context.STATE && entity.context.STATE.getId) {
try {
const user = await this._getUserId(this.config.defaultUser); //TODO: why is this always defaultUser?
const user = this._modules.person.getUserIDFromName(this.config.defaultUser); //TODO: why is this always defaultUser?
const state = await this.adapter.getForeignStateAsync(entity.context.STATE.getId, {user});
if (state) {
await this.onStateChange(entity.context.STATE.getId, state);
Expand Down Expand Up @@ -1477,7 +1477,7 @@ class WebServer {
file = file.substring(0, pos);
}
try {
const user = await this._getUserId(this.config.defaultUser); //TODO: why is this always default user?
const user = this._modules.person.getUserIDFromName(this.config.defaultUser); //TODO: why is this always default user?
let data;
if (file.startsWith('/lovelace/')) {
file = file.replace('/lovelace/', '');
Expand All @@ -1501,7 +1501,7 @@ class WebServer {
file = file.substring(0, pos);
}
try {
const user = await this._getUserId(this.config.defaultUser); //TODO: why is this always default user?
const user = this._modules.person.getUserIDFromName(this.config.defaultUser); //TODO: why is this always default user?
const data = await this.adapter.readFileAsync(this.adapter.namespace, file.replace('/local/custom_ui/', '/cards/'), {user});
const pos = req.url.lastIndexOf('.');
res.setHeader('content-type', (mime.getType || mime.lookup).call(data.mimeType, file.substring(pos + 1).toLowerCase()));
Expand Down Expand Up @@ -1844,7 +1844,7 @@ class WebServer {
}

try {
const user = await this._getUserId(this.config.defaultUser); //TODO: why is this always default user?
const user = this._modules.person.getUserIDFromName(this.config.defaultUser); //TODO: why is this always default user?
const image = await this.adapter.readFileAsync(id, url, {user});
if (image.file === null || image.file === undefined) {
throw new Error('File empty');
Expand All @@ -1868,7 +1868,7 @@ class WebServer {
if (obj && obj.common.type === 'file') {
contentType = (mime.getType || mime.lookup).call(mime, fileName[0]);
}
const user = await this._getUserId(this.config.defaultUser);
const user = this._modules.person.getUserIDFromName(this.config.defaultUser);
const data = await this.adapter.getBinaryStateAsync(fileName[0], {user});
if (data !== null && obj !== undefined) {
if (data && typeof data === 'object' && data.val !== undefined && data.ack !== undefined) {
Expand Down Expand Up @@ -1937,7 +1937,7 @@ class WebServer {
return res.status(404).json({error: 'Start or end misformated'});
}

const user = await this._getUserId(this.config.defaultUser);
const user = this._modules.person.getUserIDFromName(this.config.defaultUser);
try {
const state = await this.adapter.getForeignStateAsync(entity.context.STATE.getId, {user});
if (state && state.val) {
Expand Down Expand Up @@ -2018,29 +2018,8 @@ class WebServer {
});
}

async _getUserId(user) {
let userId = this._userNamesToIds[user];
if (userId) {
return userId;
}

if (typeof this.adapter.getUserID === 'function') {
try {
userId = await this.adapter.getUserID(user);
this._userNamesToIds[user] = userId;
} catch (err) {
this.log.warn(`Could not get user id for ${user} - ${err}`);
}
}
if (!userId) {
this.log.warn(`Could not get user id for ${user} - Trying with username`);
userId = 'system.user.' + user.toLowerCase(); //hack and not correct since js-controller 3.2
}
return userId;
}

async _getCurrentUser(ws) {
const user = await this._getUserId(ws.__auth.username || this.config.defaultUser);
const user = this._modules.person.getUserIDFromName(ws.__auth.username || this.config.defaultUser);
const userObj = {
id: user,
name: ws.__auth.username || this.config.defaultUser,
Expand Down Expand Up @@ -2159,7 +2138,7 @@ class WebServer {
}
if (id) {
try {
const user = await this._getUserId(userName);
const user = this._modules.person.getUserIDFromName(userName);
const state = await this.adapter.getForeignStateAsync(id, {user});
if (state && state.val && typeof state.val === 'string') {
const val = state.val.split('?')[0] || '';
Expand Down

0 comments on commit 06dcf9d

Please sign in to comment.