Zum Inhalt springen

Rollenmanagement

Anleitung zur Verwaltung von Rollen und Berechtigungen.

Erstelle eine Rollenkonfiguration in deinem Worker:

const ROLES = {
admin: ['users:read', 'users:write', 'posts:read', 'posts:write'],
moderator: ['posts:read', 'posts:write'],
user: ['posts:read']
};
// In KV speichern
await env.AUTH_KV.put(
`user_roles:${userId}`,
JSON.stringify({ roles: ['user', 'moderator'] })
);
async function hasPermission(userId, permission) {
const userRoles = await getUserRoles(userId);
for (const role of userRoles) {
const permissions = ROLES[role] || [];
if (permissions.includes(permission)) {
return true;
}
}
return false;
}
// Verwenden
if (await hasPermission(userId, 'posts:write')) {
// Erlaubt
}