/*
* 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 .
*/
import * as Sequelize from 'sequelize'
import { optionalPasswordRegex, optionalSaltRegex } from '../util/password'
import { booleanColumn, createEnumColumn, familyIdColumn, idWithinFamilyColumn, labelColumn, optionalIdWithinFamilyColumn, timestampColumn } from './columns'
import { SequelizeAttributes } from './types'
export interface UserAttributesVersion1 {
familyId: string
userId: string
name: string
passwordHash: string
secondPasswordHash: string
secondPasswordSalt: string
type: 'parent' | 'child'
mail: string
timeZone: string
disableTimelimitsUntil: string
// empty = unset; can contain an invalid device id or the id of an device which is not used by this user
// in this case, it should be treated like unset
currentDevice: string
}
export interface UserAttributesVersion2 {
categoryForNotAssignedApps: string
}
export interface UserAttributesVersion3 {
relaxPrimaryDeviceRule: boolean
}
export interface UserAttributesVersion4 {
// 1: manipulation warnings
mailNotificationFlags: number
}
export type UserAttributes = UserAttributesVersion1 & UserAttributesVersion2 &
UserAttributesVersion3 & UserAttributesVersion4
export type UserInstance = Sequelize.Instance & UserAttributes
export type UserModel = Sequelize.Model
export const attributesVersion1: SequelizeAttributes = {
familyId: {
...familyIdColumn,
primaryKey: true
},
userId: {
...idWithinFamilyColumn,
primaryKey: true
},
name: { ...labelColumn },
passwordHash: {
type: Sequelize.STRING,
allowNull: false,
validate: {
is: optionalPasswordRegex
}
},
secondPasswordHash: {
type: Sequelize.STRING,
allowNull: false,
validate: {
is: optionalPasswordRegex
}
},
secondPasswordSalt: {
type: Sequelize.STRING,
allowNull: false,
validate: {
is: optionalSaltRegex
}
},
type: createEnumColumn(['parent', 'child']),
mail: {
type: Sequelize.STRING,
allowNull: false
},
timeZone: { ...labelColumn },
disableTimelimitsUntil: { ...timestampColumn },
currentDevice: { ...optionalIdWithinFamilyColumn }
}
export const attributesVersion2: SequelizeAttributes = {
categoryForNotAssignedApps: {
...optionalIdWithinFamilyColumn,
defaultValue: ''
}
}
export const attributesVersion3: SequelizeAttributes = {
relaxPrimaryDeviceRule: {
...booleanColumn,
defaultValue: false
}
}
export const attributesVersion4: SequelizeAttributes = {
mailNotificationFlags: {
type: Sequelize.INTEGER,
defaultValue: 0,
validate: {
min: 0,
max: 1
}
}
}
export const attributes: SequelizeAttributes = {
...attributesVersion1,
...attributesVersion2,
...attributesVersion3,
...attributesVersion4
}
export const createUserModel = (sequelize: Sequelize.Sequelize): UserModel => sequelize.define('User', attributes)