Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async checkLinkPermission(matrixRoomId: string, userId: string) {
const STATE_DEFAULT = 50;
// We decide to allow a user to link or unlink, if they have a powerlevel
// sufficient to affect the 'm.room.power_levels' state; i.e. the
// "operator" heuristic.
const powerLevels = await this.getState(matrixRoomId, "m.room.power_levels");
const userLevel =
(powerLevels.users && userId in powerLevels.users) ? powerLevels.users[userId] :
powerLevels.users_default;
const requiresLevel =
(powerLevels.events && "m.room.power_levels" in powerLevels.events) ?
powerLevels.events["m.room.power_levels"] :
("state_default" in powerLevels) ? powerLevels.powerLevels : STATE_DEFAULT;
return userLevel >= requiresLevel;
}
// What level do we need for this event type?
var defaultLevel = event.content.events_default;
if (STATE_EVENT_TYPES.indexOf(eventType) !== -1) {
defaultLevel = event.content.state_default;
}
var requiredLevel = event.content.events[eventType] || defaultLevel;
// Parse out what level the client has by abusing the JS SDK
var roomMember = new RoomMember(roomId, userId);
roomMember.setPowerLevelEvent(powerLevelEvent);
if (requiredLevel > roomMember.powerLevel) {
// can the bot update our power level?
var bot = new RoomMember(roomId, self.botClient.credentials.userId);
bot.setPowerLevelEvent(powerLevelEvent);
var levelRequiredToModifyPowerLevels = event.content.events[
"m.room.power_levels"
] || event.content.state_default;
if (levelRequiredToModifyPowerLevels > bot.powerLevel) {
// even the bot has no power here.. give up.
throw new Error(
"Cannot ensure client has power level for event " + eventType +
" : client has " + roomMember.powerLevel + " and we require " +
requiredLevel + " and the bot doesn't have permission to " +
"edit the client's power level."
);
}
// update the client's power level first
return self.botClient.setPowerLevel(
roomId, userId, requiredLevel, powerLevelEvent
).then(function() {
// tweak the level for the client to reflect the new reality
_calculateCanPermissions: function(me, them, powerLevels) {
var can = {
kick: false,
ban: false,
mute: false,
modifyLevel: false
};
var canAffectUser = them.powerLevel < me.powerLevel;
if (!canAffectUser) {
//console.log("Cannot affect user: %s >= %s", them.powerLevel, me.powerLevel);
return can;
}
var editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
can.kick = me.powerLevel >= powerLevels.kick;
can.ban = me.powerLevel >= powerLevels.ban;
can.mute = me.powerLevel >= editPowerLevel;
can.modifyLevel = me.powerLevel > them.powerLevel;
return can;
},
_calculateCanPermissions: function(me, them, powerLevels) {
const isMe = me.userId === them.userId;
const can = {
kick: false,
ban: false,
mute: false,
modifyLevel: false,
modifyLevelMax: 0,
};
const canAffectUser = them.powerLevel < me.powerLevel || isMe;
if (!canAffectUser) {
//console.log("Cannot affect user: %s >= %s", them.powerLevel, me.powerLevel);
return can;
}
const editPowerLevel = (
(powerLevels.events ? powerLevels.events["m.room.power_levels"] : null) ||
powerLevels.state_default
);
can.kick = me.powerLevel >= powerLevels.kick;
can.ban = me.powerLevel >= powerLevels.ban;
can.mute = me.powerLevel >= editPowerLevel;
can.modifyLevel = me.powerLevel >= editPowerLevel && (isMe || me.powerLevel > them.powerLevel);
can.modifyLevelMax = me.powerLevel;
return can;
},
let actualPower = 0;
if (powerState.users[userId] !== undefined) {
actualPower = powerState.users[userId];
}
else if (powerState.users_default !== undefined) {
actualPower = powerState.users_default;
}
else {
log.error(`Error getting power level of ${userId} in ${roomId}`);
return Promise.reject({err: 403, body: 'Could not determine power level of the user.'});
}
let requiredPower = this._config.required_power_level;
if (powerState.events["m.room.power_levels"] !== undefined) {
requiredPower = powerState.events["m.room.power_levels"]
}
else if (powerState.state_default !== undefined) {
requiredPower = powerState.state_default;
}
return Promise.resolve(actualPower >= requiredPower);
}
var send_level = parseInt(power_levels.events_default || 0);
var state_level = parseInt(power_levels.state_default || 0);
var default_user_level = parseInt(power_levels.users_default || 0);
if (power_levels.ban == undefined) ban_level = 50;
if (power_levels.kick == undefined) kick_level = 50;
if (power_levels.redact == undefined) redact_level = 50;
var user_levels = power_levels.users || {};
var user_id = MatrixClientPeg.get().credentials.userId;
var current_user_level = user_levels[user_id];
if (current_user_level == undefined) current_user_level = default_user_level;
var power_level_level = events_levels["m.room.power_levels"];
if (power_level_level == undefined) {
power_level_level = state_level;
}
var can_change_levels = current_user_level >= power_level_level;
} else {
var ban_level = 50;
var kick_level = 50;
var redact_level = 50;
var invite_level = 0;
var send_level = 0;
var state_level = 0;
var default_user_level = 0;
var user_levels = [];
var events_levels = [];
return this.getState(matrix_room_id, "m.room.power_levels").then((levels) => {
var user_level =
(levels.users && user_id in levels.users) ? levels.users[user_id] :
levels.users_default;
var requires_level =
(levels.events && "m.room.power_levels" in levels.events) ? levels.events["m.room.power_levels"] :
("state_default" in levels) ? levels.state_default :
50;
return user_level >= requires_level;
});
};
maybe(() => room.content['m.room.power_levels'][''].users[member.user_id]),
},
}, LEAVE_UNPROVIS_AFTER_MS);
let actualPower = 0;
if (powerState.users[userId] !== undefined) {
actualPower = powerState.users[userId];
}
else if (powerState.users_default !== undefined) {
actualPower = powerState.users_default;
}
else {
log.error(`Error getting power level of ${userId} in ${roomId}`);
return Promise.reject({err: 403, body: 'Could not determine power level of the user.'});
}
let requiredPower = this._config.required_power_level;
if (powerState.events["m.room.power_levels"] !== undefined) {
requiredPower = powerState.events["m.room.power_levels"]
}
else if (powerState.state_default !== undefined) {
requiredPower = powerState.state_default;
}
return Promise.resolve(actualPower >= requiredPower);
}
room.power.levels = function()
{
return this.content['m.room.power_levels'][''];
};