江南白衣

陌上發花,可以緩緩醉矣
忍把浮名,換了淺斟低唱
我不是聖賢豪士,我衹有一腔熱血
posts - 114, comments - 442, trackbacks - 14, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

  大家都知道,微软企业库中的Security Application Block是把权限规则写在配置文件中的(app.config,web.config),并没有提供存在数据库的实现形式。我去年就向企业库项目组的人建议在SAB中加入这一实现形式,遗憾的是,直到现在的3.1版本,还是没有实现这一功能。
   还好GotDotNet上有人提供了这一功能的扩展:Database Rules Provider ,不过这个扩展没有权限操作的功能(添加,删除,修改权限。。。),不方便大家的日常使用,所以我就对其作了一些修改,加入了权限操作功能。现在放出来与大家共享:)
  项目下载:Database Authorization Provider.rar
  DbRulesManager.cs:
 

  1using System;
  2using System.Data;
  3using System.Data.Common;
  4using System.Data.SqlClient;
  5using System.Configuration;
  6using System.Collections.Generic;
  7using System.Security.Principal;
  8using System.Web.Security;
  9
 10using Microsoft.Practices.ObjectBuilder;
 11using Microsoft.Practices.EnterpriseLibrary.Security;
 12//using Microsoft.Practices.EnterpriseLibrary.Security.Authorization;
 13using Microsoft.Practices.EnterpriseLibrary.Security.Configuration;
 14using Microsoft.Practices.EnterpriseLibrary.Data;
 15using Microsoft.Practices.EnterpriseLibrary.Configuration;
 16using System.Configuration.Provider;
 17
 18namespace Kreeg.EnterpriseLibrary.Security.Database.Authorization
 19{
 20    /// <summary>
 21    /// Class for retrieving rules from the database
 22    /// </summary>

 23    public class DbRulesManager
 24    {
 25
 26        private Microsoft.Practices.EnterpriseLibrary.Data.Database dbRules = null;
 27        /// <summary>
 28        /// Creates a Database Rules Manager instance
 29        /// </summary>
 30        /// <param name="databaseService">The Database Instance to use to query the data(要查询数据的数据库实例)</param>
 31        /// <param name="config">The configuration context</param>

 32        public DbRulesManager(string databaseService)
 33        {
 34            //DatabaseProviderFactory factory = new DatabaseProviderFactory(config);
 35            dbRules = DatabaseFactory.CreateDatabase(databaseService);
 36        }

 37
 38
 39        /// <summary>
 40        /// Retrieves a rule from the database
 41        /// </summary>
 42        /// <param name="Name">The name of the rule</param>
 43        /// <returns>An AuthorizationRuleData object</returns>

 44        public AuthorizationRuleData GetRule(string name)
 45        {
 46            
 47            AuthorizationRuleData rule = null;
 48
 49            DbCommand cmd = dbRules.GetStoredProcCommand("dbo.GetRuleByName");
 50            dbRules.AddInParameter(cmd, "Name", DbType.String, name);
 51            
 52            using(IDataReader reader = dbRules.ExecuteReader(cmd))
 53            {
 54                if(reader.Read())
 55                {
 56                    rule = GetRuleFromReader(reader);
 57                }

 58            }

 59
 60            return rule;
 61        }

 62
 63        private AuthorizationRuleData GetRuleFromReader(IDataReader reader)
 64        {
 65            AuthorizationRuleData rule = new AuthorizationRuleData();
 66            rule.Name = reader.GetString(reader.GetOrdinal("Name"));
 67            rule.Expression = reader.GetString(reader.GetOrdinal("Expression"));
 68
 69            return rule;
 70        }

 71
 72        
 73        ///// <summary>
 74        ///// Retrieves all rules in the database as a DataSet
 75        ///// </summary>
 76        ///// <returns>A DataSet containing all of the rules</returns>

 77        //public DataSet GetAllRules()
 78        //{
 79        //    DbCommand cmd = dbRules.GetStoredProcCommand("dbo.GetAllRules");
 80
 81        //    using(DataSet ds = dbRules.ExecuteDataSet(cmd))
 82        //    {
 83        //        return ds;
 84        //    }
 85        //}
 86
 87
 88        /// <summary>
 89        /// Retrieves all rules in the database as a Collection
 90        /// </summary>
 91        /// <returns>An AuthorizationRuleDataCollection containing all of the rules</returns>

 92        public List<AuthorizationRuleData> GetAllRulesAsCollection()
 93        {
 94            List<AuthorizationRuleData> rules = new List<AuthorizationRuleData>();
 95
 96            DbCommand cmd = dbRules.GetStoredProcCommand("dbo.GetAllRules");
 97 
 98            using(IDataReader reader = dbRules.ExecuteReader(cmd))
 99            {
100                while(reader.Read())
101                {
102                    AuthorizationRuleData rule = GetRuleFromReader(reader);
103                    rules.Add(rule);
104                }

105            }

106            return rules;
107        }

108
109        /// <summary>
110        /// Inserts a rule into the database
111        /// </summary>
112        /// <param name="name">The name of the rule</param>
113        /// <param name="expression">The expression defining the rule</param>

114        public void InsertRule(string name, string expression,string description)
115        {
116            DbCommand cmd = dbRules.GetStoredProcCommand("dbo.InsertRule");
117            dbRules.AddInParameter(cmd, "Name", DbType.String, name);
118            dbRules.AddInParameter(cmd, "Expression", DbType.String, expression);
119            dbRules.AddInParameter(cmd, "Description",DbType.String, description);
120
121            dbRules.ExecuteNonQuery(cmd);
122        }

123
124        /// <summary>
125        /// Saves the rule to the database
126        /// </summary>
127        /// <param name="ruleId">The Rule Id</param>
128        /// <param name="name">The name of the rule</param>
129        /// <param name="expression">The expression</param>

130        public void UpdateRuleById(int ruleId, string name, string expression)
131        {
132            DbCommand cmd = dbRules.GetStoredProcCommand("dbo.UpdateRuleById");
133            dbRules.AddInParameter(cmd, "id", DbType.Int32, ruleId);
134            dbRules.AddInParameter(cmd, "Name", DbType.String, name);
135            dbRules.AddInParameter(cmd, "Expression",  DbType.String, expression);
136            //dbRules.AddInParameter(cmd, "Description", DbType.String, description);
137
138            dbRules.ExecuteNonQuery(cmd);
139        }

140
141        /// <summary>
142        /// Removes a rule from the database
143        /// </summary>
144        /// <param name="ruleId">The ruleid to remove</param>

145        public void DeleteRuleById(int ruleId)
146        {
147            DbCommand cmd = dbRules.GetStoredProcCommand("dbo.DeleteRuleById");
148            dbRules.AddInParameter(cmd, "id", DbType.Int32, ruleId);
149
150            dbRules.ExecuteNonQuery(cmd);
151        }

152
153
154        /***************** Follow Function Created by levinknight 2006.06.07 *****************/
155
156        GetAllRules
179
180        GetRulesForUser by IPrincipal
191
192        GetRulesForuser by Username
201
202        GetRulesForRole by Role'Name
211
212        GetEffectiveRules Service for GetRulesFor User or Role
243
244        AddUserToRule
310
311        RemoveUserFromRule
363
364        AddRoleToRule
418
419        RemoveRoleFromRule
493
494        UpdateRuleByName
504
505        DeleteRuleByName
519
520        CreateRule
564
565        IsInRule
588    }

589}

590

Feedback

#1楼   回复  引用  查看    

2007-05-31 23:50 by 迪奥尼所思      
谢谢 辛苦了

#2楼   回复  引用    

2007-06-01 00:16 by db661音乐站[未注册用户]
谢谢 辛苦了 辛苦了

#3楼   回复  引用  查看    

2007-06-01 01:03 by volnet(可以叫我大V)      
牛人

#4楼   回复  引用  查看    

2007-06-01 07:04 by 菌哥      
爽就一个字!

#5楼   回复  引用  查看    

2007-06-01 08:40 by Trove      
ms的Security Application Block好难用

#6楼   回复  引用  查看    

2007-06-01 09:04 by 网魂小兵      
牛逼!

#7楼   回复  引用  查看    

2007-06-01 09:11 by 夜风      
好长呀。

#8楼   回复  引用    

2007-06-01 13:34 by me[未注册用户]
2007-06-01 01:03 by volnet(可以叫我大V)
牛人
--------------------------------
汗,这是力气活啊,不敢当:)

#9楼   回复  引用  查看    

2007-06-01 13:45 by 高海东      
支持 不错 有使用的例子吗 对好有个demo

#10楼   回复  引用  查看    

2007-06-01 14:25 by Justin      
精神可嘉!

#11楼[楼主]   回复  引用  查看    

2007-06-01 15:02 by levinknight      
2007-06-01 13:45 by 高海东
支持 不错 有使用的例子吗 对好有个demo
-----------------------------------------
这很容易使用,请下载代码,参考Readme里的说明

#12楼[楼主]   回复  引用  查看    

2007-06-01 15:05 by levinknight      
2007-06-01 14:25 by Justin
精神可嘉!
---------------
多谢鼓励:)

#13楼   回复  引用    

2008-04-20 15:01 by 程程[未注册用户]
辛苦了,谢谢

#14楼[楼主]   回复  引用  查看    

2008-04-20 20:43 by 江南白衣      
2008-04-20 15:01 by 程程 [未注册用户]
辛苦了,谢谢
=====================
不客气:)

#15楼   回复  引用    

2009-01-10 16:37 by stoneca[未注册用户]
写的好,理当嘉奖!

博客园对FireFox的支持有点问题哟。那个下载链接还非用IE来下才行。



发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 766942




相关文章:

相关链接: