示例#1
0
        /// <summary>
        /// Get DataType from Name
        /// </summary>
        /// <param name="typeName">Name of the type.</param>
        /// <returns></returns>
        public DataType FromName(string typeName, bool generateEmpty = false)
        {
            DataType dt;

            if (this.Types.TryGetValue(DataTypeFactory.NormalizeTypeName(typeName), out dt))
            {
                return(dt);
            }

            if (generateEmpty)
            {
                return(this.FromJson(typeName, "{}"));
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Create new instance of NancyBlack Database - the instance is designed to be used per request
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ctx"></param>
        public NancyBlackDatabase(SQLiteConnection db, NancyContext ctx)
        {
            this.CurrentContext = ctx;

            _db       = db;
            _dataType = DataTypeFactory.GetForDatabase(db);

            _db.InstanceCreated += (object obj) => {
                var type = obj.GetType();

                Action <NancyBlackDatabase, dynamic> processor;
                if (NancyBlackDatabase.ObjectPostProcessors.TryGetValue(type, out processor))
                {
                    processor(this, obj);
                }
            };
        }
示例#3
0
        /// <summary>
        /// Gets a DataType Factory for given database.
        /// Database with same connection string will share the instance of DataTypeFactory
        /// </summary>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public static DataTypeFactory GetForDatabase(SQLiteConnection db)
        {
            var key = "DataTypeFactory-" + db.DatabasePath;

            var cached = MemoryCache.Default.Get(key);

            if (cached != null)
            {
                return((DataTypeFactory)cached);
            }

            var created = new DataTypeFactory(db);

            created.Initialize();

            MemoryCache.Default.Add(key, created, DateTimeOffset.MaxValue);

            return(created);
        }
示例#4
0
        /// <summary>
        /// Gets a DataType Factory for given database.
        /// Database with same connection string will share the instance of DataTypeFactory
        /// </summary>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public static DataTypeFactory GetForDatabase(SQLiteConnection db)
        {
            var key = "DataTypeFactory-" + db.DatabasePath;

            var cached = MemoryCache.Default.Get(key);

            if (cached != null)
            {
                return((DataTypeFactory)cached);
            }

            var created = new DataTypeFactory(db);

            // Triggers migration
            created.RegisteredTypes.ToList();

            MemoryCache.Default.Add(key, created, DateTimeOffset.MaxValue);

            return(created);
        }
示例#5
0
        /// <summary>
        /// Set Table Security
        /// </summary>
        /// <param name="type"></param>
        /// <param name="create"></param>
        /// <param name="update"></param>
        /// <param name="delete"></param>
        /// <param name="query"></param>
        public static void SetTableSec(NancyContext context, string name, bool create, bool update, bool delete, bool query)
        {
            var setting = new
            {
                name   = name,
                create = new
                {
                    enable = create,
                },
                update = new
                {
                    enable = update,
                },
                delete = new
                {
                    enable = delete,
                },
                query = new
                {
                    enable = query
                }
            };

            var settingJObject = JObject.FromObject(setting);

            var sitesettings = context.GetSiteSettings();

            if (sitesettings.tablesec == null)
            {
                sitesettings.tablesec = new JObject();
            }

            sitesettings.tablesec[DataTypeFactory.NormalizeTypeName(name)] = settingJObject;

            AdminModule.WriteSiteSettings(context, sitesettings);
        }
示例#6
0
        /// <summary>
        /// Determine whether current user has permission to perform action on the given type
        /// </summary>
        /// <param name="context"></param>
        /// <param name="typeName"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static bool HasPermission(NancyContext context, string typeName, string action)
        {
            NcbUser user = context.CurrentUser as NcbUser;

            if (user.HasClaim("admin"))
            {
                return(true);
            }

            var sitesettings = context.GetSiteSettings();

            if (sitesettings.tablesec == null)
            {
                return(true); // tablesec was not configured, default to allow
            }

            if (sitesettings.tablesec.enable == false)
            {
                return(true); // tablesec was turned off
            }

            var     normalizedName = DataTypeFactory.NormalizeTypeName(typeName);
            JObject permission     = sitesettings.tablesec[normalizedName];

            if (permission == null)
            {
                return(false);
            }

            if (permission[action] == null)
            {
                return(false);
            }

            return((bool)permission[action]["enable"] == true);
        }
示例#7
0
        /// <summary>
        /// Gets a DataType Factory for given database.
        /// Database with same connection string will share the instance of DataTypeFactory
        /// </summary>
        /// <param name="db">The database.</param>
        /// <returns></returns>
        public static DataTypeFactory GetForDatabase(SQLiteConnection db)
        {
            var key = "DataTypeFactory-" + db.DatabasePath;

            var cached = MemoryCache.Default.Get(key);
            if (cached != null)
            {
                return (DataTypeFactory)cached;
            }

            var created = new DataTypeFactory(db);
            MemoryCache.Default.Add(key, created, DateTimeOffset.MaxValue );

            return created;
        }
示例#8
0
 public NancyBlackDatabase(SQLiteConnection db)
 {
     _db       = db;
     _dataType = DataTypeFactory.GetForDatabase(db);
 }