mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add limit login category support
This commit is contained in:
@@ -56,7 +56,8 @@ import {
|
||||
UpdateParentBlockedTimesAction,
|
||||
UpdateParentNotificationFlagsAction,
|
||||
UpdateTimelimitRuleAction,
|
||||
UpdateUserFlagsAction
|
||||
UpdateUserFlagsAction,
|
||||
UpdateUserLimitLoginCategory
|
||||
} from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { dispatchAddCategoryApps } from './addcategoryapps'
|
||||
@@ -99,6 +100,7 @@ import { dispatchUpdateParentBlockedTimes } from './updateparentblockedtimes'
|
||||
import { dispatchUpdateParentNotificationFlags } from './updateparentnotificationflags'
|
||||
import { dispatchUpdateTimelimitRule } from './updatetimelimitrule'
|
||||
import { dispatchUpdateUserFlagsAction } from './updateuserflags'
|
||||
import { dispatchUpdateUserLimitLoginCategoryAction } from './updateuserlimitlogincategory'
|
||||
|
||||
export const dispatchParentAction = async ({ action, cache, parentUserId, sourceDeviceId }: {
|
||||
action: ParentAction
|
||||
@@ -183,9 +185,11 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
|
||||
} else if (action instanceof ResetParentBlockedTimesAction) {
|
||||
await dispatchResetParentBlockedTimes({ action, cache })
|
||||
} else if (action instanceof UpdateParentBlockedTimesAction) {
|
||||
await dispatchUpdateParentBlockedTimes({ action, cache })
|
||||
await dispatchUpdateParentBlockedTimes({ action, cache, parentUserId })
|
||||
} else if (action instanceof UpdateUserFlagsAction) {
|
||||
await dispatchUpdateUserFlagsAction({ action, cache })
|
||||
} else if (action instanceof UpdateUserLimitLoginCategory) {
|
||||
await dispatchUpdateUserLimitLoginCategoryAction({ action, cache, parentUserId })
|
||||
} else {
|
||||
throw new Error('unsupported action type')
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
import { createHash } from 'crypto'
|
||||
import { InternalServerError } from 'http-errors'
|
||||
import { difference } from 'lodash'
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { RemoveUserAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
@@ -71,6 +72,29 @@ export async function dispatchRemoveUser ({ action, cache, parentUserId }: {
|
||||
throw new Error('this user is the last one with a linked mail address')
|
||||
}
|
||||
}
|
||||
|
||||
const usersWithLimitLoginCategories = (await cache.database.userLimitLoginCategory.findAll({
|
||||
transaction: cache.transaction,
|
||||
where: {
|
||||
familyId: cache.familyId
|
||||
},
|
||||
attributes: ['userId']
|
||||
})).map((item) => item.userId)
|
||||
|
||||
const allParentUserIds = (await cache.database.user.findAll({
|
||||
transaction: cache.transaction,
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
type: 'parent'
|
||||
},
|
||||
attributes: ['userId']
|
||||
})).map((item) => item.userId)
|
||||
|
||||
const allOtherParentUserIds = allParentUserIds.filter((item) => item !== action.userId)
|
||||
|
||||
if (difference(allOtherParentUserIds, usersWithLimitLoginCategories).length === 0) {
|
||||
throw new Error('can not delete the last user without limit login category')
|
||||
}
|
||||
}
|
||||
|
||||
if (user.type === 'child') {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* 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
|
||||
@@ -18,10 +18,15 @@
|
||||
import { UpdateParentBlockedTimesAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export async function dispatchUpdateParentBlockedTimes ({ action, cache }: {
|
||||
export async function dispatchUpdateParentBlockedTimes ({ action, cache, parentUserId }: {
|
||||
action: UpdateParentBlockedTimesAction
|
||||
cache: Cache
|
||||
parentUserId: string
|
||||
}) {
|
||||
if (parentUserId !== action.parentId && action.blockedTimes !== '') {
|
||||
throw new Error('only a parent itself can add limits')
|
||||
}
|
||||
|
||||
const [affectedRows] = await cache.database.user.update({
|
||||
blockedTimes: action.blockedTimes
|
||||
}, {
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 { UpdateUserLimitLoginCategory } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export async function dispatchUpdateUserLimitLoginCategoryAction ({ action, cache, parentUserId }: {
|
||||
action: UpdateUserLimitLoginCategory
|
||||
cache: Cache
|
||||
parentUserId: string
|
||||
}) {
|
||||
const userEntry = await cache.database.user.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
userId: action.userId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (!userEntry) {
|
||||
throw new Error('user not found')
|
||||
}
|
||||
|
||||
if (userEntry.type !== 'parent') {
|
||||
throw new Error('user must be a parent')
|
||||
}
|
||||
|
||||
if (action.categoryId !== undefined && parentUserId !== action.userId) {
|
||||
throw new Error('only the user itself can add a limit')
|
||||
}
|
||||
|
||||
await cache.database.userLimitLoginCategory.destroy({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
userId: action.userId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (action.categoryId !== undefined) {
|
||||
const categoryEntry = await cache.database.category.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: action.categoryId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (!categoryEntry) {
|
||||
throw new Error('category must exist')
|
||||
}
|
||||
|
||||
await cache.database.userLimitLoginCategory.create({
|
||||
familyId: cache.familyId,
|
||||
userId: action.userId,
|
||||
categoryId: action.categoryId
|
||||
}, {
|
||||
transaction: cache.transaction
|
||||
})
|
||||
}
|
||||
|
||||
cache.invalidiateUserList = true
|
||||
}
|
||||
@@ -146,6 +146,30 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
flags: item.flags
|
||||
}))
|
||||
|
||||
const limitLoginCategories = (await database.userLimitLoginCategory.findAll({
|
||||
where: {
|
||||
familyId
|
||||
},
|
||||
attributes: [
|
||||
'userId',
|
||||
'categoryId'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
userId: item.userId,
|
||||
categoryId: item.categoryId
|
||||
}))
|
||||
|
||||
const getLimitLoginCategory = (userId: string) => {
|
||||
const item = limitLoginCategories.find((item) => item.userId === userId)
|
||||
|
||||
if (item) {
|
||||
return item.categoryId
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
result.users = {
|
||||
version: familyEntry.userListVersion,
|
||||
data: users.map((item) => ({
|
||||
@@ -162,7 +186,8 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
relaxPrimaryDevice: item.relaxPrimaryDeviceRule,
|
||||
mailNotificationFlags: item.mailNotificationFlags,
|
||||
blockedTimes: item.blockedTimes,
|
||||
flags: parseInt(item.flags, 10)
|
||||
flags: parseInt(item.flags, 10),
|
||||
llc: getLimitLoginCategory(item.userId)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user