Commit 5968301d1f8b49d5fffaa98fe36eca9a0343f60f
1 parent
e0544dd6
v1.13.0.0
- user kezelés funkcióinak bővítése (init action blokkok)
Showing
4 changed files
with
350 additions
and
30 deletions
Show diff stats
Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs
| @@ -198,6 +198,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS | @@ -198,6 +198,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS | ||
| 198 | public static class CreateSuperuser { public const string KEY = "CSU"; } | 198 | public static class CreateSuperuser { public const string KEY = "CSU"; } |
| 199 | public static class CreateAdminusers { public const string KEY = "CAU"; } | 199 | public static class CreateAdminusers { public const string KEY = "CAU"; } |
| 200 | public static class DeleteUsers { public const string KEY = "DEU"; } | 200 | public static class DeleteUsers { public const string KEY = "DEU"; } |
| 201 | + public static class ExecuteInitAction{ public const string KEY = "EIA"; } | ||
| 201 | } | 202 | } |
| 202 | } | 203 | } |
| 203 | public static class WebApplicationManager | 204 | public static class WebApplicationManager |
Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs
| @@ -136,33 +136,41 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -136,33 +136,41 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 136 | { | 136 | { |
| 137 | string rgnamelist = string.Join(",", MembershipDBContext.RoleGroups.Select(rg => rg.Name).ToArray()); | 137 | string rgnamelist = string.Join(",", MembershipDBContext.RoleGroups.Select(rg => rg.Name).ToArray()); |
| 138 | if (!string.IsNullOrWhiteSpace(rgnamelist)) { Assign.RoleGroupsToUsers(rgnamelist, username); } | 138 | if (!string.IsNullOrWhiteSpace(rgnamelist)) { Assign.RoleGroupsToUsers(rgnamelist, username); } |
| 139 | - foreach (var rn in Roles.GetAllRoles()) | 139 | + foreach (var rn in System.Web.Security.Roles.GetAllRoles()) |
| 140 | { | 140 | { |
| 141 | - if (!Users.IsInRole(username, rn)) { Roles.AddUserToRole(username, rn); } | 141 | + if (!Users.IsInRole(username, rn)) { System.Web.Security.Roles.AddUserToRole(username, rn); } |
| 142 | } | 142 | } |
| 143 | } | 143 | } |
| 144 | else if (administrator) | 144 | else if (administrator) |
| 145 | { | 145 | { |
| 146 | - if (!Roles.IsUserInRole(username, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(username, Constants.ROLENAME_ADMINISTRATOR); } | ||
| 147 | - if (!Roles.IsUserInRole(username, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(username, Constants.ROLENAME_ADMIN); } | 146 | + if (!System.Web.Security.Roles.IsUserInRole(username, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(username, Constants.ROLENAME_ADMINISTRATOR); } |
| 147 | + if (!System.Web.Security.Roles.IsUserInRole(username, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(username, Constants.ROLENAME_ADMIN); } | ||
| 148 | } | 148 | } |
| 149 | 149 | ||
| 150 | string[] selectedrolenames; | 150 | string[] selectedrolenames; |
| 151 | - if (rolenames != null) | 151 | + if (rolenames != null && rolenames.Any()) |
| 152 | { | 152 | { |
| 153 | - selectedrolenames = rolenames.Contains("*") ? Roles.GetAllRoles() : rolenames; | 153 | + selectedrolenames = rolenames.Contains("*") ? System.Web.Security.Roles.GetAllRoles() : rolenames; |
| 154 | if (selectedrolenames != null && selectedrolenames.Any()) | 154 | if (selectedrolenames != null && selectedrolenames.Any()) |
| 155 | { | 155 | { |
| 156 | - foreach (var rname in selectedrolenames) { if (!Roles.IsUserInRole(username, rname)) { Roles.AddUserToRole(username, rname); } } | 156 | + foreach (var rname in selectedrolenames) |
| 157 | + { | ||
| 158 | + if (string.IsNullOrWhiteSpace(rname)) { continue; } | ||
| 159 | + if (!System.Web.Security.Roles.IsUserInRole(username, rname)) { System.Web.Security.Roles.AddUserToRole(username, rname); } | ||
| 160 | + } | ||
| 157 | } | 161 | } |
| 158 | } | 162 | } |
| 159 | string[] selectedrolegroupnames; | 163 | string[] selectedrolegroupnames; |
| 160 | - if (rolegroupnames != null) | 164 | + if (rolegroupnames != null && rolegroupnames.Any()) |
| 161 | { | 165 | { |
| 162 | selectedrolegroupnames = rolegroupnames.Contains("*") ? RoleGroups.GetAllNames().ToArray() : rolegroupnames; | 166 | selectedrolegroupnames = rolegroupnames.Contains("*") ? RoleGroups.GetAllNames().ToArray() : rolegroupnames; |
| 163 | if (selectedrolegroupnames != null && selectedrolegroupnames.Any()) | 167 | if (selectedrolegroupnames != null && selectedrolegroupnames.Any()) |
| 164 | { | 168 | { |
| 165 | - foreach (var rgname in selectedrolegroupnames) { if (!RoleGroups.IsUserInRoleGroup(username, rgname)) { Assign.RoleGroupsToUsers(rgname, username); } } | 169 | + foreach (var rgname in selectedrolegroupnames) |
| 170 | + { | ||
| 171 | + if (string.IsNullOrWhiteSpace(rgname)) { continue; } | ||
| 172 | + if (!RoleGroups.IsUserInRoleGroup(username, rgname)) { Assign.RoleGroupsToUsers(rgname, username); } | ||
| 173 | + } | ||
| 166 | } | 174 | } |
| 167 | } | 175 | } |
| 168 | return user; | 176 | return user; |
| @@ -172,24 +180,24 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -172,24 +180,24 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 172 | public static void CreateAdminRolesAndUsers() | 180 | public static void CreateAdminRolesAndUsers() |
| 173 | { | 181 | { |
| 174 | MembershipUser user; | 182 | MembershipUser user; |
| 175 | - if (!Roles.RoleExists(Constants.ROLENAME_ADMINISTRATOR)) { Roles.CreateRole(Constants.ROLENAME_ADMINISTRATOR); } | ||
| 176 | - if (!Roles.RoleExists(Constants.ROLENAME_ADMIN)) { Roles.CreateRole(Constants.ROLENAME_ADMIN); } | 183 | + if (!System.Web.Security.Roles.RoleExists(Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.CreateRole(Constants.ROLENAME_ADMINISTRATOR); } |
| 184 | + if (!System.Web.Security.Roles.RoleExists(Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.CreateRole(Constants.ROLENAME_ADMIN); } | ||
| 177 | 185 | ||
| 178 | user = Membership.GetUser(Constants.USERNAME_ADMIN); | 186 | user = Membership.GetUser(Constants.USERNAME_ADMIN); |
| 179 | if (user == null) | 187 | if (user == null) |
| 180 | { | 188 | { |
| 181 | user = Membership.CreateUser(Constants.USERNAME_ADMIN, Constants.PASSWORD_ADMIN); | 189 | user = Membership.CreateUser(Constants.USERNAME_ADMIN, Constants.PASSWORD_ADMIN); |
| 182 | } | 190 | } |
| 183 | - if (!Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR); } | ||
| 184 | - if (!Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN); } | 191 | + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR); } |
| 192 | + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN); } | ||
| 185 | 193 | ||
| 186 | user = Membership.GetUser(Constants.USERNAME_ADMINISTRATOR); | 194 | user = Membership.GetUser(Constants.USERNAME_ADMINISTRATOR); |
| 187 | if (user == null) | 195 | if (user == null) |
| 188 | { | 196 | { |
| 189 | user = Membership.CreateUser(Constants.USERNAME_ADMINISTRATOR, Constants.PASSWORD_ADMINISTRATOR); | 197 | user = Membership.CreateUser(Constants.USERNAME_ADMINISTRATOR, Constants.PASSWORD_ADMINISTRATOR); |
| 190 | } | 198 | } |
| 191 | - if (!Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR); } | ||
| 192 | - if (!Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN); } | 199 | + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR); } |
| 200 | + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN); } | ||
| 193 | } | 201 | } |
| 194 | #endregion CreateAdminRolesAndUsers | 202 | #endregion CreateAdminRolesAndUsers |
| 195 | #region IsInRole public method | 203 | #region IsInRole public method |
| @@ -397,6 +405,109 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -397,6 +405,109 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 397 | #endregion Remove methods | 405 | #endregion Remove methods |
| 398 | } | 406 | } |
| 399 | #endregion RoleGroups | 407 | #endregion RoleGroups |
| 408 | + #region Roles | ||
| 409 | + public static class Roles | ||
| 410 | + { | ||
| 411 | + #region Get methods | ||
| 412 | + /// <summary> | ||
| 413 | + /// Szerep lekérése az egyedi azonosítója alapján. | ||
| 414 | + /// </summary> | ||
| 415 | + /// <param name="id">A keresett funkció azonosítója.</param> | ||
| 416 | + /// <returns>A kért szerepkör, egyébként null.</returns> | ||
| 417 | + public static DAL.Role Get(Guid id) | ||
| 418 | + { | ||
| 419 | + return MembershipDBContext.Roles.FirstOrDefault(x => x.RoleId == id); | ||
| 420 | + } | ||
| 421 | + /// <summary> | ||
| 422 | + /// Szerep lekérése az egyedi neve alapján. | ||
| 423 | + /// </summary> | ||
| 424 | + /// <param name="name">A keresett szerep egyedi neve.</param> | ||
| 425 | + /// <returns>A kért szerep, egyébként null.</returns> | ||
| 426 | + public static DAL.Role Get(string name) | ||
| 427 | + { | ||
| 428 | + return MembershipDBContext.Roles.FirstOrDefault(x => x.RoleName== name); | ||
| 429 | + } | ||
| 430 | + /// <summary> | ||
| 431 | + /// Szerepek neveinek lekérése. | ||
| 432 | + /// </summary> | ||
| 433 | + /// <returns>A szerepek neveinek listája.</returns> | ||
| 434 | + public static List<string> GetAllNames() | ||
| 435 | + { | ||
| 436 | + return MembershipDBContext.Roles.Select(r => r.RoleName).ToList(); | ||
| 437 | + } | ||
| 438 | + /// <summary> | ||
| 439 | + /// A megadott nevű szerep benne van-e a megadott szerepkörben tartozik-e. | ||
| 440 | + /// </summary> | ||
| 441 | + /// <param name="rolename"></param> | ||
| 442 | + /// <param name="rolegroupname"></param> | ||
| 443 | + /// <returns>true, ha igen</returns> | ||
| 444 | + public static bool IsRoleInRoleGroup(string rolename, string rolegroupname) | ||
| 445 | + { | ||
| 446 | + return MembershipDBContext.RoleGroupRoles.Select(rtorg => rtorg.Role.RoleName == rolename && rtorg.RoleGroup.Name == rolegroupname).ToList().Any(); | ||
| 447 | + } | ||
| 448 | + #endregion Get methods | ||
| 449 | + | ||
| 450 | + #region Create method | ||
| 451 | + /// <summary> | ||
| 452 | + /// Szerep létrehozása. | ||
| 453 | + /// </summary> | ||
| 454 | + /// <param name="rolename">A létrehozandó szerep neve.</param> | ||
| 455 | + /// <exception cref="ApplicationException"> | ||
| 456 | + /// Ha üres vagy null a szerep neve. | ||
| 457 | + /// Ha már létezik a megadott név. | ||
| 458 | + /// </exception> | ||
| 459 | + public static void Create(string rolename, string appname=null) | ||
| 460 | + { | ||
| 461 | + if (String.IsNullOrWhiteSpace(rolename)) { throw new ApplicationException("Role név megadása kötelező!"); } | ||
| 462 | + if (appname==null) { appname = MembershipDBContext.Applications.FirstOrDefault(a => a.ApplicationName == "/")?.ApplicationName; } | ||
| 463 | + if (appname == null) { appname = MembershipDBContext.Applications.First()?.ApplicationName; } | ||
| 464 | + if (appname==null) { throw new ApplicationException("Application nem létezik!"); } | ||
| 465 | + var app = MembershipDBContext.Applications.FirstOrDefault(a => a.ApplicationName == appname); | ||
| 466 | + | ||
| 467 | + if (MembershipDBContext.Roles.Any(x => x.RoleName == rolename)) { throw new ApplicationException($"Role {rolename} already exist!"); } | ||
| 468 | + | ||
| 469 | + System.Web.Security.Roles.CreateRole(rolename); | ||
| 470 | + var rolecreated = MembershipDBContext.Roles.First(x => x.RoleName == rolename); | ||
| 471 | + if (rolecreated==null) { throw new ApplicationException($"Creating role failed. Role name:{rolename}!"); } | ||
| 472 | + //MembershipDBContext.Roles.Add(new DAL.Role() { RoleName = rolename, ApplicationId=app.ApplicationId,Description=null, }); | ||
| 473 | + rolecreated.ApplicationId = app.ApplicationId; | ||
| 474 | + MembershipDBContext.SaveChanges(); | ||
| 475 | + } | ||
| 476 | + #endregion Create method | ||
| 477 | + | ||
| 478 | + #region Remove methods | ||
| 479 | + /// <summary> | ||
| 480 | + /// Szerep törlése az egyedi azonosítója megadásával. | ||
| 481 | + /// A szerephez tartozó összerendelések is megszűnnek! | ||
| 482 | + /// </summary> | ||
| 483 | + /// <param name="id">Törlendő szerep egyedi azonosítója.</param> | ||
| 484 | + /// <exception cref="ApplicationException">Ha nem található a törlendő szerep.</exception> | ||
| 485 | + public static void Remove(int id) | ||
| 486 | + { | ||
| 487 | + var row = MembershipDBContext.Roles.Find(id); | ||
| 488 | + if (row == null) { throw new ApplicationException("Role does not exist!!"); } | ||
| 489 | + else { MembershipDBContext.Roles.Remove(row); MembershipDBContext.SaveChanges(); } | ||
| 490 | + } | ||
| 491 | + /// <summary> | ||
| 492 | + /// Szerep törlése az egyedi neve megadásával. | ||
| 493 | + /// A szerephez tartozó összerendelések is megszűnnek! | ||
| 494 | + /// </summary> | ||
| 495 | + /// <param name="name">Törlendő szerep egyedi neve.</param> | ||
| 496 | + /// <exception cref="ApplicationException">Ha nem található a törlendő szerep.</exception> | ||
| 497 | + public static void Remove(string name) | ||
| 498 | + { | ||
| 499 | + var row = MembershipDBContext.Roles.FirstOrDefault(x => x.RoleName == name); | ||
| 500 | + if (row == null) { throw new ApplicationException("Role does not exist!!"); } | ||
| 501 | + else | ||
| 502 | + { | ||
| 503 | + System.Web.Security.Roles.DeleteRole(name); | ||
| 504 | + MembershipDBContext.Roles.Remove(row); | ||
| 505 | + MembershipDBContext.SaveChanges(); | ||
| 506 | + } | ||
| 507 | + } | ||
| 508 | + #endregion Remove methods | ||
| 509 | + } | ||
| 510 | + #endregion Roles | ||
| 400 | #region Assign | 511 | #region Assign |
| 401 | public static class Assign | 512 | public static class Assign |
| 402 | { | 513 | { |
| @@ -433,7 +544,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -433,7 +544,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 433 | foreach (var username in usernamelist) | 544 | foreach (var username in usernamelist) |
| 434 | { | 545 | { |
| 435 | CheckUsersExists(username); | 546 | CheckUsersExists(username); |
| 436 | - if (!Users.IsInRole(username, rolename)) { Roles.AddUserToRole(username, rolename); } | 547 | + if (!Users.IsInRole(username, rolename)) { System.Web.Security.Roles.AddUserToRole(username, rolename); } |
| 437 | } | 548 | } |
| 438 | } | 549 | } |
| 439 | } | 550 | } |
| @@ -571,17 +682,17 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -571,17 +682,17 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 571 | if (user != null) | 682 | if (user != null) |
| 572 | { | 683 | { |
| 573 | // eltávolítjuk az összes szerepkörét a felhasználónak, majd a felhasználóhoz tartozó UserRoleGroup-okban lévő Roles-okat adjuk hozzá a felhasználóhoz | 684 | // eltávolítjuk az összes szerepkörét a felhasználónak, majd a felhasználóhoz tartozó UserRoleGroup-okban lévő Roles-okat adjuk hozzá a felhasználóhoz |
| 574 | - if (Roles.GetRolesForUser(user.UserName).Any()) | 685 | + if (System.Web.Security.Roles.GetRolesForUser(user.UserName).Any()) |
| 575 | { | 686 | { |
| 576 | - Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName)); | 687 | + System.Web.Security.Roles.RemoveUserFromRoles(user.UserName, System.Web.Security.Roles.GetRolesForUser(user.UserName)); |
| 577 | } | 688 | } |
| 578 | foreach (DAL.RoleGroup urg in MembershipDBContext.RoleGroups.Where(x => x.Users.Any(y => y.UserId == userId))) | 689 | foreach (DAL.RoleGroup urg in MembershipDBContext.RoleGroups.Where(x => x.Users.Any(y => y.UserId == userId))) |
| 579 | { | 690 | { |
| 580 | foreach (string roleName in urg.Roles.Select(x => x.Role.RoleName)) | 691 | foreach (string roleName in urg.Roles.Select(x => x.Role.RoleName)) |
| 581 | { | 692 | { |
| 582 | - if (!Roles.IsUserInRole(user.UserName, roleName)) | 693 | + if (!System.Web.Security.Roles.IsUserInRole(user.UserName, roleName)) |
| 583 | { | 694 | { |
| 584 | - Roles.AddUserToRole(user.UserName, roleName); | 695 | + System.Web.Security.Roles.AddUserToRole(user.UserName, roleName); |
| 585 | } | 696 | } |
| 586 | } | 697 | } |
| 587 | } | 698 | } |
| @@ -794,6 +905,32 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -794,6 +905,32 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 794 | public string Description { get; set; } | 905 | public string Description { get; set; } |
| 795 | } | 906 | } |
| 796 | #endregion table-Role | 907 | #endregion table-Role |
| 908 | + #region table-Application | ||
| 909 | + /// <summary> | ||
| 910 | + /// DefaultMembershipProvider által létrehozott User tábla. | ||
| 911 | + /// </summary> | ||
| 912 | + [Table("Applications", Schema = "dbo")] | ||
| 913 | + public partial class Application | ||
| 914 | + { | ||
| 915 | + /// <summary> | ||
| 916 | + /// Alkalmazás egyedi azonosítója. | ||
| 917 | + /// </summary> | ||
| 918 | + [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] | ||
| 919 | + public Guid ApplicationId { get; set; } | ||
| 920 | + | ||
| 921 | + /// <summary> | ||
| 922 | + /// Szerep neve. | ||
| 923 | + /// </summary> | ||
| 924 | + [Required, MaxLength(256)] | ||
| 925 | + public string ApplicationName { get; set; } | ||
| 926 | + | ||
| 927 | + /// <summary> | ||
| 928 | + /// Alkalmazás rövid leírása. | ||
| 929 | + /// </summary> | ||
| 930 | + [MaxLength(256)] | ||
| 931 | + public string Description { get; set; } | ||
| 932 | + } | ||
| 933 | + #endregion table-Application | ||
| 797 | #region table-SecondaryFunction | 934 | #region table-SecondaryFunction |
| 798 | /// <summary> | 935 | /// <summary> |
| 799 | /// Lehetséges funkciókat tartalmazó táblázat, mely funkciókhoz | 936 | /// Lehetséges funkciókat tartalmazó táblázat, mely funkciókhoz |
| @@ -1044,7 +1181,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | @@ -1044,7 +1181,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS | ||
| 1044 | 1181 | ||
| 1045 | #region DbSets | 1182 | #region DbSets |
| 1046 | 1183 | ||
| 1047 | - #region !!! A migrációból kivett két tábla. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! | 1184 | + #region !!! A migrációból kivett táblák. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! |
| 1185 | + public virtual DbSet<Application> Applications { get; set; } | ||
| 1048 | public virtual DbSet<Role> Roles { get; set; } | 1186 | public virtual DbSet<Role> Roles { get; set; } |
| 1049 | public virtual DbSet<User> Users { get; set; } | 1187 | public virtual DbSet<User> Users { get; set; } |
| 1050 | #endregion !!! A migrációból kivett két tábla. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! | 1188 | #endregion !!! A migrációból kivett két tábla. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! |
Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs
| @@ -23,7 +23,7 @@ using System.Text.RegularExpressions; | @@ -23,7 +23,7 @@ using System.Text.RegularExpressions; | ||
| 23 | 23 | ||
| 24 | namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | 24 | namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS |
| 25 | { | 25 | { |
| 26 | - #region MaintenanceTools class | 26 | + #region UserManager class |
| 27 | public static class UserManager | 27 | public static class UserManager |
| 28 | { | 28 | { |
| 29 | private static Log4ProUserManagerXmlProcessor Config; | 29 | private static Log4ProUserManagerXmlProcessor Config; |
| @@ -44,6 +44,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -44,6 +44,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 44 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateSuperuser.KEY, "Create superuser", CreateSuperuser, new Menu.ExecutorParameter(cfg: config))) | 44 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateSuperuser.KEY, "Create superuser", CreateSuperuser, new Menu.ExecutorParameter(cfg: config))) |
| 45 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateAdminusers.KEY, "Create Admin and Administrator roles and users", CreateAdminusers, new Menu.ExecutorParameter(cfg: config, null))) | 45 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateAdminusers.KEY, "Create Admin and Administrator roles and users", CreateAdminusers, new Menu.ExecutorParameter(cfg: config, null))) |
| 46 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.DeleteUsers.KEY, "Remove user", DeleteUsers, new Menu.ExecutorParameter(cfg: config, null))) | 46 | .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.DeleteUsers.KEY, "Remove user", DeleteUsers, new Menu.ExecutorParameter(cfg: config, null))) |
| 47 | + .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.ExecuteInitAction.KEY, "Execute init actionblock", ExecuteInitActionBlock, new Menu.ExecutorParameter(cfg: config, null))) | ||
| 47 | .SetMenuHeaderDisplayer(UserListDisplayer) | 48 | .SetMenuHeaderDisplayer(UserListDisplayer) |
| 48 | .SetSelectionMode(Menu.SelectionMode.Single); | 49 | .SetSelectionMode(Menu.SelectionMode.Single); |
| 49 | menufunctions.ExecuteMenu(); | 50 | menufunctions.ExecuteMenu(); |
| @@ -70,6 +71,65 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -70,6 +71,65 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 70 | #endregion private UserListDisplayer | 71 | #endregion private UserListDisplayer |
| 71 | 72 | ||
| 72 | #region First level Executors with UI | 73 | #region First level Executors with UI |
| 74 | + #region ExecuteInitActionBlock | ||
| 75 | + private static object ExecuteInitActionBlock(object parameter, object o) | ||
| 76 | + { | ||
| 77 | + var config = (parameter as Menu.ExecutorParameter).GetConfig<Log4ProUserManagerXmlProcessor>(); | ||
| 78 | + if (config.InitActionBlockList == null || !config.InitActionBlockList.Any()) return o; | ||
| 79 | + | ||
| 80 | + var initactionblocknamelist = string.Join(",", config.InitActionBlockList.Select(iab => iab.Name).ToArray()); | ||
| 81 | + ColorConsole.WriteLine($"Select one init action block from this list: {initactionblocknamelist}", ConsoleColor.Yellow); | ||
| 82 | + var iabname = ColorConsole.ReadLine($"Enter init action block name:", ConsoleColor.Yellow).ToLower(); | ||
| 83 | + if (iabname == "EX") { return null; } | ||
| 84 | + var stepbystep = ColorConsole.ReadLine($"Do You want to execute step-by-step (true/false/yes/no/?:", ConsoleColor.Yellow).ToLower().Replace("yes","true")==bool.TrueString.ToLower(); | ||
| 85 | + if (iabname == "EX") { return null; } | ||
| 86 | + try | ||
| 87 | + { | ||
| 88 | + var iab = config.InitActionBlockList.FirstOrDefault(_iab => _iab.Name.ToLower() == iabname); | ||
| 89 | + if (iab == null) { throw new ApplicationException($"InitActionBlock with name '{iabname}' does not exist!"); } | ||
| 90 | + foreach (var cr in iab.GetCreateRoleActions()) | ||
| 91 | + { | ||
| 92 | + if (stepbystep) | ||
| 93 | + { | ||
| 94 | + ColorConsole.WriteLine($"Action:{cr.Type}, role names: {cr.Roles}.", ConsoleColor.Yellow); | ||
| 95 | + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; | ||
| 96 | + } | ||
| 97 | + foreach (var rn in cr.RoleArray) { try { MembershipTools.Roles.Create(rn); } catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; } | ||
| 98 | + } | ||
| 99 | + foreach (var cr in iab.GetCreateRoleGroupActions()) | ||
| 100 | + { | ||
| 101 | + if (stepbystep) | ||
| 102 | + { | ||
| 103 | + ColorConsole.WriteLine($"Action:{cr.Type}, rolegroup name: {cr.Name}, roles: {cr.Roles}.", ConsoleColor.Yellow); | ||
| 104 | + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; | ||
| 105 | + } | ||
| 106 | + try | ||
| 107 | + { | ||
| 108 | + MembershipTools.RoleGroups.Create(cr.Name); | ||
| 109 | + MembershipTools.Assign.RolesToRoleGroups(cr.Roles, cr.Name); | ||
| 110 | + } catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; | ||
| 111 | + | ||
| 112 | + } | ||
| 113 | + foreach (var cr in iab.GetCreateUserActions()) | ||
| 114 | + { | ||
| 115 | + if (stepbystep) | ||
| 116 | + { | ||
| 117 | + var crtext = cr.Superuser ? $"superuser" : $"rolegroups: {cr.RoleGroups}, roles: {cr.Roles}"; | ||
| 118 | + ColorConsole.WriteLine($"Action:{cr.Type}, username: {cr.Name}[{cr.Password}], {crtext}.", ConsoleColor.Yellow); | ||
| 119 | + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; | ||
| 120 | + } | ||
| 121 | + try | ||
| 122 | + { | ||
| 123 | + MembershipTools.Users.Create(cr.Name, cr.Password, administrator: false, superuser: cr.Superuser, rolenames: cr.RoleArray, rolegroupnames: cr.RoleGroupArray); | ||
| 124 | + } | ||
| 125 | + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; | ||
| 126 | + } | ||
| 127 | + ColorConsole.WriteLine($"Executing init action block '{iabname}' was successful!", ConsoleColor.Green); | ||
| 128 | + } | ||
| 129 | + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } | ||
| 130 | + return o; | ||
| 131 | + } | ||
| 132 | + #endregion ExecuteInitActionBlock | ||
| 73 | #region CreateSuperuser | 133 | #region CreateSuperuser |
| 74 | private static object CreateSuperuser(object parameter, object o) | 134 | private static object CreateSuperuser(object parameter, object o) |
| 75 | { | 135 | { |
| @@ -123,7 +183,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -123,7 +183,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 123 | #endregion DeleteUsers | 183 | #endregion DeleteUsers |
| 124 | #endregion First level Executors with UI | 184 | #endregion First level Executors with UI |
| 125 | } | 185 | } |
| 126 | - #endregion MaintenanceTools class | 186 | + #endregion UserManager class |
| 127 | #region MaintenanceToolsXmlProcessor class | 187 | #region MaintenanceToolsXmlProcessor class |
| 128 | public class Log4ProUserManagerXmlProcessor : XmlParser | 188 | public class Log4ProUserManagerXmlProcessor : XmlParser |
| 129 | { | 189 | { |
| @@ -131,6 +191,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -131,6 +191,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 131 | public string Xml_SQLConnectionString; | 191 | public string Xml_SQLConnectionString; |
| 132 | public string Xml_ProtectedUserNameCommaList; | 192 | public string Xml_ProtectedUserNameCommaList; |
| 133 | public List<UserGroup> UserGroupList; | 193 | public List<UserGroup> UserGroupList; |
| 194 | + public List<InitActionBlock> InitActionBlockList; | ||
| 134 | public List<string> ProtectedUserNameList; | 195 | public List<string> ProtectedUserNameList; |
| 135 | #endregion fields | 196 | #endregion fields |
| 136 | #region constructor | 197 | #region constructor |
| @@ -143,13 +204,16 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -143,13 +204,16 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 143 | if (string.IsNullOrEmpty(Xml_ProtectedUserNameCommaList)) { Xml_ProtectedUserNameCommaList = XmlStructure.Attributes.ProtectedUsernameList.Values.DEFAULT; } | 204 | if (string.IsNullOrEmpty(Xml_ProtectedUserNameCommaList)) { Xml_ProtectedUserNameCommaList = XmlStructure.Attributes.ProtectedUsernameList.Values.DEFAULT; } |
| 144 | ProtectedUserNameList = Xml_ProtectedUserNameCommaList.Split(',').ToList(); | 205 | ProtectedUserNameList = Xml_ProtectedUserNameCommaList.Split(',').ToList(); |
| 145 | UserGroupList = new List<UserGroup>(); | 206 | UserGroupList = new List<UserGroup>(); |
| 146 | - var ugxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.UserGroups))).Elements(XName.Get(nameof(XmlStructure.UserGroups.UserGroup))); | 207 | + var ugxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.UserGroups)))?.Elements(XName.Get(nameof(XmlStructure.UserGroups.UserGroup))); |
| 147 | if (ugxmllist!=null && ugxmllist.Any()) | 208 | if (ugxmllist!=null && ugxmllist.Any()) |
| 148 | { | 209 | { |
| 149 | - foreach (var ugxml in ugxmllist) | ||
| 150 | - { | ||
| 151 | - UserGroupList.Add(new UserGroup(ugxml)); | ||
| 152 | - }; | 210 | + foreach (var ugxml in ugxmllist) { UserGroupList.Add(new UserGroup(ugxml)); }; |
| 211 | + } | ||
| 212 | + InitActionBlockList = new List<InitActionBlock>(); | ||
| 213 | + var iabxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.InitActionBlocks)))?.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock))); | ||
| 214 | + if (iabxmllist != null && iabxmllist.Any()) | ||
| 215 | + { | ||
| 216 | + foreach (var iabxml in iabxmllist) { InitActionBlockList.Add(new InitActionBlock(iabxml)); }; | ||
| 153 | } | 217 | } |
| 154 | } | 218 | } |
| 155 | #region ProtectedUser | 219 | #region ProtectedUser |
| @@ -173,6 +237,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -173,6 +237,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 173 | public static class ProtectedUsernameList { public static class Values { public const string DEFAULT = "Admin,Administrator"; } } | 237 | public static class ProtectedUsernameList { public static class Values { public const string DEFAULT = "Admin,Administrator"; } } |
| 174 | 238 | ||
| 175 | } | 239 | } |
| 240 | + public static class InitActionBlocks | ||
| 241 | + { | ||
| 242 | + public static class InitActionBlock | ||
| 243 | + { | ||
| 244 | + public static class Attributes | ||
| 245 | + { | ||
| 246 | + public static class Name { } | ||
| 247 | + } | ||
| 248 | + public static class CreateRoles | ||
| 249 | + { | ||
| 250 | + public static class Attributes | ||
| 251 | + { | ||
| 252 | + public static class Roles | ||
| 253 | + { | ||
| 254 | + public static class Values | ||
| 255 | + { | ||
| 256 | + public const string DEFAULT = "Admin,Administrator"; | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | + } | ||
| 260 | + } | ||
| 261 | + public static class CreateRoleGroup | ||
| 262 | + { | ||
| 263 | + public static class Attributes | ||
| 264 | + { | ||
| 265 | + public static class Name { } | ||
| 266 | + public static class Roles { } | ||
| 267 | + } | ||
| 268 | + } | ||
| 269 | + public static class CreateUser | ||
| 270 | + { | ||
| 271 | + public static class Attributes | ||
| 272 | + { | ||
| 273 | + public static class Name { } | ||
| 274 | + public static class Password { } | ||
| 275 | + public static class RoleGroups { } | ||
| 276 | + public static class Roles { } | ||
| 277 | + public static class Superuser | ||
| 278 | + { | ||
| 279 | + public static class Values | ||
| 280 | + { | ||
| 281 | + public const bool DEFAULT = false; | ||
| 282 | + } | ||
| 283 | + } | ||
| 284 | + } | ||
| 285 | + } | ||
| 286 | + } | ||
| 287 | + } | ||
| 176 | public static class UserGroups | 288 | public static class UserGroups |
| 177 | { | 289 | { |
| 178 | public static class UserGroup | 290 | public static class UserGroup |
| @@ -199,6 +311,75 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | @@ -199,6 +311,75 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS | ||
| 199 | } | 311 | } |
| 200 | } | 312 | } |
| 201 | #endregion XmlStructure | 313 | #endregion XmlStructure |
| 314 | + #region InitActionParameters | ||
| 315 | + public class InitActionBlock : XmlLinqBase | ||
| 316 | + { | ||
| 317 | + public InitActionBlock() { } | ||
| 318 | + public InitActionBlock(XElement iabxml) | ||
| 319 | + { | ||
| 320 | + Name = iabxml.Attribute(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.Attributes.Name)))?.Value; | ||
| 321 | + if (string.IsNullOrEmpty(Name)) { throw new Exception($"Attribute is mandatory! Name {nameof(XmlStructure.InitActionBlocks.InitActionBlock.Attributes.Name)}."); } | ||
| 322 | + | ||
| 323 | + InitActionList = new List<InitAction>(); | ||
| 324 | + var crlist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoles))); | ||
| 325 | + if (crlist != null && crlist.Any()) { foreach (var cr in crlist) { InitActionList.Add(new CreateRoleAction(cr)); } } | ||
| 326 | + | ||
| 327 | + var crglist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup))); | ||
| 328 | + if (crglist != null && crglist.Any()) { foreach (var crg in crglist) { InitActionList.Add(new CreateRoleGroupAction(crg)); } } | ||
| 329 | + | ||
| 330 | + var culist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser))); | ||
| 331 | + if (culist != null && culist.Any()) { foreach (var cu in culist) { InitActionList.Add(new CreateUserAction(cu)); } } | ||
| 332 | + } | ||
| 333 | + public enum ActionType { CreateRole, CreateRoleGroup, CreateUser, } | ||
| 334 | + public string Name; | ||
| 335 | + public List<InitAction> InitActionList; | ||
| 336 | + public List<InitAction> GetCreateRoleActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateRole).ToList(); } | ||
| 337 | + public List<InitAction> GetCreateRoleGroupActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateRoleGroup).ToList(); } | ||
| 338 | + public List<InitAction> GetCreateUserActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateUser).ToList(); } | ||
| 339 | + } | ||
| 340 | + public class CreateRoleAction : InitAction | ||
| 341 | + { | ||
| 342 | + public CreateRoleAction(XElement cr){ Type = InitActionBlock.ActionType.CreateRole; Roles = GetValue(cr.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoles.Attributes.Roles)), nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoles.Attributes.Roles.Values.DEFAULT)); } | ||
| 343 | + } | ||
| 344 | + public class CreateRoleGroupAction : InitAction | ||
| 345 | + { | ||
| 346 | + public CreateRoleGroupAction(XElement crg) | ||
| 347 | + { | ||
| 348 | + Type = InitActionBlock.ActionType.CreateRoleGroup; | ||
| 349 | + Name = crg.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup.Attributes.Name)).Value; | ||
| 350 | + Roles = crg.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup.Attributes.Roles)).Value; | ||
| 351 | + } | ||
| 352 | + } | ||
| 353 | + public class CreateUserAction : InitAction | ||
| 354 | + { | ||
| 355 | + public CreateUserAction(XElement cu) | ||
| 356 | + { | ||
| 357 | + Type = InitActionBlock.ActionType.CreateUser; | ||
| 358 | + Name = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Name)).Value; | ||
| 359 | + if (string.IsNullOrWhiteSpace(Name)) { Name = "DEFAULT"; } | ||
| 360 | + Password = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Password))?.Value; | ||
| 361 | + if (string.IsNullOrWhiteSpace(Password)) { Password = Name; } | ||
| 362 | + RoleGroups = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.RoleGroups))?.Value; | ||
| 363 | + if (string.IsNullOrWhiteSpace(RoleGroups)) { RoleGroups = ""; } | ||
| 364 | + Roles = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Roles))?.Value; | ||
| 365 | + if (string.IsNullOrWhiteSpace(Roles)) { Roles = ""; } | ||
| 366 | + var superuservaluestring = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Superuser))?.Value; | ||
| 367 | + Superuser = !string.IsNullOrWhiteSpace(superuservaluestring) && (superuservaluestring.ToLower() == bool.TrueString.ToLower()); | ||
| 368 | + } | ||
| 369 | + } | ||
| 370 | + public class InitAction: XmlLinqBase | ||
| 371 | + { | ||
| 372 | + public InitActionBlock.ActionType Type; | ||
| 373 | + public string Name; | ||
| 374 | + public string Password; | ||
| 375 | + public string Roles; | ||
| 376 | + public string RoleGroups; | ||
| 377 | + public string[] RoleArray { get { return Roles.Split(',', ';'); } } | ||
| 378 | + public string[] RoleGroupArray { get { return RoleGroups.Split(',', ';'); } } | ||
| 379 | + public bool Superuser; | ||
| 380 | + } | ||
| 381 | + | ||
| 382 | + #endregion InitActionParameters | ||
| 202 | #region UserGroup | 383 | #region UserGroup |
| 203 | public class UserGroup:XmlLinqBase | 384 | public class UserGroup:XmlLinqBase |
| 204 | { | 385 | { |
Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs
| @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; | @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; | ||
| 32 | // You can specify all the values or you can default the Build and Revision Numbers | 32 | // You can specify all the values or you can default the Build and Revision Numbers |
| 33 | // by using the '*' as shown below: | 33 | // by using the '*' as shown below: |
| 34 | // [assembly: AssemblyVersion("1.0.*")] | 34 | // [assembly: AssemblyVersion("1.0.*")] |
| 35 | -[assembly: AssemblyVersion("1.12.0.0")] | ||
| 36 | -[assembly: AssemblyFileVersion("1.12.0.0")] | 35 | +[assembly: AssemblyVersion("1.13.0.0")] |
| 36 | +[assembly: AssemblyFileVersion("1.13.0.0")] |