mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 { ChildChangePasswordAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export const dispatchChildChangePassword = async ({ action, childUserId, cache }: {
|
||||
action: ChildChangePasswordAction
|
||||
childUserId: string
|
||||
cache: Cache
|
||||
}) => {
|
||||
const childEntry = await cache.database.user.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
userId: childUserId,
|
||||
type: 'child'
|
||||
},
|
||||
transaction: cache.transaction,
|
||||
lock: Sequelize.Transaction.LOCK.UPDATE
|
||||
})
|
||||
|
||||
if (!childEntry) {
|
||||
throw new Error('child entry not found')
|
||||
}
|
||||
|
||||
childEntry.passwordHash = action.password.hash
|
||||
childEntry.secondPasswordSalt = action.password.secondSalt
|
||||
childEntry.secondPasswordHash = action.password.secondHash
|
||||
|
||||
await childEntry.save({ transaction: cache.transaction })
|
||||
|
||||
cache.getSecondPasswordHashOfChild.cache.clear()
|
||||
cache.invalidiateUserList = true
|
||||
cache.areChangesImportant = true
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 { ChildSignInAction, SetDeviceUserAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { dispatchSetDeviceUser } from '../dispatch-parent-action/setdeviceuser'
|
||||
|
||||
export const dispatchChildSignIn = async ({ action, deviceId, childUserId, cache }: {
|
||||
action: ChildSignInAction
|
||||
deviceId: string
|
||||
childUserId: string
|
||||
cache: Cache
|
||||
}) => {
|
||||
if (!cache.hasFullVersion) {
|
||||
throw new Error('action requires full version')
|
||||
}
|
||||
|
||||
await dispatchSetDeviceUser({
|
||||
action: new SetDeviceUserAction({
|
||||
deviceId,
|
||||
userId: childUserId
|
||||
}),
|
||||
cache
|
||||
})
|
||||
|
||||
const userEntryUnsafe = await cache.database.user.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
type: 'child',
|
||||
userId: childUserId
|
||||
},
|
||||
transaction: cache.transaction,
|
||||
attributes: [
|
||||
'currentDevice'
|
||||
]
|
||||
})
|
||||
|
||||
if (!userEntryUnsafe) {
|
||||
throw new Error('illegal state')
|
||||
}
|
||||
|
||||
if (userEntryUnsafe.currentDevice === deviceId) {
|
||||
// unassign to prevent way aroundprimary device rule
|
||||
|
||||
await cache.database.user.update({
|
||||
currentDevice: ''
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
type: 'child',
|
||||
userId: childUserId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
cache.invalidiateUserList = true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 {
|
||||
ChildAction,
|
||||
ChildChangePasswordAction,
|
||||
ChildSignInAction
|
||||
} from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { dispatchChildChangePassword } from './childchangepassword'
|
||||
import { dispatchChildSignIn } from './childsignin'
|
||||
|
||||
export const dispatchChildAction = async ({ action, deviceId, childUserId, cache }: {
|
||||
action: ChildAction
|
||||
deviceId: string
|
||||
childUserId: string
|
||||
cache: Cache
|
||||
}) => {
|
||||
if (action instanceof ChildChangePasswordAction) {
|
||||
await dispatchChildChangePassword({ action, childUserId, cache })
|
||||
} else if (action instanceof ChildSignInAction) {
|
||||
await dispatchChildSignIn({ action, childUserId, deviceId, cache })
|
||||
} else {
|
||||
throw new Error('unsupported action type')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user