2019-09-08 01:56:29 +00:00
|
|
|
const self = {}
|
2018-10-13 11:06:58 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.permissions = {
|
|
|
|
user: 0, // Upload & delete own files, create & delete albums
|
|
|
|
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
|
2018-10-13 11:06:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-09-08 01:56:29 +00:00
|
|
|
if (user.username === 'root')
|
|
|
|
return true
|
|
|
|
|
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
|