protected override void OnModelCreating(ModelBuilder modelBuilder) { List <Type> typeToRegisters = new List <Type>(); foreach (var module in GlobalContext.Modules.Where(x => x.ModuleStatus == (int)NccModule.NccModuleStatus.Active).ToList()) { try { typeToRegisters.AddRange(module.Assembly.DefinedTypes.Select(t => t.AsType())); } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "OnModelCreating", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 60) ); } } //ScanEntities(modelBuilder, typeToRegisters); //SetTableNameByConvention(modelBuilder); base.OnModelCreating(modelBuilder); RegisterUserModuleModels(modelBuilder, typeToRegisters); RegisterCoreModels(modelBuilder); }
private static void RegisterUserModuleModels(ModelBuilder modelBuilder, IEnumerable <Type> typeToRegisters) { var customModelBuilderTypes = typeToRegisters.Where(x => typeof(IModelBuilder).IsAssignableFrom(x)); foreach (var builderType in customModelBuilderTypes) { try { if (builderType != null && builderType != typeof(IModelBuilder)) { var builder = (IModelBuilder)Activator.CreateInstance(builderType); builder.Build(modelBuilder); } } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "RegisterUserModuleModels", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 60) ); } } }
public void AddModuleAuthorizationHandlers(IServiceCollection services) { services.AddScoped <NccAuthRequireHandler>(); var activeModules = instantiatedModuleList.Where(x => x.ModuleStatus == (int)NccModule.NccModuleStatus.Active).ToList(); foreach (var module in activeModules) { try { var actionFilters = module.Assembly.GetTypes().Where(x => typeof(INccAuthorizationHandler).IsAssignableFrom(x)).ToList(); foreach (var item in actionFilters) { services.AddScoped(item); } } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddModuleAuthorizationHandlers", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } } }
public void AddShortcodes(IServiceCollection services, ILogger logger) { _logger = logger; var activeModules = instantiatedModuleList.Where(x => x.ModuleStatus == (int)NccModule.NccModuleStatus.Active).ToList(); foreach (var module in activeModules) { try { var shortCodeTypeList = module.Assembly.GetTypes().Where(x => typeof(IShortCode).IsAssignableFrom(x)).ToList(); foreach (var item in shortCodeTypeList) { services.AddScoped(item); } } catch (Exception ex) { _logger.LogError(ex.Message, ex); GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddShortcodes", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } } }
public void AddModuleWidgets(IServiceCollection services) { foreach (var module in instantiatedModuleList) { try { module.Widgets = new List <Widget>(); var widgetTypeList = module.Assembly.GetTypes().Where(x => typeof(Widget).IsAssignableFrom(x)).ToList(); foreach (var widgetType in widgetTypeList) { services.AddScoped(widgetType); } } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddModuleWidgets", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } } }
public async Task LoadModuleMenus(ILogger logger) { _logger = logger; foreach (var item in instantiatedModuleList.Where(x => x.ModuleStatus == (int)NccModule.NccModuleStatus.Active).ToList()) { try { item.Menus = LoadMenus(item); } catch (Exception ex) { _logger.LogError(ex.Message, ex); GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "LoadMenus", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 2, 0) ); } } }
/// <summary> /// Provide admin menu and it's items /// </summary> /// <returns>A dictionary of menu and menu items. Key is the menu and valus is a list of menu items.</returns> public static Dictionary <AdminMenu, List <AdminMenuItem> > GetModulesAdminMenus() { Dictionary <AdminMenu, List <AdminMenuItem> > adminMenuDic = new Dictionary <AdminMenu, List <AdminMenuItem> >(); foreach (var module in GlobalContext.GetActiveModules()) { var controllers = module.Assembly.DefinedTypes.Select(t => t.AsType()).Where(x => typeof(NccController).IsAssignableFrom(x)); foreach (var controller in controllers) { try { var atrib = controller.GetTypeInfo().GetCustomAttribute <AdminMenu>(); if (atrib != null && atrib.IsVisible) { var key = adminMenuDic.Keys.Where(x => x.Name == atrib.Name).FirstOrDefault(); if (key == null) { adminMenuDic.Add(atrib, new List <AdminMenuItem>()); key = atrib; } var actions = controller.GetMethods(); foreach (var item in actions) { var menuItem = item.GetCustomAttribute <AdminMenuItem>(); if (menuItem != null && menuItem.IsVisible) { menuItem.Module = module.ModuleName; menuItem.Area = module.Area; menuItem.Controller = controller.Name.Substring(0, controller.Name.Length - 10); menuItem.Action = item.Name; adminMenuDic[key].Add(menuItem); if (string.IsNullOrWhiteSpace(module.Area)) { menuItem.Url = "/" + menuItem.Controller + "/" + menuItem.Action; } else { menuItem.Url = "/" + module.Area + "/" + menuItem.Controller + "/" + menuItem.Action; } } } } } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage(new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "MenuHelper", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 1, 0)); } } } return(adminMenuDic); }
public ThemeActivity Handle(OnThemeActivity message) { GlobalMessageRegistry.RegisterMessage(new GlobalMessage() { For = GlobalMessage.MessageFor.Both, Registrater = "CoreModule", Text = message.Data.ActivityType.ToString(), Type = GlobalMessage.MessageType.Info }, new System.TimeSpan(0, 0, 10)); return(message.Data); }
private void RegisterErrorMessage(string message) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { Registrater = typeof(ThemeManager).Name, Text = message, Type = GlobalMessage.MessageType.Error, For = GlobalMessage.MessageFor.Admin }, new TimeSpan(0, 1, 0) ); }
public void OnException(ExceptionContext context) { _logger.LogError(context.Exception, "Global Exception Filter"); GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "GlobalExceptionFilter", Text = context.Exception?.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 10) ); }
public Task SendEmailAsync(string email, string subject, string message) { var smtpSettings = _nccSettingsService.GetByKey <SmtpSettings>(); if (smtpSettings == null) { _logger.LogError("SMTP Settings not found"); GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Both, Registrater = nameof(AuthMessageSender), Type = GlobalMessage.MessageType.Error, Text = "Email Send Failed. SMTP Settings not set yet." }, new System.TimeSpan(0, 1, 0) ); } else { var emailMessage = new MimeMessage(); emailMessage.From.Add(new MailboxAddress(smtpSettings.FromName, smtpSettings.FromEmail)); emailMessage.To.Add(new MailboxAddress("User", email)); emailMessage.Subject = subject; emailMessage.Body = new TextPart(TextFormat.Html) { Text = message }; using (var client = new SmtpClient()) { client.LocalDomain = smtpSettings.Host; client.ConnectAsync(smtpSettings.Host, smtpSettings.Port, smtpSettings.UseSSL).Wait(); client.Authenticate(new NetworkCredential(smtpSettings.UserName, smtpSettings.Password)); client.SendAsync(emailMessage).Wait(); client.DisconnectAsync(true).Wait(); } } return(Task.FromResult(0)); }
public List <Widget> RegisterModuleWidgets(IMvcBuilder mvcBuilder, IServiceCollection services, IServiceProvider serviceProvider, ILogger logger) { _logger = logger; var widgetList = new List <Widget>(); foreach (var module in instantiatedModuleList) { try { if (module.ModuleStatus == (int)NccModule.NccModuleStatus.Active) { module.Widgets = new List <Widget>(); var widgetTypeList = module.Assembly.GetTypes().Where(x => typeof(Widget).IsAssignableFrom(x)).ToList(); foreach (var widgetType in widgetTypeList) { //var widgetInstance = (IWidget)Activator.CreateInstance(widgetType); var widgetInstance = (Widget)serviceProvider.GetService(widgetType); module.Widgets.Add(widgetInstance); widgetList.Add(widgetInstance); GlobalContext.Widgets.Add(widgetInstance); //GlobalContext.WidgetTypes.Add(widgetInstance.WidgetId, widgetType); } } } catch (Exception ex) { _logger.LogError(ex.Message, ex); GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "RegisterModuleWidget", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } } return(widgetList); }
public void LoadModuleInfo(IModule module, IModule moduleInfo) { var moduleConfigFile = Path.Combine(moduleInfo.Path, Constants.ModuleConfigFileName); if (File.Exists(moduleConfigFile)) { var moduleInfoFileJson = File.ReadAllText(moduleConfigFile); var loadedModule = JsonConvert.DeserializeObject <Module>(moduleInfoFileJson); module.ModuleId = loadedModule.ModuleId; module.AntiForgery = loadedModule.AntiForgery; module.Author = loadedModule.Author; module.Category = loadedModule.Category; module.Dependencies = loadedModule.Dependencies; module.Description = loadedModule.Description; module.ModuleId = loadedModule.ModuleId; module.TablePrefix = loadedModule.TablePrefix; module.ModuleTitle = loadedModule.ModuleTitle; module.MinNccVersion = loadedModule.MinNccVersion; module.MaxNccVersion = loadedModule.MaxNccVersion; module.SortName = loadedModule.SortName; module.Version = loadedModule.Version; module.Website = loadedModule.Website; module.Assembly = moduleInfo.Assembly; module.Path = moduleInfo.Path; module.ModuleStatus = moduleInfo.ModuleStatus; module.Folder = moduleInfo.Folder; } else { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "LoadModuleInfo", Text = $"Could not load module info for '{module.ModuleTitle}'", Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } }
public void OnActionExecuting(ActionExecutingContext context) { var isAuthorized = true; var ctrl = (NccController)context.Controller; var action = (ControllerActionDescriptor)context.ActionDescriptor; var model = ctrl.ViewData.Model; var actionAttributes = ctrl.ControllerContext.ActionDescriptor.MethodInfo.GetCustomAttributes(true); // Check Authorization attributes. foreach (var item in actionAttributes) { if (item is NccAuthorize) { var attrib = (NccAuthorize)item; var nccAuthRequirement = GetNccAuthRequirement(attrib, ctrl); var authorizationService = (INccAuthorizationHandler)context.HttpContext.RequestServices.GetService(typeof(NccAuthRequireHandler)); var handlerName = attrib.GetHandlerClassName(); if (handlerName != nameof(NccAuthRequireHandler)) { var handlerType = GlobalContext.GetTypeContainsInModules(handlerName); if (handlerType == null) { GlobalMessageRegistry.RegisterMessage(new GlobalMessage() { For = GlobalMessage.MessageFor.Both, ForUsers = new List <string>() { context.HttpContext.User.Identity.Name }, Registrater = "NccAuthFilter", Text = $"No implimentation of handler class name {handlerName} found. Please check handler class name at NccAuthorize attribute.", Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30)); } else { authorizationService = (INccAuthorizationHandler)context.HttpContext.RequestServices.GetService(handlerType); } } var result = authorizationService.HandleRequirement(context, nccAuthRequirement, model).Result; isAuthorized = result.Succeeded; if (isAuthorized == false) { context.Result = new ChallengeResult(new AuthenticationProperties()); context.HttpContext.Items["ErrorMessage"] = "You have not enough permission."; context.HttpContext.Response.Redirect("/Home/NotAuthorized"); return; } } } if (isAuthorized == false) { context.Result = new ChallengeResult(new AuthenticationProperties()); context.HttpContext.Items["ErrorMessage"] = "You have not enough permission."; context.HttpContext.Response.Redirect("/Home/NotAuthorized"); } }
public void AddModuleServices(IServiceCollection services) { foreach (var module in instantiatedModuleList) { try { if (module.ModuleStatus == (int)NccModule.NccModuleStatus.Active) { var repositoryTypes = module.Assembly.GetTypes().Where(x => x.BaseType.IsGenericType).Where(y => y.BaseType.GetGenericTypeDefinition() == typeof(BaseRepository <,>)).ToList(); foreach (var item in repositoryTypes) { var singleton = item.GetInterfaces().Where(x => typeof(ISingleton).IsAssignableFrom(x)).FirstOrDefault(); if (singleton != null) { services.AddSingleton(item); continue; } var scoped = item.GetInterfaces().Where(x => typeof(IScoped).IsAssignableFrom(x)).FirstOrDefault(); if (scoped != null) { services.AddScoped(item); continue; } var transient = item.GetInterfaces().Where(x => typeof(ITransient).IsAssignableFrom(x)).FirstOrDefault(); if (transient != null) { services.AddTransient(item); continue; } services.AddScoped(item); } var serviceTypes = module.Assembly.GetTypes().Where(x => x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(IBaseService <>))).ToList(); foreach (var item in serviceTypes) { var singleton = item.GetInterfaces().Where(x => typeof(ISingleton).IsAssignableFrom(x)).FirstOrDefault(); if (singleton != null) { services.AddSingleton(item); continue; } var scoped = item.GetInterfaces().Where(x => typeof(IScoped).IsAssignableFrom(x)).FirstOrDefault(); if (scoped != null) { services.AddScoped(item); continue; } var transient = item.GetInterfaces().Where(x => typeof(ITransient).IsAssignableFrom(x)).FirstOrDefault(); if (transient != null) { services.AddTransient(item); continue; } services.AddScoped(item); } } } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddModuleServices", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); } } }
public List <IModule> AddModulesAsApplicationPart(IMvcBuilder mvcBuilder, IServiceCollection services, IServiceProvider serviceProvider) { var nccSettingsService = serviceProvider.GetService <INccSettingsService>(); foreach (var module in modules) { try { var moduleInitializerType = module.Assembly.GetTypes().Where(x => typeof(IModule).IsAssignableFrom(x)).FirstOrDefault(); if (moduleInitializerType != null && moduleInitializerType != typeof(IModule)) { var moduleInstance = (IModule)Activator.CreateInstance(moduleInitializerType); LoadModuleInfo(moduleInstance, module); NccModule.NccModuleStatus moduleStatus = VerifyModuleInstallation(moduleInstance, serviceProvider); moduleInstance.ModuleStatus = (int)moduleStatus; if (moduleStatus == NccModule.NccModuleStatus.Active) { // Register controller from modules mvcBuilder.AddApplicationPart(module.Assembly); // Register dependency in modules LoadModuleDependencies(module.Path, module.Folder); moduleInstance.Init(services, nccSettingsService); } else if (moduleStatus == NccModule.NccModuleStatus.Duplicate) { //TODO: Raise duplicate error message GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddModulesAsApplicationPart", Text = $"Duplicate module {module.ModuleTitle}", Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 30) ); continue; } instantiatedModuleList.Add(moduleInstance); GlobalContext.Modules.Add(moduleInstance); } } catch (ReflectionTypeLoadException rtle) { } catch (Exception ex) { GlobalMessageRegistry.RegisterMessage( new GlobalMessage() { For = GlobalMessage.MessageFor.Admin, Registrater = "AddModulesAsApplicationPart", Text = ex.Message, Type = GlobalMessage.MessageType.Error }, new TimeSpan(0, 0, 60) ); } } GlobalContext.SetModuleDependencies(_moduleDependencies); services.Configure <RazorViewEngineOptions>(options => { options.ViewLocationExpanders.Add(new ModuleViewLocationExpendar()); }); mvcBuilder.AddRazorOptions(o => { foreach (var module in instantiatedModuleList.Where(x => x.ModuleStatus == (int)NccModule.NccModuleStatus.Active).ToList()) { o.AdditionalCompilationReferences.Add(MetadataReference.CreateFromFile(module.Assembly.Location)); } }); return(instantiatedModuleList); }