UNPKG

casbin

Version:

An authorization library that supports access control models like ACL, RBAC, ABAC in Node.JS

148 lines (147 loc) 4.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // Copyright 2018 The Casbin Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. const fs_1 = require("fs"); class Config { constructor() { this.data = new Map(); } /** * newConfig create an empty configuration representation from file. * * @param confName the path of the model file. * @return the constructor of Config. */ static newConfig(confName) { const config = new Config(); config.parse(confName); return config; } /** * newConfigFromText create an empty configuration representation from text. * * @param text the model text. * @return the constructor of Config. */ static newConfigFromText(text) { const config = new Config(); config.parseBuffer(Buffer.from(text)); return config; } /** * addConfig adds a new section->key:value to the configuration. */ addConfig(section, option, value) { if (section === '') { section = Config.DEFAULT_SECTION; } const hasKey = this.data.has(section); if (!hasKey) { this.data.set(section, new Map()); } const item = this.data.get(section); if (item) { item.set(option, value); return item.has(option); } else { return false; } } parse(path) { const buf = fs_1.readFileSync(path); this.parseBuffer(buf); } parseBuffer(buf) { let section = ''; const lines = buf.toString().split('\n'); lines.forEach((n, index) => { const line = n.trim(); if (!line) { return; } if (line.startsWith(Config.DEFAULT_COMMENT)) { return; } else if (line.startsWith(Config.DEFAULT_COMMENT_SEM)) { return; } else if (line.startsWith('[') && line.endsWith(']')) { section = line.substring(1, line.length - 1); } else { const equalIndex = line.indexOf('='); if (equalIndex === -1) { throw new Error(`parse the content error : line ${index + 1}`); } const key = line.substring(0, equalIndex); const value = line.substring(equalIndex + 1); this.addConfig(section, key.trim(), value.trim()); } }); } getBool(key) { return !!this.get(key); } getInt(key) { return Number.parseInt(this.get(key), 10); } getFloat(key) { return Number.parseFloat(this.get(key)); } getString(key) { return this.get(key); } getStrings(key) { const v = this.get(key); return v.split(','); } set(key, value) { if (!key) { throw new Error('key is empty'); } let section = ''; let option; const keys = key.toLowerCase().split('::'); if (keys.length >= 2) { section = keys[0]; option = keys[1]; } else { option = keys[0]; } this.addConfig(section, option, value); } get(key) { let section; let option; const keys = key.toLowerCase().split('::'); if (keys.length >= 2) { section = keys[0]; option = keys[1]; } else { section = Config.DEFAULT_SECTION; option = keys[0]; } const item = this.data.get(section); const itemChild = item && item.get(option); return itemChild ? itemChild : ''; } } Config.DEFAULT_SECTION = 'default'; Config.DEFAULT_COMMENT = '#'; Config.DEFAULT_COMMENT_SEM = ';'; exports.Config = Config;