1. 程式人生 > >控制檯程式讀取Excel設定角色許可權

控制檯程式讀取Excel設定角色許可權

摘要: 本人微信公眾號:微軟動態CRM專家羅勇 ,回覆283或者20181118可方便獲取本文,同時可以在第一間得到我釋出的最新博文資訊,follow me!我的網站是 www.luoyong.me 。

在實體角色比較多的情況下手工設定許可權麻煩,我這裡開發了一個控制檯應用程式,讀取前面博文 自定義控制檯程式匯出角色對實體的許可權為Excel檔案 匯出並修改的角色對實體的許可權來設定。

  1 using Microsoft.Crm.Sdk.Messages;
  2 using Microsoft.Xrm.Sdk;
  3 using Microsoft.Xrm.Sdk.Client;
4 using Microsoft.Xrm.Sdk.Messages; 5 using Microsoft.Xrm.Sdk.Metadata; 6 using Microsoft.Xrm.Sdk.Query; 7 using System; 8 using System.Collections.Generic; 9 using System.Configuration; 10 using System.Linq; 11 using System.Runtime.InteropServices; 12 using System.ServiceModel.Description;
13 using System.Text; 14 using Excel = Microsoft.Office.Interop.Excel; 15 16 namespace SetRolePrivileges 17 { 18 class Program 19 { 20 static void Main(string[] args) 21 { 22 IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new
Uri(ConfigurationManager.AppSettings["orgUrl"])); 23 AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials(); 24 orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"]; 25 orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"]; 26 string readFrom = ConfigurationManager.AppSettings["ReadFrom"]; 27 Console.OutputEncoding = Encoding.UTF8; 28 Privilege privilege = new Privilege(); 29 PrivilegeDataRow privilegeDataRow = new PrivilegeDataRow(); 30 List<PrivilegeDataRow> lstPrivilegeDataRow = new List<PrivilegeDataRow>(); 31 List<Privilege> lstPrivileges = new List<Privilege>(); 32 Dictionary<Guid, string> roles = new Dictionary<Guid, string>(); 33 string privilegeName = string.Empty; 34 Guid roleId = Guid.Empty; 35 List<RolePrivilege> addRolePrivileges = new List<RolePrivilege>(); 36 List<RolePrivilege> removeRolePrivileges = new List<RolePrivilege>(); 37 using (OrganizationServiceProxy orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials)) 38 { 39 WhoAmIRequest whoReq = new WhoAmIRequest(); 40 WhoAmIResponse whoRep = orgSvc.Execute(whoReq) as WhoAmIResponse; 41 var userEntity = orgSvc.Retrieve("systemuser", whoRep.UserId, new ColumnSet("fullname")); 42 Console.WriteLine(string.Format("登入組織{0}成功,歡迎{1},準備提取{2}中的許可權設定進行處理,繼續操作請輸入y!", ConfigurationManager.AppSettings["orgUrl"], userEntity.GetAttributeValue<string>("fullname"), readFrom)); 43 var input = Console.ReadLine().ToString().ToUpper(); 44 if (input == "Y") 45 { 46 Console.WriteLine(string.Format("程式開始處理 - {0}", DateTime.Now.ToString())); 47 lstPrivileges = GetPrivileges(orgSvc); 48 roles = GetRoleList(orgSvc); 49 var excelApp = new Excel.Application(); 50 excelApp.Visible = false; 51 Excel.Workbook rolePrivilegesWB = excelApp.Workbooks.Open(readFrom); 52 try 53 { 54 int worksheetcount = rolePrivilegesWB.Worksheets.Count; 55 Console.WriteLine(string.Format("讀取的Workbook中共有{0}個Worksheet.", worksheetcount)); 56 for (var i = 1; i < rolePrivilegesWB.Worksheets.Count; i++) 57 { 58 Excel._Worksheet currentSheet = rolePrivilegesWB.Sheets[i]; 59 Console.WriteLine(string.Format("開始處理角色 - {0} - {1}", currentSheet.Name, DateTime.Now.ToString())); 60 if (roles.Where(t => t.Value == currentSheet.Name).Count() >= 1) 61 { 62 roleId = roles.Where(t => t.Value == currentSheet.Name).FirstOrDefault().Key; 63 Excel.Range xlRange = currentSheet.UsedRange; 64 int rowCount = xlRange.Rows.Count; //行數 65 int colCount = xlRange.Columns.Count;//列數 66 for (int j = 2; j <= rowCount; j++) 67 { 68 privilegeDataRow = new PrivilegeDataRow(); 69 //if (xlRange.Cells[j, k] != null && xlRange.Cells[j, k].Value2 != null) //判斷單元格欄位值是否為空 70 privilegeDataRow.SchemaName = xlRange.Cells[j, 1].Value2; 71 privilegeDataRow.DisplayName = xlRange.Cells[j, 2].Value2; 72 privilegeDataRow.CreatePrivilege = xlRange.Cells[j, 3].Value2 == null?string.Empty: Convert.ToString(xlRange.Cells[j, 3].Value2); 73 privilegeDataRow.ReadPrivilege = xlRange.Cells[j, 4].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 4].Value2); 74 privilegeDataRow.WritePrivilege = xlRange.Cells[j, 5].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 5].Value2); 75 privilegeDataRow.DeletePrivilege = xlRange.Cells[j, 6].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 6].Value2); 76 privilegeDataRow.AppendPrivilege = xlRange.Cells[j, 7].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 7].Value2); 77 privilegeDataRow.AppendToPrivilege = xlRange.Cells[j, 8].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 8].Value2); 78 privilegeDataRow.AssignPrivilege = xlRange.Cells[j, 9].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 9].Value2); 79 privilegeDataRow.SharePrivilege = xlRange.Cells[j, 10].Value2 == null ? string.Empty : Convert.ToString(xlRange.Cells[j, 10].Value2); 80 lstPrivilegeDataRow.Add(privilegeDataRow); 81 } 82 foreach (var item in lstPrivilegeDataRow) 83 { 84 #region 處理建立許可權 85 if (!string.IsNullOrEmpty(item.CreatePrivilege)) 86 { 87 privilegeName = string.Format("prvCreate{0}", item.SchemaName); 88 if (item.CreatePrivilege.Trim() == "0" 89 || item.CreatePrivilege.Trim() == "1" 90 || item.CreatePrivilege.Trim() == "2" 91 || item.CreatePrivilege.Trim() == "3" 92 || item.CreatePrivilege.Trim() == "4") 93 { 94 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 95 { 96 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 97 switch (item.CreatePrivilege.Trim()) 98 { 99 case "0"://如果設定為0需要清除此許可權 100 removeRolePrivileges.Add(new RolePrivilege() 101 { 102 PrivilegeId = privilege.PrivilegeId 103 }); 104 break; 105 case "1": 106 if (privilege.CanBeBasic) 107 { 108 addRolePrivileges.Add(new RolePrivilege() 109 { 110 PrivilegeId = privilege.PrivilegeId, 111 Depth = PrivilegeDepth.Basic 112 }); 113 } 114 break; 115 case "2": 116 if (privilege.CanBeLocal) 117 { 118 addRolePrivileges.Add(new RolePrivilege() 119 { 120 PrivilegeId = privilege.PrivilegeId, 121 Depth = PrivilegeDepth.Local 122 }); 123 } 124 break; 125 case "3": 126 if (privilege.CanBeDeep) 127 { 128 addRolePrivileges.Add(new RolePrivilege() 129 { 130 PrivilegeId = privilege.PrivilegeId, 131 Depth = PrivilegeDepth.Deep 132 }); 133 } 134 break; 135 case "4": 136 if (privilege.CanBeGlobal) 137 { 138 addRolePrivileges.Add(new RolePrivilege() 139 { 140 PrivilegeId = privilege.PrivilegeId, 141 Depth = PrivilegeDepth.Global 142 }); 143 } 144 break; 145 } 146 147 } 148 else 149 { 150 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 151 } 152 } 153 } 154 else 155 { 156 Console.WriteLine(string.Format("角色{0}的建立許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 157 } 158 #endregion 159 #region 處理讀許可權 160 if (!string.IsNullOrEmpty(item.ReadPrivilege)) 161 { 162 privilegeName = string.Format("prvRead{0}", item.SchemaName); 163 if (item.ReadPrivilege.Trim() == "0" 164 || item.ReadPrivilege.Trim() == "1" 165 || item.ReadPrivilege.Trim() == "2" 166 || item.ReadPrivilege.Trim() == "3" 167 || item.ReadPrivilege.Trim() == "4") 168 { 169 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 170 { 171 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 172 switch (item.ReadPrivilege.Trim()) 173 { 174 case "0"://如果設定為0需要清除此許可權 175 removeRolePrivileges.Add(new RolePrivilege() 176 { 177 PrivilegeId = privilege.PrivilegeId 178 }); 179 break; 180 case "1": 181 if (privilege.CanBeBasic) 182 { 183 addRolePrivileges.Add(new RolePrivilege() 184 { 185 PrivilegeId = privilege.PrivilegeId, 186 Depth = PrivilegeDepth.Basic 187 }); 188 } 189 break; 190 case "2": 191 if (privilege.CanBeLocal) 192 { 193 addRolePrivileges.Add(new RolePrivilege() 194 { 195 PrivilegeId = privilege.PrivilegeId, 196 Depth = PrivilegeDepth.Local 197 }); 198 } 199 break; 200 case "3": 201 if (privilege.CanBeDeep) 202 { 203 addRolePrivileges.Add(new RolePrivilege() 204 { 205 PrivilegeId = privilege.PrivilegeId, 206 Depth = PrivilegeDepth.Deep 207 }); 208 } 209 break; 210 case "4": 211 if (privilege.CanBeGlobal) 212 { 213 addRolePrivileges.Add(new RolePrivilege() 214 { 215 PrivilegeId = privilege.PrivilegeId, 216 Depth = PrivilegeDepth.Global 217 }); 218 } 219 break; 220 } 221 222 } 223 else 224 { 225 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 226 } 227 } 228 } 229 else 230 { 231 Console.WriteLine(string.Format("角色{0}的讀許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 232 } 233 #endregion 234 #region 處理寫許可權 235 if (!string.IsNullOrEmpty(item.WritePrivilege)) 236 { 237 privilegeName = string.Format("prvWrite{0}", item.SchemaName); 238 if (item.WritePrivilege.Trim() == "0" 239 || item.WritePrivilege.Trim() == "1" 240 || item.WritePrivilege.Trim() == "2" 241 || item.WritePrivilege.Trim() == "3" 242 || item.WritePrivilege.Trim() == "4") 243 { 244 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 245 { 246 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 247 switch (item.WritePrivilege.Trim()) 248 { 249 case "0"://如果設定為0需要清除此許可權 250 removeRolePrivileges.Add(new RolePrivilege() 251 { 252 PrivilegeId = privilege.PrivilegeId 253 }); 254 break; 255 case "1": 256 if (privilege.CanBeBasic) 257 { 258 addRolePrivileges.Add(new RolePrivilege() 259 { 260 PrivilegeId = privilege.PrivilegeId, 261 Depth = PrivilegeDepth.Basic 262 }); 263 } 264 break; 265 case "2": 266 if (privilege.CanBeLocal) 267 { 268 addRolePrivileges.Add(new RolePrivilege() 269 { 270 PrivilegeId = privilege.PrivilegeId, 271 Depth = PrivilegeDepth.Local 272 }); 273 } 274 break; 275 case "3": 276 if (privilege.CanBeDeep) 277 { 278 addRolePrivileges.Add(new RolePrivilege() 279 { 280 PrivilegeId = privilege.PrivilegeId, 281 Depth = PrivilegeDepth.Deep 282 }); 283 } 284 break; 285 case "4": 286 if (privilege.CanBeGlobal) 287 { 288 addRolePrivileges.Add(new RolePrivilege() 289 { 290 PrivilegeId = privilege.PrivilegeId, 291 Depth = PrivilegeDepth.Global 292 }); 293 } 294 break; 295 } 296 297 } 298 else 299 { 300 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 301 } 302 } 303 } 304 else 305 { 306 Console.WriteLine(string.Format("角色{0}的寫許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 307 } 308 #endregion 309 #region 處理刪除許可權 310 if (!string.IsNullOrEmpty(item.DeletePrivilege)) 311 { 312 privilegeName = string.Format("prvDelete{0}", item.SchemaName); 313 if (item.DeletePrivilege.Trim() == "0" 314 || item.DeletePrivilege.Trim() == "1" 315 || item.DeletePrivilege.Trim() == "2" 316 || item.DeletePrivilege.Trim() == "3" 317 || item.DeletePrivilege.Trim() == "4") 318 { 319 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 320 { 321 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 322 switch (item.DeletePrivilege.Trim()) 323 { 324 case "0"://如果設定為0需要清除此許可權 325 removeRolePrivileges.Add(new RolePrivilege() 326 { 327 PrivilegeId = privilege.PrivilegeId 328 }); 329 break; 330 case "1": 331 if (privilege.CanBeBasic) 332 { 333 addRolePrivileges.Add(new RolePrivilege() 334 { 335 PrivilegeId = privilege.PrivilegeId, 336 Depth = PrivilegeDepth.Basic 337 }); 338 } 339 break; 340 case "2": 341 if (privilege.CanBeLocal) 342 { 343 addRolePrivileges.Add(new RolePrivilege() 344 { 345 PrivilegeId = privilege.PrivilegeId, 346 Depth = PrivilegeDepth.Local 347 }); 348 } 349 break; 350 case "3": 351 if (privilege.CanBeDeep) 352 { 353 addRolePrivileges.Add(new RolePrivilege() 354 { 355 PrivilegeId = privilege.PrivilegeId, 356 Depth = PrivilegeDepth.Deep 357 }); 358 } 359 break; 360 case "4": 361 if (privilege.CanBeGlobal) 362 { 363 addRolePrivileges.Add(new RolePrivilege() 364 { 365 PrivilegeId = privilege.PrivilegeId, 366 Depth = PrivilegeDepth.Global 367 }); 368 } 369 break; 370 } 371 372 } 373 else 374 { 375 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 376 } 377 } 378 } 379 else 380 { 381 Console.WriteLine(string.Format("角色{0}的刪除許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 382 } 383 #endregion 384 #region 處理追加許可權 385 if (!string.IsNullOrEmpty(item.AppendPrivilege)) 386 { 387 privilegeName = string.Format("prvAppend{0}", item.SchemaName); 388 if (item.AppendPrivilege.Trim() == "0" 389 || item.AppendPrivilege.Trim() == "1" 390 || item.AppendPrivilege.Trim() == "2" 391 || item.AppendPrivilege.Trim() == "3" 392 || item.AppendPrivilege.Trim() == "4") 393 { 394 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 395 { 396 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 397 switch (item.AppendPrivilege.Trim()) 398 { 399 case "0"://如果設定為0需要清除此許可權 400 removeRolePrivileges.Add(new RolePrivilege() 401 { 402 PrivilegeId = privilege.PrivilegeId 403 }); 404 break; 405 case "1": 406 if (privilege.CanBeBasic) 407 { 408 addRolePrivileges.Add(new RolePrivilege() 409 { 410 PrivilegeId = privilege.PrivilegeId, 411 Depth = PrivilegeDepth.Basic 412 }); 413 } 414 break; 415 case "2": 416 if (privilege.CanBeLocal) 417 { 418 addRolePrivileges.Add(new RolePrivilege() 419 { 420 PrivilegeId = privilege.PrivilegeId, 421 Depth = PrivilegeDepth.Local 422 }); 423 } 424 break; 425 case "3": 426 if (privilege.CanBeDeep) 427 { 428 addRolePrivileges.Add(new RolePrivilege() 429 { 430 PrivilegeId = privilege.PrivilegeId, 431 Depth = PrivilegeDepth.Deep 432 }); 433 } 434 break; 435 case "4": 436 if (privilege.CanBeGlobal) 437 { 438 addRolePrivileges.Add(new RolePrivilege() 439 { 440 PrivilegeId = privilege.PrivilegeId, 441 Depth = PrivilegeDepth.Global 442 }); 443 } 444 break; 445 } 446 447 } 448 else 449 { 450 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 451 } 452 } 453 } 454 else 455 { 456 Console.WriteLine(string.Format("角色{0}的追加許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 457 } 458 #endregion 459 #region 處理追加到許可權 460 if (!string.IsNullOrEmpty(item.AppendToPrivilege)) 461 { 462 privilegeName = string.Format("prvAppendTo{0}", item.SchemaName); 463 if (item.AppendToPrivilege.Trim() == "0" 464 || item.AppendToPrivilege.Trim() == "1" 465 || item.AppendToPrivilege.Trim() == "2" 466 || item.AppendToPrivilege.Trim() == "3" 467 || item.AppendToPrivilege.Trim() == "4") 468 { 469 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 470 { 471 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 472 switch (item.AppendToPrivilege.Trim()) 473 { 474 case "0"://如果設定為0需要清除此許可權 475 removeRolePrivileges.Add(new RolePrivilege() 476 { 477 PrivilegeId = privilege.PrivilegeId 478 }); 479 break; 480 case "1": 481 if (privilege.CanBeBasic) 482 { 483 addRolePrivileges.Add(new RolePrivilege() 484 { 485 PrivilegeId = privilege.PrivilegeId, 486 Depth = PrivilegeDepth.Basic 487 }); 488 } 489 break; 490 case "2": 491 if (privilege.CanBeLocal) 492 { 493 addRolePrivileges.Add(new RolePrivilege() 494 { 495 PrivilegeId = privilege.PrivilegeId, 496 Depth = PrivilegeDepth.Local 497 }); 498 } 499 break; 500 case "3": 501 if (privilege.CanBeDeep) 502 { 503 addRolePrivileges.Add(new RolePrivilege() 504 { 505 PrivilegeId = privilege.PrivilegeId, 506 Depth = PrivilegeDepth.Deep 507 }); 508 } 509 break; 510 case "4": 511 if (privilege.CanBeGlobal) 512 { 513 addRolePrivileges.Add(new RolePrivilege() 514 { 515 PrivilegeId = privilege.PrivilegeId, 516 Depth = PrivilegeDepth.Global 517 }); 518 } 519 break; 520 } 521 522 } 523 else 524 { 525 Console.WriteLine(string.Format("許可權 - {0} 找不到!- {1}", privilegeName, DateTime.Now.ToString())); 526 } 527 } 528 } 529 else 530 { 531 Console.WriteLine(string.Format("角色{0}的追加到許可權設定為空不需要處理!- {1}", currentSheet.Name, DateTime.Now.ToString())); 532 } 533 #endregion 534 #region 處理分派許可權 535 if (!string.IsNullOrEmpty(item.AssignPrivilege)) 536 { 537 privilegeName = string.Format("prvAssign{0}", item.SchemaName); 538 if (item.AssignPrivilege.Trim() == "0" 539 || item.AssignPrivilege.Trim() == "1" 540 || item.AssignPrivilege.Trim() == "2" 541 || item.AssignPrivilege.Trim() == "3" 542 || item.AssignPrivilege.Trim() == "4") 543 { 544 if (lstPrivileges.Where(t => t.Name == privilegeName).Count() >= 1) 545 { 546 privilege = lstPrivileges.Where(t => t.Name == privilegeName).First(); 547 switch (item.AssignPrivilege.Trim()) 548 { 549 case "0"://如果設定為0需要清除此許可權 550 removeRolePrivileges.Add(new RolePrivilege() 551 { 552 PrivilegeId = privilege.PrivilegeId 553 }); 554 break; 555 case "1": 556 if (privilege.CanBeBasic) 557 { 558 addRolePrivileges.Add(new RolePrivilege() 559 {