今天闲来无事,做了个通用权限框架,感觉能有点像吉日嘎啦的,又很不像

 

先来贴数据表结构图:

User_Role 表:

User_Group表:

User_Account表

 

再来贴代码:

 

1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Reflection;
6
7  namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
8 {
9 public class UserPermission
10 {
11 //前台管理权限
12   public bool UnitOpenRoom { get; set; }
13 public bool TeamOpenRoom { get; set; }
14 public bool AddConsume { get; set; }
15 public bool TransferConsume { get; set; }
16 public bool CancelConsume { get; set; }
17 public bool ExchangeGood { get; set; }
18 public bool UnionBill { get; set; }
19 public bool SplitBill { get; set; }
20 public bool SettleBill { get; set; }
21 public bool SettleBillOnAccount { get; set; }
22 public bool SettleBillFree { get; set; }
23 public bool CancelBill { get; set; }
24 public bool SettleOnAccountBill { get; set; }
25 public bool SetSettleDiscount { get; set; }
26 public bool ModifyCheckInfo { get; set; }
27 public bool ChangeRoom { get; set; }
28 public bool LongLiveRoomManage { get; set; }
29 public bool ChangeRoomToAvailable { get; set; }
30 public bool ChangeRoomToBooked { get; set; }
31 public bool ChangeRoomToDisabled { get; set; }
32 public bool ChangeRoomToCleaning { get; set; }
33 public bool ChangeRoomToUsed { get; set; }
34 public bool ChangeRoomToLongLive { get; set; }
35 public bool PartSettleBill { get; set; }
36 public bool KeepRoomPriceAfterChangeRoom { get; set; }
37 public bool TakeAwaySale { get; set; }
38 //预定管理权限
39   public bool AddRoomBooking { get; set; }
40 public bool EditRoomBooking { get; set; }
41 public bool DelRoomBooking { get; set; }
42 public bool CancelOrRecancelBooking { get; set; }
43 public bool LookUpRoomBooking { get; set; }
44 public bool LookUpMemberRoomPrice { get; set; }
45 public bool LookUpAgreementRoomPrice { get; set; }
46 //营业查询权限
47   public bool LookUpGuestInfo { get; set; }
48 public bool LookUpGuestConsume { get; set; }
49 public bool LookUpMoneyCollection { get; set; }
50 public bool LookUpSettledBill { get; set; }
51 public bool LookUpCanceledBill { get; set; }
52 public bool LookUpPhoneCalled { get; set; }
53 public bool LookUpExpiredLiveRoom { get; set; }
54 public bool LookUpCheckInOutReport { get; set; }
55 public bool LookUpCheckInOutHistory { get; set; }
56 public bool LookUpFrequentGuestSummary { get; set; }
57 public bool LookUpBookingSummary { get; set; }
58 public bool LookUpRoomStateSummary { get; set; }
59 public bool LookUpDailyMonthlySummaryReport { get; set; }
60 public bool LookUpDailyMonthlySummaryChart { get; set; }
61 public bool LookUpBusinessSummaryRanking { get; set; }
62 public bool LookUpRoomSalesAnalysis { get; set; }
63 public bool LookUpBusinessYearlySummary { get; set; }
64 public bool LookUpRoomLiveSummary { get; set; }
65 public bool LookUpRoomDiscountSummary { get; set; }
66 public bool LookUpRoomLiveRateSummary { get; set; }
67 public bool LookUpBillTransferSummary { get; set; }
68 public bool LookUpCommonSummaryReport { get; set; }
69 public bool LookUpBillSortSummary { get; set; }
70 public bool LookUpConsumeSortSummary { get; set; }
71 public bool LookUpDepartmentSortSummary { get; set; }
72 public bool LookUpTechnicianCommissionSummary { get; set; }
73 public bool LookUpNightAudit { get; set; }
74 public bool LookUpSettledBillSort { get; set; }
75 public bool LookUpMoneyCollectionDetail { get; set; }
76 public bool LookUpRealtimeConsumeSummary { get; set; }
77 //营销管理权限
78   public bool AddCustomerInfo { get; set; }
79 public bool ModifyCustomerInfo { get; set; }
80 public bool DelCustomerInfo { get; set; }
81 public bool AddMemberInfo { get; set; }
82 public bool ModifyMemberInfo { get; set; }
83 public bool DelMemberInfo { get; set; }
84 public bool ChangeMemberPassword { get; set; }
85 public bool MemberCardCharge { get; set; }
86 public bool MemberCardLossRegister { get; set; }
87 public bool AddGuestToMember { get; set; }
88 public bool AddGuestToBlackList { get; set; }
89 public bool AddGuestBlackList { get; set; }
90 public bool ModifyGuestBlackList { get; set; }
91 public bool DelGuestBlackList { get; set; }
92 public bool DelGuestInfo { get; set; }
93 public bool LookUpCustomerInfo { get; set; }
94 public bool LookUpCustomerAgreement { get; set; }
95 public bool LookUpCustomerAccounting { get; set; }
96 public bool LookUpSalesmanAccounting { get; set; }
97 public bool MemberInfoConserve { get; set; }
98 public bool LookUpMemberConsume { get; set; }
99 public bool LookUpMemberCardCharge { get; set; }
100 public bool LookUpGuestCheckIn { get; set; }
101 public bool LookUpGuestBlackList { get; set; }
102 //public bool LookUpGuestInfo { get; set; }
103   public bool AddCustomerAgreement { get; set; }
104 public bool ModifyCustomerAgreement { get; set; }
105 public bool DelCustomerAgreement { get; set; }
106 public bool MemberCardTransferMoney { get; set; }
107 //商品管理权限
108   public bool Purchasing { get; set; }
109 public bool PurchasingReturn { get; set; }
110 public bool StockAllocation { get; set; }
111 public bool ReportLossOverflow { get; set; }
112 public bool StockRequisition { get; set; }
113 public bool ContactAccounts { get; set; }
114 public bool StockCheck { get; set; }
115 public bool CostAnalysis { get; set; }
116 public bool SalesCheck { get; set; }
117 public bool StockInventory { get; set; }
118 public bool ProviderSetting { get; set; }
119 public bool CommodityItemSetting { get; set; }
120 public bool WarehouseManage { get; set; }
121 //财务管理权限
122   public bool FeeAndIncomingSummary { get; set; }
123 public bool BusinessSummary { get; set; }
124 public bool PayablesAndReceivables { get; set; }
125 //系统设置权限
126   public bool RoomSetting { get; set; }
127 public bool ItemSetting { get; set; }
128 public bool TechnicianSetting { get; set; }
129 public bool CustomerSetting { get; set; }
130 public bool MemberSetting { get; set; }
131 public bool UserSetting { get; set; }
132 public bool AccountingSetting { get; set; }
133 public bool ExternalDeviceSetting { get; set; }
134 public bool DataBaseManage { get; set; }
135 public bool Reminding { get; set; }
136 public bool AgentSetting { get; set; }
137 public bool SystemLogManage { get; set; }
138 public bool DataBaseCompressAndRepair { get; set; }
139 public bool DataBaseBackupAndRestore { get; set; }
140 //其他模块
141   public bool AvailableRooms { get; set; }
142 public bool CurrentRoomStates { get; set; }
143 public bool HistoryFutureRoomStates { get; set; }
144 public bool DepositStateSummary { get; set; }
145 public bool DepositStateDetail { get; set; }
146 public bool LiveInMemberDeposit { get; set; }
147 public bool HistoryHandOvers { get; set; }
148 private SortedList<string, PropertyInfo> sortedPropertieInfos;
149 public UserPermission()
150 {
151 getPermissionPropertyInfos();
152 }
153 public UserPermission(string permission)
154 {
155 getPermissionPropertyInfos();
156 if (permission.Length != sortedPropertieInfos.Values.Count)
157 {
158 throw new Exception("权限字符串序列错误!");
159 }
160 char trueChar = Convert.ToChar("1");
161 for (int i = 0; i < sortedPropertieInfos.Values.Count; i++)
162 {
163 if (permission[i] == trueChar)
164 {
165 sortedPropertieInfos.Values[i].SetValue(this, true, null);
166 }
167 else
168 {
169 sortedPropertieInfos.Values[i].SetValue(this, false, null);
170 }
171 }
172 }
173 protected SortedList<string, PropertyInfo> getPermissionPropertyInfos()
174 {
175 sortedPropertieInfos = new SortedList<string, PropertyInfo>();
176 foreach (PropertyInfo info in this.GetType().GetProperties())
177 {
178 object value = info.GetValue(this, null);
179 if (value != null)
180 {
181 if (value.GetType() == typeof(bool))
182 {
183 sortedPropertieInfos.Add(info.Name, info);
184 }
185 }
186 }
187 return sortedPropertieInfos;
188 }
189 public override string ToString()
190 {
191 StringBuilder sb = new StringBuilder();
192 foreach (PropertyInfo info in sortedPropertieInfos.Values)
193 {
194 bool value = (bool)info.GetValue(this, null);
195 sb.Append(value ? "1" : "0");
196 }
197 return sb.ToString();
198 }
199 public UserPermission Combine(UserPermission permission)
200 {
201 for (int i = 0; i < sortedPropertieInfos.Values.Count; i++)
202 {
203 bool value1 = (bool)sortedPropertieInfos.Values[i].GetValue(this, null);
204 bool value2 = (bool)sortedPropertieInfos.Values[i].GetValue(permission, null);
205 sortedPropertieInfos.Values[i].SetValue(this, value1 | value2, null);
206 }
207 return this;
208 }
209 public UserPermission Except(UserPermission permission)
210 {
211 for (int i = 0; i < sortedPropertieInfos.Values.Count; i++)
212 {
213 bool value1 = (bool)sortedPropertieInfos.Values[i].GetValue(this, null);
214 bool value2 = (bool)sortedPropertieInfos.Values[i].GetValue(permission, null);
215 sortedPropertieInfos.Values[i].SetValue(this, value1 & value2, null);
216 }
217 return this;
218 }
219 public UserPermission Clear()
220 {
221 for (int i = 0; i < sortedPropertieInfos.Values.Count; i++)
222 {
223 sortedPropertieInfos.Values[i].SetValue(this, false, null);
224 }
225 return this;
226 }
227 }
228 }
229  

 

UserRole.cs

 

1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5
6  namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
7 {
8 public class UserRole
9 {
10 public string RoleName { get; set; }
11 public UserPermission Permission { get; set; }
12 public UserRole AddRoleToUser(UserAccount user)
13 {
14 user.AddRoleToUser(this);
15 return this;
16 }
17 public UserRole RemoveRoleFromUser(UserAccount user)
18 {
19 user.RemoveRoleFromUser(this);
20 return this;
21 }
22 public override string ToString()
23 {
24 return RoleName;
25 }
26 }
27 }
28  

 

 

UserGroup.cs

1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4 using System.Text;
5
6 namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
7 {
8 public class UserGroup
9 {
10 public string GroupName { get; set; }
11 public string Location { get; set; }
12 public UserPermission Permission { get; set; }
13 //public static UserGroup Load(string groupName)
14 //{
15 //}
16 public UserGroup AddUserToGroup(UserAccount user)
17 {
18 user.AddUserToGroup(this);
19 return this;
20 }
21 public UserGroup RemoveUserFromGroup(UserAccount user)
22 {
23 user.RemoveUserFromGroup(this);
24 return this;
25 }
26 public override string ToString()
27 {
28 return GroupName;
29 }
30 }
31 }
32

 

UserAccount.cs

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
7 {
8 public class UserAccount
9 {
10 private List<UserRole> roleList = new List<UserRole>();
11 private List<UserGroup> groupList = new List<UserGroup>();
12 private UserPermission permission = new UserPermission();
13 public string UserName { get; set; }
14 public UserPermission CustomPermission { get; set; }
15 public UserPermission Permission
16 {
17 get
18 {
19 return permission;
20 }
21 }
22 public UserRole[] UserRole
23 {
24 get
25 {
26 return roleList.ToArray();
27 }
28 }
29 public UserGroup[] UserGroup
30 {
31 get
32 {
33 return groupList.ToArray();
34 }
35 }
36 private void reCombinePermission()
37 {
38 permission.Clear();
39 permission = permission.Combine(CustomPermission);
40 foreach(UserRole role in roleList)
41 {
42 permission.Combine(role.Permission);
43 }
44 foreach (UserGroup group in groupList)
45 {
46 permission.Combine(group.Permission);
47 }
48 }
49 public UserAccount AddRoleToUser(UserRole role)
50 {
51 roleList.Add(role);
52 Permission.Combine(role.Permission);
53 return this;
54 }
55 public UserAccount RemoveRoleFromUser(UserRole role)
56 {
57 roleList.Remove(role);
58 reCombinePermission();
59 return this;
60 }
61 public UserAccount AddUserToGroup(UserGroup group)
62 {
63 Permission.Combine(group.Permission);
64 groupList.Add(group);
65 return this;
66 }
67 public UserAccount RemoveUserFromGroup(UserGroup group)
68 {
69 groupList.Remove(group);
70 reCombinePermission();
71 return this;
72 }
73 public override string ToString()
74 {
75 return UserName;
76 }
77 }
78 }
79

 

UserRoleExtension.cs

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Data.SqlClient;
7 using Com.Mojay.Hotel.Common;
8
9 namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
10 {
11 public static class UserRoleExtension
12 {
13 public static bool Load(this UserRole role, string roleName)
14 {
15 string sqlstr = "select * from User_Role where User_Role_Name ='" + roleName + "' ";
16 SqlDataAdapter da = new SqlDataAdapter(sqlstr, EstablishConnection.getSQLConn());
17 DataSet ds = new DataSet();
18 da.Fill(ds);
19 if (ds.Tables[0].Rows.Count > 0)
20 {
21 DataRow dr = ds.Tables[0].Rows[0];
22 role.RoleName = dr["User_Role_Name"].ToString();
23 string permission = dr["User_Role_Permission"].ToString();
24 role.Permission = new UserPermission(permission);
25 return true;
26 }
27 return false;
28 }
29 }
30 }
31

 

UserGroupExtension.cs

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Data.SqlClient;
7 using Com.Mojay.Hotel.Common;
8
9 namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
10 {
11 public static class UserGroupExtension
12 {
13 public static bool Load(this UserGroup group, string groupName)
14 {
15 string sqlstr = "select * from User_Group where User_Group_Name ='" + groupName + "' ";
16 SqlDataAdapter da = new SqlDataAdapter(sqlstr, EstablishConnection.getSQLConn());
17 DataSet ds = new DataSet();
18 da.Fill(ds);
19 if (ds.Tables[0].Rows.Count > 0)
20 {
21 DataRow dr = ds.Tables[0].Rows[0];
22 group.GroupName = dr["User_Group_Name"].ToString();
23 string permission = dr["User_Group_Permission"].ToString();
24 group.Permission = new UserPermission(permission);
25 return true;
26 }
27 return false;
28 }
29 }
30 }
31

 

UserAccountExtension.cs

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Data.SqlClient;
7 using Com.Mojay.Hotel.Common;
8
9 namespace Com.Mojay.Hotel.BusinessObjects.UserAccount
10 {
11 public static class UserAccountExtension
12 {
13 public static bool Load(this UserAccount user, string userName, string password)
14 {
15 string sqlstr = "select * from User_Account where User_Name ='" + userName + "' and User_Password='" + password + "' ";
16 SqlDataAdapter da = new SqlDataAdapter(sqlstr, EstablishConnection.getSQLConn());
17 DataSet ds = new DataSet();
18 da.Fill(ds);
19 if (ds.Tables[0].Rows.Count > 0)
20 {
21 DataRow dr = ds.Tables[0].Rows[0];
22 user.UserName = dr["User_Name"].ToString();
23 user.CustomPermission = new UserPermission(dr["User_Custom_Permission"].ToString());
24 string roleNameStr = dr["User_Role"].ToString();
25 string[] roleNames = roleNameStr.Split(Convert.ToChar(","));
26 foreach (string roleName in roleNames)
27 {
28 user.AddRoleToUserByName(roleName);
29 }
30 string groupNameStr = dr["User_Group"].ToString();
31 string[] groupNames = groupNameStr.Split(Convert.ToChar(","));
32 foreach (string groupName in groupNames)
33 {
34 user.AddUserToGroupByName(groupName);
35 }
36 return true;
37 }
38 return false;
39 }
40 public static UserAccount AddRoleToUserByName(this UserAccount user, string roleName)
41 {
42 if ((roleName != null) && (roleName!=string.Empty))
43 {
44 UserRole newrole = new UserRole();
45 if (newrole.Load(roleName))
46 {
47 user.AddRoleToUser(newrole);
48 }
49 }
50 return user;
51 }
52 public static UserAccount AddUserToGroupByName(this UserAccount user, string groupName)
53 {
54 if ((groupName != null) && (groupName != string.Empty))
55 {
56 UserGroup newgroup = new UserGroup();
57 if (newgroup.Load(groupName))
58 {
59 user.AddUserToGroup(newgroup);
60 }
61 }
62 return user;
63 }
64 }
65 }
66

 

下面简单说明下这个类该如何使用呢:

 

首先修改 public class UserPermission 中的权限属性:

如:

 

1 public class UserPermission
2 {
3 //前台管理权限
4 public bool UnitOpenRoom { get; set; }
5 public bool TeamOpenRoom { get; set; }
6 public bool AddConsume { get; set; }
7 public bool TransferConsume { get; set; }
8 public bool CancelConsume { get; set; }
9 public bool ExchangeGood { get; set; }
10 public bool UnionBill { get; set; }
11 public bool SplitBill { get; set; }
12 public bool SettleBill { get; set; }
13 public bool SettleBillOnAccount { get; set; }
14 public bool SettleBillFree { get; set; }
15 public bool CancelBill { get; set; }

 

然后编写如下代码:用去权限判断。其中这个UserAccount实例可以作为整个程序的全局静态变量。

 

UserAccount调用

1 UserAccount user = new UserAccount();
2 if (user.Load("admin", "admin"))
3 {
4 //Console.WriteLine(user.Permission.ToString());
5 if (user.Permission.LookUpCustomerInfo)
6 {
7 // Do something here!
8 }
9 }
10 else
11 {
12 Console.WriteLine("Load Failed!@");
13 }

 

UserGroup调用

1 UserGroup group = new UserGroup();
2 if (group.Load("Restaurant"))
3 {
4 Console.WriteLine(group.Permission.ToString());
5 }
6 else
7 {
8 Console.WriteLine("Load Failed!@");
9 }

 

 

UserRole调用

1 UserRole role = new UserRole();
2 if (role.Load("Manage"))
3 {
4 Console.WriteLine(role.Permission.AddConsume);
5 Console.WriteLine(role.Permission.ToString());
6 }
7 else
8 {
9 Console.WriteLine("Load Failed!@");
10 }

 

可以用类似于

user.AddRoleToUser(UserRole) ;  

user.AddUserToGroup(UserGroup); 

role.AddRoleToUser(User);

 group.AddUserToGroup(User);  

 

来进行角色 UserRole 和 用户组 UserGroup 的 增减修改!

 

谢谢大家。Over~!~

posted on 2010-12-04 12:04  gongji  阅读(1848)  评论(14编辑  收藏  举报

导航