Allow adding category network ids

This commit is contained in:
Jonas Lochmann
2020-08-24 02:00:00 +02:00
parent 9730d13b2e
commit eb6f668b43
34 changed files with 1257 additions and 62 deletions
+66
View File
@@ -0,0 +1,66 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { anonymizedNetworkIdLength } from '../database/categorynetworkid'
import { assertIsHexString } from '../util/hexstring'
import { assertIdWithinFamily } from '../util/token'
import { ParentAction } from './basetypes'
export class AddCategoryNetworkIdAction extends ParentAction {
readonly categoryId: string
readonly itemId: string
readonly hashedNetworkId: string
constructor ({ categoryId, itemId, hashedNetworkId }: {
categoryId: string
itemId: string
hashedNetworkId: string
}) {
super()
assertIdWithinFamily(categoryId)
assertIdWithinFamily(itemId)
assertIsHexString(hashedNetworkId)
if (hashedNetworkId.length !== anonymizedNetworkIdLength) throw new Error('wrong network id length')
this.categoryId = categoryId
this.itemId = itemId
this.hashedNetworkId = hashedNetworkId
}
serialize = (): SerializedAddCategoryNetworkIdAction => ({
type: 'ADD_CATEGORY_NETWORK_ID',
categoryId: this.categoryId,
itemId: this.itemId,
hashedNetworkId: this.hashedNetworkId
})
static parse = ({ categoryId, itemId, hashedNetworkId }: SerializedAddCategoryNetworkIdAction) => (
new AddCategoryNetworkIdAction({
categoryId,
itemId,
hashedNetworkId
})
)
}
export interface SerializedAddCategoryNetworkIdAction {
type: 'ADD_CATEGORY_NETWORK_ID'
categoryId: string
itemId: string
hashedNetworkId: string
}
+2
View File
@@ -18,6 +18,7 @@
export { AppLogicAction, ChildAction, ParentAction } from './basetypes'
export { AddCategoryAppsAction } from './addcategoryapps'
export { AddCategoryNetworkIdAction } from './addcategorynetworkid'
export { AddUserAction } from './adduser'
export { AddInstalledAppsAction } from './addinstalledapps'
export { AddUsedTimeAction } from './addusedtime'
@@ -34,6 +35,7 @@ export { IncrementCategoryExtraTimeAction } from './incrementcategoryextratime'
export { RemoveCategoryAppsAction } from './removecategoryapps'
export { RemoveInstalledAppsAction } from './removeinstalledapps'
export { RemoveUserAction } from './removeuser'
export { ResetCategoryNetworkIdsAction } from './resetcategorynetworkids'
export { RenameChildAction } from './renamechild'
export { ResetParentBlockedTimesAction } from './resetparentblockedtimes'
export { SetCategoryExtraTimeAction } from './setcategoryextratime'
+49
View File
@@ -0,0 +1,49 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { assertIdWithinFamily } from '../util/token'
import { ParentAction } from './basetypes'
export class ResetCategoryNetworkIdsAction extends ParentAction {
readonly categoryId: string
constructor ({ categoryId }: {
categoryId: string
}) {
super()
assertIdWithinFamily(categoryId)
this.categoryId = categoryId
}
serialize = (): SerializeResetCategoryNetworkIdsAction => ({
type: 'RESET_CATEGORY_NETWORK_IDS',
categoryId: this.categoryId
})
static parse = ({ categoryId }: SerializeResetCategoryNetworkIdsAction) => (
new ResetCategoryNetworkIdsAction({
categoryId
})
)
}
export interface SerializeResetCategoryNetworkIdsAction {
type: 'RESET_CATEGORY_NETWORK_IDS'
categoryId: string
}
+8
View File
@@ -16,6 +16,7 @@
*/
import { AddCategoryAppsAction, SerializedAddCategoryAppsAction } from '../addcategoryapps'
import { AddCategoryNetworkIdAction, SerializedAddCategoryNetworkIdAction } from '../addcategorynetworkid'
import { AddUserAction, SerializedAddUserAction } from '../adduser'
import { ParentAction } from '../basetypes'
import { ChangeParentPasswordAction, SerializedChangeParentPasswordAction } from '../changeparentpassword'
@@ -28,6 +29,7 @@ import { IncrementCategoryExtraTimeAction, SerializedIncrementCategoryExtraTimeA
import { RemoveCategoryAppsAction, SerializedRemoveCategoryAppsAction } from '../removecategoryapps'
import { RemoveUserAction, SerializedRemoveUserAction } from '../removeuser'
import { RenameChildAction, SerializedRenameChildAction } from '../renamechild'
import { ResetCategoryNetworkIdsAction, SerializeResetCategoryNetworkIdsAction } from '../resetcategorynetworkids'
import { ResetParentBlockedTimesAction, SerializedResetParentBlockedTimesAction } from '../resetparentblockedtimes'
import { SerializedSetCategoryExtraTimeAction, SetCategoryExtraTimeAction } from '../setcategoryextratime'
import { SerializedSetCategoryForUnassignedAppsAction, SetCategoryForUnassignedAppsAction } from '../setcategoryforunassignedapps'
@@ -60,6 +62,7 @@ import { SerializedUpdateUserLimitLoginCategory, UpdateUserLimitLoginCategory }
export type SerializedParentAction =
SerializedAddCategoryAppsAction |
SerializedAddCategoryNetworkIdAction |
SerializedAddUserAction |
SerializedChangeParentPasswordAction |
SerializedCreateCategoryAction |
@@ -71,6 +74,7 @@ export type SerializedParentAction =
SerializedRemoveCategoryAppsAction |
SerializedRemoveUserAction |
SerializedRenameChildAction |
SerializeResetCategoryNetworkIdsAction |
SerializedResetParentBlockedTimesAction |
SerializedSetCategoryForUnassignedAppsAction |
SerializedSetChildPasswordAction |
@@ -104,6 +108,8 @@ export type SerializedParentAction =
export const parseParentAction = (action: SerializedParentAction): ParentAction => {
if (action.type === 'ADD_CATEGORY_APPS') {
return AddCategoryAppsAction.parse(action)
} else if (action.type === 'ADD_CATEGORY_NETWORK_ID') {
return AddCategoryNetworkIdAction.parse(action)
} else if (action.type === 'ADD_USER') {
return AddUserAction.parse(action)
} else if (action.type === 'CHANGE_PARENT_PASSWORD') {
@@ -126,6 +132,8 @@ export const parseParentAction = (action: SerializedParentAction): ParentAction
return RemoveUserAction.parse(action)
} else if (action.type === 'RENAME_CHILD') {
return RenameChildAction.parse(action)
} else if (action.type === 'RESET_CATEGORY_NETWORK_IDS') {
return ResetCategoryNetworkIdsAction.parse(action)
} else if (action.type === 'RESET_PARENT_BLOCKED_TIMES') {
return ResetParentBlockedTimesAction.parse(action)
} else if (action.type === 'SET_CATEGORY_EXTRA_TIME') {
+75
View File
@@ -119,6 +119,33 @@ const definitions = {
"type"
]
},
"SerializedAddCategoryNetworkIdAction": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"ADD_CATEGORY_NETWORK_ID"
]
},
"categoryId": {
"type": "string"
},
"itemId": {
"type": "string"
},
"hashedNetworkId": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"categoryId",
"hashedNetworkId",
"itemId",
"type"
]
},
"SerializedAddUserAction": {
"type": "object",
"properties": {
@@ -468,6 +495,25 @@ const definitions = {
"type"
]
},
"SerializeResetCategoryNetworkIdsAction": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"RESET_CATEGORY_NETWORK_IDS"
]
},
"categoryId": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"categoryId",
"type"
]
},
"SerializedResetParentBlockedTimesAction": {
"type": "object",
"properties": {
@@ -1835,6 +1881,12 @@ const definitions = {
},
"sort": {
"type": "number"
},
"networks": {
"type": "array",
"items": {
"$ref": "#/definitions/ServerCategoryNetworkId"
}
}
},
"additionalProperties": false,
@@ -1847,6 +1899,7 @@ const definitions = {
"extraTimeDay",
"mblCharging",
"mblMobile",
"networks",
"parentCategoryId",
"sort",
"tempBlockTime",
@@ -1856,6 +1909,22 @@ const definitions = {
"version"
]
},
"ServerCategoryNetworkId": {
"type": "object",
"properties": {
"itemId": {
"type": "string"
},
"hashedNetworkId": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"hashedNetworkId",
"itemId"
]
},
"ServerUpdatedCategoryAssignedApps": {
"type": "object",
"properties": {
@@ -2327,6 +2396,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
{
"$ref": "#/definitions/SerializedAddCategoryAppsAction"
},
{
"$ref": "#/definitions/SerializedAddCategoryNetworkIdAction"
},
{
"$ref": "#/definitions/SerializedAddUserAction"
},
@@ -2360,6 +2432,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
{
"$ref": "#/definitions/SerializedRenameChildAction"
},
{
"$ref": "#/definitions/SerializeResetCategoryNetworkIdsAction"
},
{
"$ref": "#/definitions/SerializedResetParentBlockedTimesAction"
},
+60
View File
@@ -0,0 +1,60 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { familyIdColumn, idWithinFamilyColumn } from './columns'
import { SequelizeAttributes } from './types'
export interface CategoryNetworkIdAttributes {
familyId: string
categoryId: string
networkItemId: string
hashedNetworkId: string
}
export type CategoryNetworkIdModel = Sequelize.Model & CategoryNetworkIdAttributes
export type CategoryNetworkIdModelStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): CategoryNetworkIdModel;
}
export const anonymizedNetworkIdLength = 8
export const maxNetworkIdsPerCategory = 8
export const attributes: SequelizeAttributes<CategoryNetworkIdAttributes> = {
familyId: {
...familyIdColumn,
primaryKey: true
},
categoryId: {
...idWithinFamilyColumn,
primaryKey: true
},
networkItemId: {
...idWithinFamilyColumn,
primaryKey: true
},
hashedNetworkId: {
type: Sequelize.STRING(anonymizedNetworkIdLength),
allowNull: false,
validate: {
notEmpty: true,
is: /^([0-9a-f]{8})?$/
}
}
}
export const createCategoryNetworkIdModel = (sequelize: Sequelize.Sequelize): CategoryNetworkIdModelStatic => sequelize.define('CategoryNetworkId', attributes) as CategoryNetworkIdModelStatic
+3
View File
@@ -22,6 +22,7 @@ import { AppActivityModelStatic, createAppActivityModel } from './appactivity'
import { AuthTokenModelStatic, createAuthtokenModel } from './authtoken'
import { CategoryModelStatic, createCategoryModel } from './category'
import { CategoryAppModelStatic, createCategoryAppModel } from './categoryapp'
import { CategoryNetworkIdModelStatic, createCategoryNetworkIdModel } from './categorynetworkid'
import { ConfigModelStatic, createConfigModel } from './config'
import { createDeviceModel, DeviceModelStatic } from './device'
import { createFamilyModel, FamilyModelStatic } from './family'
@@ -42,6 +43,7 @@ export interface Database {
appActivity: AppActivityModelStatic
category: CategoryModelStatic
categoryApp: CategoryAppModelStatic
categoryNetworkId: CategoryNetworkIdModelStatic
config: ConfigModelStatic
device: DeviceModelStatic
family: FamilyModelStatic
@@ -63,6 +65,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
appActivity: createAppActivityModel(sequelize),
category: createCategoryModel(sequelize),
categoryApp: createCategoryAppModel(sequelize),
categoryNetworkId: createCategoryNetworkIdModel(sequelize),
config: createConfigModel(sequelize),
device: createDeviceModel(sequelize),
family: createFamilyModel(sequelize),
@@ -0,0 +1,41 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { QueryInterface, Sequelize, Transaction } from 'sequelize'
export async function up (_: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await sequelize.query(
'CREATE TABLE `CategoryNetworkIds` ' +
'(`familyId` VARCHAR(10) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
'`networkItemId` VARCHAR(6) NOT NULL, `hashedNetworkId` VARCHAR(8) NOT NULL,' +
'PRIMARY KEY(`familyId`, `categoryId`, `networkItemId`), FOREIGN KEY(`familyId`, `categoryId`)' +
'REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
{ transaction }
)
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.dropTable('CategoryNetworkIds', { transaction })
})
}
@@ -0,0 +1,73 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { AddCategoryNetworkIdAction } from '../../../../action'
import { maxNetworkIdsPerCategory } from '../../../../database/categorynetworkid'
import { Cache } from '../cache'
export async function dispatchAddCategoryNetworkId ({ action, cache }: {
action: AddCategoryNetworkIdAction
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 for new rule')
}
const count = await cache.database.categoryNetworkId.count({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (count + 1 > maxNetworkIdsPerCategory) {
throw new Error('category network limit reached')
}
const hasOldItem = (await cache.database.categoryNetworkId.count({
where: {
familyId: cache.familyId,
categoryId: action.categoryId,
networkItemId: action.itemId
},
transaction: cache.transaction
})) !== 0
if (hasOldItem) {
throw new Error('id already used')
}
await cache.database.categoryNetworkId.create({
familyId: cache.familyId,
categoryId: action.categoryId,
networkItemId: action.itemId,
hashedNetworkId: action.hashedNetworkId
}, { transaction: cache.transaction })
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
@@ -17,6 +17,7 @@
import {
AddCategoryAppsAction,
AddCategoryNetworkIdAction,
AddUserAction,
ChangeParentPasswordAction,
CreateCategoryAction,
@@ -29,6 +30,7 @@ import {
RemoveCategoryAppsAction,
RemoveUserAction,
RenameChildAction,
ResetCategoryNetworkIdsAction,
ResetParentBlockedTimesAction,
SetCategoryExtraTimeAction,
SetCategoryForUnassignedAppsAction,
@@ -61,6 +63,7 @@ import {
} from '../../../../action'
import { Cache } from '../cache'
import { dispatchAddCategoryApps } from './addcategoryapps'
import { dispatchAddCategoryNetworkId } from './addcategorynetworkid'
import { dispatchAddUser } from './adduser'
import { dispatchChangeParentPassword } from './changeparentpassword'
import { dispatchCreateCategory } from './createcategory'
@@ -72,6 +75,7 @@ import { dispatchIncrementCategoryExtraTime } from './incrementcategoryextratime
import { dispatchRemoveCategoryApps } from './removecategoryapps'
import { dispatchRemoveUser } from './removeuser'
import { dispatchRenameChild } from './renamechild'
import { dispatchResetCategoryNetworkIds } from './resetcategorynetworkids'
import { dispatchResetParentBlockedTimes } from './resetparentblockedtimes'
import { dispatchSetCategoryExtraTime } from './setcategoryextratime'
import { dispatchSetCategoryForUnassignedApps } from './setcategoryforunassignedapps'
@@ -124,7 +128,9 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
}
if (fromChildSelfLimitAddChildUserId === null) {
if (action instanceof AddUserAction) {
if (action instanceof AddCategoryNetworkIdAction) {
return dispatchAddCategoryNetworkId({ action, cache })
} else if (action instanceof AddUserAction) {
return dispatchAddUser({ action, cache })
} else if (action instanceof RemoveCategoryAppsAction) {
return dispatchRemoveCategoryApps({ action, cache })
@@ -178,6 +184,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
return dispatchUpdateTimelimitRule({ action, cache })
} else if (action instanceof RemoveUserAction) {
return dispatchRemoveUser({ action, cache, parentUserId })
} else if (action instanceof ResetCategoryNetworkIdsAction) {
return dispatchResetCategoryNetworkIds({ action, cache })
} else if (action instanceof RenameChildAction) {
return dispatchRenameChild({ action, cache })
} else if (action instanceof ChangeParentPasswordAction) {
@@ -0,0 +1,48 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { ResetCategoryNetworkIdsAction } from '../../../../action'
import { Cache } from '../cache'
export async function dispatchResetCategoryNetworkIds ({ action, cache }: {
action: ResetCategoryNetworkIdsAction
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 for new rule')
}
await cache.database.categoryNetworkId.destroy({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
cache.categoriesWithModifiedBaseData.push(action.categoryId)
cache.areChangesImportant = true
}
+26 -1
View File
@@ -397,6 +397,25 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
sort: item.sort
}))
const networkIdsForSyncing = (await database.categoryNetworkId.findAll({
where: {
familyId,
categoryId: {
[Sequelize.Op.in]: categoryIdsToSyncBaseData
}
},
attributes: [
'categoryId',
'networkItemId',
'hashedNetworkId'
],
transaction
})).map((item) => ({
categoryId: item.categoryId,
networkItemId: item.networkItemId,
hashedNetworkId: item.hashedNetworkId
}))
result.categoryBase = dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({
categoryId: item.categoryId,
childId: item.childId,
@@ -412,7 +431,13 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
mblMobile: item.minBatteryMobile,
mblCharging: item.minBatteryCharging,
tempBlockTime: item.temporarilyBlockedEndTime,
sort: item.sort
sort: item.sort,
networks: networkIdsForSyncing
.filter((network) => network.categoryId === item.categoryId)
.map((network) => ({
itemId: network.networkItemId,
hashedNetworkId: network.hashedNetworkId
}))
}))
}
+6
View File
@@ -112,6 +112,12 @@ export interface ServerUpdatedCategoryBaseData {
mblCharging: number
mblMobile: number
sort: number
networks: Array<ServerCategoryNetworkId>
}
export interface ServerCategoryNetworkId {
itemId: string
hashedNetworkId: string
}
export interface ServerUpdatedCategoryAssignedApps {