Initial commit

This commit is contained in:
Jonas L
2019-02-25 00:00:00 +00:00
commit 22c372e246
200 changed files with 27781 additions and 0 deletions
@@ -0,0 +1,77 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { AddCategoryAppsAction } from '../../../../action'
import { CategoryAppAttributes } from '../../../../database/categoryapp'
import { Cache } from '../cache'
export async function dispatchAddCategoryApps ({ action, cache }: {
action: AddCategoryAppsAction
cache: Cache
}) {
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!categoryEntryUnsafe) {
throw new Error('invalid category id')
}
const { childId } = categoryEntryUnsafe
const categoriesOfSameChild = await cache.database.category.findAll({
where: {
familyId: cache.familyId,
childId
},
attributes: ['categoryId'],
transaction: cache.transaction
}).map((item) => ({ categoryId: item.categoryId }))
await cache.database.categoryApp.destroy({
where: {
familyId: cache.familyId,
categoryId: {
[Sequelize.Op.in]: categoriesOfSameChild.map((item) => item.categoryId)
},
packageName: {
[Sequelize.Op.in]: action.packageNames
}
},
transaction: cache.transaction
})
await cache.database.categoryApp.bulkCreate(
action.packageNames.map((packageName): CategoryAppAttributes => ({
familyId: cache.familyId,
categoryId: action.categoryId,
packageName
})),
{
transaction: cache.transaction
}
)
cache.categoriesWithModifiedApps.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -0,0 +1,46 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { AddUserAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchAddUser ({ action, cache }: {
action: AddUserAction
cache: Cache
}) {
await cache.database.user.create({
familyId: cache.familyId,
userId: action.userId,
type: action.userType,
name: action.name,
timeZone: action.timeZone,
passwordHash: action.password ? action.password.hash : '',
secondPasswordHash: action.password ? action.password.secondHash : '',
secondPasswordSalt: action.password ? action.password.secondSalt : '',
mail: '',
disableTimelimitsUntil: '0',
currentDevice: '',
categoryForNotAssignedApps: '',
relaxPrimaryDeviceRule: false,
mailNotificationFlags: 0
}, { transaction: cache.transaction })
cache.invalidiateUserList = true
cache.areChangesImportant = true
cache.doesUserExist.cache.set(action.userId, true)
}
@@ -0,0 +1,52 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { ChangeParentPasswordAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchChangeParentPassword ({ action, cache }: {
action: ChangeParentPasswordAction
cache: Cache
}) {
const parentEntry = await cache.database.user.findOne({
where: {
familyId: cache.familyId,
userId: action.parentUserId,
type: 'parent'
},
transaction: cache.transaction,
lock: Sequelize.Transaction.LOCK.UPDATE
})
if (!parentEntry) {
throw new Error('parent entry not found')
}
action.assertIntegrityValid({ oldPasswordSecondHash: parentEntry.secondPasswordHash })
const newSecondPasswordHash = action.decryptSecondHash({ oldPasswordSecondHash: parentEntry.secondPasswordHash })
parentEntry.passwordHash = action.newPasswordFirstHash
parentEntry.secondPasswordSalt = action.newPasswordSecondSalt
parentEntry.secondPasswordHash = newSecondPasswordHash
await parentEntry.save({ transaction: cache.transaction })
cache.getSecondPasswordHashOfParent.cache.clear()
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,59 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { CreateCategoryAction } from '../../../../action'
import { generateVersionId } from '../../../../util/token'
import { Cache } from '../cache'
export async function dispatchCreateCategory ({ action, cache }: {
action: CreateCategoryAction
cache: Cache
}) {
// check that the child exists
const childEntry = await cache.database.user.findOne({
where: {
familyId: cache.familyId,
userId: action.childId,
type: 'child'
},
transaction: cache.transaction
})
if (!childEntry) {
throw new Error('missing child for new category')
}
// no version number needs to be updated
await cache.database.category.create({
familyId: cache.familyId,
categoryId: action.categoryId,
childId: action.childId,
title: action.title,
blockedMinutesInWeek: '',
temporarilyBlocked: false,
extraTimeInMillis: 0,
timeLimitRulesVersion: generateVersionId(),
baseVersion: generateVersionId(),
assignedAppsVersion: generateVersionId(),
usedTimesVersion: generateVersionId(),
parentCategoryId: ''
}, { transaction: cache.transaction })
// update the cache
cache.doesCategoryExist.cache.set(action.categoryId, true)
cache.areChangesImportant = true
}
@@ -0,0 +1,42 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { CreateTimeLimitRuleAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchCreateTimeLimitRule ({ action, cache }: {
action: CreateTimeLimitRuleAction
cache: Cache
}) {
const doesCategoryExist = await cache.doesCategoryExist(action.rule.categoryId)
if (!doesCategoryExist) {
throw new Error('invalid category id for new rule')
}
await cache.database.timelimitRule.create({
familyId: cache.familyId,
ruleId: action.rule.ruleId,
categoryId: action.rule.categoryId,
applyToExtraTimeUsage: action.rule.applyToExtraTimeUsage,
maximumTimeInMillis: action.rule.maxTimeInMillis,
dayMaskAsBitmask: action.rule.dayMask
}, { transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(action.rule.categoryId)
cache.areChangesImportant = true
}
@@ -0,0 +1,78 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { DeleteCategoryAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchDeleteCategory ({ action, cache }: {
action: DeleteCategoryAction
cache: Cache
}) {
// no version number needs to be updated
const { familyId, transaction } = cache
const { categoryId } = action
await cache.database.timelimitRule.destroy({
where: {
familyId,
categoryId
},
transaction
})
await cache.database.usedTime.destroy({
where: {
familyId,
categoryId
},
transaction
})
await cache.database.categoryApp.destroy({
where: {
familyId,
categoryId
},
transaction
})
const [affectedUserRows] = await cache.database.user.update({
categoryForNotAssignedApps: ''
}, {
where: {
familyId,
categoryForNotAssignedApps: categoryId
},
transaction
})
await cache.database.category.destroy({
where: {
familyId,
categoryId
},
transaction
})
// update the cache
cache.doesCategoryExist.cache.set(action.categoryId, false)
cache.areChangesImportant = true
if (affectedUserRows !== 0) {
cache.invalidiateUserList = true
}
}
@@ -0,0 +1,39 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { DeleteTimeLimitRuleAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchDeleteTimeLimitRule ({ action, cache }: {
action: DeleteTimeLimitRuleAction
cache: Cache
}) {
const ruleEntry = await cache.database.timelimitRule.findOne({
where: {
familyId: cache.familyId,
ruleId: action.ruleId
},
transaction: cache.transaction
})
if (ruleEntry) {
await ruleEntry.destroy({ transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(ruleEntry.categoryId)
cache.areChangesImportant = true
}
}
@@ -0,0 +1,67 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { IgnoreManipulationAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchIgnoreManipulation ({ action, cache }: {
action: IgnoreManipulationAction
cache: Cache
}) {
const deviceEntry = await cache.database.device.findOne({
where: {
familyId: cache.familyId,
deviceId: action.deviceId
},
transaction: cache.transaction
})
if (deviceEntry === null) {
throw new Error('illegal state: missing device which dispatched the action')
}
if (action.ignoreDeviceAdminManipulation) {
deviceEntry.highestProtectionLevel = deviceEntry.currentProtectionLevel
}
if (action.ignoreDeviceAdminManipulationAttempt) {
deviceEntry.triedDisablingDeviceAdmin = false
}
if (action.ignoreAppDowngrade) {
deviceEntry.highestAppVersion = deviceEntry.currentAppVersion
}
if (action.ignoreNotificationAccessManipulation) {
deviceEntry.highestNotificationAccessPermission = deviceEntry.currentNotificationAccessPermission
}
if (action.ignoreUsageStatsAccessManipulation) {
deviceEntry.highestUsageStatsPermission = deviceEntry.currentUsageStatsPermission
}
if (action.ignoreDidReboot) {
deviceEntry.didReboot = false
}
if (action.ignoreHadManipulation) {
deviceEntry.hadManipulation = false
}
await deviceEntry.save({ transaction: cache.transaction })
cache.invalidiateDeviceList = true
}
@@ -0,0 +1,80 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { IncrementCategoryExtraTimeAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchIncrementCategoryExtraTime ({ action, cache }: {
action: IncrementCategoryExtraTimeAction
cache: Cache
}) {
if (!cache.hasFullVersion) {
throw new Error('action requires full version')
}
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: [
'childId',
'parentCategoryId'
]
})
if (!categoryEntryUnsafe) {
throw new Error(`tried to add extra time to ${action.categoryId} but it does not exist`)
}
const categoryEntry = {
childId: categoryEntryUnsafe.childId,
parentCategoryId: categoryEntryUnsafe.parentCategoryId
}
await cache.database.category.update({
extraTimeInMillis: Sequelize.literal(`extraTimeInMillis + ${action.addedExtraTime}`) as any
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
if (categoryEntry.parentCategoryId !== '') {
const [affectedRows] = await cache.database.category.update({
extraTimeInMillis: Sequelize.literal(`extraTimeInMillis + ${action.addedExtraTime}`) as any
}, {
where: {
familyId: cache.familyId,
categoryId: categoryEntry.parentCategoryId,
childId: categoryEntry.childId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(categoryEntry.parentCategoryId)
}
}
}
@@ -0,0 +1,160 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
AddCategoryAppsAction,
AddUserAction,
ChangeParentPasswordAction,
CreateCategoryAction,
CreateTimeLimitRuleAction,
DeleteCategoryAction,
DeleteTimeLimitRuleAction,
IgnoreManipulationAction,
IncrementCategoryExtraTimeAction,
ParentAction,
RemoveCategoryAppsAction,
RemoveUserAction,
RenameChildAction,
SetCategoryExtraTimeAction,
SetCategoryForUnassignedAppsAction,
SetChildPasswordAction,
SetConsiderRebootManipulationAction,
SetDeviceDefaultUserAction,
SetDeviceDefaultUserTimeoutAction,
SetDeviceUserAction,
SetKeepSignedInAction,
SetParentCategoryAction,
SetRelaxPrimaryDeviceAction,
SetSendDeviceConnected,
SetUserDisableLimitsUntilAction,
SetUserTimezoneAction,
UpdateCategoryBlockedTimesAction,
UpdateCategoryTemporarilyBlockedAction,
UpdateCategoryTitleAction,
UpdateDeviceNameAction,
UpdateNetworkTimeVerificationAction,
UpdateParentNotificationFlagsAction,
UpdateTimelimitRuleAction
} from '../../../../action'
import { Cache } from '../cache'
import { dispatchAddCategoryApps } from './addcategoryapps'
import { dispatchAddUser } from './adduser'
import { dispatchChangeParentPassword } from './changeparentpassword'
import { dispatchCreateCategory } from './createcategory'
import { dispatchCreateTimeLimitRule } from './createtimelimitrule'
import { dispatchDeleteCategory } from './deletecategory'
import { dispatchDeleteTimeLimitRule } from './deletetimelimitrule'
import { dispatchIgnoreManipulation } from './ignoremanipulation'
import { dispatchIncrementCategoryExtraTime } from './incrementcategoryextratime'
import { dispatchRemoveCategoryApps } from './removecategoryapps'
import { dispatchRemoveUser } from './removeuser'
import { dispatchRenameChild } from './renamechild'
import { dispatchSetCategoryExtraTime } from './setcategoryextratime'
import { dispatchSetCategoryForUnassignedApps } from './setcategoryforunassignedapps'
import { dispatchSetChildPassword } from './setchildpassword'
import { dispatchSetConsiderRebootManipulation } from './setconsiderrebootmanipulation'
import { dispatchSetDeviceDefaultUser } from './setdevicedefaultuser'
import { dispatchSetDeviceDefaultUserTimeout } from './setdevicedefaultusertimeout'
import { dispatchSetDeviceUser } from './setdeviceuser'
import { dispatchSetKeepSignedIn } from './setkeepsignedin'
import { dispatchSetParentCategory } from './setparentcategory'
import { dispatchSetRelaxPrimaryDevice } from './setrelaxprimarydevice'
import { dispatchSetSendDeviceConnected } from './setsenddeviceconnected'
import { dispatchUserSetDisableLimitsUntil } from './setuserdisablelmitsuntil'
import { dispatchSetUserTimezone } from './setusertimezone'
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
import { dispatchUpdateCategoryTitle } from './updatecategorytitle'
import { dispatchUpdateDeviceName } from './updatedevicename'
import { dispatchUpdateNetworkTimeVerification } from './updatenetworktimeverification'
import { dispatchUpdateParentNotificationFlags } from './updateparentnotificationflags'
import { dispatchUpdateTimelimitRule } from './updatetimelimitrule'
export const dispatchParentAction = async ({ action, cache, parentUserId, sourceDeviceId }: {
action: ParentAction
cache: Cache
parentUserId: string
sourceDeviceId: string | null
}) => {
if (action instanceof AddCategoryAppsAction) {
await dispatchAddCategoryApps({ action, cache })
} else if (action instanceof AddUserAction) {
await dispatchAddUser({ action, cache })
} else if (action instanceof RemoveCategoryAppsAction) {
await dispatchRemoveCategoryApps({ action, cache })
} else if (action instanceof CreateCategoryAction) {
await dispatchCreateCategory({ action, cache })
} else if (action instanceof CreateTimeLimitRuleAction) {
await dispatchCreateTimeLimitRule({ action, cache })
} else if (action instanceof DeleteCategoryAction) {
await dispatchDeleteCategory({ action, cache })
} else if (action instanceof UpdateCategoryTitleAction) {
await dispatchUpdateCategoryTitle({ action, cache })
} else if (action instanceof SetCategoryExtraTimeAction) {
await dispatchSetCategoryExtraTime({ action, cache })
} else if (action instanceof SetCategoryForUnassignedAppsAction) {
await dispatchSetCategoryForUnassignedApps({ action, cache })
} else if (action instanceof SetChildPasswordAction) {
await dispatchSetChildPassword({ action, cache })
} else if (action instanceof SetConsiderRebootManipulationAction) {
await dispatchSetConsiderRebootManipulation({ action, cache })
} else if (action instanceof SetDeviceDefaultUserAction) {
await dispatchSetDeviceDefaultUser({ action, cache })
} else if (action instanceof SetDeviceDefaultUserTimeoutAction) {
await dispatchSetDeviceDefaultUserTimeout({ action, cache })
} else if (action instanceof SetDeviceUserAction) {
await dispatchSetDeviceUser({ action, cache })
} else if (action instanceof SetKeepSignedInAction) {
await dispatchSetKeepSignedIn({ action, cache, parentUserId })
} else if (action instanceof SetParentCategoryAction) {
await dispatchSetParentCategory({ action, cache })
} else if (action instanceof SetRelaxPrimaryDeviceAction) {
await dispatchSetRelaxPrimaryDevice({ action, cache })
} else if (action instanceof SetSendDeviceConnected) {
await dispatchSetSendDeviceConnected({ action, cache, sourceDeviceId })
} else if (action instanceof SetUserDisableLimitsUntilAction) {
await dispatchUserSetDisableLimitsUntil({ action, cache })
} else if (action instanceof SetUserTimezoneAction) {
await dispatchSetUserTimezone({ action, cache })
} else if (action instanceof UpdateCategoryBlockedTimesAction) {
await dispatchUpdateCategoryBlockedTimes({ action, cache })
} else if (action instanceof IncrementCategoryExtraTimeAction) {
await dispatchIncrementCategoryExtraTime({ action, cache })
} else if (action instanceof UpdateCategoryTemporarilyBlockedAction) {
await dispatchUpdateCategoryTemporarilyBlocked({ action, cache })
} else if (action instanceof DeleteTimeLimitRuleAction) {
await dispatchDeleteTimeLimitRule({ action, cache })
} else if (action instanceof UpdateDeviceNameAction) {
await dispatchUpdateDeviceName({ action, cache })
} else if (action instanceof UpdateNetworkTimeVerificationAction) {
await dispatchUpdateNetworkTimeVerification({ action, cache })
} else if (action instanceof UpdateParentNotificationFlagsAction) {
await dispatchUpdateParentNotificationFlags({ action, cache })
} else if (action instanceof UpdateTimelimitRuleAction) {
await dispatchUpdateTimelimitRule({ action, cache })
} else if (action instanceof RemoveUserAction) {
await dispatchRemoveUser({ action, cache, parentUserId })
} else if (action instanceof RenameChildAction) {
await dispatchRenameChild({ action, cache })
} else if (action instanceof ChangeParentPasswordAction) {
await dispatchChangeParentPassword({ action, cache })
} else if (action instanceof IgnoreManipulationAction) {
await dispatchIgnoreManipulation({ action, cache })
} else {
throw new Error('unsupported action type')
}
}
@@ -0,0 +1,43 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { RemoveCategoryAppsAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchRemoveCategoryApps ({ action, cache }: {
action: RemoveCategoryAppsAction
cache: Cache
}) {
const affectedRows = await cache.database.categoryApp.destroy({
where: {
familyId: cache.familyId,
categoryId: action.categoryId,
packageName: {
[Sequelize.Op.in]: action.packageNames
}
},
transaction: cache.transaction
})
if (affectedRows !== action.packageNames.length) {
throw new Error('could not delete as much entries as requested')
}
cache.categoriesWithModifiedApps.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -0,0 +1,159 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { createHash } from 'crypto'
import { InternalServerError } from 'http-errors'
import * as Sequelize from 'sequelize'
import { RemoveUserAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchRemoveUser ({ action, cache, parentUserId }: {
action: RemoveUserAction
cache: Cache
parentUserId: string
}) {
const user = await cache.database.user.findOne({
where: {
familyId: cache.familyId,
userId: action.userId
},
transaction: cache.transaction
})
if (!user) {
throw new Error('invalid user id')
}
if (user.type === 'parent') {
if (!parentUserId) {
throw new InternalServerError()
}
if (parentUserId === action.userId) {
throw new Error('users can not delete themself')
}
const expectedIntegrityValue = createHash('sha512').update(
action.userId + user.secondPasswordHash + 'remove'
).digest('hex').substring(0, 16)
if (expectedIntegrityValue !== action.authentication) {
throw new Error('invalid authentication value')
}
if (user.mail !== '') {
const usersWithLinkedMail = await cache.database.user.count({
transaction: cache.transaction,
where: {
familyId: cache.familyId,
type: 'parent',
mail: {
[Sequelize.Op.not]: ''
}
}
})
if (usersWithLinkedMail <= 1) {
throw new Error('this user is the last one with a linked mail address')
}
}
}
if (user.type === 'child') {
const categories = await cache.database.category.findAll({
where: {
familyId: cache.familyId,
childId: action.userId
},
transaction: cache.transaction,
lock: Sequelize.Transaction.LOCK.UPDATE
})
await cache.database.categoryApp.destroy({
where: {
familyId: cache.familyId,
categoryId: {
[Sequelize.Op.in]: categories.map((category) => category.categoryId)
}
},
transaction: cache.transaction
})
await cache.database.timelimitRule.destroy({
where: {
familyId: cache.familyId,
categoryId: {
[Sequelize.Op.in]: categories.map((category) => category.categoryId)
}
},
transaction: cache.transaction
})
await cache.database.usedTime.destroy({
where: {
familyId: cache.familyId,
categoryId: {
[Sequelize.Op.in]: categories.map((category) => category.categoryId)
}
},
transaction: cache.transaction
})
await cache.database.category.destroy({
where: {
familyId: cache.familyId,
categoryId: {
[Sequelize.Op.in]: categories.map((category) => category.categoryId)
}
},
transaction: cache.transaction
})
}
const [updatedDevices1] = await cache.database.device.update({
currentUserId: '',
isUserKeptSignedIn: false
}, {
where: {
familyId: cache.familyId,
currentUserId: action.userId
},
transaction: cache.transaction
})
const [updatedDevices2] = await cache.database.device.update({
defaultUserId: ''
}, {
where: {
familyId: cache.familyId,
defaultUserId: action.userId
},
transaction: cache.transaction
})
if (updatedDevices1 > 0 || updatedDevices2 > 0) {
cache.invalidiateDeviceList = true
}
await user.destroy({ transaction: cache.transaction })
cache.invalidiateUserList = true
cache.areChangesImportant = true
cache.doesUserExist.cache.set(action.userId, false)
cache.getSecondPasswordHashOfParent.cache.delete(action.userId)
}
@@ -0,0 +1,42 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { RenameChildAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchRenameChild ({ action, cache }: {
action: RenameChildAction
cache: Cache
}) {
const [affectedRows] = await cache.database.user.update({
name: action.newName
}, {
where: {
familyId: cache.familyId,
userId: action.childId,
type: 'child'
},
transaction: cache.transaction
})
if (affectedRows !== 1) {
throw new Error('can not update child name if child does not exist')
}
cache.invalidiateUserList = true
cache.doesUserExist.cache.set(action.childId, false)
}
@@ -0,0 +1,43 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetCategoryExtraTimeAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetCategoryExtraTime ({ action, cache }: {
action: SetCategoryExtraTimeAction
cache: Cache
}) {
if (!cache.hasFullVersion) {
throw new Error('action requires full version')
}
const [affectedRows] = await cache.database.category.update({
extraTimeInMillis: action.newExtraTime
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
}
@@ -0,0 +1,67 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetCategoryForUnassignedAppsAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetCategoryForUnassignedApps ({ action, cache }: {
action: SetCategoryForUnassignedAppsAction
cache: Cache
}) {
if (action.categoryId === '') {
// nothing to check
} else {
const categoryEntryUnsafe = await cache.database.category.findOne({
attributes: ['childId'],
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (!categoryEntryUnsafe) {
throw new Error('can not set a category which does not exist as category for unassigned apps')
}
const categoryEntry = {
childId: categoryEntryUnsafe.childId
}
if (categoryEntry.childId !== action.childId) {
throw new Error('can not set a category of one child as category for unassigned apps for an other child')
}
}
const [affectedRows] = await cache.database.user.update({
categoryForNotAssignedApps: action.categoryId
}, {
where: {
familyId: cache.familyId,
userId: action.childId,
type: 'child'
},
transaction: cache.transaction
})
if (affectedRows !== 1) {
throw new Error('could not find a child with matching id for setting the category for not assigned apps')
}
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,49 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as Sequelize from 'sequelize'
import { SetChildPasswordAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetChildPassword ({ action, cache }: {
action: SetChildPasswordAction
cache: Cache
}) {
const childEntry = await cache.database.user.findOne({
where: {
familyId: cache.familyId,
userId: action.childUserId,
type: 'child'
},
transaction: cache.transaction,
lock: Sequelize.Transaction.LOCK.UPDATE
})
if (!childEntry) {
throw new Error('parent entry not found')
}
childEntry.passwordHash = action.newPassword.hash
childEntry.secondPasswordSalt = action.newPassword.secondSalt
childEntry.secondPasswordHash = action.newPassword.secondHash
await childEntry.save({ transaction: cache.transaction })
cache.getSecondPasswordHashOfChild.cache.clear()
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetConsiderRebootManipulationAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetConsiderRebootManipulation ({ action, cache }: {
action: SetConsiderRebootManipulationAction
cache: Cache
}) {
const [affectedRows] = await cache.database.device.update({
considerRebootManipulation: action.enable
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
deviceId: action.deviceId
}
})
if (affectedRows === 0) {
throw new Error('did not find device to update consider reboot manipulation')
}
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,49 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetDeviceDefaultUserAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetDeviceDefaultUser ({ action, cache }: {
action: SetDeviceDefaultUserAction
cache: Cache
}) {
if (action.defaultUserId !== '') {
const doesUserExist = await cache.doesUserExist(action.defaultUserId)
if (!doesUserExist) {
throw new Error('can not set invalid user as default user')
}
}
const [affectedRows] = await cache.database.device.update({
defaultUserId: action.defaultUserId
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
deviceId: action.deviceId
}
})
if (affectedRows === 0) {
throw new Error('did not find device to update default user')
}
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetDeviceDefaultUserTimeoutAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetDeviceDefaultUserTimeout ({ action, cache }: {
action: SetDeviceDefaultUserTimeoutAction
cache: Cache
}) {
const [affectedRows] = await cache.database.device.update({
defaultUserTimeout: action.timeout
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
deviceId: action.deviceId
}
})
if (affectedRows === 0) {
throw new Error('did not find device to update default user timeout')
}
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,48 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetDeviceUserAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetDeviceUser ({ action, cache }: {
action: SetDeviceUserAction
cache: Cache
}) {
if (action.userId !== '') {
const doesUserExist = await cache.doesUserExist(action.userId)
if (!doesUserExist) {
throw new Error('invalid user id provided')
}
}
const [affectedRows] = await cache.database.device.update({
currentUserId: action.userId,
isUserKeptSignedIn: false
}, {
where: {
familyId: cache.familyId,
deviceId: action.deviceId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
}
@@ -0,0 +1,65 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetKeepSignedInAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetKeepSignedIn ({ action, cache, parentUserId }: {
action: SetKeepSignedInAction
cache: Cache
parentUserId: string
}) {
const doesUserExist = await cache.doesUserExist(parentUserId)
if (!doesUserExist) {
throw new Error('invalid user id provided')
}
const deviceEntry = await cache.database.device.findOne({
where: {
familyId: cache.familyId,
deviceId: action.deviceId
},
transaction: cache.transaction
})
if (!deviceEntry) {
throw new Error('device does not exist')
}
if (deviceEntry.currentUserId !== parentUserId) {
if (action.keepSignedIn) {
throw new Error('only the user itself can disable asking for the password')
}
}
const [affectedRows] = await cache.database.device.update({
isUserKeptSignedIn: action.keepSignedIn
}, {
where: {
familyId: cache.familyId,
deviceId: action.deviceId,
currentUserId: deviceEntry.currentUserId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
}
@@ -0,0 +1,80 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetParentCategoryAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetParentCategory ({ action, cache }: {
action: SetParentCategoryAction
cache: Cache
}) {
const categoryEntry = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (!categoryEntry) {
throw new Error('tried to set parent category of non existent category')
}
if (action.parentCategory !== '') {
const parentCategoryEntry = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.parentCategory,
childId: categoryEntry.childId
},
transaction: cache.transaction
})
if (!parentCategoryEntry) {
throw new Error('tried to set parent category to non existent category')
}
if (parentCategoryEntry.parentCategoryId !== '') {
throw new Error('tried to set a category as parent which itself has got a parent')
}
const countChildCategories = await cache.database.category.findAndCountAll({
where: {
familyId: cache.familyId,
parentCategoryId: action.categoryId
},
transaction: cache.transaction
})
if (countChildCategories.count > 0) {
throw new Error('tried to make category a child category altough it is already a parent category')
}
}
await cache.database.category.update({
parentCategoryId: action.parentCategory
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -0,0 +1,42 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetRelaxPrimaryDeviceAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetRelaxPrimaryDevice ({ action, cache }: {
action: SetRelaxPrimaryDeviceAction
cache: Cache
}) {
const [affectedRows] = await cache.database.user.update({
relaxPrimaryDeviceRule: action.relax
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
userId: action.userId,
type: 'child'
}
})
if (affectedRows === 0) {
throw new Error('did not find user to update relax primary device')
}
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,46 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetSendDeviceConnected } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetSendDeviceConnected ({ action, cache, sourceDeviceId }: {
action: SetSendDeviceConnected
cache: Cache
sourceDeviceId: string | null
}) {
if (sourceDeviceId === null || action.deviceId !== sourceDeviceId) {
throw new Error('only can do that from the device itself')
}
const [affectedRows] = await cache.database.device.update({
showDeviceConnected: action.enable
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
deviceId: action.deviceId
}
})
if (affectedRows === 0) {
throw new Error('did not find device to update send if connected')
}
cache.devicesWithModifiedShowDeviceConnected.set(action.deviceId, action.enable)
cache.invalidiateDeviceList = true
}
@@ -0,0 +1,48 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetUserDisableLimitsUntilAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUserSetDisableLimitsUntil ({ action, cache }: {
action: SetUserDisableLimitsUntilAction
cache: Cache
}) {
if (action.timestamp !== 0) {
if (!cache.hasFullVersion) {
throw new Error('action requires full version')
}
}
const [affectedRows] = await cache.database.user.update({
disableTimelimitsUntil: action.timestamp.toString(10)
}, {
where: {
familyId: cache.familyId,
userId: action.childId,
type: 'child'
},
transaction: cache.transaction
})
if (affectedRows === 0) {
throw new Error('invalid user id provided')
}
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { SetUserTimezoneAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchSetUserTimezone ({ action, cache }: {
action: SetUserTimezoneAction
cache: Cache
}) {
const [affectedRows] = await cache.database.user.update({
timeZone: action.timezone
}, {
transaction: cache.transaction,
where: {
familyId: cache.familyId,
userId: action.userId
}
})
if (affectedRows === 0) {
throw new Error('did not find user to update timezone')
}
cache.invalidiateUserList = true
cache.areChangesImportant = true
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateCategoryBlockedTimesAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateCategoryBlockedTimes ({ action, cache }: {
action: UpdateCategoryBlockedTimesAction
cache: Cache
}) {
const [affectedRows] = await cache.database.category.update({
blockedMinutesInWeek: action.blockedTimes
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (affectedRows === 0) {
throw new Error('invalid category id provided')
}
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -0,0 +1,45 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateCategoryTemporarilyBlockedAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateCategoryTemporarilyBlocked ({ action, cache }: {
action: UpdateCategoryTemporarilyBlockedAction
cache: Cache
}) {
if (action.blocked === true) {
if (!cache.hasFullVersion) {
throw new Error('action requires full version')
}
}
const [affectedRows] = await cache.database.category.update({
temporarilyBlocked: action.blocked
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
}
@@ -0,0 +1,39 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateCategoryTitleAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateCategoryTitle ({ action, cache }: {
action: UpdateCategoryTitleAction
cache: Cache
}) {
const [affectedRows] = await cache.database.category.update({
title: action.newTitle
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateDeviceNameAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateDeviceName ({ action, cache }: {
action: UpdateDeviceNameAction
cache: Cache
}) {
const [affectedRows] = await cache.database.device.update({
name: action.name
}, {
where: {
familyId: cache.familyId,
deviceId: action.deviceId
},
transaction: cache.transaction
})
if (affectedRows === 0) {
throw new Error('invalid device id')
} else {
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
}
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateNetworkTimeVerificationAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateNetworkTimeVerification ({ action, cache }: {
action: UpdateNetworkTimeVerificationAction
cache: Cache
}) {
const [affectedRows] = await cache.database.device.update({
networkTime: action.mode
}, {
where: {
familyId: cache.familyId,
deviceId: action.deviceId
},
transaction: cache.transaction
})
if (affectedRows === 0) {
throw new Error('invalid device id')
} else {
cache.invalidiateDeviceList = true
cache.areChangesImportant = true
}
}
@@ -0,0 +1,47 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateParentNotificationFlagsAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateParentNotificationFlags ({ action, cache }: {
action: UpdateParentNotificationFlagsAction
cache: Cache
}) {
const parentEntry = await cache.database.user.findOne({
where: {
familyId: cache.familyId,
userId: action.parentId,
type: 'parent'
},
transaction: cache.transaction
})
if (!parentEntry) {
throw new Error('parent not found')
}
if (action.set) {
parentEntry.mailNotificationFlags |= action.flags
} else {
parentEntry.mailNotificationFlags &= ~action.flags
}
await parentEntry.save({ transaction: cache.transaction })
cache.invalidiateUserList = true
}
@@ -0,0 +1,45 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { UpdateTimelimitRuleAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchUpdateTimelimitRule ({ action, cache }: {
action: UpdateTimelimitRuleAction
cache: Cache
}) {
const ruleEntry = await cache.database.timelimitRule.findOne({
where: {
familyId: cache.familyId,
ruleId: action.ruleId
},
transaction: cache.transaction
})
if (!ruleEntry) {
throw new Error('invalid rule id provided')
}
ruleEntry.applyToExtraTimeUsage = action.applyToExtraTimeUsage
ruleEntry.dayMaskAsBitmask = action.dayMask
ruleEntry.maximumTimeInMillis = action.maximumTimeInMillis
await ruleEntry.save({ transaction: cache.transaction })
cache.categoriesWithModifiedTimeLimitRules.push(ruleEntry.categoryId)
cache.areChangesImportant = true
}