2019-09-08 01:56:29 +00:00
|
|
|
const self = {}
|
2018-10-13 11:06:58 +00:00
|
|
|
|
2021-01-08 19:51:23 +00:00
|
|
|
self.permissions = Object.freeze({
|
2019-09-08 01:56:29 +00:00
|
|
|
user: 0, // Upload & delete own files, create & delete albums
|
2022-05-06 19:17:31 +00:00
|
|
|
vip: 5, // If used with "retentionPeriods" in config, may have additional retention period options
|
|
|
|
vvip: 10, // If used with "retentionPeriods" in config, may have additional retention period options
|
2019-09-08 01:56:29 +00:00
|
|
|
moderator: 50, // Delete other user's files
|
|
|
|
admin: 80, // Manage users (disable accounts) & create moderators
|
|
|
|
superadmin: 100 // Create admins
|
|
|
|
// Groups will inherit permissions from groups which have lower value
|
2022-05-06 18:36:38 +00:00
|
|
|
// You should NOT have multiple groups with exact same values
|
2021-01-08 19:51:23 +00:00
|
|
|
})
|
2018-10-13 11:06:58 +00:00
|
|
|
|
2022-05-06 18:36:38 +00:00
|
|
|
self.keys = Object.freeze(Object.keys(self.permissions))
|
|
|
|
|
|
|
|
self.group = user => {
|
|
|
|
// root bypass
|
|
|
|
if (user.username === 'root') return 'superadmin'
|
|
|
|
for (const key of self.keys) {
|
|
|
|
if (user.permission === self.permissions[key]) {
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2019-11-05 20:35:04 +00:00
|
|
|
// returns true if user is in the group OR higher
|
2019-09-08 01:56:29 +00:00
|
|
|
self.is = (user, group) => {
|
2018-10-13 11:06:58 +00:00
|
|
|
// root bypass
|
2020-10-30 18:12:09 +00:00
|
|
|
if (user.username === 'root') return true
|
2021-01-08 19:51:23 +00:00
|
|
|
if (typeof group !== 'string' || !group) return false
|
2018-10-13 11:06:58 +00:00
|
|
|
const permission = user.permission || 0
|
2019-09-08 01:56:29 +00:00
|
|
|
return permission >= self.permissions[group]
|
2018-10-13 11:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.higher = (user, target) => {
|
2018-10-13 11:06:58 +00:00
|
|
|
const userPermission = user.permission || 0
|
|
|
|
const targetPermission = target.permission || 0
|
|
|
|
return userPermission > targetPermission
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.mapPermissions = user => {
|
2018-10-13 11:06:58 +00:00
|
|
|
const map = {}
|
2019-09-08 01:56:29 +00:00
|
|
|
Object.keys(self.permissions).forEach(group => {
|
|
|
|
map[group] = self.is(user, group)
|
2018-10-13 11:06:58 +00:00
|
|
|
})
|
|
|
|
return map
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
module.exports = self
|