1
0
Fork 0
mirror of synced 2024-07-19 13:25:42 +12:00
appwrite/public/scripts/permissions-matrix.js

138 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-08-13 21:07:07 +12:00
(function (window) {
2022-08-10 22:23:53 +12:00
document.addEventListener('alpine:init', () => {
Alpine.data('permissionsMatrix', () => ({
permissions: [],
rawPermissions: [],
load(permissions) {
2022-08-13 18:33:11 +12:00
if (permissions === undefined) {
return;
}
2022-08-13 21:07:07 +12:00
this.rawPermissions = permissions;
permissions.map(p => {
2022-08-13 21:07:07 +12:00
let {type, role} = this.parsePermission(p);
type = this.parseInputPermission(type);
let index = -1;
let existing = this.permissions.find((p, idx) => {
if (p.role === role) {
index = idx;
return true;
}
})
if (existing === undefined) {
this.permissions.push({
role,
[type]: true,
});
}
if (index !== -1) {
existing[type] = true;
this.permissions[index] = existing;
}
});
},
2022-08-13 21:07:07 +12:00
addPermission(formId, role, permissions) {
if (!this.validate(formId, role, permissions)) {
2022-08-13 21:07:07 +12:00
return;
}
Object.entries(permissions).forEach(entry => {
let [type, enabled] = entry;
type = this.parseOutputPermission(type);
if (enabled) {
2022-08-15 13:04:14 +12:00
this.rawPermissions.push(this.buildPermission(type, role));
2022-08-13 21:07:07 +12:00
}
});
this.permissions.push({
role,
2022-08-13 21:07:07 +12:00
...permissions,
});
this.reset();
},
updatePermission(index) {
// Because the x-model does not update before the click event,
// we setTimeout to give Alpine enough time to update the model.
setTimeout(() => {
const permission = this.permissions[index];
2022-08-13 21:07:07 +12:00
Object.keys(permission).forEach(key => {
if (key === 'role') {
2022-08-13 21:07:07 +12:00
return;
}
const parsedKey = this.parseOutputPermission(key);
2022-08-15 13:04:14 +12:00
const permissionString = this.buildPermission(parsedKey, permission.role);
if (permission[key]) {
2022-08-15 13:04:14 +12:00
if (!this.rawPermissions.includes(permissionString)) {
this.rawPermissions.push(permissionString);
}
} else {
this.rawPermissions = this.rawPermissions.filter(p => {
2022-08-15 13:04:14 +12:00
return !p.includes(permissionString);
});
}
2022-08-13 21:07:07 +12:00
});
});
2022-08-10 22:23:53 +12:00
},
removePermission(index) {
let row = this.permissions.splice(index, 1);
2022-08-12 12:26:58 +12:00
if (row.length === 1) {
this.rawPermissions = this.rawPermissions.filter(p => !p.includes(row[0].role));
}
},
parsePermission(permission) {
let parts = permission.split('(');
let type = parts[0];
2022-08-15 13:04:14 +12:00
let role = parts[1]
.replace(')', '')
.replace(' ', '')
.replaceAll('"', '');
2022-08-13 21:07:07 +12:00
return {type, role};
},
2022-08-15 13:04:14 +12:00
buildPermission(type, role) {
return `${type}("${role}")`
},
parseInputPermission(key) {
// Can't bind to a property named delete
if (key === 'delete') {
return 'xdelete';
}
return key;
},
parseOutputPermission(key) {
// Can't bind to a property named delete
if (key === 'xdelete') {
return 'delete';
}
return key;
},
validate(formId, role, permissions) {
const form = document.getElementById(formId);
const input = document.getElementById(`${formId}Input`);
input.setCustomValidity('');
if (!Object.values(permissions).some(p => p)) {
input.setCustomValidity('No permissions selected');
}
if (this.permissions.some(p => p.role === role)) {
input.setCustomValidity('Role entry already exists');
}
return form.reportValidity();
}
}));
Alpine.data('permissionsRow', () => ({
role: '',
read: false,
create: false,
update: false,
xdelete: false,
reset() {
this.role = '';
this.read = this.create = this.update = this.xdelete = false;
2022-08-10 22:23:53 +12:00
}
}));
2022-08-10 22:23:53 +12:00
});
})(window);