refactor: migrate to PostgreSQL + Sequelize
This commit is contained in:
10
server/models/_relations.js
Normal file
10
server/models/_relations.js
Normal file
@@ -0,0 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Associate DB Model relations
|
||||
*/
|
||||
module.exports = db => {
|
||||
db.User.belongsToMany(db.Group, { through: 'UserGroups' })
|
||||
db.Group.hasMany(db.Right, { as: 'GroupRights' })
|
||||
db.File.belongsTo(db.Folder)
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
|
||||
/**
|
||||
* BruteForce schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var bruteForceSchema = Mongoose.Schema({
|
||||
_id: { type: String, index: 1 },
|
||||
data: {
|
||||
count: Number,
|
||||
lastRequest: Date,
|
||||
firstRequest: Date
|
||||
},
|
||||
expires: { type: Date, index: { expires: '1d' } }
|
||||
})
|
||||
|
||||
module.exports = Mongoose.model('Bruteforce', bruteForceSchema)
|
56
server/models/document.js
Normal file
56
server/models/document.js
Normal file
@@ -0,0 +1,56 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Document schema
|
||||
*/
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let documentSchema = sequelize.define('setting', {
|
||||
path: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
len: [2, 255]
|
||||
}
|
||||
},
|
||||
subtitle: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
parentPath: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
parentTitle: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true,
|
||||
defaultValue: ''
|
||||
},
|
||||
isDirectory: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
},
|
||||
isEntry: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true,
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['path']
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
return documentSchema
|
||||
}
|
@@ -1,42 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
|
||||
/**
|
||||
* Entry schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var entrySchema = Mongoose.Schema({
|
||||
_id: String,
|
||||
|
||||
title: {
|
||||
type: String,
|
||||
required: true,
|
||||
minlength: 2
|
||||
},
|
||||
subtitle: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
parentTitle: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
parentPath: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
isDirectory: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
isEntry: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
}, {
|
||||
timestamps: {}
|
||||
})
|
||||
|
||||
module.exports = Mongoose.model('Entry', entrySchema)
|
44
server/models/file.js
Normal file
44
server/models/file.js
Normal file
@@ -0,0 +1,44 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* File schema
|
||||
*/
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let fileSchema = sequelize.define('file', {
|
||||
category: {
|
||||
type: DataTypes.ENUM('binary', 'image'),
|
||||
allowNull: false,
|
||||
defaultValue: 'binary'
|
||||
},
|
||||
mime: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
defaultValue: 'application/octet-stream'
|
||||
},
|
||||
extra: {
|
||||
type: DataTypes.JSONB,
|
||||
allowNull: true
|
||||
},
|
||||
filename: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
basename: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
filesize: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
isInt: true,
|
||||
min: 0
|
||||
}
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true
|
||||
})
|
||||
|
||||
return fileSchema
|
||||
}
|
24
server/models/folder.js
Normal file
24
server/models/folder.js
Normal file
@@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Folder schema
|
||||
*/
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let folderSchema = sequelize.define('folder', {
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true,
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['name']
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
return folderSchema
|
||||
}
|
18
server/models/group.js
Normal file
18
server/models/group.js
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Group schema
|
||||
*/
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let groupSchema = sequelize.define('group', {
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true
|
||||
})
|
||||
|
||||
return groupSchema
|
||||
}
|
38
server/models/right.js
Normal file
38
server/models/right.js
Normal file
@@ -0,0 +1,38 @@
|
||||
'use strict'
|
||||
|
||||
/**
|
||||
* Right schema
|
||||
*/
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let rightSchema = sequelize.define('right', {
|
||||
path: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
role: {
|
||||
type: DataTypes.ENUM('read', 'write', 'manage'),
|
||||
allowNull: false,
|
||||
defaultValue: 'read'
|
||||
},
|
||||
exact: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
},
|
||||
allow: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
allowNull: false,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true,
|
||||
indexes: [
|
||||
{
|
||||
fields: ['path']
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
return rightSchema
|
||||
}
|
@@ -1,22 +1,28 @@
|
||||
'use strict'
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
|
||||
/**
|
||||
* Settings schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var settingSchema = Mongoose.Schema({
|
||||
key: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true
|
||||
},
|
||||
value: {
|
||||
type: String,
|
||||
required: true
|
||||
}
|
||||
}, { timestamps: {} })
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let settingSchema = sequelize.define('setting', {
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
config: {
|
||||
type: DataTypes.JSONB,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true,
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['key']
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
module.exports = Mongoose.model('Setting', settingSchema)
|
||||
return settingSchema
|
||||
}
|
||||
|
@@ -1,46 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
|
||||
/**
|
||||
* Upload File schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var uplFileSchema = Mongoose.Schema({
|
||||
|
||||
_id: String,
|
||||
|
||||
category: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: 'binary'
|
||||
},
|
||||
mime: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: 'application/octet-stream'
|
||||
},
|
||||
extra: {
|
||||
type: Object
|
||||
},
|
||||
folder: {
|
||||
type: String,
|
||||
ref: 'UplFolder'
|
||||
},
|
||||
filename: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
basename: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
filesize: {
|
||||
type: Number,
|
||||
required: true
|
||||
}
|
||||
|
||||
}, { timestamps: {} })
|
||||
|
||||
module.exports = Mongoose.model('UplFile', uplFileSchema)
|
@@ -1,21 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
|
||||
/**
|
||||
* Upload Folder schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var uplFolderSchema = Mongoose.Schema({
|
||||
|
||||
_id: String,
|
||||
|
||||
name: {
|
||||
type: String,
|
||||
index: true
|
||||
}
|
||||
|
||||
}, { timestamps: {} })
|
||||
|
||||
module.exports = Mongoose.model('UplFolder', uplFolderSchema)
|
@@ -1,109 +1,113 @@
|
||||
'use strict'
|
||||
|
||||
/* global db, lang */
|
||||
/* global wiki */
|
||||
|
||||
const Mongoose = require('mongoose')
|
||||
const Promise = require('bluebird')
|
||||
const bcrypt = require('bcryptjs-then')
|
||||
const _ = require('lodash')
|
||||
|
||||
/**
|
||||
* Users schema
|
||||
*
|
||||
* @type {<Mongoose.Schema>}
|
||||
*/
|
||||
var userSchema = Mongoose.Schema({
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
let userSchema = sequelize.define('user', {
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
validate: {
|
||||
isEmail: true
|
||||
}
|
||||
},
|
||||
provider: {
|
||||
type: DataTypes.ENUM(wiki.data.authProviders),
|
||||
allowNull: false
|
||||
},
|
||||
providerId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
password: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: true
|
||||
},
|
||||
role: {
|
||||
type: DataTypes.ENUM('admin', 'user', 'guest'),
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
timestamps: true,
|
||||
version: true,
|
||||
indexes: [
|
||||
{
|
||||
unique: true,
|
||||
fields: ['provider', 'email']
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
index: true
|
||||
},
|
||||
|
||||
provider: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
|
||||
providerId: {
|
||||
type: String
|
||||
},
|
||||
|
||||
password: {
|
||||
type: String
|
||||
},
|
||||
|
||||
name: {
|
||||
type: String
|
||||
},
|
||||
|
||||
rights: [{
|
||||
role: String,
|
||||
path: String,
|
||||
exact: Boolean,
|
||||
deny: Boolean
|
||||
}]
|
||||
|
||||
}, { timestamps: {} })
|
||||
|
||||
userSchema.statics.processProfile = (profile) => {
|
||||
let primaryEmail = ''
|
||||
if (_.isArray(profile.emails)) {
|
||||
let e = _.find(profile.emails, ['primary', true])
|
||||
primaryEmail = (e) ? e.value : _.first(profile.emails).value
|
||||
} else if (_.isString(profile.email) && profile.email.length > 5) {
|
||||
primaryEmail = profile.email
|
||||
} else if (_.isString(profile.mail) && profile.mail.length > 5) {
|
||||
primaryEmail = profile.mail
|
||||
} else if (profile.user && profile.user.email && profile.user.email.length > 5) {
|
||||
primaryEmail = profile.user.email
|
||||
} else {
|
||||
return Promise.reject(new Error(lang.t('auth:errors.invaliduseremail')))
|
||||
userSchema.prototype.validatePassword = function (rawPwd) {
|
||||
return bcrypt.compare(rawPwd, this.password).then((isValid) => {
|
||||
return (isValid) ? true : Promise.reject(new Error(wiki.lang.t('auth:errors:invalidlogin')))
|
||||
})
|
||||
}
|
||||
|
||||
profile.provider = _.lowerCase(profile.provider)
|
||||
primaryEmail = _.toLower(primaryEmail)
|
||||
|
||||
return db.User.findOneAndUpdate({
|
||||
email: primaryEmail,
|
||||
provider: profile.provider
|
||||
}, {
|
||||
email: primaryEmail,
|
||||
provider: profile.provider,
|
||||
providerId: profile.id,
|
||||
name: profile.displayName || _.split(primaryEmail, '@')[0]
|
||||
}, {
|
||||
new: true
|
||||
}).then((user) => {
|
||||
// Handle unregistered accounts
|
||||
if (!user && profile.provider !== 'local' && (appconfig.auth.defaultReadAccess || profile.provider === 'ldap' || profile.provider === 'azure')) {
|
||||
let nUsr = {
|
||||
email: primaryEmail,
|
||||
provider: profile.provider,
|
||||
providerId: profile.id,
|
||||
password: '',
|
||||
name: profile.displayName || profile.name || profile.cn,
|
||||
rights: [{
|
||||
role: 'read',
|
||||
path: '/',
|
||||
exact: false,
|
||||
deny: false
|
||||
}]
|
||||
}
|
||||
return db.User.create(nUsr)
|
||||
userSchema.processProfile = (profile) => {
|
||||
let primaryEmail = ''
|
||||
if (_.isArray(profile.emails)) {
|
||||
let e = _.find(profile.emails, ['primary', true])
|
||||
primaryEmail = (e) ? e.value : _.first(profile.emails).value
|
||||
} else if (_.isString(profile.email) && profile.email.length > 5) {
|
||||
primaryEmail = profile.email
|
||||
} else if (_.isString(profile.mail) && profile.mail.length > 5) {
|
||||
primaryEmail = profile.mail
|
||||
} else if (profile.user && profile.user.email && profile.user.email.length > 5) {
|
||||
primaryEmail = profile.user.email
|
||||
} else {
|
||||
return Promise.reject(new Error(wiki.lang.t('auth:errors.invaliduseremail')))
|
||||
}
|
||||
return user || Promise.reject(new Error(lang.t('auth:errors:notyetauthorized')))
|
||||
})
|
||||
}
|
||||
|
||||
userSchema.statics.hashPassword = (rawPwd) => {
|
||||
return bcrypt.hash(rawPwd)
|
||||
}
|
||||
profile.provider = _.lowerCase(profile.provider)
|
||||
primaryEmail = _.toLower(primaryEmail)
|
||||
|
||||
userSchema.methods.validatePassword = function (rawPwd) {
|
||||
return bcrypt.compare(rawPwd, this.password).then((isValid) => {
|
||||
return (isValid) ? true : Promise.reject(new Error(lang.t('auth:errors:invalidlogin')))
|
||||
})
|
||||
}
|
||||
return wiki.db.User.findOneAndUpdate({
|
||||
email: primaryEmail,
|
||||
provider: profile.provider
|
||||
}, {
|
||||
email: primaryEmail,
|
||||
provider: profile.provider,
|
||||
providerId: profile.id,
|
||||
name: profile.displayName || _.split(primaryEmail, '@')[0]
|
||||
}, {
|
||||
new: true
|
||||
}).then((user) => {
|
||||
// Handle unregistered accounts
|
||||
if (!user && profile.provider !== 'local' && (appconfig.auth.defaultReadAccess || profile.provider === 'ldap' || profile.provider === 'azure')) {
|
||||
let nUsr = {
|
||||
email: primaryEmail,
|
||||
provider: profile.provider,
|
||||
providerId: profile.id,
|
||||
password: '',
|
||||
name: profile.displayName || profile.name || profile.cn,
|
||||
rights: [{
|
||||
role: 'read',
|
||||
path: '/',
|
||||
exact: false,
|
||||
deny: false
|
||||
}]
|
||||
}
|
||||
return db.User.create(nUsr)
|
||||
}
|
||||
return user || Promise.reject(new Error(wiki.lang.t('auth:errors:notyetauthorized')))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = Mongoose.model('User', userSchema)
|
||||
userSchema.hashPassword = (rawPwd) => {
|
||||
return bcrypt.hash(rawPwd)
|
||||
}
|
||||
|
||||
return userSchema
|
||||
}
|
||||
|
Reference in New Issue
Block a user