From 5968301d1f8b49d5fffaa98fe36eca9a0343f60f Mon Sep 17 00:00:00 2001 From: Schwirg László Date: Tue, 6 Sep 2022 18:25:56 +0200 Subject: [PATCH] v1.13.0.0 - user kezelés funkcióinak bővítése (init action blokkok) --- Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs | 1 + Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------- Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs | 4 ++-- 4 files changed, 350 insertions(+), 30 deletions(-) diff --git a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs index dffe79d..8cb44ea 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - CommandLineParser.cs @@ -198,6 +198,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.CommandLineParserNS public static class CreateSuperuser { public const string KEY = "CSU"; } public static class CreateAdminusers { public const string KEY = "CAU"; } public static class DeleteUsers { public const string KEY = "DEU"; } + public static class ExecuteInitAction{ public const string KEY = "EIA"; } } } public static class WebApplicationManager diff --git a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs index c832174..099165e 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/ConsoleFunction - Tools - Membership.cs @@ -136,33 +136,41 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS { string rgnamelist = string.Join(",", MembershipDBContext.RoleGroups.Select(rg => rg.Name).ToArray()); if (!string.IsNullOrWhiteSpace(rgnamelist)) { Assign.RoleGroupsToUsers(rgnamelist, username); } - foreach (var rn in Roles.GetAllRoles()) + foreach (var rn in System.Web.Security.Roles.GetAllRoles()) { - if (!Users.IsInRole(username, rn)) { Roles.AddUserToRole(username, rn); } + if (!Users.IsInRole(username, rn)) { System.Web.Security.Roles.AddUserToRole(username, rn); } } } else if (administrator) { - if (!Roles.IsUserInRole(username, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(username, Constants.ROLENAME_ADMINISTRATOR); } - if (!Roles.IsUserInRole(username, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(username, Constants.ROLENAME_ADMIN); } + if (!System.Web.Security.Roles.IsUserInRole(username, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(username, Constants.ROLENAME_ADMINISTRATOR); } + if (!System.Web.Security.Roles.IsUserInRole(username, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(username, Constants.ROLENAME_ADMIN); } } string[] selectedrolenames; - if (rolenames != null) + if (rolenames != null && rolenames.Any()) { - selectedrolenames = rolenames.Contains("*") ? Roles.GetAllRoles() : rolenames; + selectedrolenames = rolenames.Contains("*") ? System.Web.Security.Roles.GetAllRoles() : rolenames; if (selectedrolenames != null && selectedrolenames.Any()) { - foreach (var rname in selectedrolenames) { if (!Roles.IsUserInRole(username, rname)) { Roles.AddUserToRole(username, rname); } } + foreach (var rname in selectedrolenames) + { + if (string.IsNullOrWhiteSpace(rname)) { continue; } + if (!System.Web.Security.Roles.IsUserInRole(username, rname)) { System.Web.Security.Roles.AddUserToRole(username, rname); } + } } } string[] selectedrolegroupnames; - if (rolegroupnames != null) + if (rolegroupnames != null && rolegroupnames.Any()) { selectedrolegroupnames = rolegroupnames.Contains("*") ? RoleGroups.GetAllNames().ToArray() : rolegroupnames; if (selectedrolegroupnames != null && selectedrolegroupnames.Any()) { - foreach (var rgname in selectedrolegroupnames) { if (!RoleGroups.IsUserInRoleGroup(username, rgname)) { Assign.RoleGroupsToUsers(rgname, username); } } + foreach (var rgname in selectedrolegroupnames) + { + if (string.IsNullOrWhiteSpace(rgname)) { continue; } + if (!RoleGroups.IsUserInRoleGroup(username, rgname)) { Assign.RoleGroupsToUsers(rgname, username); } + } } } return user; @@ -172,24 +180,24 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS public static void CreateAdminRolesAndUsers() { MembershipUser user; - if (!Roles.RoleExists(Constants.ROLENAME_ADMINISTRATOR)) { Roles.CreateRole(Constants.ROLENAME_ADMINISTRATOR); } - if (!Roles.RoleExists(Constants.ROLENAME_ADMIN)) { Roles.CreateRole(Constants.ROLENAME_ADMIN); } + if (!System.Web.Security.Roles.RoleExists(Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.CreateRole(Constants.ROLENAME_ADMINISTRATOR); } + if (!System.Web.Security.Roles.RoleExists(Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.CreateRole(Constants.ROLENAME_ADMIN); } user = Membership.GetUser(Constants.USERNAME_ADMIN); if (user == null) { user = Membership.CreateUser(Constants.USERNAME_ADMIN, Constants.PASSWORD_ADMIN); } - if (!Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR); } - if (!Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN); } + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMINISTRATOR); } + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMIN, Constants.ROLENAME_ADMIN); } user = Membership.GetUser(Constants.USERNAME_ADMINISTRATOR); if (user == null) { user = Membership.CreateUser(Constants.USERNAME_ADMINISTRATOR, Constants.PASSWORD_ADMINISTRATOR); } - if (!Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR)) { Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR); } - if (!Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN)) { Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN); } + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMINISTRATOR); } + if (!System.Web.Security.Roles.IsUserInRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN)) { System.Web.Security.Roles.AddUserToRole(Constants.USERNAME_ADMINISTRATOR, Constants.ROLENAME_ADMIN); } } #endregion CreateAdminRolesAndUsers #region IsInRole public method @@ -397,6 +405,109 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS #endregion Remove methods } #endregion RoleGroups + #region Roles + public static class Roles + { + #region Get methods + /// + /// Szerep lekérése az egyedi azonosítója alapján. + /// + /// A keresett funkció azonosítója. + /// A kért szerepkör, egyébként null. + public static DAL.Role Get(Guid id) + { + return MembershipDBContext.Roles.FirstOrDefault(x => x.RoleId == id); + } + /// + /// Szerep lekérése az egyedi neve alapján. + /// + /// A keresett szerep egyedi neve. + /// A kért szerep, egyébként null. + public static DAL.Role Get(string name) + { + return MembershipDBContext.Roles.FirstOrDefault(x => x.RoleName== name); + } + /// + /// Szerepek neveinek lekérése. + /// + /// A szerepek neveinek listája. + public static List GetAllNames() + { + return MembershipDBContext.Roles.Select(r => r.RoleName).ToList(); + } + /// + /// A megadott nevű szerep benne van-e a megadott szerepkörben tartozik-e. + /// + /// + /// + /// true, ha igen + public static bool IsRoleInRoleGroup(string rolename, string rolegroupname) + { + return MembershipDBContext.RoleGroupRoles.Select(rtorg => rtorg.Role.RoleName == rolename && rtorg.RoleGroup.Name == rolegroupname).ToList().Any(); + } + #endregion Get methods + + #region Create method + /// + /// Szerep létrehozása. + /// + /// A létrehozandó szerep neve. + /// + /// Ha üres vagy null a szerep neve. + /// Ha már létezik a megadott név. + /// + public static void Create(string rolename, string appname=null) + { + if (String.IsNullOrWhiteSpace(rolename)) { throw new ApplicationException("Role név megadása kötelező!"); } + if (appname==null) { appname = MembershipDBContext.Applications.FirstOrDefault(a => a.ApplicationName == "/")?.ApplicationName; } + if (appname == null) { appname = MembershipDBContext.Applications.First()?.ApplicationName; } + if (appname==null) { throw new ApplicationException("Application nem létezik!"); } + var app = MembershipDBContext.Applications.FirstOrDefault(a => a.ApplicationName == appname); + + if (MembershipDBContext.Roles.Any(x => x.RoleName == rolename)) { throw new ApplicationException($"Role {rolename} already exist!"); } + + System.Web.Security.Roles.CreateRole(rolename); + var rolecreated = MembershipDBContext.Roles.First(x => x.RoleName == rolename); + if (rolecreated==null) { throw new ApplicationException($"Creating role failed. Role name:{rolename}!"); } + //MembershipDBContext.Roles.Add(new DAL.Role() { RoleName = rolename, ApplicationId=app.ApplicationId,Description=null, }); + rolecreated.ApplicationId = app.ApplicationId; + MembershipDBContext.SaveChanges(); + } + #endregion Create method + + #region Remove methods + /// + /// Szerep törlése az egyedi azonosítója megadásával. + /// A szerephez tartozó összerendelések is megszűnnek! + /// + /// Törlendő szerep egyedi azonosítója. + /// Ha nem található a törlendő szerep. + public static void Remove(int id) + { + var row = MembershipDBContext.Roles.Find(id); + if (row == null) { throw new ApplicationException("Role does not exist!!"); } + else { MembershipDBContext.Roles.Remove(row); MembershipDBContext.SaveChanges(); } + } + /// + /// Szerep törlése az egyedi neve megadásával. + /// A szerephez tartozó összerendelések is megszűnnek! + /// + /// Törlendő szerep egyedi neve. + /// Ha nem található a törlendő szerep. + public static void Remove(string name) + { + var row = MembershipDBContext.Roles.FirstOrDefault(x => x.RoleName == name); + if (row == null) { throw new ApplicationException("Role does not exist!!"); } + else + { + System.Web.Security.Roles.DeleteRole(name); + MembershipDBContext.Roles.Remove(row); + MembershipDBContext.SaveChanges(); + } + } + #endregion Remove methods + } + #endregion Roles #region Assign public static class Assign { @@ -433,7 +544,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS foreach (var username in usernamelist) { CheckUsersExists(username); - if (!Users.IsInRole(username, rolename)) { Roles.AddUserToRole(username, rolename); } + if (!Users.IsInRole(username, rolename)) { System.Web.Security.Roles.AddUserToRole(username, rolename); } } } } @@ -571,17 +682,17 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS if (user != null) { // 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 - if (Roles.GetRolesForUser(user.UserName).Any()) + if (System.Web.Security.Roles.GetRolesForUser(user.UserName).Any()) { - Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName)); + System.Web.Security.Roles.RemoveUserFromRoles(user.UserName, System.Web.Security.Roles.GetRolesForUser(user.UserName)); } foreach (DAL.RoleGroup urg in MembershipDBContext.RoleGroups.Where(x => x.Users.Any(y => y.UserId == userId))) { foreach (string roleName in urg.Roles.Select(x => x.Role.RoleName)) { - if (!Roles.IsUserInRole(user.UserName, roleName)) + if (!System.Web.Security.Roles.IsUserInRole(user.UserName, roleName)) { - Roles.AddUserToRole(user.UserName, roleName); + System.Web.Security.Roles.AddUserToRole(user.UserName, roleName); } } } @@ -794,6 +905,32 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS public string Description { get; set; } } #endregion table-Role + #region table-Application + /// + /// DefaultMembershipProvider által létrehozott User tábla. + /// + [Table("Applications", Schema = "dbo")] + public partial class Application + { + /// + /// Alkalmazás egyedi azonosítója. + /// + [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] + public Guid ApplicationId { get; set; } + + /// + /// Szerep neve. + /// + [Required, MaxLength(256)] + public string ApplicationName { get; set; } + + /// + /// Alkalmazás rövid leírása. + /// + [MaxLength(256)] + public string Description { get; set; } + } + #endregion table-Application #region table-SecondaryFunction /// /// Lehetséges funkciókat tartalmazó táblázat, mely funkciókhoz @@ -1044,7 +1181,8 @@ namespace Vrh.Log4Pro.MaintenanceConsole.ToolsNS #region DbSets - #region !!! A migrációból kivett két tábla. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! + #region !!! A migrációból kivett táblák. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! + public virtual DbSet Applications { get; set; } public virtual DbSet Roles { get; set; } public virtual DbSet Users { get; set; } #endregion !!! A migrációból kivett két tábla. Ezeket a DefaultMembershipProvider kezeli és hozza létre. !!! diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs index dedbb82..385cfa2 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - UserManager.cs @@ -23,7 +23,7 @@ using System.Text.RegularExpressions; namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS { - #region MaintenanceTools class + #region UserManager class public static class UserManager { private static Log4ProUserManagerXmlProcessor Config; @@ -44,6 +44,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateSuperuser.KEY, "Create superuser", CreateSuperuser, new Menu.ExecutorParameter(cfg: config))) .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.CreateAdminusers.KEY, "Create Admin and Administrator roles and users", CreateAdminusers, new Menu.ExecutorParameter(cfg: config, null))) .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.DeleteUsers.KEY, "Remove user", DeleteUsers, new Menu.ExecutorParameter(cfg: config, null))) + .AddMenuItem(new Menu.Item(CLP.Module.Log4ProUserManager.Functions.ExecuteInitAction.KEY, "Execute init actionblock", ExecuteInitActionBlock, new Menu.ExecutorParameter(cfg: config, null))) .SetMenuHeaderDisplayer(UserListDisplayer) .SetSelectionMode(Menu.SelectionMode.Single); menufunctions.ExecuteMenu(); @@ -70,6 +71,65 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS #endregion private UserListDisplayer #region First level Executors with UI + #region ExecuteInitActionBlock + private static object ExecuteInitActionBlock(object parameter, object o) + { + var config = (parameter as Menu.ExecutorParameter).GetConfig(); + if (config.InitActionBlockList == null || !config.InitActionBlockList.Any()) return o; + + var initactionblocknamelist = string.Join(",", config.InitActionBlockList.Select(iab => iab.Name).ToArray()); + ColorConsole.WriteLine($"Select one init action block from this list: {initactionblocknamelist}", ConsoleColor.Yellow); + var iabname = ColorConsole.ReadLine($"Enter init action block name:", ConsoleColor.Yellow).ToLower(); + if (iabname == "EX") { return null; } + 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(); + if (iabname == "EX") { return null; } + try + { + var iab = config.InitActionBlockList.FirstOrDefault(_iab => _iab.Name.ToLower() == iabname); + if (iab == null) { throw new ApplicationException($"InitActionBlock with name '{iabname}' does not exist!"); } + foreach (var cr in iab.GetCreateRoleActions()) + { + if (stepbystep) + { + ColorConsole.WriteLine($"Action:{cr.Type}, role names: {cr.Roles}.", ConsoleColor.Yellow); + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; + } + foreach (var rn in cr.RoleArray) { try { MembershipTools.Roles.Create(rn); } catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; } + } + foreach (var cr in iab.GetCreateRoleGroupActions()) + { + if (stepbystep) + { + ColorConsole.WriteLine($"Action:{cr.Type}, rolegroup name: {cr.Name}, roles: {cr.Roles}.", ConsoleColor.Yellow); + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; + } + try + { + MembershipTools.RoleGroups.Create(cr.Name); + MembershipTools.Assign.RolesToRoleGroups(cr.Roles, cr.Name); + } catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; + + } + foreach (var cr in iab.GetCreateUserActions()) + { + if (stepbystep) + { + var crtext = cr.Superuser ? $"superuser" : $"rolegroups: {cr.RoleGroups}, roles: {cr.Roles}"; + ColorConsole.WriteLine($"Action:{cr.Type}, username: {cr.Name}[{cr.Password}], {crtext}.", ConsoleColor.Yellow); + var sel = ColorConsole.ReadLine($"Press enter to continue.", ConsoleColor.Gray); if (sel.ToLower() == "ex") return o; + } + try + { + MembershipTools.Users.Create(cr.Name, cr.Password, administrator: false, superuser: cr.Superuser, rolenames: cr.RoleArray, rolegroupnames: cr.RoleGroupArray); + } + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); }; + } + ColorConsole.WriteLine($"Executing init action block '{iabname}' was successful!", ConsoleColor.Green); + } + catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } + return o; + } + #endregion ExecuteInitActionBlock #region CreateSuperuser private static object CreateSuperuser(object parameter, object o) { @@ -123,7 +183,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS #endregion DeleteUsers #endregion First level Executors with UI } - #endregion MaintenanceTools class + #endregion UserManager class #region MaintenanceToolsXmlProcessor class public class Log4ProUserManagerXmlProcessor : XmlParser { @@ -131,6 +191,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS public string Xml_SQLConnectionString; public string Xml_ProtectedUserNameCommaList; public List UserGroupList; + public List InitActionBlockList; public List ProtectedUserNameList; #endregion fields #region constructor @@ -143,13 +204,16 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS if (string.IsNullOrEmpty(Xml_ProtectedUserNameCommaList)) { Xml_ProtectedUserNameCommaList = XmlStructure.Attributes.ProtectedUsernameList.Values.DEFAULT; } ProtectedUserNameList = Xml_ProtectedUserNameCommaList.Split(',').ToList(); UserGroupList = new List(); - var ugxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.UserGroups))).Elements(XName.Get(nameof(XmlStructure.UserGroups.UserGroup))); + var ugxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.UserGroups)))?.Elements(XName.Get(nameof(XmlStructure.UserGroups.UserGroup))); if (ugxmllist!=null && ugxmllist.Any()) { - foreach (var ugxml in ugxmllist) - { - UserGroupList.Add(new UserGroup(ugxml)); - }; + foreach (var ugxml in ugxmllist) { UserGroupList.Add(new UserGroup(ugxml)); }; + } + InitActionBlockList = new List(); + var iabxmllist = RootElement.Element(XName.Get(nameof(XmlStructure.InitActionBlocks)))?.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock))); + if (iabxmllist != null && iabxmllist.Any()) + { + foreach (var iabxml in iabxmllist) { InitActionBlockList.Add(new InitActionBlock(iabxml)); }; } } #region ProtectedUser @@ -173,6 +237,54 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS public static class ProtectedUsernameList { public static class Values { public const string DEFAULT = "Admin,Administrator"; } } } + public static class InitActionBlocks + { + public static class InitActionBlock + { + public static class Attributes + { + public static class Name { } + } + public static class CreateRoles + { + public static class Attributes + { + public static class Roles + { + public static class Values + { + public const string DEFAULT = "Admin,Administrator"; + } + } + } + } + public static class CreateRoleGroup + { + public static class Attributes + { + public static class Name { } + public static class Roles { } + } + } + public static class CreateUser + { + public static class Attributes + { + public static class Name { } + public static class Password { } + public static class RoleGroups { } + public static class Roles { } + public static class Superuser + { + public static class Values + { + public const bool DEFAULT = false; + } + } + } + } + } + } public static class UserGroups { public static class UserGroup @@ -199,6 +311,75 @@ namespace Vrh.Log4Pro.MaintenanceConsole.UserManagerNS } } #endregion XmlStructure + #region InitActionParameters + public class InitActionBlock : XmlLinqBase + { + public InitActionBlock() { } + public InitActionBlock(XElement iabxml) + { + Name = iabxml.Attribute(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.Attributes.Name)))?.Value; + if (string.IsNullOrEmpty(Name)) { throw new Exception($"Attribute is mandatory! Name {nameof(XmlStructure.InitActionBlocks.InitActionBlock.Attributes.Name)}."); } + + InitActionList = new List(); + var crlist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoles))); + if (crlist != null && crlist.Any()) { foreach (var cr in crlist) { InitActionList.Add(new CreateRoleAction(cr)); } } + + var crglist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup))); + if (crglist != null && crglist.Any()) { foreach (var crg in crglist) { InitActionList.Add(new CreateRoleGroupAction(crg)); } } + + var culist = iabxml.Elements(XName.Get(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser))); + if (culist != null && culist.Any()) { foreach (var cu in culist) { InitActionList.Add(new CreateUserAction(cu)); } } + } + public enum ActionType { CreateRole, CreateRoleGroup, CreateUser, } + public string Name; + public List InitActionList; + public List GetCreateRoleActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateRole).ToList(); } + public List GetCreateRoleGroupActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateRoleGroup).ToList(); } + public List GetCreateUserActions() { return InitActionList.Where(ia => ia.Type == Log4ProUserManagerXmlProcessor.InitActionBlock.ActionType.CreateUser).ToList(); } + } + public class CreateRoleAction : InitAction + { + 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)); } + } + public class CreateRoleGroupAction : InitAction + { + public CreateRoleGroupAction(XElement crg) + { + Type = InitActionBlock.ActionType.CreateRoleGroup; + Name = crg.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup.Attributes.Name)).Value; + Roles = crg.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateRoleGroup.Attributes.Roles)).Value; + } + } + public class CreateUserAction : InitAction + { + public CreateUserAction(XElement cu) + { + Type = InitActionBlock.ActionType.CreateUser; + Name = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Name)).Value; + if (string.IsNullOrWhiteSpace(Name)) { Name = "DEFAULT"; } + Password = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Password))?.Value; + if (string.IsNullOrWhiteSpace(Password)) { Password = Name; } + RoleGroups = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.RoleGroups))?.Value; + if (string.IsNullOrWhiteSpace(RoleGroups)) { RoleGroups = ""; } + Roles = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Roles))?.Value; + if (string.IsNullOrWhiteSpace(Roles)) { Roles = ""; } + var superuservaluestring = cu.Attribute(nameof(XmlStructure.InitActionBlocks.InitActionBlock.CreateUser.Attributes.Superuser))?.Value; + Superuser = !string.IsNullOrWhiteSpace(superuservaluestring) && (superuservaluestring.ToLower() == bool.TrueString.ToLower()); + } + } + public class InitAction: XmlLinqBase + { + public InitActionBlock.ActionType Type; + public string Name; + public string Password; + public string Roles; + public string RoleGroups; + public string[] RoleArray { get { return Roles.Split(',', ';'); } } + public string[] RoleGroupArray { get { return RoleGroups.Split(',', ';'); } } + public bool Superuser; + } + + #endregion InitActionParameters #region UserGroup public class UserGroup:XmlLinqBase { diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 6e51a73..c1b14f6 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.12.0.0")] -[assembly: AssemblyFileVersion("1.12.0.0")] +[assembly: AssemblyVersion("1.13.0.0")] +[assembly: AssemblyFileVersion("1.13.0.0")] -- libgit2 0.21.2