NewPermissionsParserFromValue creates a PermissionsParser from a frontmatter value (any type)
(permissionsValue any)
| 242 | |
| 243 | // NewPermissionsParserFromValue creates a PermissionsParser from a frontmatter value (any type) |
| 244 | func NewPermissionsParserFromValue(permissionsValue any) *PermissionsParser { |
| 245 | parser := &PermissionsParser{ |
| 246 | parsedPerms: make(map[string]string), |
| 247 | } |
| 248 | |
| 249 | if permissionsValue == nil { |
| 250 | return parser |
| 251 | } |
| 252 | |
| 253 | // Handle string shorthand (read-all, write-all, etc.) |
| 254 | if strValue, ok := permissionsValue.(string); ok { |
| 255 | parser.isShorthand = true |
| 256 | parser.shorthandValue = strValue |
| 257 | return parser |
| 258 | } |
| 259 | |
| 260 | // Handle map format |
| 261 | if mapValue, ok := permissionsValue.(map[string]any); ok { |
| 262 | // Handle 'all' key specially |
| 263 | if allValue, exists := mapValue["all"]; exists { |
| 264 | if strValue, ok := allValue.(string); ok { |
| 265 | if strValue == "write" { |
| 266 | // all: write is not allowed, return empty parser |
| 267 | return parser |
| 268 | } |
| 269 | if strValue == "read" { |
| 270 | // Check that no other permissions are set to 'none' when all: read is used |
| 271 | for key, value := range mapValue { |
| 272 | if key != "all" { |
| 273 | if permValue, ok := value.(string); ok && permValue == "none" { |
| 274 | // all: read cannot be combined with : none, return empty parser |
| 275 | return parser |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | parser.hasAll = true |
| 280 | parser.allLevel = strValue |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | for key, value := range mapValue { |
| 286 | if strValue, ok := value.(string); ok { |
| 287 | parser.parsedPerms[key] = strValue |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return parser |
| 293 | } |
| 294 | |
| 295 | // ToPermissions converts a PermissionsParser to a Permissions object |
| 296 | func (p *PermissionsParser) ToPermissions() *Permissions { |
no outgoing calls