public PermissionViewModel(NccPermission permission) { _permission = permission; Id = permission.Id; Group = permission.Group; Name = permission.Name; Description = permission.Description; Rank = permission.Rank; ModuleCount = permission.PermissionDetails.GroupBy(x => x.ModuleId).Count(); MenuCount = permission.PermissionDetails.GroupBy(x => x.Action).Count(); UserCount = permission.Users.Count; Modules = new List <ModuleViewModel>(); foreach (var item in GlobalContext.GetActiveModules()) { var module = new ModuleViewModel(); var menus = item.Menus; var adminMenus = menus .Where(x => x.Type == Menu.MenuType.Admin) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(x => x.MenuItems).ToList() } ).ToList(); var siteMenus = menus.Where(x => x.Type == Menu.MenuType.WebSite) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(z => z.MenuItems).ToList() } ).ToList(); foreach (var adminMenu in adminMenus) { var menu = new MenuViewModel() { Type = "Admin", Name = adminMenu.MenuName, Order = adminMenu.Menu.Order, MenuItems = GetMenuItems(adminMenu.Items, item.ModuleId), }; module.AdminMenus.Add(menu); } foreach (var webSiteMenu in siteMenus) { var menu = new MenuViewModel() { Type = "WebSite", Name = webSiteMenu.MenuName, Order = webSiteMenu.Menu.Order, MenuItems = GetMenuItems(webSiteMenu.Items, item.ModuleId), }; module.SiteMenus.Add(menu); } Modules.Add(module); } }
private List <ModuleViewModel> GetAllowModules(NccUser user) { var activeModules = GlobalContext.GetActiveModules(); var modules = new List <ModuleViewModel>(); foreach (var module in modules) { } return(modules); }
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { if (!values.ContainsKey("area")) { return(false); } var area = values["area"].ToString().ToLower(); var exists = GlobalContext.GetActiveModules().Where(x => x.Area.Equals(area)).Count() > 0; return(exists); }
public ActionResult Index(string sLayout = "") { ViewBag.sLayout = ""; ViewBag.Modules = GlobalContext.GetActiveModules(); ViewBag.Theme = ThemeHelper.ActiveTheme; if (ThemeHelper.ActiveTheme.Layouts.Where(x => x.Name == sLayout).Count() > 0) { ViewBag.sLayout = sLayout; } ViewBag.WebsiteWidgetZones = _nccWebSiteWidgetService.LoadAll().OrderBy(x => x.WidgetOrder).ToList(); return(View()); }
public UserViewModel(NccPermission permission) { _permission = permission; PermissionId = permission.Id; DenyModules = new List <ModuleViewModel>(); AllowModules = new List <ModuleViewModel>(); foreach (var item in GlobalContext.GetActiveModules()) { var module = new ModuleViewModel(); var menus = item.Menus; var adminMenus = menus .Where(x => x.Type == Menu.MenuType.Admin) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(x => x.MenuItems).ToList() } ).ToList(); var siteMenus = menus.Where(x => x.Type == Menu.MenuType.WebSite) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(z => z.MenuItems).ToList() } ).ToList(); foreach (var adminMenu in adminMenus) { var menu = new MenuViewModel() { Type = "Admin", Name = adminMenu.MenuName, Order = adminMenu.Menu.Order, MenuItems = GetMenuItems(adminMenu.Items, item.ModuleName), }; module.AdminMenus.Add(menu); } foreach (var webSiteMenu in siteMenus) { var menu = new MenuViewModel() { Type = "WebSite", Name = webSiteMenu.MenuName, Order = webSiteMenu.Menu.Order, MenuItems = GetMenuItems(webSiteMenu.Items, item.ModuleName), }; module.SiteMenus.Add(menu); } DenyModules.Add(module); AllowModules.Add(module); } }
private List <ModuleViewModel> GetModules(NccUser user, bool isAllowModule) { var activeModules = GlobalContext.GetActiveModules(); var modules = new List <ModuleViewModel>(); foreach (var module in modules) { var mvm = new ModuleViewModel(); mvm.ModuleName = module.ModuleName; mvm.Name = module.Name; mvm.AdminMenus = GetMenus(module.ModuleName, module.AdminMenus, user, isAllowModule, "Admin"); mvm.SiteMenus = GetMenus(module.ModuleName, module.SiteMenus, user, isAllowModule, "WebSite"); } return(modules); }
private List <TranslationFile> GetModuleResources() { var list = new List <TranslationFile>(); foreach (var item in GlobalContext.GetActiveModules()) { var resourceFolder = "\\Bin\\Release\\netcoreapp2.0\\Resources"; if (GlobalContext.HostingEnvironment.EnvironmentName.Equals("Development")) { resourceFolder = "\\Bin\\Debug\\netcoreapp2.0\\Resources"; } var tfList = GetTranslationFiles(item.ModuleTitle, item.Path + resourceFolder); list.AddRange(tfList); } return(list); }
public static Assembly LoadFromModuleFolders(string assemblyName) { foreach (var item in GlobalContext.GetActiveModules()) { var assemblyFullPath = Path.Combine(item.Path, assemblyName); var fileNameWithOutExtension = Path.GetFileNameWithoutExtension(assemblyFullPath); var inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase)); var inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(l => l.Name.Equals(fileNameWithOutExtension, StringComparison.OrdinalIgnoreCase)); var assembly = (inCompileLibraries || inRuntimeLibraries) ? Assembly.Load(new AssemblyName(fileNameWithOutExtension)) : AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFullPath); return(assembly); } return(null); }
public ActionResult CreateEdit(string userName = "") { var activeModules = GlobalContext.GetActiveModules(); ViewBag.Modules = activeModules; var permissions = _nccPermissionService.LoadAll(); ViewBag.Roles = new SelectList(permissions, "Id", "Name"); var user = new UserViewModel(); if (!string.IsNullOrEmpty(userName)) { NccUser nccUser = _nccUserService.GetByUserName(userName); user = new UserViewModel(nccUser); ViewBag.Roles = new SelectList(permissions, "Id", "Name", nccUser.Permissions.Select(x => x.PermissionId).ToArray()); } return(View(user)); }
public ActionResult CreateEditRoles(long roleId = 0) { var model = new PermissionViewModel(); var activeModules = GlobalContext.GetActiveModules(); ViewBag.Modules = activeModules; if (roleId > 0) { var permission = _nccPermissionService.Get(roleId); if (permission != null) { model = GetPermissionViewModel(permission); } else { ViewBag.InfoMessage = "Role not found."; } } return(View(model)); }
public ActionResult UserPermissions(long roleId = 0) { var roles = _roleManager.Roles.ToList(); var roleUsers = new List <NccUser>(); if (roleId > 0) { roleUsers = _userManager.Users.Include("Roles").Where(x => x.Roles.Where(y => y.Role.Id == roleId).ToList().Count > 0).ToList(); } else { roleUsers = _userManager.Users.ToList(); } var activeModules = GlobalContext.GetActiveModules(); ViewBag.Roles = new SelectList(roles, "Name", "Name"); ViewBag.Users = new SelectList(roleUsers, "Id", "Name"); ViewBag.Modules = activeModules; return(View()); }
public static IServiceProvider BuildModules(this IServiceCollection services, IConfigurationRoot configuration, IHostingEnvironment hostingEnvironment) { var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly).AsImplementedInterfaces(); builder.Register <SingleInstanceFactory>(ctx => { var c = ctx.Resolve <IComponentContext>(); return(t => c.Resolve(t)); }); builder.Register <MultiInstanceFactory>(ctx => { var c = ctx.Resolve <IComponentContext>(); return(t => (IEnumerable <object>)c.Resolve(typeof(IEnumerable <>).MakeGenericType(t))); }); //Added into MediatR because default handlers are decleard at NetCoreCMS.Framework library. var coreFrameworkAssembly = typeof(NccInfo).Assembly; builder.RegisterAssemblyTypes(coreFrameworkAssembly).PropertiesAutowired().AsImplementedInterfaces(); services.AddMediatR(coreFrameworkAssembly); foreach (var module in GlobalContext.GetActiveModules()) { builder.RegisterAssemblyTypes(module.Assembly).PropertiesAutowired().AsImplementedInterfaces(); services.AddMediatR(module.Assembly); } builder.RegisterInstance(configuration); builder.RegisterInstance(hostingEnvironment); builder.Populate(services); var container = builder.Build(); return(container.Resolve <IServiceProvider>()); }
public ActionResult CreateEdit(UserViewModel user, string SendEmail) { bool isSuccess = false; string returnMessage = "User Creation failed"; if (user.Id > 0 && !string.IsNullOrEmpty(user.Email) && !string.IsNullOrEmpty(user.FullName) && !string.IsNullOrEmpty(user.Mobile)) { var oldUser = _nccUserService.GetNccUser(user.Id); oldUser.FullName = user.FullName; oldUser.Email = user.Email; oldUser.Mobile = user.Mobile; oldUser.ExtraDenies.RemoveAll(x => x.ExtraDenyUserId == user.Id); oldUser.ExtraPermissions.RemoveAll(x => x.ExtraAllowUserId == user.Id); var allowedAdminMenuItems = user.AllowModules.Select(x => new { ModuleId = x.ModuleName, Items = x.AdminMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }); var allowedWebSiteMenuItems = user.AllowModules.Select(x => new { ModuleId = x.ModuleName, Items = x.SiteMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }).ToList(); foreach (var moduleMenu in allowedAdminMenuItems) { foreach (var menuItem in moduleMenu.Items) { oldUser.ExtraPermissions.Add(new NccPermissionDetails() { Action = menuItem.Action, AllowUser = oldUser, Controller = menuItem.Controller, ExtraAllowUserId = oldUser.Id, ModuleName = moduleMenu.ModuleId }); } } foreach (var moduleMenu in allowedWebSiteMenuItems) { foreach (var menuItem in moduleMenu.Items) { oldUser.ExtraPermissions.Add(new NccPermissionDetails() { Action = menuItem.Action, AllowUser = oldUser, Controller = menuItem.Controller, ExtraAllowUserId = oldUser.Id, ModuleName = moduleMenu.ModuleId }); } } var deniedAdminMenuItems = user.DenyModules.Select(x => new { ModuleId = x.ModuleName, Items = x.AdminMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }); var deniedWebSiteMenuItems = user.DenyModules.Select(x => new { ModuleId = x.ModuleName, Items = x.SiteMenus.SelectMany(y => y.MenuItems.Where(z => z.IsChecked == true)) }).ToList(); foreach (var moduleMenu in deniedAdminMenuItems) { foreach (var menuItem in moduleMenu.Items) { oldUser.ExtraDenies.Add(new NccPermissionDetails() { Action = menuItem.Action, DenyUser = oldUser, Controller = menuItem.Controller, ExtraDenyUserId = oldUser.Id, ModuleName = moduleMenu.ModuleId }); } } foreach (var moduleMenu in deniedWebSiteMenuItems) { foreach (var menuItem in moduleMenu.Items) { oldUser.ExtraDenies.Add(new NccPermissionDetails() { Action = menuItem.Action, DenyUser = oldUser, Controller = menuItem.Controller, ExtraDenyUserId = oldUser.Id, ModuleName = moduleMenu.ModuleId }); } } _nccUserService.Update(oldUser); if (user.IsSuperAdmin) { NccUser userTemp = _userManager.FindByNameAsync(oldUser.UserName).Result; var result = _userManager.AddToRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result; if (result.Succeeded) { isSuccess = true; returnMessage = " User SuperAdmin Role update successful."; } else { isSuccess = false; returnMessage = result.Errors?.FirstOrDefault()?.Description; } } else { NccUser userTemp = _userManager.FindByNameAsync(oldUser.UserName).Result; var result = _userManager.RemoveFromRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result; if (result.Succeeded) { isSuccess = true; returnMessage = " User SuperAdmin Role remove successful."; } else { isSuccess = false; returnMessage = result.Errors?.FirstOrDefault()?.Description; } } isSuccess = true; GlobalContext.GlobalCache.SetNccUser(oldUser); //return RedirectToAction("Index"); } else if (ModelState.IsValid) { if (user.Password == user.ConfirmPassword) { var nccUser = new NccUser() { Email = user.Email, FullName = user.FullName, UserName = user.UserName, Mobile = user.Mobile, Status = EntityStatus.Active }; var result = _userManager.CreateAsync(nccUser, user.Password).Result; var createdUser = _userManager.FindByNameAsync(user.UserName).Result; if (createdUser != null) { if (user.IsSuperAdmin) { NccUser userTemp = _userManager.FindByNameAsync(createdUser.UserName).Result; var temp = _userManager.AddToRoleAsync(userTemp, NccCmsRoles.SuperAdmin).Result; isSuccess = true; returnMessage = "User created successfully."; } else { foreach (var item in user.Roles) { var permission = _nccPermissionService.Get(item); createdUser.Permissions.Add(new NccUserPermission() { Permission = permission, User = createdUser }); } createdUser.ExtraPermissions = GetSelectedPermissionDetails(user.AllowModules, createdUser, true); createdUser.ExtraDenies = GetSelectedPermissionDetails(user.DenyModules, createdUser, false); var upResult = _userManager.UpdateAsync(createdUser).Result; if (upResult.Succeeded == false) { returnMessage = "User role assign failed."; } else { GlobalContext.GlobalCache.SetNccUser(createdUser); isSuccess = true; returnMessage = "User created successfully."; } } } } else { returnMessage = "Password does not match."; } } else { returnMessage = "Please enter all required fields."; ModelState.AddModelError("", "Please enter all required fields."); } if (isSuccess) { ShowMessage(returnMessage, MessageType.Success, false, true); return(RedirectToAction("CreateEdit")); } var activeModules = GlobalContext.GetActiveModules(); ViewBag.Modules = activeModules; var permissions = _nccPermissionService.LoadAll(); ViewBag.Roles = new SelectList(permissions, "Id", "Name"); ShowMessage(returnMessage, MessageType.Error); return(View("CreateEdit", user)); }
public ActionResult CreateEditRoles(PermissionViewModel model) { var activeModules = GlobalContext.GetActiveModules(); ViewBag.Modules = activeModules; var permission = new NccPermission(); permission.Description = model.Description; permission.Rank = model.Rank; permission.Group = model.Group; permission.Name = model.Name; permission.Id = model.Id; var removePermissionDetailsIdList = new List <long>(); var addedPermissionDetails = new List <NccPermissionDetails>(); foreach (var item in model.Modules) { foreach (var am in item.AdminMenus) { foreach (var mi in am.MenuItems) { if (mi.IsChecked) { var permissionDetails = new NccPermissionDetails() { Id = mi.Id, ModuleName = item.ModuleName, Name = am.Name, Action = mi.Action, Controller = mi.Controller, PermissionId = permission.Id }; permission.PermissionDetails.Add(permissionDetails); if (permission.Id > 0 && permissionDetails.Id == 0) { addedPermissionDetails.Add(permissionDetails); } } else { if (mi.Id > 0) { removePermissionDetailsIdList.Add(mi.Id); } } } } foreach (var sm in item.SiteMenus) { foreach (var mi in sm.MenuItems) { if (mi.IsChecked) { var permissionDetails = new NccPermissionDetails() { Id = mi.Id, ModuleName = item.ModuleName, Name = sm.Name, Action = mi.Action, Controller = mi.Controller, PermissionId = permission.Id }; permission.PermissionDetails.Add(permissionDetails); if (permission.Id > 0 && permissionDetails.Id == 0) { addedPermissionDetails.Add(permissionDetails); } } else { if (mi.Id > 0) { removePermissionDetailsIdList.Add(mi.Id); } } } } } var(res, message) = _nccPermissionService.SaveOrUpdate(permission, addedPermissionDetails, removePermissionDetailsIdList); if (res) { TempData["SuccessMessage"] = message; return(RedirectToAction("CreateEditRoles")); } else { ViewBag.ErrorMessage = message; } return(View(model)); }
public UserViewModel(NccUser user) { _nccUser = user; Id = user.Id; Email = user.Email; FullName = user.FullName; Mobile = user.Mobile; UserName = user.UserName; DenyModules = new List <ModuleViewModel>(); AllowModules = new List <ModuleViewModel>(); foreach (var item in GlobalContext.GetActiveModules()) { var module = new ModuleViewModel(); module.ModuleName = item.ModuleName; module.Name = item.ModuleTitle; var menus = item.Menus; var adminMenus = menus .Where(x => x.Type == Menu.MenuType.Admin) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(x => x.MenuItems).ToList() } ).ToList(); var siteMenus = menus.Where(x => x.Type == Menu.MenuType.WebSite) .GroupBy(y => y.DisplayName, (key, g) => new { MenuName = key, Menu = g.FirstOrDefault(), Items = g.SelectMany(z => z.MenuItems).ToList() } ).ToList(); foreach (var adminMenu in adminMenus) { var menu = new MenuViewModel() { Type = "Admin", Name = adminMenu.MenuName, Order = adminMenu.Menu.Order, MenuItems = GetMenuItems(adminMenu.Items, item.ModuleName), }; module.AdminMenus.Add(menu); } foreach (var webSiteMenu in siteMenus) { var menu = new MenuViewModel() { Type = "WebSite", Name = webSiteMenu.MenuName, Order = webSiteMenu.Menu.Order, MenuItems = GetMenuItems(webSiteMenu.Items, item.ModuleName), }; module.SiteMenus.Add(menu); } AllowModules.Add(module); DenyModules.Add(NccHelper.DeepClone(module)); } foreach (var module in AllowModules) { foreach (var adminMenu in module.AdminMenus) { foreach (var adminMenuItem in adminMenu.MenuItems) { var pd = user.ExtraPermissions.Where( x => x.ModuleName == module.ModuleName && x.AllowUser != null && x.ExtraAllowUserId == user.Id && x.Action == adminMenuItem.Action && x.Controller == adminMenuItem.Controller ).FirstOrDefault(); if (pd != null) { module.IsChecked = true; adminMenu.IsChecked = true; adminMenuItem.Id = pd.Id; adminMenuItem.Name = pd.Name; adminMenuItem.IsChecked = true; } else { adminMenuItem.IsChecked = false; } } } foreach (var siteMenu in module.SiteMenus) { foreach (var siteMenuItem in siteMenu.MenuItems) { var pd = user.ExtraPermissions.Where( x => x.ModuleName == module.ModuleName && x.AllowUser != null && x.ExtraAllowUserId == user.Id && x.Action == siteMenuItem.Action && x.Controller == siteMenuItem.Controller ).FirstOrDefault(); if (pd != null) { module.IsChecked = true; siteMenu.IsChecked = true; siteMenuItem.Id = pd.Id; siteMenuItem.Name = pd.Name; siteMenuItem.IsChecked = true; } else { siteMenuItem.IsChecked = false; } } } } foreach (var module in DenyModules) { foreach (var adminMenu in module.AdminMenus) { foreach (var adminMenuItem in adminMenu.MenuItems) { var pd = user.ExtraDenies.Where( x => x.ModuleName == module.ModuleName && x.DenyUser != null && x.ExtraDenyUserId == user.Id && x.Action == adminMenuItem.Action && x.Controller == adminMenuItem.Controller ).FirstOrDefault(); if (pd != null) { module.IsChecked = true; adminMenu.IsChecked = true; adminMenuItem.Id = pd.Id; adminMenuItem.Name = pd.Name; adminMenuItem.IsChecked = true; } else { adminMenuItem.IsChecked = false; } } } foreach (var siteMenu in module.SiteMenus) { foreach (var siteMenuItem in siteMenu.MenuItems) { var pd = user.ExtraDenies.Where( x => x.ModuleName == module.ModuleName && x.DenyUser != null && x.ExtraDenyUserId == user.Id && x.Action == siteMenuItem.Action && x.Controller == siteMenuItem.Controller ).FirstOrDefault(); if (pd != null) { module.IsChecked = true; siteMenu.IsChecked = true; siteMenuItem.Id = pd.Id; siteMenuItem.Name = pd.Name; siteMenuItem.IsChecked = true; } else { siteMenuItem.IsChecked = false; } } } } }