public void RevokeTemplate(Tenant tenant, TenantTemplateMarkup template, Action <TContext> afterRevoke) { db.EnsureNavUniqueness(); using (new FullSecurityAccessHelper(db, true, false)) { var fmtRoot = new { Now = DateTime.UtcNow, Tenant = tenant }; List <int> permissionsToCheck = new List <int>(); if (template.Roles != null) { foreach (var role in template.Roles) { var tmp = GetRole(tenant.TenantId, role, false); if (tmp != null) { RevokePermissions(tmp, role, permissionsToCheck); var users = (from u in db.TenantUsers join r in db.TenantUserRoles on u.TenantUserId equals r.TenantUserId where u.TenantId == tenant.TenantId select r).ToArray(); db.TenantUserRoles.RemoveRange(users); RevokeRole(tenant.TenantId, tmp); db.SecurityRoles.Remove(tmp); } } } if (template.Settings != null) { foreach (var setting in template.Settings) { setting.ParamName = fmtRoot.FormatText(setting.ParamName); setting.Value = fmtRoot.FormatText(setting.Value); var tmp = GetSetting(tenant.TenantId, setting, false); if (tmp != null) { RevokeSetting(tenant.TenantId, tmp); db.TenantSettings.Remove(tmp); } } } if (template.Constants != null) { foreach (var constant in template.Constants) { constant.Name = fmtRoot.FormatText(constant.Name); constant.Value = fmtRoot.FormatText(constant.Value); var tmp = GetConst(tenant.TenantId, constant, false); if (tmp != null) { RevokeConstant(tenant.TenantId, tmp); db.WebPluginConstants.Remove(tmp); } } } if (template.PlugIns != null) { foreach (var plugIn in template.PlugIns) { plugIn.UniqueName = fmtRoot.FormatText(plugIn.UniqueName); plugIn.Constructor = fmtRoot.FormatText(plugIn.Constructor); var tmp = GetPlugIn(tenant.TenantId, plugIn, false); if (tmp != null) { RevokePlugin(tenant.TenantId, tmp); db.WebPlugins.Remove(tmp); } } } if (template.Navigation != null) { foreach (var menu in template.Navigation) { var tmp = GetNavigationMenu(tenant.TenantId, menu, false); if (tmp != null) { if (tmp.PermissionId != null && !permissionsToCheck.Contains(tmp.PermissionId.Value)) { permissionsToCheck.Add(tmp.PermissionId.Value); } RevokeNavigation(tenant.TenantId, tmp); db.TenantNavigation.Remove(tmp); } } } if (template.Queries != null) { foreach (var query in template.Queries) { var tmp = GetQuery(tenant.TenantId, query, false); if (tmp != null) { RevokeQuery(tenant.TenantId, tmp); db.TenantDiagnosticsQueries.Remove(tmp); } } } if (template.Features != null) { foreach (var feature in template.Features) { var tmp = GetFeature(tenant.TenantId, feature, false); if (tmp != null && feature.InfiniteDuration) { RevokeFeature(tenant.TenantId, tmp); tmp.ActivationEnd = DateTime.UtcNow; } } } db.SaveChanges(); RemoveUnUsedPermissions(tenant.TenantId, permissionsToCheck); db.SaveChanges(); afterRevoke?.Invoke(db); } }
public void RevokeTemplate(Tenant tenant, TenantTemplateMarkup template) { RevokeTemplate(tenant, template, null); }
public void ApplyTemplate(Tenant tenant, TenantTemplateMarkup template, Action <TContext> afterApply) { db.EnsureNavUniqueness(); using (new FullSecurityAccessHelper(db, true, false)) { var fmtRoot = new { Now = DateTime.UtcNow, Tenant = tenant }; if (template.Roles != null) { foreach (var role in template.Roles) { var tmp = GetRole(tenant.TenantId, role, true); ApplyPermissions(tmp, role); } } if (template.Settings != null) { foreach (var setting in template.Settings) { setting.ParamName = fmtRoot.FormatText(setting.ParamName); setting.Value = fmtRoot.FormatText(setting.Value); var tmp = GetSetting(tenant.TenantId, setting, true); if (tmp.TenantSettingId != 0) { tmp.JsonSetting = setting.IsJsonSetting; tmp.SettingsValue = setting.Value; } } } if (template.Constants != null) { foreach (var constant in template.Constants) { constant.Name = fmtRoot.FormatText(constant.Name); constant.Value = fmtRoot.FormatText(constant.Value); var tmp = GetConst(tenant.TenantId, constant, true); if (tmp.WebPluginConstantId != 0) { tmp.Value = constant.Value; } } } if (template.PlugIns != null) { foreach (var plugIn in template.PlugIns) { plugIn.UniqueName = fmtRoot.FormatText(plugIn.UniqueName); plugIn.Constructor = fmtRoot.FormatText(plugIn.Constructor); var tmp = GetPlugIn(tenant.TenantId, plugIn, true); if (tmp.WebPluginId != 0) { tmp.AutoLoad = plugIn.AutoLoad; tmp.Constructor = plugIn.Constructor; } } } if (template.Navigation != null) { foreach (var menu in template.Navigation) { var tmp = GetNavigationMenu(tenant.TenantId, menu, true); if (tmp.TenantNavigationMenuId != 0) { tmp.Permission = menu.CustomPermission != null ? GetPermission(tenant.TenantId, menu.CustomPermission, true) : null; } } } if (template.Queries != null) { foreach (var query in template.Queries) { GetQuery(tenant.TenantId, query, true); } } if (template.Features != null) { foreach (var feature in template.Features) { var tmp = GetFeature(tenant.TenantId, feature, true); if (!string.IsNullOrEmpty(feature.DurationExpression) && !feature.InfiniteDuration) { var timeEx = fmtRoot.FormatText(feature.DurationExpression); var tt = new TimeTable(timeEx); var nx = tt.GetNextExecutionTime(fmtRoot.Now); tmp.ActivationStart = fmtRoot.Now; tmp.ActivationEnd = nx; } } } db.SaveChanges(); afterApply?.Invoke(db); } }
public void ApplyTemplate(Tenant tenant, TenantTemplateMarkup template) { ApplyTemplate(tenant, template, null); }