Allow making rules more strict during self limit adding

This commit is contained in:
Jonas Lochmann 2022-09-19 02:00:00 +02:00
parent 00ce5853d8
commit 35acd05d08
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
4 changed files with 65 additions and 6 deletions

View file

@ -150,6 +150,8 @@ export const dispatchParentAction = async ({
return dispatchUpdateCategoryBlockedTimes({ action, cache, fromChildSelfLimitAddChildUserId }) return dispatchUpdateCategoryBlockedTimes({ action, cache, fromChildSelfLimitAddChildUserId })
} else if (action instanceof UpdateCategoryDisableLimitsAction) { } else if (action instanceof UpdateCategoryDisableLimitsAction) {
return dispatchUpdateCategoryDisableLimits({ action, cache, fromChildSelfLimitAddChildUserId }) return dispatchUpdateCategoryDisableLimits({ action, cache, fromChildSelfLimitAddChildUserId })
} else if (action instanceof UpdateTimelimitRuleAction) {
return dispatchUpdateTimelimitRule({ action, cache, fromChildSelfLimitAddChildUserId })
} }
if (fromChildSelfLimitAddChildUserId !== null) { if (fromChildSelfLimitAddChildUserId !== null) {
@ -211,8 +213,6 @@ export const dispatchParentAction = async ({
return dispatchUpdateNetworkTimeVerification({ action, cache }) return dispatchUpdateNetworkTimeVerification({ action, cache })
} else if (action instanceof UpdateParentNotificationFlagsAction) { } else if (action instanceof UpdateParentNotificationFlagsAction) {
return dispatchUpdateParentNotificationFlags({ action, cache }) return dispatchUpdateParentNotificationFlags({ action, cache })
} else if (action instanceof UpdateTimelimitRuleAction) {
return dispatchUpdateTimelimitRule({ action, cache })
} else if (action instanceof RemoveUserAction) { } else if (action instanceof RemoveUserAction) {
return dispatchRemoveUser({ action, cache, parentUserId }) return dispatchRemoveUser({ action, cache, parentUserId })
} else if (action instanceof ReportU2fLoginAction) { } else if (action instanceof ReportU2fLoginAction) {

View file

@ -17,11 +17,17 @@
import { UpdateTimelimitRuleAction } from '../../../../action' import { UpdateTimelimitRuleAction } from '../../../../action'
import { Cache } from '../cache' import { Cache } from '../cache'
import { MissingRuleException } from '../exception/missing-item' import { MissingRuleException, MissingCategoryException } from '../exception/missing-item'
import {
CanNotModifyOtherUsersBySelfLimitationException, CanNotRelaxRestrictionsSelfLimitException
} from '../exception/self-limit'
export async function dispatchUpdateTimelimitRule ({ action, cache }: { export async function dispatchUpdateTimelimitRule ({
action, cache, fromChildSelfLimitAddChildUserId
}: {
action: UpdateTimelimitRuleAction action: UpdateTimelimitRuleAction
cache: Cache cache: Cache
fromChildSelfLimitAddChildUserId: string | null
}) { }) {
const ruleEntry = await cache.database.timelimitRule.findOne({ const ruleEntry = await cache.database.timelimitRule.findOne({
where: { where: {
@ -35,6 +41,53 @@ export async function dispatchUpdateTimelimitRule ({ action, cache }: {
throw new MissingRuleException() throw new MissingRuleException()
} }
if (fromChildSelfLimitAddChildUserId != null) {
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: ruleEntry.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!categoryEntryUnsafe) {
throw new MissingCategoryException()
}
const categoryEntry = {
childId: categoryEntryUnsafe.childId
}
if (fromChildSelfLimitAddChildUserId !== categoryEntry.childId) {
throw new CanNotModifyOtherUsersBySelfLimitationException()
}
const wasSessionDurationLimitationEnabled =
ruleEntry.sessionPauseMilliseconds > 0 && ruleEntry.sessionDurationMilliseconds > 0
const countOldAffectedDays = Array(7)
.fill(0)
.reduce((sum, _, index) => sum + ((ruleEntry.dayMaskAsBitmask >> index) & 1), 0)
const isAtLeastAsStrictAsPreviously =
action.maximumTimeInMillis <= ruleEntry.maximumTimeInMillis &&
(action.dayMask & ruleEntry.dayMaskAsBitmask) === ruleEntry.dayMaskAsBitmask &&
(action.applyToExtraTimeUsage || !ruleEntry.applyToExtraTimeUsage) &&
action.start <= ruleEntry.startMinuteOfDay &&
action.end >= ruleEntry.endMinuteOfDay &&
(!wasSessionDurationLimitationEnabled || (
action.sessionDurationMilliseconds <= ruleEntry.sessionDurationMilliseconds &&
action.sessionPauseMilliseconds >= ruleEntry.sessionPauseMilliseconds
)) &&
(!action.perDay || ruleEntry.perDay || countOldAffectedDays <= 1)
if (!isAtLeastAsStrictAsPreviously) {
throw new CanNotRelaxRestrictionsSelfLimitException()
}
}
ruleEntry.applyToExtraTimeUsage = action.applyToExtraTimeUsage ruleEntry.applyToExtraTimeUsage = action.applyToExtraTimeUsage
ruleEntry.dayMaskAsBitmask = action.dayMask ruleEntry.dayMaskAsBitmask = action.dayMask
ruleEntry.maximumTimeInMillis = action.maximumTimeInMillis ruleEntry.maximumTimeInMillis = action.maximumTimeInMillis

View file

@ -1,6 +1,6 @@
/* /*
* server component for the TimeLimit App * server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann * Copyright (C) 2019 - 2022 Jonas Lochmann
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
@ -32,3 +32,9 @@ export class ActionNotSupportedBySelfLimitationException extends SelfLimitationE
} }
export class SelfLimitNotPossibleException extends SelfLimitationException {} export class SelfLimitNotPossibleException extends SelfLimitationException {}
export class CanNotRelaxRestrictionsSelfLimitException extends SelfLimitationException {
constructor () {
super({ staticMessage: 'can not relax restrictions with the self limitation' })
}
}

View file

@ -59,7 +59,7 @@ export const generateServerDataStatus = async ({
familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0 familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0
), ),
message: await getStatusMessage({ database, transaction }) || undefined, message: await getStatusMessage({ database, transaction }) || undefined,
apiLevel: 6 apiLevel: 7
} }
if (familyEntry.deviceListVersion !== clientStatus.devices) { if (familyEntry.deviceListVersion !== clientStatus.devices) {