Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | /**
* GraphQL operations access configuration
*
* Categories:
* - publicOperations: Available to everyone (no token required)
* - userOperations: For authenticated users (USER, BLOGGER, ADMIN)
* - bloggerOperations: For BLOGGER and ADMIN roles
* - adminOperations: Only for ADMIN role
*/
import type { UserRole } from '../../types/user';
interface OperationsConfig {
publicOperations: string[];
userOperations: string[];
bloggerOperations: string[];
adminOperations: string[];
}
export const operationsConfig: OperationsConfig = {
// Public operations - no auth required
publicOperations: [
// Recipe queries
'getRecipes',
'getRecipeById',
'getRecipesByTitle',
'getRecipesByUserName',
'getRecipesByUserId',
// User queries
'getUserById',
'getUserByUserName',
'getAllUser',
// Auth mutations
'createUser',
'loginUser',
'resetPassword',
'setNewPassword',
// Metadata
'getMetadataByType',
'getAllMetadata',
// Ratings
'getRatingsByRecipe',
],
// Authenticated user operations
userOperations: [
// Recipe operations
'createRecipe',
'editRecipe',
'deleteRecipe',
// User mutations
'updateUser',
'deleteUser',
'changePassword',
// Favorite recipes
'addToFavoriteRecipes',
'removeFromFavoriteRecipes',
'getFavoriteRecipes',
// Following
'getFollowing',
'followUser',
'unfollowUser',
// Metadata
'getMetadataByKey',
// Ratings
'rateRecipe',
'deleteRating',
],
// Blogger operations (own recipes management)
// Currently same as user operations, extend as needed
bloggerOperations: [],
// Admin operations
adminOperations: [
// User management
'deleteAllUsers',
'cleanUserRecipes',
// Content moderation
'deleteAllRecipes',
// Metadata management
'createMetadata',
'deleteMetadata',
],
};
/**
* Checks if an operation is public (no auth required)
*/
export const isPublicOperation = (operationName: string): boolean => {
return operationsConfig.publicOperations.includes(operationName);
};
/**
* Checks if a user can perform an operation based on their role
*/
export const canUserPerformOperation = (
operationName: string,
userRole?: UserRole,
): boolean => {
// Public operation - anyone can execute
if (isPublicOperation(operationName)) {
return true;
}
// If no user role, only public operations allowed
if (!userRole) {
return false;
}
// Admin can do everything
if (userRole === 'ADMIN') {
return true;
}
// Blogger operations
if (userRole === 'BLOGGER') {
return (
operationsConfig.userOperations.includes(operationName) ||
operationsConfig.bloggerOperations.includes(operationName)
);
}
// User operations
if (userRole === 'USER') {
return operationsConfig.userOperations.includes(operationName);
}
return false;
};
/**
* Returns which roles can access an operation
*/
export const getRequiredRolesForOperation = (
operationName: string,
): UserRole[] | 'PUBLIC' => {
if (isPublicOperation(operationName)) {
return 'PUBLIC';
}
if (operationsConfig.adminOperations.includes(operationName)) {
return ['ADMIN'];
}
if (operationsConfig.bloggerOperations.includes(operationName)) {
return ['ADMIN', 'BLOGGER'];
}
if (operationsConfig.userOperations.includes(operationName)) {
return ['ADMIN', 'BLOGGER', 'USER'];
}
// If not configured, it's protected (admin only)
return ['ADMIN'];
};
|